Initial commit for IS31

This commit is contained in:
gamblor21 2021-11-06 14:00:11 -05:00
parent 932131b4ff
commit d063bf2675
15 changed files with 976 additions and 0 deletions

View File

@ -32,6 +32,8 @@ CIRCUITPY_BLEIO ?= 1
# No I2CPeripheral implementation
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_IS31FL3741 = 1
CIRCUITPY_RTC ?= 1
# frequencyio not yet implemented

View File

@ -209,6 +209,9 @@ endif
ifeq ($(CIRCUITPY_IPADDRESS),1)
SRC_PATTERNS += ipaddress/%
endif
ifeq ($(CIRCUITPY_IS31FL3741),1)
SRC_PATTERNS += is31fl3741/%
endif
ifeq ($(CIRCUITPY_KEYPAD),1)
SRC_PATTERNS += keypad/%
endif
@ -548,6 +551,8 @@ SRC_SHARED_MODULE_ALL = \
imagecapture/ParallelImageCapture.c \
ipaddress/IPv4Address.c \
ipaddress/__init__.c \
is31fl3741/is31fl3741.c \
is31fl3741/__init__.c \
keypad/__init__.c \
keypad/Event.c \
keypad/EventQueue.c \

View File

@ -392,6 +392,7 @@ extern const struct _mp_obj_module_t nvm_module;
// CIRCUITPY_I2CPERIPHERAL
// CIRCUITPY_IMAGECAPTURE
// CIRCUITPY_IPADDRESS
// CIRCUITPY_IS31FL3741
// CIRCUITPY_KEYPAD
// CIRCUITPY_MATH
// CIRCUITPY_MEMORYMONITOR

View File

@ -247,6 +247,9 @@ CFLAGS += -DCIRCUITPY_IMAGECAPTURE=$(CIRCUITPY_IMAGECAPTURE)
CIRCUITPY_IPADDRESS ?= $(CIRCUITPY_WIFI)
CFLAGS += -DCIRCUITPY_IPADDRESS=$(CIRCUITPY_IPADDRESS)
CIRCUITPY_IS31FL3741 ?= 0
CFLAGS += -DCIRCUITPY_IS31FL3741=$(CIRCUITPY_IS31FL3741)
CIRCUITPY_JSON ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_JSON=$(CIRCUITPY_JSON)

View File

@ -0,0 +1,49 @@
/*
* This file is part of the MicroPython 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 <stdint.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "shared-bindings/is31fl3741/is31fl3741.h"
//| """Low-level routines for bitbanged LED matrices"""
//|
STATIC const mp_rom_map_elem_t is31fl3741_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_is31fl3741) },
{ MP_ROM_QSTR(MP_QSTR_is31fl3741), MP_ROM_PTR(&is31fl3741_is31fl3741_type) },
};
STATIC MP_DEFINE_CONST_DICT(is31fl3741_module_globals, is31fl3741_module_globals_table);
const mp_obj_module_t is31fl3741_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&is31fl3741_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_is31fl3741, is31fl3741_module, CIRCUITPY_IS31FL3741);

View File

@ -0,0 +1,333 @@
/*
* 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 "py/obj.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "py/objarray.h"
// #include "common-hal/is31fl3741/is31fl3741.h"
#include "shared-bindings/is31fl3741/is31fl3741.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/util.h"
#include "shared-module/displayio/__init__.h"
#include "shared-module/framebufferio/__init__.h"
#include "shared-module/framebufferio/FramebufferDisplay.h"
#include "shared-bindings/busio/I2C.h"
//| class is31fl3741:
//| """Displays an in-memory framebuffer to a HUB75-style RGB LED matrix."""
//|
// extern Protomatter_core *_PM_protoPtr;
//| def __init__(self, *, width: int) -> None:
//| """Create a Is31fl3741 object with the given attributes. The height of
//| the display is determined by the number of rgb and address pins and the number of tiles:
//| ``len(rgb_pins) // 3 * 2 ** len(address_pins) * abs(tile)``. With 6 RGB pins, 4
//| address lines, and a single matrix, the display will be 32 pixels tall. If the optional height
//| parameter is specified and is not 0, it is checked against the calculated
//| height.
//|
//| Up to 30 RGB pins and 8 address pins are supported.
//|
//| The RGB pins must be within a single "port" and performance and memory
//| usage are best when they are all within "close by" bits of the port.
//| The clock pin must also be on the same port as the RGB pins. See the
//| documentation of the underlying protomatter C library for more
//| information. Generally, Adafruit's interface boards are designed so
//| that these requirements are met when matched with the intended
//| microcontroller board. For instance, the Feather M4 Express works
//| together with the RGB Matrix Feather.
//|
//| The framebuffer is in "RGB565" format.
//|
//| "RGB565" means that it is organized as a series of 16-bit numbers
//| where the highest 5 bits are interpreted as red, the next 6 as
//| green, and the final 5 as blue. The object can be any buffer, but
//| `array.array` and ``ulab.ndarray`` objects are most often useful.
//| To update the content, modify the framebuffer and call refresh.
//|
//| If a framebuffer is not passed in, one is allocated and initialized
//| to all black. In any case, the framebuffer can be retrieved
//| by passing the Is31fl3741 object to memoryview().
//|
//| If doublebuffer is False, some memory is saved, but the display may
//| flicker during updates.
//|
//| A Is31fl3741 is often used in conjunction with a
//| `framebufferio.FramebufferDisplay`."""
//|
STATIC mp_obj_t is31fl3741_is31fl3741_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_width, ARG_height, ARG_i2c, ARG_addr, ARG_framebuffer };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
{ MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
{ MP_QSTR_i2c, MP_ARG_OBJ | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
{ MP_QSTR_addr, MP_ARG_INT | MP_ARG_KW_ONLY, { .u_int = 0x30 } },
{ MP_QSTR_framebuffer, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_printf(&mp_plat_print, "IS make new\n");
mp_obj_t i2c = mp_arg_validate_type(args[ARG_i2c].u_obj, &busio_i2c_type, MP_QSTR_i2c_bus);
is31fl3741_is31fl3741_obj_t *self = &allocate_display_bus_or_raise()->is31fl3741;
self->base.type = &is31fl3741_is31fl3741_type;
if (args[ARG_width].u_int <= 0) {
mp_raise_ValueError(translate("width must be greater than zero"));
}
mp_printf(&mp_plat_print, "w %d h %d\n", args[ARG_width].u_int, args[ARG_height].u_int);
mp_obj_t framebuffer = args[ARG_framebuffer].u_obj;
if (framebuffer == mp_const_none) {
int width = args[ARG_width].u_int;
int height = args[ARG_height].u_int;
int bufsize = 4 * width * height;
framebuffer = mp_obj_new_bytearray_of_zeros(bufsize);
}
mp_printf(&mp_plat_print, "framebuffer is %x\n", MP_OBJ_TO_PTR(framebuffer));
common_hal_is31fl3741_is31fl3741_construct(self,
args[ARG_width].u_int,
args[ARG_height].u_int,
framebuffer,
MP_OBJ_TO_PTR(i2c),
args[ARG_addr].u_int
);
return MP_OBJ_FROM_PTR(self);
}
//| def deinit(self) -> None:
//| """Free the resources (pins, timers, etc.) associated with this
//| is31fl3741 instance. After deinitialization, no further operations
//| may be performed."""
//| ...
//|
STATIC mp_obj_t is31fl3741_is31fl3741_deinit(mp_obj_t self_in) {
mp_printf(&mp_plat_print, "IS Deinit\n");
is31fl3741_is31fl3741_obj_t *self = (is31fl3741_is31fl3741_obj_t *)self_in;
common_hal_is31fl3741_is31fl3741_deinit(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_is31fl3741_deinit_obj, is31fl3741_is31fl3741_deinit);
static void check_for_deinit(is31fl3741_is31fl3741_obj_t *self) {
// if (!self->protomatter.rgbPins) {
// raise_deinited_error();
// }
}
//| brightness: float
//| """In the current implementation, 0.0 turns the display off entirely
//| and any other value up to 1.0 turns the display on fully."""
//|
STATIC mp_obj_t is31fl3741_is31fl3741_get_brightness(mp_obj_t self_in) {
is31fl3741_is31fl3741_obj_t *self = (is31fl3741_is31fl3741_obj_t *)self_in;
check_for_deinit(self);
return mp_obj_new_float(common_hal_is31fl3741_is31fl3741_get_paused(self)? 0.0f : 1.0f);
}
MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_is31fl3741_get_brightness_obj, is31fl3741_is31fl3741_get_brightness);
STATIC mp_obj_t is31fl3741_is31fl3741_set_brightness(mp_obj_t self_in, mp_obj_t value_in) {
is31fl3741_is31fl3741_obj_t *self = (is31fl3741_is31fl3741_obj_t *)self_in;
check_for_deinit(self);
mp_float_t brightness = mp_obj_get_float(value_in);
if (brightness < 0.0f || brightness > 1.0f) {
mp_raise_ValueError(translate("Brightness must be 0-1.0"));
}
common_hal_is31fl3741_is31fl3741_set_paused(self, brightness <= 0);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(is31fl3741_is31fl3741_set_brightness_obj, is31fl3741_is31fl3741_set_brightness);
const mp_obj_property_t is31fl3741_is31fl3741_brightness_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&is31fl3741_is31fl3741_get_brightness_obj,
(mp_obj_t)&is31fl3741_is31fl3741_set_brightness_obj,
MP_ROM_NONE},
};
//| def refresh(self) -> None:
//| """Transmits the color data in the buffer to the pixels so that
//| they are shown."""
//| ...
//|
STATIC mp_obj_t is31fl3741_is31fl3741_refresh(mp_obj_t self_in) {
is31fl3741_is31fl3741_obj_t *self = (is31fl3741_is31fl3741_obj_t *)self_in;
check_for_deinit(self);
common_hal_is31fl3741_is31fl3741_refresh(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_is31fl3741_refresh_obj, is31fl3741_is31fl3741_refresh);
//| width: int
//| """The width of the display, in pixels"""
//|
STATIC mp_obj_t is31fl3741_is31fl3741_get_width(mp_obj_t self_in) {
is31fl3741_is31fl3741_obj_t *self = (is31fl3741_is31fl3741_obj_t *)self_in;
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_is31fl3741_is31fl3741_get_width(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_is31fl3741_get_width_obj, is31fl3741_is31fl3741_get_width);
const mp_obj_property_t is31fl3741_is31fl3741_width_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&is31fl3741_is31fl3741_get_width_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
//| height: int
//| """The height of the display, in pixels"""
//|
STATIC mp_obj_t is31fl3741_is31fl3741_get_height(mp_obj_t self_in) {
is31fl3741_is31fl3741_obj_t *self = (is31fl3741_is31fl3741_obj_t *)self_in;
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_is31fl3741_is31fl3741_get_height(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_is31fl3741_get_height_obj, is31fl3741_is31fl3741_get_height);
const mp_obj_property_t is31fl3741_is31fl3741_height_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&is31fl3741_is31fl3741_get_height_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
STATIC const mp_rom_map_elem_t is31fl3741_is31fl3741_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&is31fl3741_is31fl3741_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&is31fl3741_is31fl3741_brightness_obj) },
{ MP_ROM_QSTR(MP_QSTR_refresh), MP_ROM_PTR(&is31fl3741_is31fl3741_refresh_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&is31fl3741_is31fl3741_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&is31fl3741_is31fl3741_height_obj) },
};
STATIC MP_DEFINE_CONST_DICT(is31fl3741_is31fl3741_locals_dict, is31fl3741_is31fl3741_locals_dict_table);
STATIC void is31fl3741_is31fl3741_get_bufinfo(mp_obj_t self_in, mp_buffer_info_t *bufinfo) {
is31fl3741_is31fl3741_obj_t *self = (is31fl3741_is31fl3741_obj_t *)self_in;
// mp_printf(&mp_plat_print, "IS get bufinfo %x\n", self->bufinfo.buf);
check_for_deinit(self);
*bufinfo = self->bufinfo;
}
// These version exists so that the prototype matches the protocol,
// avoiding a type cast that can hide errors
STATIC void is31fl3741_is31fl3741_swapbuffers(mp_obj_t self_in, uint8_t *dirty_row_bitmap) {
// mp_printf(&mp_plat_print, "IS swapbuffers\n");
(void)dirty_row_bitmap;
common_hal_is31fl3741_is31fl3741_refresh(self_in);
}
STATIC void is31fl3741_is31fl3741_deinit_proto(mp_obj_t self_in) {
mp_printf(&mp_plat_print, "IS deinit proto\n");
common_hal_is31fl3741_is31fl3741_deinit(self_in);
}
STATIC float is31fl3741_is31fl3741_get_brightness_proto(mp_obj_t self_in) {
mp_printf(&mp_plat_print, "IS get brigthness\n");
return common_hal_is31fl3741_is31fl3741_get_paused(self_in) ? 0.0f : 1.0f;
}
STATIC bool is31fl3741_is31fl3741_set_brightness_proto(mp_obj_t self_in, mp_float_t value) {
mp_printf(&mp_plat_print, "IS set brightness\n");
common_hal_is31fl3741_is31fl3741_set_paused(self_in, value <= 0);
return true;
}
STATIC int is31fl3741_is31fl3741_get_width_proto(mp_obj_t self_in) {
mp_printf(&mp_plat_print, "IS get width\n");
return common_hal_is31fl3741_is31fl3741_get_width(self_in);
}
STATIC int is31fl3741_is31fl3741_get_height_proto(mp_obj_t self_in) {
mp_printf(&mp_plat_print, "IS get height\n");
return common_hal_is31fl3741_is31fl3741_get_height(self_in);
}
STATIC int is31fl3741_is31fl3741_get_color_depth_proto(mp_obj_t self_in) {
mp_printf(&mp_plat_print, "IS get color depth\n");
return 32;
}
STATIC int is31fl3741_is31fl3741_get_bytes_per_cell_proto(mp_obj_t self_in) {
mp_printf(&mp_plat_print, "IS get bytes per cell\n");
return 1;
}
STATIC int is31fl3741_is31fl3741_get_native_frames_per_second_proto(mp_obj_t self_in) {
mp_printf(&mp_plat_print, "IS get fps\n");
return 60;
}
STATIC const framebuffer_p_t is31fl3741_is31fl3741_proto = {
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_framebuffer)
.get_bufinfo = is31fl3741_is31fl3741_get_bufinfo,
.set_brightness = is31fl3741_is31fl3741_set_brightness_proto,
.get_brightness = is31fl3741_is31fl3741_get_brightness_proto,
.get_width = is31fl3741_is31fl3741_get_width_proto,
.get_height = is31fl3741_is31fl3741_get_height_proto,
.get_color_depth = is31fl3741_is31fl3741_get_color_depth_proto,
.get_bytes_per_cell = is31fl3741_is31fl3741_get_bytes_per_cell_proto,
.get_native_frames_per_second = is31fl3741_is31fl3741_get_native_frames_per_second_proto,
.swapbuffers = is31fl3741_is31fl3741_swapbuffers,
.deinit = is31fl3741_is31fl3741_deinit_proto,
};
STATIC mp_int_t is31fl3741_is31fl3741_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
is31fl3741_is31fl3741_obj_t *self = (is31fl3741_is31fl3741_obj_t *)self_in;
// a readonly framebuffer would be unusual but not impossible
mp_printf(&mp_plat_print, "IS IS get buffer\n");
if ((flags & MP_BUFFER_WRITE) && !(self->bufinfo.typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW)) {
return 1;
}
*bufinfo = self->bufinfo;
bufinfo->typecode = 'H';
return 0;
}
const mp_obj_type_t is31fl3741_is31fl3741_type = {
{ &mp_type_type },
.flags = MP_TYPE_FLAG_EXTENDED,
.name = MP_QSTR_is31fl3741,
.locals_dict = (mp_obj_dict_t *)&is31fl3741_is31fl3741_locals_dict,
.make_new = is31fl3741_is31fl3741_make_new,
MP_TYPE_EXTENDED_FIELDS(
.buffer_p = { .get_buffer = is31fl3741_is31fl3741_get_buffer, },
.protocol = &is31fl3741_is31fl3741_proto,
),
};

View File

@ -0,0 +1,50 @@
/*
* 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.
*/
#pragma once
#include "shared-module/is31fl3741/is31fl3741.h"
// #include "lib/protomatter/src/core.h"
extern const mp_obj_type_t is31fl3741_is31fl3741_type;
void common_hal_is31fl3741_is31fl3741_construct(is31fl3741_is31fl3741_obj_t *self, int width, int height, mp_obj_t framebuffer, busio_i2c_obj_t *i2c, uint8_t addr);
void common_hal_is31fl3741_is31fl3741_deinit(is31fl3741_is31fl3741_obj_t *);
int common_hal_is31fl3741_is31fl3741_get_width(is31fl3741_is31fl3741_obj_t *self);
int common_hal_is31fl3741_is31fl3741_get_height(is31fl3741_is31fl3741_obj_t *self);
void common_hal_is31fl3741_is31fl3741_set_paused(is31fl3741_is31fl3741_obj_t *self, bool paused);
bool common_hal_is31fl3741_is31fl3741_get_paused(is31fl3741_is31fl3741_obj_t *self);
void common_hal_is31fl3741_is31fl3741_refresh(is31fl3741_is31fl3741_obj_t *self);
void common_hal_is31fl3741_is31fl3741_reconstruct(is31fl3741_is31fl3741_obj_t *self, mp_obj_t framebuffer);
/*
void rgbmatrix_rgbmatrix_collect_ptrs(rgbmatrix_rgbmatrix_obj_t *);
*/

View File

@ -89,6 +89,9 @@ bool displayio_palette_get_color(displayio_palette_t *self, const _displayio_col
} else if (colorspace->grayscale) {
size_t bitmask = (1 << colorspace->depth) - 1;
*color = (self->colors[palette_index].luma >> colorspace->grayscale_bit) & bitmask;
} else if (colorspace->depth == 32) {
uint32_t c = self->colors[palette_index].rgb888;
*color = c;
} else {
uint16_t packed = self->colors[palette_index].rgb565;
if (colorspace->reverse_bytes_in_word) {

View File

@ -472,6 +472,8 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_c
mask[offset / 32] |= 1 << (offset % 32);
if (colorspace->depth == 16) {
*(((uint16_t *)buffer) + offset) = output_pixel.pixel;
} else if (colorspace->depth == 32) {
*(((uint32_t *)buffer) + offset) = output_pixel.pixel;
} else if (colorspace->depth == 8) {
*(((uint8_t *)buffer) + offset) = output_pixel.pixel;
} else if (colorspace->depth < 8) {

View File

@ -41,6 +41,9 @@
#if CIRCUITPY_RGBMATRIX
#include "shared-bindings/rgbmatrix/RGBMatrix.h"
#endif
#if CIRCUITPY_IS31FL3741
#include "shared-bindings/is31fl3741/is31fl3741.h"
#endif
#if CIRCUITPY_SHARPDISPLAY
#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"
#endif
@ -56,6 +59,9 @@ typedef struct {
#if CIRCUITPY_RGBMATRIX
rgbmatrix_rgbmatrix_obj_t rgbmatrix;
#endif
#if CIRCUITPY_IS31FL3741
is31fl3741_is31fl3741_obj_t is31fl3741;
#endif
#if CIRCUITPY_SHARPDISPLAY
sharpdisplay_framebuffer_obj_t sharpdisplay;
#endif

View File

View File

View File

@ -0,0 +1,37 @@
/*
* 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.
*/
#pragma once
#include <stdbool.h>
#include "py/gc.h"
#include "py/misc.h"
#include "supervisor/memory.h"
// #define _PM_allocate common_hal_rgbmatrix_allocator_impl
// #define _PM_free(x) (common_hal_rgbmatrix_free_impl((x)), (x) = NULL, (void)0)
extern void *common_hal_is31fl3741_allocator_impl(size_t sz);
extern void common_hal_is31fl3741_free_impl(void *);

View File

@ -0,0 +1,348 @@
/*
* 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"
// #include "common-hal/is31fl3741/Is31fl3741.h"
#include "shared-module/is31fl3741/allocator.h"
#include "shared-bindings/is31fl3741/is31fl3741.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/util.h"
#include "shared-module/framebufferio/FramebufferDisplay.h"
#include "shared-bindings/busio/I2C.h"
extern Protomatter_core *_PM_protoPtr;
uint8_t cur_page = 99;
void send_unlock(busio_i2c_obj_t *i2c, uint8_t addr) {
uint8_t unlock[2] = { 0xFE, 0xC5 }; // unlock command
uint8_t result = common_hal_busio_i2c_write(i2c, addr, unlock, 2, true);
if (result != 0) {
mp_printf(&mp_plat_print, "Unlock error %x\n", result);
}
}
void set_page(busio_i2c_obj_t *i2c, uint8_t addr, uint8_t p) {
if (p == cur_page) {
return;
}
cur_page = p;
send_unlock(i2c, addr);
uint8_t page[2] = { 0xFD, 0x00 }; // page command
page[1] = p;
// mp_printf(&mp_plat_print, "Set Page %x -> %x\n", page[0], page[1]);
uint8_t result = common_hal_busio_i2c_write(i2c, addr, page, 2, true);
if (result != 0) {
mp_printf(&mp_plat_print, "Set Page error %x\n", result);
}
}
void send_enable(busio_i2c_obj_t *i2c, uint8_t addr) {
set_page(i2c, addr, 4);
uint8_t enable[2] = { 0x00, 0x01 }; // enable command
uint8_t result = common_hal_busio_i2c_write(i2c, addr, enable, 2, true);
if (result != 0) {
mp_printf(&mp_plat_print, "Enable error %x\n", result);
}
mp_printf(&mp_plat_print, "CH IS construct: enable\n");
}
void send_reset(busio_i2c_obj_t *i2c, uint8_t addr) {
set_page(i2c, addr, 4);
uint8_t rst[2] = { 0x3F, 0xAE }; // reset command
uint8_t result = common_hal_busio_i2c_write(i2c, addr, rst, 2, true);
if (result != 0) {
mp_printf(&mp_plat_print, "reset error %x\n", result);
}
mp_printf(&mp_plat_print, "CH IS construct: reset\n");
}
void set_current(busio_i2c_obj_t *i2c, uint8_t addr, uint8_t cur) {
set_page(i2c, addr, 4);
uint8_t gcur[2] = { 0x01, 0x00 }; // global current command
gcur[1] = cur;
uint8_t result = common_hal_busio_i2c_write(i2c, addr, gcur, 2, true);
if (result != 0) {
mp_printf(&mp_plat_print, "set current error %x\n", result);
}
mp_printf(&mp_plat_print, "CH IS construct: set global current\n");
}
void set_led(busio_i2c_obj_t *i2c, uint8_t addr, uint16_t led, uint8_t level, uint8_t page) {
uint8_t cmd[2] = { 0x00, 0x00 };
if (led < 180) {
set_page(i2c, addr, page);
cmd[0] = (uint8_t)led;
} else {
set_page(i2c, addr, page + 1);
cmd[0] = (uint8_t)(led - 180);
}
cmd[1] = level;
uint8_t result = common_hal_busio_i2c_write(i2c, addr, cmd, 2, true);
if (result != 0) {
mp_printf(&mp_plat_print, "set led error %x\n", result);
}
// mp_printf(&mp_plat_print, "CH IS construct: set led %x -> %x\n", led, level);
}
void drawPixel(busio_i2c_obj_t *i2c, uint8_t addr, int16_t x, int16_t y, uint32_t color) {
uint8_t r = color >> 16 & 0xFF;
uint8_t g = color >> 8 & 0xFF;
uint8_t b = color & 0xFF;
int16_t x1 = (x * 5 + y) * 3;
uint16_t ridx = glassesmatrix_ledmap[x1 + 2];
if (ridx != 65535) {
uint16_t gidx = glassesmatrix_ledmap[x1 + 1];
uint16_t bidx = glassesmatrix_ledmap[x1 + 0];
set_led(i2c, addr, ridx, r, 0);
set_led(i2c, addr, gidx, g, 0);
set_led(i2c, addr, bidx, b, 0);
// mp_printf(&mp_plat_print, "drawPixel: x %d y %d r %02x g %02x b %02x ri %d gi %d bi %d\n", x, y, r, g, b, ridx, gidx, bidx);
} else {
// mp_printf(&mp_plat_print, "drawPixel: x %d y %d r %02x g %02x b %02x OOB\n", x, y, r, g, b);
}
}
void common_hal_is31fl3741_is31fl3741_construct(is31fl3741_is31fl3741_obj_t *self, int width, int height, mp_obj_t framebuffer, busio_i2c_obj_t *i2c, uint8_t addr) {
mp_printf(&mp_plat_print, "CH IS construct %x\n", addr);
self->width = width;
self->height = height;
self->bufsize = 4 * width * height;
// Probe the bus to see if a device acknowledges the given address.
if (!common_hal_busio_i2c_probe(i2c, addr)) {
self->base.type = &mp_type_NoneType;
mp_raise_ValueError_varg(translate("Unable to find I2C Display at %x"), addr);
}
self->i2c = i2c;
self->device_address = addr;
common_hal_is31fl3741_is31fl3741_reconstruct(self, framebuffer);
common_hal_busio_i2c_try_lock(i2c);
uint8_t command = 0xFC;
common_hal_busio_i2c_write(i2c, addr, &command, 1, false);
uint8_t data = 0;
common_hal_busio_i2c_read(i2c, addr, &data, 1);
mp_printf(&mp_plat_print, "CH IS construct device %x\n", data);
send_reset(i2c, addr);
send_enable(i2c, addr);
set_current(i2c, addr, 0x08);
// set scale to max for all
for (int i; i < 351; i++) {
set_led(i2c, addr, i, 0xFF, 2);
}
// set_led(i2c, addr, 0x09, 0xA1, 0);
// set_led(i2c, addr, 309, 0xA1, 0);
// set_led(i2c, addr, 0x09, 0xCC, 2);
// set_led(i2c, addr, 0x19, 0xA2, 0);
// set_led(i2c, addr, 0x19, 0x02, 2);
// set_led(i2c, addr, 0x2A, 0xA3, 1);
// set_led(i2c, addr, 0x29, 0xEE, 3);
common_hal_busio_i2c_unlock(i2c);
}
void common_hal_is31fl3741_is31fl3741_reconstruct(is31fl3741_is31fl3741_obj_t *self, mp_obj_t framebuffer) {
self->paused = 1;
if (framebuffer) {
mp_printf(&mp_plat_print, "CH IS reconstruct framebuffer %x\n", MP_OBJ_TO_PTR(framebuffer));
self->framebuffer = framebuffer;
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';
}
mp_printf(&mp_plat_print, "CH IS reconstruct self->bufinfo is %x\n", self->bufinfo.buf);
// 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 {
mp_printf(&mp_plat_print, "CH IS reconstruct NO framebuffer\n");
common_hal_is31fl3741_free_impl(self->bufinfo.buf);
self->framebuffer = NULL;
self->bufinfo.buf = common_hal_is31fl3741_allocator_impl(self->bufsize);
self->bufinfo.len = self->bufsize;
self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW;
}
/*
memset(&self->protomatter, 0, sizeof(self->protomatter));
ProtomatterStatus stat = _PM_init(&self->protomatter,
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->serpentine ? -self->tile : self->tile,
self->timer);
if (stat == PROTOMATTER_OK) {
_PM_protoPtr = &self->protomatter;
common_hal_is31fl3741_timer_enable(self->timer);
stat = _PM_begin(&self->protomatter);
if (stat == PROTOMATTER_OK) {
_PM_convert_565(&self->protomatter, self->bufinfo.buf, self->width);
_PM_swapbuffer_maybe(&self->protomatter);
}
}
if (stat != PROTOMATTER_OK) {
common_hal_is31fl3741_is31fl3741_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:
mp_raise_msg(&mp_type_MemoryError, NULL);
break;
default:
mp_raise_msg_varg(&mp_type_RuntimeError,
translate("Internal error #%d"), (int)stat);
break;
}
}
*/
self->paused = 0;
}
void common_hal_is31fl3741_is31fl3741_deinit(is31fl3741_is31fl3741_obj_t *self) {
mp_printf(&mp_plat_print, "IS CH Deinit\n");
/*
if (self->timer) {
common_hal_is31fl3741_timer_free(self->timer);
self->timer = 0;
}
if (_PM_protoPtr == &self->protomatter) {
_PM_protoPtr = NULL;
}
if (self->protomatter.rgbPins) {
_PM_deallocate(&self->protomatter);
}
memset(&self->protomatter, 0, sizeof(self->protomatter));
// 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 common_hal_is31fl3741_is31fl3741_set_paused(is31fl3741_is31fl3741_obj_t *self, bool paused) {
mp_printf(&mp_plat_print, "CH IS set paused\n");
/*if (paused && !self->paused) {
_PM_stop(&self->protomatter);
} else if (!paused && self->paused) {
_PM_resume(&self->protomatter);
_PM_convert_565(&self->protomatter, self->bufinfo.buf, self->width);
_PM_swapbuffer_maybe(&self->protomatter);
}*/
self->paused = paused;
}
bool common_hal_is31fl3741_is31fl3741_get_paused(is31fl3741_is31fl3741_obj_t *self) {
mp_printf(&mp_plat_print, "CH IS get paused\n");
return self->paused;
}
void common_hal_is31fl3741_is31fl3741_refresh(is31fl3741_is31fl3741_obj_t *self) {
// mp_printf(&mp_plat_print, "CH IS refresh len %x addr %x\n", self->bufinfo.len, self->bufinfo.buf);
if (!self->paused) {
uint32_t *buffer = self->bufinfo.buf;
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 18; x++) {
drawPixel(self->i2c, self->device_address, x, y, *buffer);
// mp_printf(&mp_plat_print, "%06x ", *buffer);
buffer++;
}
// mp_printf(&mp_plat_print, "\n");
}
//
// _PM_convert_565(&self->protomatter, self->bufinfo.buf, self->width);
// _PM_swapbuffer_maybe(&self->protomatter);
}
}
int common_hal_is31fl3741_is31fl3741_get_width(is31fl3741_is31fl3741_obj_t *self) {
mp_printf(&mp_plat_print, "CH IS get width\n");
return self->width;
}
int common_hal_is31fl3741_is31fl3741_get_height(is31fl3741_is31fl3741_obj_t *self) {
mp_printf(&mp_plat_print, "CH IS get height\n");
return self->height;
}
void *common_hal_is31fl3741_allocator_impl(size_t sz) {
mp_printf(&mp_plat_print, "CH IS allocator\n");
supervisor_allocation *allocation = allocate_memory(align32_size(sz), false, true);
return allocation ? allocation->ptr : NULL;
}
void common_hal_is31fl3741_free_impl(void *ptr_in) {
mp_printf(&mp_plat_print, "CH IS free\n");
free_memory(allocation_from_ptr(ptr_in));
}

View File

@ -0,0 +1,137 @@
/*
* 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.
*/
#pragma once
#include "py/obj.h"
#include "lib/protomatter/src/core.h"
#include "shared-bindings/busio/I2C.h"
extern const mp_obj_type_t is31fl3741_is31fl3741_type;
typedef struct {
mp_obj_base_t base;
mp_obj_t framebuffer;
mp_buffer_info_t bufinfo;
uint16_t bufsize, width, height;
busio_i2c_obj_t *i2c;
uint8_t device_address;
uint8_t bit_depth;
bool paused;
bool doublebuffer;
} is31fl3741_is31fl3741_obj_t;
static const uint16_t glassesmatrix_ledmap[18 * 5 * 3] = {
65535, 65535, 65535, // (0,0) (clipped, corner)
10, 8, 9, // (0,1) / right ring pixel 20
13, 11, 12, // (0,2) / 19
16, 14, 15, // (0,3) / 18
4, 2, 3, // (0,4) / 17
217, 215, 216, // (1,0) / right ring pixel #21
220, 218, 219, // (1,1)
223, 221, 222, // (1,2)
226, 224, 225, // (1,3)
214, 212, 213, // (1,4)
187, 185, 186, // (2,0)
190, 188, 189, // (2,1)
193, 191, 192, // (2,2)
196, 194, 195, // (2,3)
184, 182, 183, // (2,4)
37, 35, 36, // (3,0)
40, 38, 39, // (3,1)
43, 41, 42, // (3,2)
46, 44, 45, // (3,3)
34, 32, 33, // (3,4)
67, 65, 66, // (4,0)
70, 68, 69, // (4,1)
73, 71, 72, // (4,2)
76, 74, 75, // (4,3)
64, 62, 63, // (4,4)
97, 95, 96, // (5,0)
100, 98, 99, // (5,1)
103, 101, 102, // (5,2)
106, 104, 105, // (5,3)
94, 92, 93, // (5,4)
127, 125, 126, // (6,0) / right ring pixel 3
130, 128, 129, // (6,1)
133, 131, 132, // (6,2)
136, 134, 135, // (6,3)
124, 122, 123, // (6,4)
157, 155, 156, // (7,0)
160, 158, 159, // (7,1)
163, 161, 162, // (7,2) / right ring pixel 5
166, 164, 165, // (7,3) / 6
244, 242, 243, // (7,4) / 7
247, 245, 246, // (8,0)
250, 248, 249, // (8,1)
253, 251, 252, // (8,2)
256, 254, 255, // (8,3)
65535, 65535, 65535, // (8,4) (clipped, nose bridge)
345, 347, 346, // (9,0)
342, 344, 343, // (9,1)
267, 269, 268, // (9,2)
263, 265, 264, // (9,3)
65535, 65535, 65535, // (9,4) (clipped, nose bridge)
336, 338, 337, // (10,0)
333, 335, 334, // (10,1)
237, 239, 238, // (10,2) / left ring pixel 19
233, 235, 234, // (10,3) / 18
348, 262, 349, // (10,4) / 17
327, 329, 328, // (11,0) / left ring pixel 21
324, 326, 325, // (11,1)
207, 209, 208, // (11,2)
203, 205, 204, // (11,3)
330, 202, 331, // (11,4)
318, 320, 319, // (12,0)
315, 317, 316, // (12,1)
177, 179, 178, // (12,2)
173, 175, 174, // (12,3)
321, 172, 322, // (12,4)
309, 311, 310, // (13,0)
306, 308, 307, // (13,1)
147, 149, 148, // (13,2)
143, 145, 144, // (13,3)
312, 142, 313, // (13,4)
300, 302, 301, // (14,0)
297, 299, 298, // (14,1)
117, 119, 118, // (14,2)
113, 115, 114, // (14,3)
303, 112, 304, // (14,4)
291, 293, 292, // (15,0)
288, 290, 289, // (15,1)
87, 89, 88, // (15,2)
83, 85, 84, // (15,3)
294, 82, 295, // (15,4)
282, 284, 283, // (16,0) / left ring pixel 3
279, 281, 280, // (16,1)
57, 59, 58, // (16,2)
53, 55, 54, // (16,3)
285, 52, 286, // (16,4)
65535, 65535, 65535, // (17,0) (clipped, corner)
270, 272, 271, // (17,1) / left ring pixel 4
27, 29, 28, // (17,2) / 5
23, 25, 24, // (17,3) / 6
276, 22, 277, // (17,4) / 7
};