2020-03-10 14:12:01 -04:00
|
|
|
/*
|
|
|
|
* 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 <string.h>
|
|
|
|
|
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/objarray.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2020-04-15 12:22:16 -04:00
|
|
|
#include "common-hal/rgbmatrix/RGBMatrix.h"
|
|
|
|
#include "shared-module/rgbmatrix/allocator.h"
|
|
|
|
#include "shared-bindings/rgbmatrix/RGBMatrix.h"
|
2020-03-10 14:12:01 -04:00
|
|
|
#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;
|
|
|
|
|
2023-06-05 11:36:31 -04:00
|
|
|
STATIC void common_hal_rgbmatrix_rgbmatrix_construct1(rgbmatrix_rgbmatrix_obj_t *self, mp_obj_t framebuffer);
|
|
|
|
|
2021-01-26 15:33:48 -05:00
|
|
|
void common_hal_rgbmatrix_rgbmatrix_construct(rgbmatrix_rgbmatrix_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, int8_t tile, bool serpentine, void *timer) {
|
2020-03-10 14:12:01 -04:00
|
|
|
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;
|
2021-01-26 10:19:44 -05:00
|
|
|
self->tile = tile;
|
2021-01-26 15:33:48 -05:00
|
|
|
self->serpentine = serpentine;
|
2020-04-13 09:51:35 -04:00
|
|
|
|
2021-02-11 17:37:34 -05:00
|
|
|
self->timer = timer ? timer : common_hal_rgbmatrix_timer_allocate(self);
|
2020-03-10 14:12:01 -04:00
|
|
|
if (self->timer == NULL) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("No timer available"));
|
2020-03-10 14:12:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
self->width = width;
|
2021-01-26 15:35:26 -05:00
|
|
|
self->bufsize = 2 * width * common_hal_rgbmatrix_rgbmatrix_get_height(self);
|
2020-03-10 14:12:01 -04:00
|
|
|
|
2023-06-05 11:36:31 -04:00
|
|
|
common_hal_rgbmatrix_rgbmatrix_construct1(self, framebuffer);
|
2020-03-10 14:12:01 -04:00
|
|
|
}
|
|
|
|
|
2023-06-05 11:36:31 -04:00
|
|
|
STATIC void common_hal_rgbmatrix_rgbmatrix_construct1(rgbmatrix_rgbmatrix_obj_t *self, mp_obj_t framebuffer) {
|
|
|
|
if (framebuffer != mp_const_none) {
|
2020-03-10 14:12:01 -04:00
|
|
|
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
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_get_index(mp_obj_get_type(self->framebuffer), self->bufinfo.len, MP_OBJ_NEW_SMALL_INT(self->bufsize - 1), false);
|
2020-03-10 14:12:01 -04:00
|
|
|
} else {
|
2023-10-02 19:49:31 -04:00
|
|
|
self->bufinfo.buf = port_malloc(self->bufsize, false);
|
|
|
|
if (self->bufinfo.buf == NULL) {
|
|
|
|
m_malloc_fail(self->bufsize);
|
|
|
|
}
|
2020-03-10 14:12:01 -04:00
|
|
|
self->bufinfo.len = self->bufsize;
|
|
|
|
self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW;
|
|
|
|
}
|
2023-06-05 11:36:31 -04:00
|
|
|
self->framebuffer = framebuffer;
|
2020-03-10 14:12:01 -04:00
|
|
|
|
2020-09-02 14:34:16 -04:00
|
|
|
memset(&self->protomatter, 0, sizeof(self->protomatter));
|
|
|
|
ProtomatterStatus stat = _PM_init(&self->protomatter,
|
2020-04-13 09:51:35 -04:00
|
|
|
self->width, self->bit_depth,
|
2021-03-15 09:57:36 -04:00
|
|
|
self->rgb_count / 6, self->rgb_pins,
|
2020-03-10 14:12:01 -04:00
|
|
|
self->addr_count, self->addr_pins,
|
|
|
|
self->clock_pin, self->latch_pin, self->oe_pin,
|
2021-01-26 15:33:48 -05:00
|
|
|
self->doublebuffer, self->serpentine ? -self->tile : self->tile,
|
|
|
|
self->timer);
|
2020-03-10 14:12:01 -04:00
|
|
|
|
|
|
|
if (stat == PROTOMATTER_OK) {
|
2020-09-02 14:34:16 -04:00
|
|
|
_PM_protoPtr = &self->protomatter;
|
2020-04-15 16:01:24 -04:00
|
|
|
common_hal_rgbmatrix_timer_enable(self->timer);
|
2020-09-02 14:34:16 -04:00
|
|
|
stat = _PM_begin(&self->protomatter);
|
2020-08-28 12:17:07 -04:00
|
|
|
|
|
|
|
if (stat == PROTOMATTER_OK) {
|
2020-09-02 14:34:16 -04:00
|
|
|
_PM_convert_565(&self->protomatter, self->bufinfo.buf, self->width);
|
|
|
|
_PM_swapbuffer_maybe(&self->protomatter);
|
2020-08-28 12:17:07 -04:00
|
|
|
}
|
2020-03-10 14:12:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stat != PROTOMATTER_OK) {
|
2020-04-15 16:01:24 -04:00
|
|
|
common_hal_rgbmatrix_rgbmatrix_deinit(self);
|
2023-06-05 11:36:31 -04:00
|
|
|
if (!gc_alloc_possible()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-10 14:12:01 -04:00
|
|
|
switch (stat) {
|
2021-03-15 09:57:36 -04:00
|
|
|
case PROTOMATTER_ERR_PINS:
|
2022-05-13 15:33:43 -04:00
|
|
|
raise_ValueError_invalid_pin();
|
2021-03-15 09:57:36 -04:00
|
|
|
break;
|
|
|
|
case PROTOMATTER_ERR_ARG:
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_arg_error_invalid(MP_QSTR_args);
|
2021-03-15 09:57:36 -04:00
|
|
|
break;
|
|
|
|
case PROTOMATTER_ERR_MALLOC:
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_msg_varg(&mp_type_MemoryError, MP_ERROR_TEXT("Failed to allocate %q buffer"), MP_QSTR_RGBMatrix);
|
2021-03-15 09:57:36 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mp_raise_msg_varg(&mp_type_RuntimeError,
|
2023-10-30 04:49:06 -04:00
|
|
|
MP_ERROR_TEXT("Internal error #%d"), (int)stat);
|
2021-03-15 09:57:36 -04:00
|
|
|
break;
|
2020-03-10 14:12:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-03-15 09:57:36 -04:00
|
|
|
for (int i = 0; i < count; i++) {
|
2020-03-10 14:12:01 -04:00
|
|
|
free_pin(&seq[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 11:36:31 -04:00
|
|
|
extern int pm_row_count;
|
|
|
|
STATIC void common_hal_rgbmatrix_rgbmatrix_deinit1(rgbmatrix_rgbmatrix_obj_t *self) {
|
2023-10-02 19:49:31 -04:00
|
|
|
common_hal_rgbmatrix_timer_disable(self->timer);
|
2020-03-10 14:12:01 -04:00
|
|
|
|
2020-09-02 14:34:16 -04:00
|
|
|
if (_PM_protoPtr == &self->protomatter) {
|
2020-03-10 14:12:01 -04:00
|
|
|
_PM_protoPtr = NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-02 14:34:16 -04:00
|
|
|
if (self->protomatter.rgbPins) {
|
2020-10-10 15:30:37 -04:00
|
|
|
_PM_deallocate(&self->protomatter);
|
2020-03-10 14:12:01 -04:00
|
|
|
}
|
2023-06-05 11:36:31 -04:00
|
|
|
|
2020-09-02 14:34:16 -04:00
|
|
|
memset(&self->protomatter, 0, sizeof(self->protomatter));
|
2020-03-10 14:12:01 -04:00
|
|
|
|
2020-04-03 11:55:14 -04:00
|
|
|
// If it was supervisor-allocated, it is supervisor-freed and the pointer
|
|
|
|
// is zeroed, otherwise the pointer is just zeroed
|
2023-10-02 19:49:31 -04:00
|
|
|
if (self->framebuffer == mp_const_none) {
|
|
|
|
port_free(self->bufinfo.buf);
|
2023-06-05 11:36:31 -04:00
|
|
|
}
|
2023-10-02 19:49:31 -04:00
|
|
|
self->bufinfo.buf = NULL;
|
2023-06-05 11:36:31 -04:00
|
|
|
|
|
|
|
// If a framebuffer was passed in to the constructor, clear the reference
|
2020-04-03 11:55:14 -04:00
|
|
|
// here so that it will become GC'able
|
2023-06-05 11:36:31 -04:00
|
|
|
self->framebuffer = mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_rgbmatrix_rgbmatrix_deinit(rgbmatrix_rgbmatrix_obj_t *self) {
|
|
|
|
common_hal_rgbmatrix_rgbmatrix_deinit1(self);
|
|
|
|
if (self->timer) {
|
|
|
|
common_hal_rgbmatrix_timer_free(self->timer);
|
|
|
|
self->timer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
self->base.type = &mp_type_NoneType;
|
|
|
|
}
|
|
|
|
|
2023-09-29 17:49:59 -04:00
|
|
|
void common_hal_rgbmatrix_rgbmatrix_get_bufinfo(rgbmatrix_rgbmatrix_obj_t *self, mp_buffer_info_t *bufinfo) {
|
|
|
|
*bufinfo = self->bufinfo;
|
|
|
|
}
|
|
|
|
|
2023-06-05 11:36:31 -04:00
|
|
|
void common_hal_rgbmatrix_rgbmatrix_reconstruct(rgbmatrix_rgbmatrix_obj_t *self) {
|
2023-10-02 19:49:31 -04:00
|
|
|
common_hal_rgbmatrix_rgbmatrix_set_paused(self, true);
|
|
|
|
// Stop using any Python provided framebuffer.
|
2023-06-05 11:36:31 -04:00
|
|
|
if (self->framebuffer != mp_const_none) {
|
|
|
|
memset(&self->bufinfo, 0, sizeof(self->bufinfo));
|
2023-10-02 19:49:31 -04:00
|
|
|
self->bufinfo.buf = port_malloc(self->bufsize, false);
|
|
|
|
if (self->bufinfo.buf == NULL) {
|
|
|
|
common_hal_rgbmatrix_rgbmatrix_deinit(self);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self->bufinfo.len = self->bufsize;
|
|
|
|
self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW;
|
2023-06-05 11:36:31 -04:00
|
|
|
}
|
|
|
|
memset(self->bufinfo.buf, 0, self->bufinfo.len);
|
|
|
|
common_hal_rgbmatrix_rgbmatrix_set_paused(self, false);
|
2020-03-10 14:12:01 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void rgbmatrix_rgbmatrix_collect_ptrs(rgbmatrix_rgbmatrix_obj_t *self) {
|
2020-03-10 14:12:01 -04:00
|
|
|
gc_collect_ptr(self->framebuffer);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_rgbmatrix_rgbmatrix_set_paused(rgbmatrix_rgbmatrix_obj_t *self, bool paused) {
|
2020-04-03 11:52:50 -04:00
|
|
|
if (paused && !self->paused) {
|
2020-09-02 14:34:16 -04:00
|
|
|
_PM_stop(&self->protomatter);
|
2020-04-03 11:52:50 -04:00
|
|
|
} else if (!paused && self->paused) {
|
2020-09-02 14:34:16 -04:00
|
|
|
_PM_resume(&self->protomatter);
|
2020-10-12 14:26:58 -04:00
|
|
|
_PM_convert_565(&self->protomatter, self->bufinfo.buf, self->width);
|
|
|
|
_PM_swapbuffer_maybe(&self->protomatter);
|
2020-04-03 11:52:50 -04:00
|
|
|
}
|
|
|
|
self->paused = paused;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_rgbmatrix_rgbmatrix_get_paused(rgbmatrix_rgbmatrix_obj_t *self) {
|
2020-04-03 11:52:50 -04:00
|
|
|
return self->paused;
|
|
|
|
}
|
2020-04-13 09:51:35 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_rgbmatrix_rgbmatrix_refresh(rgbmatrix_rgbmatrix_obj_t *self) {
|
2020-10-12 14:26:58 -04:00
|
|
|
if (!self->paused) {
|
|
|
|
_PM_convert_565(&self->protomatter, self->bufinfo.buf, self->width);
|
|
|
|
_PM_swapbuffer_maybe(&self->protomatter);
|
|
|
|
}
|
2020-04-13 09:51:35 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
int common_hal_rgbmatrix_rgbmatrix_get_width(rgbmatrix_rgbmatrix_obj_t *self) {
|
2020-04-15 11:19:33 -04:00
|
|
|
return self->width;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
int common_hal_rgbmatrix_rgbmatrix_get_height(rgbmatrix_rgbmatrix_obj_t *self) {
|
2021-01-26 15:33:48 -05:00
|
|
|
int computed_height = (self->rgb_count / 3) * (1 << (self->addr_count)) * self->tile;
|
2020-04-15 11:19:33 -04:00
|
|
|
return computed_height;
|
|
|
|
}
|