|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Mark Komus |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/obj.h" |
| 28 | +#include "py/objproperty.h" |
| 29 | +#include "py/runtime.h" |
| 30 | +#include "py/objarray.h" |
| 31 | + |
| 32 | +#include "shared-bindings/is31fl3741/IS31FL3741.h" |
| 33 | +#include "shared-bindings/is31fl3741/FrameBuffer.h" |
| 34 | +#include "shared-bindings/util.h" |
| 35 | +#include "shared-module/displayio/__init__.h" |
| 36 | +#include "shared-module/framebufferio/__init__.h" |
| 37 | +#include "shared-module/framebufferio/FramebufferDisplay.h" |
| 38 | +#include "shared-bindings/busio/I2C.h" |
| 39 | + |
| 40 | +//| class IS31FL3741_FrameBuffer: |
| 41 | +//| """Creates an in-memory framebuffer for a IS31FL3741 device.""" |
| 42 | +//| |
| 43 | +//| def __init__(self, is31: is31fl3741.IS31FL3741, width: int, height: int, mapping: Tuple[int, ...], *, |
| 44 | +//| framebuffer: Optional[WriteableBuffer] = None, scale: bool = False, gamma: bool = False) -> None: |
| 45 | +//| """Create a IS31FL3741_FrameBuffer object with the given attributes. |
| 46 | +//| |
| 47 | +//| The framebuffer is in "RGB888" format using 4 bytes per pixel. |
| 48 | +//| Bits 24-31 are ignored. The format is in RGB order. |
| 49 | +//| |
| 50 | +//| If a framebuffer is not passed in, one is allocated and initialized |
| 51 | +//| to all black. In any case, the framebuffer can be retrieved |
| 52 | +//| by passing the Is31fl3741 object to memoryview(). |
| 53 | +//| |
| 54 | +//| A Is31fl3741 is often used in conjunction with a |
| 55 | +//| `framebufferio.FramebufferDisplay`. |
| 56 | +//| |
| 57 | +//| :param is31fl3741.IS31FL3741 is31: base IS31FL3741 instance to drive the framebuffer |
| 58 | +//| :param int width: width of the display |
| 59 | +//| :param int height: height of the display |
| 60 | +//| :param Tuple[int, ...] mapping: mapping of matrix locations to LEDs |
| 61 | +//| :param Optional[WriteableBuffer] framebuffer: Optional buffer to hold the display |
| 62 | +//| :param bool scale: if True display is scaled down by 3 when displayed |
| 63 | +//| :param bool gamma: if True apply gamma correction to all LEDs""" |
| 64 | +//| ... |
| 65 | +//| |
| 66 | +STATIC mp_obj_t is31fl3741_FrameBuffer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 67 | + enum { ARG_is31, ARG_width, ARG_height, ARG_mapping, ARG_framebuffer, ARG_scale, ARG_gamma }; |
| 68 | + static const mp_arg_t allowed_args[] = { |
| 69 | + { MP_QSTR_is31, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
| 70 | + { MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED }, |
| 71 | + { MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED }, |
| 72 | + { MP_QSTR_mapping, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
| 73 | + { MP_QSTR_framebuffer, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } }, |
| 74 | + { MP_QSTR_scale, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } }, |
| 75 | + { MP_QSTR_gamma, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } }, |
| 76 | + }; |
| 77 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 78 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 79 | + |
| 80 | + is31fl3741_FrameBuffer_obj_t *self = &allocate_display_bus_or_raise()->is31fl3741; |
| 81 | + self->base.type = &is31fl3741_FrameBuffer_type; |
| 82 | + |
| 83 | + if (args[ARG_width].u_int <= 0) { |
| 84 | + mp_raise_ValueError(translate("width must be greater than zero")); |
| 85 | + } |
| 86 | + |
| 87 | + self->scale = args[ARG_scale].u_bool; |
| 88 | + if (self->scale) { |
| 89 | + if (((args[ARG_height].u_int % 3) != 0) || ((args[ARG_width].u_int % 3) != 0)) { |
| 90 | + mp_raise_ValueError(translate("Scale dimensions must divide by 3")); |
| 91 | + } |
| 92 | + |
| 93 | + self->scale_width = args[ARG_width].u_int / 3; |
| 94 | + self->scale_height = args[ARG_height].u_int / 3; |
| 95 | + } else { |
| 96 | + self->scale_width = args[ARG_width].u_int; |
| 97 | + self->scale_height = args[ARG_height].u_int; |
| 98 | + } |
| 99 | + |
| 100 | + self->auto_gamma = args[ARG_gamma].u_bool; |
| 101 | + |
| 102 | + mp_obj_t framebuffer = args[ARG_framebuffer].u_obj; |
| 103 | + if (framebuffer == mp_const_none) { |
| 104 | + int width = args[ARG_width].u_int; |
| 105 | + int height = args[ARG_height].u_int; |
| 106 | + int bufsize = 4 * width * height; |
| 107 | + framebuffer = mp_obj_new_bytearray_of_zeros(bufsize); |
| 108 | + } |
| 109 | + |
| 110 | + common_hal_is31fl3741_FrameBuffer_construct(self, |
| 111 | + args[ARG_width].u_int, |
| 112 | + args[ARG_height].u_int, |
| 113 | + framebuffer, |
| 114 | + args[ARG_is31].u_obj, |
| 115 | + args[ARG_mapping].u_obj |
| 116 | + ); |
| 117 | + |
| 118 | + return MP_OBJ_FROM_PTR(self); |
| 119 | +} |
| 120 | + |
| 121 | +//| def deinit(self) -> None: |
| 122 | +//| """Free the resources associated with this |
| 123 | +//| IS31FL3741 instance. After deinitialization, no further operations |
| 124 | +//| may be performed.""" |
| 125 | +//| ... |
| 126 | +//| |
| 127 | +STATIC mp_obj_t is31fl3741_FrameBuffer_deinit(mp_obj_t self_in) { |
| 128 | + is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in; |
| 129 | + common_hal_is31fl3741_FrameBuffer_deinit(self); |
| 130 | + return mp_const_none; |
| 131 | +} |
| 132 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_FrameBuffer_deinit_obj, is31fl3741_FrameBuffer_deinit); |
| 133 | + |
| 134 | +static void check_for_deinit(is31fl3741_FrameBuffer_obj_t *self) { |
| 135 | + if (self->framebuffer == NULL) { |
| 136 | + raise_deinited_error(); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +//| brightness: float |
| 141 | +//| """In the current implementation, 0.0 turns the display off entirely |
| 142 | +//| and any other value up to 1.0 turns the display on fully.""" |
| 143 | +//| |
| 144 | +STATIC mp_obj_t is31fl3741_FrameBuffer_get_brightness(mp_obj_t self_in) { |
| 145 | + is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in; |
| 146 | + check_for_deinit(self); |
| 147 | + uint8_t current = common_hal_is31fl3741_get_current(self->is31fl3741); |
| 148 | + |
| 149 | + float brightness = (float)current / (float)0xFF; |
| 150 | + return mp_obj_new_float(brightness); |
| 151 | +} |
| 152 | +MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_FrameBuffer_get_brightness_obj, is31fl3741_FrameBuffer_get_brightness); |
| 153 | + |
| 154 | +STATIC mp_obj_t is31fl3741_FrameBuffer_set_brightness(mp_obj_t self_in, mp_obj_t value_in) { |
| 155 | + is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in; |
| 156 | + check_for_deinit(self); |
| 157 | + mp_float_t brightness = mp_obj_get_float(value_in); |
| 158 | + if (brightness < 0.0f || brightness > 1.0f) { |
| 159 | + mp_raise_ValueError(translate("Brightness must be 0-1.0")); |
| 160 | + } |
| 161 | + |
| 162 | + uint8_t current = (uint8_t)(brightness * 0xFF); |
| 163 | + common_hal_is31fl3741_set_current(self->is31fl3741, current); |
| 164 | + |
| 165 | + return mp_const_none; |
| 166 | +} |
| 167 | +MP_DEFINE_CONST_FUN_OBJ_2(is31fl3741_FrameBuffer_set_brightness_obj, is31fl3741_FrameBuffer_set_brightness); |
| 168 | + |
| 169 | +const mp_obj_property_t is31fl3741_FrameBuffer_brightness_obj = { |
| 170 | + .base.type = &mp_type_property, |
| 171 | + .proxy = {(mp_obj_t)&is31fl3741_FrameBuffer_get_brightness_obj, |
| 172 | + (mp_obj_t)&is31fl3741_FrameBuffer_set_brightness_obj, |
| 173 | + MP_ROM_NONE}, |
| 174 | +}; |
| 175 | + |
| 176 | +//| def refresh(self) -> None: |
| 177 | +//| """Transmits the color data in the buffer to the pixels so that |
| 178 | +//| they are shown.""" |
| 179 | +//| ... |
| 180 | +//| |
| 181 | +STATIC mp_obj_t is31fl3741_FrameBuffer_refresh(mp_obj_t self_in) { |
| 182 | + is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in; |
| 183 | + check_for_deinit(self); |
| 184 | + common_hal_is31fl3741_FrameBuffer_refresh(self, 0); |
| 185 | + return mp_const_none; |
| 186 | +} |
| 187 | +MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_FrameBuffer_refresh_obj, is31fl3741_FrameBuffer_refresh); |
| 188 | + |
| 189 | +//| width: int |
| 190 | +//| """The width of the display, in pixels""" |
| 191 | +//| |
| 192 | +STATIC mp_obj_t is31fl3741_FrameBuffer_get_width(mp_obj_t self_in) { |
| 193 | + is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in; |
| 194 | + check_for_deinit(self); |
| 195 | + return MP_OBJ_NEW_SMALL_INT(common_hal_is31fl3741_FrameBuffer_get_width(self)); |
| 196 | +} |
| 197 | +MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_FrameBuffer_get_width_obj, is31fl3741_FrameBuffer_get_width); |
| 198 | +const mp_obj_property_t is31fl3741_FrameBuffer_width_obj = { |
| 199 | + .base.type = &mp_type_property, |
| 200 | + .proxy = {(mp_obj_t)&is31fl3741_FrameBuffer_get_width_obj, |
| 201 | + MP_ROM_NONE, |
| 202 | + MP_ROM_NONE}, |
| 203 | +}; |
| 204 | + |
| 205 | +//| height: int |
| 206 | +//| """The height of the display, in pixels""" |
| 207 | +//| |
| 208 | +STATIC mp_obj_t is31fl3741_FrameBuffer_get_height(mp_obj_t self_in) { |
| 209 | + is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in; |
| 210 | + check_for_deinit(self); |
| 211 | + return MP_OBJ_NEW_SMALL_INT(common_hal_is31fl3741_FrameBuffer_get_height(self)); |
| 212 | +} |
| 213 | +MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_FrameBuffer_get_height_obj, is31fl3741_FrameBuffer_get_height); |
| 214 | +const mp_obj_property_t is31fl3741_FrameBuffer_height_obj = { |
| 215 | + .base.type = &mp_type_property, |
| 216 | + .proxy = {(mp_obj_t)&is31fl3741_FrameBuffer_get_height_obj, |
| 217 | + MP_ROM_NONE, |
| 218 | + MP_ROM_NONE}, |
| 219 | +}; |
| 220 | + |
| 221 | +STATIC const mp_rom_map_elem_t is31fl3741_FrameBuffer_locals_dict_table[] = { |
| 222 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&is31fl3741_FrameBuffer_deinit_obj) }, |
| 223 | + { MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&is31fl3741_FrameBuffer_brightness_obj) }, |
| 224 | + { MP_ROM_QSTR(MP_QSTR_refresh), MP_ROM_PTR(&is31fl3741_FrameBuffer_refresh_obj) }, |
| 225 | + { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&is31fl3741_FrameBuffer_width_obj) }, |
| 226 | + { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&is31fl3741_FrameBuffer_height_obj) }, |
| 227 | +}; |
| 228 | +STATIC MP_DEFINE_CONST_DICT(is31fl3741_FrameBuffer_locals_dict, is31fl3741_FrameBuffer_locals_dict_table); |
| 229 | + |
| 230 | +STATIC void is31fl3741_FrameBuffer_get_bufinfo(mp_obj_t self_in, mp_buffer_info_t *bufinfo) { |
| 231 | + is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in; |
| 232 | + check_for_deinit(self); |
| 233 | + |
| 234 | + *bufinfo = self->bufinfo; |
| 235 | +} |
| 236 | + |
| 237 | +STATIC void is31fl3741_FrameBuffer_swapbuffers(mp_obj_t self_in, uint8_t *dirty_row_bitmap) { |
| 238 | + common_hal_is31fl3741_FrameBuffer_refresh(self_in, dirty_row_bitmap); |
| 239 | +} |
| 240 | + |
| 241 | +STATIC void is31fl3741_FrameBuffer_deinit_proto(mp_obj_t self_in) { |
| 242 | + common_hal_is31fl3741_FrameBuffer_deinit(self_in); |
| 243 | +} |
| 244 | + |
| 245 | +STATIC float is31fl3741_FrameBuffer_get_brightness_proto(mp_obj_t self_in) { |
| 246 | + return common_hal_is31fl3741_FrameBuffer_get_paused(self_in) ? 0.0f : 1.0f; |
| 247 | +} |
| 248 | + |
| 249 | +STATIC bool is31fl3741_FrameBuffer_set_brightness_proto(mp_obj_t self_in, mp_float_t value) { |
| 250 | + common_hal_is31fl3741_FrameBuffer_set_paused(self_in, value <= 0); |
| 251 | + return true; |
| 252 | +} |
| 253 | + |
| 254 | +STATIC int is31fl3741_FrameBuffer_get_width_proto(mp_obj_t self_in) { |
| 255 | + return common_hal_is31fl3741_FrameBuffer_get_width(self_in); |
| 256 | +} |
| 257 | + |
| 258 | +STATIC int is31fl3741_FrameBuffer_get_height_proto(mp_obj_t self_in) { |
| 259 | + return common_hal_is31fl3741_FrameBuffer_get_height(self_in); |
| 260 | +} |
| 261 | + |
| 262 | +STATIC int is31fl3741_FrameBuffer_get_color_depth_proto(mp_obj_t self_in) { |
| 263 | + // The way displayio works depth is used to calculate bytes |
| 264 | + // We use an uint32_t for color already so setting to 24 causes |
| 265 | + // more changes required |
| 266 | + return 32; |
| 267 | +} |
| 268 | + |
| 269 | +STATIC int is31fl3741_FrameBuffer_get_bytes_per_cell_proto(mp_obj_t self_in) { |
| 270 | + return 1; |
| 271 | +} |
| 272 | + |
| 273 | +STATIC int is31fl3741_FrameBuffer_get_native_frames_per_second_proto(mp_obj_t self_in) { |
| 274 | + return 60; // This was just chosen may vary based on LEDs used? |
| 275 | +} |
| 276 | + |
| 277 | +STATIC const framebuffer_p_t is31fl3741_FrameBuffer_proto = { |
| 278 | + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_framebuffer) |
| 279 | + .get_bufinfo = is31fl3741_FrameBuffer_get_bufinfo, |
| 280 | + .set_brightness = is31fl3741_FrameBuffer_set_brightness_proto, |
| 281 | + .get_brightness = is31fl3741_FrameBuffer_get_brightness_proto, |
| 282 | + .get_width = is31fl3741_FrameBuffer_get_width_proto, |
| 283 | + .get_height = is31fl3741_FrameBuffer_get_height_proto, |
| 284 | + .get_color_depth = is31fl3741_FrameBuffer_get_color_depth_proto, |
| 285 | + .get_bytes_per_cell = is31fl3741_FrameBuffer_get_bytes_per_cell_proto, |
| 286 | + .get_native_frames_per_second = is31fl3741_FrameBuffer_get_native_frames_per_second_proto, |
| 287 | + .swapbuffers = is31fl3741_FrameBuffer_swapbuffers, |
| 288 | + .deinit = is31fl3741_FrameBuffer_deinit_proto, |
| 289 | +}; |
| 290 | + |
| 291 | +STATIC mp_int_t is31fl3741_FrameBuffer_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { |
| 292 | + is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in; |
| 293 | + // a readonly framebuffer would be unusual but not impossible |
| 294 | + if ((flags & MP_BUFFER_WRITE) && !(self->bufinfo.typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW)) { |
| 295 | + return 1; |
| 296 | + } |
| 297 | + *bufinfo = self->bufinfo; |
| 298 | + bufinfo->typecode = 'H'; |
| 299 | + return 0; |
| 300 | +} |
| 301 | + |
| 302 | +const mp_obj_type_t is31fl3741_FrameBuffer_type = { |
| 303 | + { &mp_type_type }, |
| 304 | + .flags = MP_TYPE_FLAG_EXTENDED, |
| 305 | + .name = MP_QSTR_is31fl3741, |
| 306 | + .locals_dict = (mp_obj_dict_t *)&is31fl3741_FrameBuffer_locals_dict, |
| 307 | + .make_new = is31fl3741_FrameBuffer_make_new, |
| 308 | + MP_TYPE_EXTENDED_FIELDS( |
| 309 | + .buffer_p = { .get_buffer = is31fl3741_FrameBuffer_get_buffer, }, |
| 310 | + .protocol = &is31fl3741_FrameBuffer_proto, |
| 311 | + ), |
| 312 | +}; |
0 commit comments