From 3d6258f63dce84519704251b86a16c832958c8a6 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 15 Apr 2020 11:22:16 -0500 Subject: Rename Protomatter -> RGBMatrix This is a quick rename, it changes the user-facing names but not the internal names of things. --- shared-module/displayio/__init__.h | 2 +- shared-module/protomatter/Protomatter.c | 209 -------------------------------- shared-module/protomatter/Protomatter.h | 0 shared-module/protomatter/__init__.c | 0 shared-module/protomatter/__init__.h | 0 shared-module/protomatter/allocator.h | 29 ----- shared-module/rgbmatrix/RGBMatrix.c | 209 ++++++++++++++++++++++++++++++++ shared-module/rgbmatrix/RGBMatrix.h | 0 shared-module/rgbmatrix/__init__.c | 0 shared-module/rgbmatrix/__init__.h | 0 shared-module/rgbmatrix/allocator.h | 29 +++++ 11 files changed, 239 insertions(+), 239 deletions(-) delete mode 100644 shared-module/protomatter/Protomatter.c delete mode 100644 shared-module/protomatter/Protomatter.h delete mode 100644 shared-module/protomatter/__init__.c delete mode 100644 shared-module/protomatter/__init__.h delete mode 100644 shared-module/protomatter/allocator.h create mode 100644 shared-module/rgbmatrix/RGBMatrix.c create mode 100644 shared-module/rgbmatrix/RGBMatrix.h create mode 100644 shared-module/rgbmatrix/__init__.c create mode 100644 shared-module/rgbmatrix/__init__.h create mode 100644 shared-module/rgbmatrix/allocator.h (limited to 'shared-module') diff --git a/shared-module/displayio/__init__.h b/shared-module/displayio/__init__.h index f5045b19a..87c0d3356 100644 --- a/shared-module/displayio/__init__.h +++ b/shared-module/displayio/__init__.h @@ -36,7 +36,7 @@ #include "shared-bindings/displayio/Group.h" #include "shared-bindings/displayio/I2CDisplay.h" #include "shared-bindings/displayio/ParallelBus.h" -#include "shared-bindings/protomatter/Protomatter.h" +#include "shared-bindings/rgbmatrix/RGBMatrix.h" typedef struct { union { diff --git a/shared-module/protomatter/Protomatter.c b/shared-module/protomatter/Protomatter.c deleted file mode 100644 index e7fa4ba9a..000000000 --- a/shared-module/protomatter/Protomatter.c +++ /dev/null @@ -1,209 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Jeff Epler for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include - -#include "py/gc.h" -#include "py/obj.h" -#include "py/objarray.h" -#include "py/objproperty.h" -#include "py/runtime.h" - -#include "common-hal/protomatter/Protomatter.h" -#include "shared-module/protomatter/allocator.h" -#include "shared-bindings/protomatter/Protomatter.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/util.h" -#include "shared-module/framebufferio/FramebufferDisplay.h" - -extern Protomatter_core *_PM_protoPtr; - -void common_hal_protomatter_protomatter_construct(protomatter_protomatter_obj_t *self, int width, int bit_depth, uint8_t rgb_count, uint8_t *rgb_pins, uint8_t addr_count, uint8_t *addr_pins, uint8_t clock_pin, uint8_t latch_pin, uint8_t oe_pin, bool doublebuffer, mp_obj_t framebuffer, void *timer) { - self->width = width; - self->bit_depth = bit_depth; - self->rgb_count = rgb_count; - memcpy(self->rgb_pins, rgb_pins, rgb_count); - self->addr_count = addr_count; - memcpy(self->addr_pins, addr_pins, addr_count); - self->clock_pin = clock_pin; - self->oe_pin = oe_pin; - self->latch_pin = latch_pin; - self->doublebuffer = doublebuffer; - - self->timer = timer ? timer : common_hal_protomatter_timer_allocate(); - if (self->timer == NULL) { - mp_raise_ValueError(translate("No timer available")); - } - - self->width = width; - self->bufsize = 2 * width * rgb_count / 3 * (1 << addr_count); - - common_hal_protomatter_protomatter_reconstruct(self, framebuffer); -} - -void common_hal_protomatter_protomatter_reconstruct(protomatter_protomatter_obj_t* self, mp_obj_t framebuffer) { - if (framebuffer) { - self->framebuffer = framebuffer; - framebuffer = mp_obj_new_bytearray_of_zeros(self->bufsize); - mp_get_buffer_raise(self->framebuffer, &self->bufinfo, MP_BUFFER_READ); - if (mp_get_buffer(self->framebuffer, &self->bufinfo, MP_BUFFER_RW)) { - self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW; - } else { - self->bufinfo.typecode = 'H'; - } - // verify that the matrix is big enough - mp_get_index(mp_obj_get_type(self->framebuffer), self->bufinfo.len, MP_OBJ_NEW_SMALL_INT(self->bufsize-1), false); - } else { - _PM_FREE(self->bufinfo.buf); - _PM_FREE(self->core.rgbPins); - _PM_FREE(self->core.addr); - _PM_FREE(self->core.screenData); - - self->framebuffer = NULL; - self->bufinfo.buf = _PM_allocator_impl(self->bufsize); - self->bufinfo.len = self->bufsize; - self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW; - } - - ProtomatterStatus stat = _PM_init(&self->core, - self->width, self->bit_depth, - self->rgb_count/6, self->rgb_pins, - self->addr_count, self->addr_pins, - self->clock_pin, self->latch_pin, self->oe_pin, - self->doublebuffer, self->timer); - - if (stat == PROTOMATTER_OK) { - _PM_protoPtr = &self->core; - common_hal_mcu_disable_interrupts(); - common_hal_protomatter_timer_enable(self->timer); - stat = _PM_begin(&self->core); - _PM_convert_565(&self->core, self->bufinfo.buf, self->width); - common_hal_mcu_enable_interrupts(); - _PM_swapbuffer_maybe(&self->core); - } - - if (stat != PROTOMATTER_OK) { - // XXX this deinit() actually makes crashy-crashy - // can trigger it by sending inappropriate pins - common_hal_protomatter_protomatter_deinit(self); - switch (stat) { - case PROTOMATTER_ERR_PINS: - mp_raise_ValueError(translate("Invalid pin")); - break; - case PROTOMATTER_ERR_ARG: - mp_raise_ValueError(translate("Invalid argument")); - break; - case PROTOMATTER_ERR_MALLOC: /// should have already been signaled as NLR - default: - mp_raise_msg_varg(&mp_type_RuntimeError, - translate("Protomatter internal error #%d"), (int)stat); - break; - } - } - - self->paused = 0; - -} - -STATIC void free_pin(uint8_t *pin) { - if (*pin != COMMON_HAL_MCU_NO_PIN) { - common_hal_mcu_pin_reset_number(*pin); - } - *pin = COMMON_HAL_MCU_NO_PIN; -} - -STATIC void free_pin_seq(uint8_t *seq, int count) { - for (int i=0; itimer) { - common_hal_protomatter_timer_free(self->timer); - self->timer = 0; - } - - if (_PM_protoPtr == &self->core) { - _PM_protoPtr = NULL; - } - - free_pin_seq(self->rgb_pins, self->rgb_count); - free_pin_seq(self->addr_pins, self->addr_count); - free_pin(&self->clock_pin); - free_pin(&self->latch_pin); - free_pin(&self->oe_pin); - - if (self->core.rgbPins) { - _PM_free(&self->core); - } - memset(&self->core, 0, sizeof(self->core)); - - // If it was supervisor-allocated, it is supervisor-freed and the pointer - // is zeroed, otherwise the pointer is just zeroed - _PM_FREE(self->bufinfo.buf); - self->base.type = NULL; - - // If a framebuffer was passed in to the constructor, NULL the reference - // here so that it will become GC'able - self->framebuffer = NULL; -} - -void protomatter_protomatter_collect_ptrs(protomatter_protomatter_obj_t* self) { - gc_collect_ptr(self->framebuffer); - gc_collect_ptr(self->core.rgbPins); - gc_collect_ptr(self->core.addr); - gc_collect_ptr(self->core.screenData); -} - -void common_hal_protomatter_protomatter_set_paused(protomatter_protomatter_obj_t* self, bool paused) { - if (paused && !self->paused) { - _PM_stop(&self->core); - } else if (!paused && self->paused) { - _PM_resume(&self->core); - } - self->paused = paused; -} - -bool common_hal_protomatter_protomatter_get_paused(protomatter_protomatter_obj_t* self) { - return self->paused; -} - -void common_hal_protomatter_protomatter_refresh(protomatter_protomatter_obj_t* self) { - _PM_convert_565(&self->core, self->bufinfo.buf, self->width); - _PM_swapbuffer_maybe(&self->core); -} - -int common_hal_protomatter_protomatter_get_width(protomatter_protomatter_obj_t* self) { - return self->width; -} - -int common_hal_protomatter_protomatter_get_height(protomatter_protomatter_obj_t* self) { - int computed_height = (self->rgb_count / 3) << (self->addr_count); - return computed_height; -} - diff --git a/shared-module/protomatter/Protomatter.h b/shared-module/protomatter/Protomatter.h deleted file mode 100644 index e69de29bb..000000000 diff --git a/shared-module/protomatter/__init__.c b/shared-module/protomatter/__init__.c deleted file mode 100644 index e69de29bb..000000000 diff --git a/shared-module/protomatter/__init__.h b/shared-module/protomatter/__init__.h deleted file mode 100644 index e69de29bb..000000000 diff --git a/shared-module/protomatter/allocator.h b/shared-module/protomatter/allocator.h deleted file mode 100644 index b7f517ce5..000000000 --- a/shared-module/protomatter/allocator.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H -#define MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H - -#include -#include "py/gc.h" -#include "py/misc.h" -#include "supervisor/memory.h" - -#define _PM_ALLOCATOR _PM_allocator_impl -#define _PM_FREE(x) (_PM_free_impl((x)), (x)=NULL, (void)0) - -static inline void *_PM_allocator_impl(size_t sz) { - if (gc_alloc_possible()) { - return m_malloc(sz + sizeof(void*), true); - } else { - supervisor_allocation *allocation = allocate_memory(align32_size(sz), false); - return allocation ? allocation->ptr : NULL; - } -} - -static inline void _PM_free_impl(void *ptr_in) { - supervisor_allocation *allocation = allocation_from_ptr(ptr_in); - - if (allocation) { - free_memory(allocation); - } -} - -#endif diff --git a/shared-module/rgbmatrix/RGBMatrix.c b/shared-module/rgbmatrix/RGBMatrix.c new file mode 100644 index 000000000..17049494d --- /dev/null +++ b/shared-module/rgbmatrix/RGBMatrix.c @@ -0,0 +1,209 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/gc.h" +#include "py/obj.h" +#include "py/objarray.h" +#include "py/objproperty.h" +#include "py/runtime.h" + +#include "common-hal/rgbmatrix/RGBMatrix.h" +#include "shared-module/rgbmatrix/allocator.h" +#include "shared-bindings/rgbmatrix/RGBMatrix.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/util.h" +#include "shared-module/framebufferio/FramebufferDisplay.h" + +extern Protomatter_core *_PM_protoPtr; + +void common_hal_protomatter_protomatter_construct(protomatter_protomatter_obj_t *self, int width, int bit_depth, uint8_t rgb_count, uint8_t *rgb_pins, uint8_t addr_count, uint8_t *addr_pins, uint8_t clock_pin, uint8_t latch_pin, uint8_t oe_pin, bool doublebuffer, mp_obj_t framebuffer, void *timer) { + self->width = width; + self->bit_depth = bit_depth; + self->rgb_count = rgb_count; + memcpy(self->rgb_pins, rgb_pins, rgb_count); + self->addr_count = addr_count; + memcpy(self->addr_pins, addr_pins, addr_count); + self->clock_pin = clock_pin; + self->oe_pin = oe_pin; + self->latch_pin = latch_pin; + self->doublebuffer = doublebuffer; + + self->timer = timer ? timer : common_hal_protomatter_timer_allocate(); + if (self->timer == NULL) { + mp_raise_ValueError(translate("No timer available")); + } + + self->width = width; + self->bufsize = 2 * width * rgb_count / 3 * (1 << addr_count); + + common_hal_protomatter_protomatter_reconstruct(self, framebuffer); +} + +void common_hal_protomatter_protomatter_reconstruct(protomatter_protomatter_obj_t* self, mp_obj_t framebuffer) { + if (framebuffer) { + self->framebuffer = framebuffer; + framebuffer = mp_obj_new_bytearray_of_zeros(self->bufsize); + mp_get_buffer_raise(self->framebuffer, &self->bufinfo, MP_BUFFER_READ); + if (mp_get_buffer(self->framebuffer, &self->bufinfo, MP_BUFFER_RW)) { + self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW; + } else { + self->bufinfo.typecode = 'H'; + } + // verify that the matrix is big enough + mp_get_index(mp_obj_get_type(self->framebuffer), self->bufinfo.len, MP_OBJ_NEW_SMALL_INT(self->bufsize-1), false); + } else { + _PM_FREE(self->bufinfo.buf); + _PM_FREE(self->core.rgbPins); + _PM_FREE(self->core.addr); + _PM_FREE(self->core.screenData); + + self->framebuffer = NULL; + self->bufinfo.buf = _PM_allocator_impl(self->bufsize); + self->bufinfo.len = self->bufsize; + self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW; + } + + ProtomatterStatus stat = _PM_init(&self->core, + self->width, self->bit_depth, + self->rgb_count/6, self->rgb_pins, + self->addr_count, self->addr_pins, + self->clock_pin, self->latch_pin, self->oe_pin, + self->doublebuffer, self->timer); + + if (stat == PROTOMATTER_OK) { + _PM_protoPtr = &self->core; + common_hal_mcu_disable_interrupts(); + common_hal_protomatter_timer_enable(self->timer); + stat = _PM_begin(&self->core); + _PM_convert_565(&self->core, self->bufinfo.buf, self->width); + common_hal_mcu_enable_interrupts(); + _PM_swapbuffer_maybe(&self->core); + } + + if (stat != PROTOMATTER_OK) { + // XXX this deinit() actually makes crashy-crashy + // can trigger it by sending inappropriate pins + common_hal_protomatter_protomatter_deinit(self); + switch (stat) { + case PROTOMATTER_ERR_PINS: + mp_raise_ValueError(translate("Invalid pin")); + break; + case PROTOMATTER_ERR_ARG: + mp_raise_ValueError(translate("Invalid argument")); + break; + case PROTOMATTER_ERR_MALLOC: /// should have already been signaled as NLR + default: + mp_raise_msg_varg(&mp_type_RuntimeError, + translate("Protomatter internal error #%d"), (int)stat); + break; + } + } + + self->paused = 0; + +} + +STATIC void free_pin(uint8_t *pin) { + if (*pin != COMMON_HAL_MCU_NO_PIN) { + common_hal_mcu_pin_reset_number(*pin); + } + *pin = COMMON_HAL_MCU_NO_PIN; +} + +STATIC void free_pin_seq(uint8_t *seq, int count) { + for (int i=0; itimer) { + common_hal_protomatter_timer_free(self->timer); + self->timer = 0; + } + + if (_PM_protoPtr == &self->core) { + _PM_protoPtr = NULL; + } + + free_pin_seq(self->rgb_pins, self->rgb_count); + free_pin_seq(self->addr_pins, self->addr_count); + free_pin(&self->clock_pin); + free_pin(&self->latch_pin); + free_pin(&self->oe_pin); + + if (self->core.rgbPins) { + _PM_free(&self->core); + } + memset(&self->core, 0, sizeof(self->core)); + + // If it was supervisor-allocated, it is supervisor-freed and the pointer + // is zeroed, otherwise the pointer is just zeroed + _PM_FREE(self->bufinfo.buf); + self->base.type = NULL; + + // If a framebuffer was passed in to the constructor, NULL the reference + // here so that it will become GC'able + self->framebuffer = NULL; +} + +void protomatter_protomatter_collect_ptrs(protomatter_protomatter_obj_t* self) { + gc_collect_ptr(self->framebuffer); + gc_collect_ptr(self->core.rgbPins); + gc_collect_ptr(self->core.addr); + gc_collect_ptr(self->core.screenData); +} + +void common_hal_protomatter_protomatter_set_paused(protomatter_protomatter_obj_t* self, bool paused) { + if (paused && !self->paused) { + _PM_stop(&self->core); + } else if (!paused && self->paused) { + _PM_resume(&self->core); + } + self->paused = paused; +} + +bool common_hal_protomatter_protomatter_get_paused(protomatter_protomatter_obj_t* self) { + return self->paused; +} + +void common_hal_protomatter_protomatter_refresh(protomatter_protomatter_obj_t* self) { + _PM_convert_565(&self->core, self->bufinfo.buf, self->width); + _PM_swapbuffer_maybe(&self->core); +} + +int common_hal_protomatter_protomatter_get_width(protomatter_protomatter_obj_t* self) { + return self->width; +} + +int common_hal_protomatter_protomatter_get_height(protomatter_protomatter_obj_t* self) { + int computed_height = (self->rgb_count / 3) << (self->addr_count); + return computed_height; +} + diff --git a/shared-module/rgbmatrix/RGBMatrix.h b/shared-module/rgbmatrix/RGBMatrix.h new file mode 100644 index 000000000..e69de29bb diff --git a/shared-module/rgbmatrix/__init__.c b/shared-module/rgbmatrix/__init__.c new file mode 100644 index 000000000..e69de29bb diff --git a/shared-module/rgbmatrix/__init__.h b/shared-module/rgbmatrix/__init__.h new file mode 100644 index 000000000..e69de29bb diff --git a/shared-module/rgbmatrix/allocator.h b/shared-module/rgbmatrix/allocator.h new file mode 100644 index 000000000..b7f517ce5 --- /dev/null +++ b/shared-module/rgbmatrix/allocator.h @@ -0,0 +1,29 @@ +#ifndef MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H +#define MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H + +#include +#include "py/gc.h" +#include "py/misc.h" +#include "supervisor/memory.h" + +#define _PM_ALLOCATOR _PM_allocator_impl +#define _PM_FREE(x) (_PM_free_impl((x)), (x)=NULL, (void)0) + +static inline void *_PM_allocator_impl(size_t sz) { + if (gc_alloc_possible()) { + return m_malloc(sz + sizeof(void*), true); + } else { + supervisor_allocation *allocation = allocate_memory(align32_size(sz), false); + return allocation ? allocation->ptr : NULL; + } +} + +static inline void _PM_free_impl(void *ptr_in) { + supervisor_allocation *allocation = allocation_from_ptr(ptr_in); + + if (allocation) { + free_memory(allocation); + } +} + +#endif -- cgit v1.2.3