Skip to content

Commit 563d8f2

Browse files
committed
Major refractor to make a common base object
1 parent cf2c8ee commit 563d8f2

File tree

13 files changed

+729
-571
lines changed

13 files changed

+729
-571
lines changed

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ SRC_SHARED_MODULE_ALL = \
555555
ipaddress/IPv4Address.c \
556556
ipaddress/__init__.c \
557557
is31fl3741/IS31FL3741.c \
558+
is31fl3741/FrameBuffer.c \
558559
is31fl3741/__init__.c \
559560
keypad/__init__.c \
560561
keypad/Event.c \
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
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+
//| """Displays an in-memory framebuffer to a IS31FL3741 drive display."""
42+
//|
43+
44+
//| def __init__(self, *, width: int) -> None:
45+
//| """Create a IS31FL3741 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+
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) {
58+
enum { ARG_is31, ARG_width, ARG_height, ARG_framebuffer, ARG_mapping, ARG_scale, ARG_gamma };
59+
static const mp_arg_t allowed_args[] = {
60+
{ MP_QSTR_is31, MP_ARG_OBJ | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
61+
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
62+
{ MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
63+
{ MP_QSTR_framebuffer, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } },
64+
{ MP_QSTR_mapping, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
65+
{ MP_QSTR_scale, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
66+
{ MP_QSTR_gamma, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
67+
};
68+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
69+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
70+
71+
is31fl3741_FrameBuffer_obj_t *self = &allocate_display_bus_or_raise()->is31fl3741;
72+
self->base.type = &is31fl3741_FrameBuffer_type;
73+
74+
if (args[ARG_width].u_int <= 0) {
75+
mp_raise_ValueError(translate("width must be greater than zero"));
76+
}
77+
78+
self->scale = args[ARG_scale].u_bool;
79+
if (self->scale) {
80+
if (((args[ARG_height].u_int % 3) != 0) || ((args[ARG_width].u_int % 3) != 0)) {
81+
mp_raise_ValueError(translate("Scale dimensions must divide by 3"));
82+
}
83+
84+
self->scale_width = args[ARG_width].u_int / 3;
85+
self->scale_height = args[ARG_height].u_int / 3;
86+
} else {
87+
self->scale_width = args[ARG_width].u_int;
88+
self->scale_height = args[ARG_height].u_int;
89+
}
90+
91+
self->auto_gamma = args[ARG_gamma].u_bool;
92+
93+
mp_obj_t framebuffer = args[ARG_framebuffer].u_obj;
94+
if (framebuffer == mp_const_none) {
95+
int width = args[ARG_width].u_int;
96+
int height = args[ARG_height].u_int;
97+
int bufsize = 4 * width * height;
98+
framebuffer = mp_obj_new_bytearray_of_zeros(bufsize);
99+
}
100+
101+
common_hal_is31fl3741_FrameBuffer_construct(self,
102+
args[ARG_width].u_int,
103+
args[ARG_height].u_int,
104+
framebuffer,
105+
args[ARG_is31].u_obj,
106+
args[ARG_mapping].u_obj
107+
);
108+
109+
return MP_OBJ_FROM_PTR(self);
110+
}
111+
112+
//| def deinit(self) -> None:
113+
//| """Free the resources associated with this
114+
//| IS31FL3741 instance. After deinitialization, no further operations
115+
//| may be performed."""
116+
//| ...
117+
//|
118+
STATIC mp_obj_t is31fl3741_FrameBuffer_deinit(mp_obj_t self_in) {
119+
is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in;
120+
common_hal_is31fl3741_FrameBuffer_deinit(self);
121+
return mp_const_none;
122+
}
123+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_FrameBuffer_deinit_obj, is31fl3741_FrameBuffer_deinit);
124+
125+
static void check_for_deinit(is31fl3741_FrameBuffer_obj_t *self) {
126+
if (self->framebuffer == NULL) {
127+
raise_deinited_error();
128+
}
129+
}
130+
131+
//| brightness: float
132+
//| """In the current implementation, 0.0 turns the display off entirely
133+
//| and any other value up to 1.0 turns the display on fully."""
134+
//|
135+
STATIC mp_obj_t is31fl3741_FrameBuffer_get_brightness(mp_obj_t self_in) {
136+
is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in;
137+
check_for_deinit(self);
138+
uint8_t current = common_hal_is31fl3741_get_current(self->is31fl3741);
139+
140+
float brightness = (float)current / (float)0xFF;
141+
return mp_obj_new_float(brightness);
142+
}
143+
MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_FrameBuffer_get_brightness_obj, is31fl3741_FrameBuffer_get_brightness);
144+
145+
STATIC mp_obj_t is31fl3741_FrameBuffer_set_brightness(mp_obj_t self_in, mp_obj_t value_in) {
146+
is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in;
147+
check_for_deinit(self);
148+
mp_float_t brightness = mp_obj_get_float(value_in);
149+
if (brightness < 0.0f || brightness > 1.0f) {
150+
mp_raise_ValueError(translate("Brightness must be 0-1.0"));
151+
}
152+
153+
uint8_t current = (uint8_t)(brightness * 0xFF);
154+
common_hal_is31fl3741_set_current(self->is31fl3741, current);
155+
156+
return mp_const_none;
157+
}
158+
MP_DEFINE_CONST_FUN_OBJ_2(is31fl3741_FrameBuffer_set_brightness_obj, is31fl3741_FrameBuffer_set_brightness);
159+
160+
const mp_obj_property_t is31fl3741_FrameBuffer_brightness_obj = {
161+
.base.type = &mp_type_property,
162+
.proxy = {(mp_obj_t)&is31fl3741_FrameBuffer_get_brightness_obj,
163+
(mp_obj_t)&is31fl3741_FrameBuffer_set_brightness_obj,
164+
MP_ROM_NONE},
165+
};
166+
167+
//| def refresh(self) -> None:
168+
//| """Transmits the color data in the buffer to the pixels so that
169+
//| they are shown."""
170+
//| ...
171+
//|
172+
STATIC mp_obj_t is31fl3741_FrameBuffer_refresh(mp_obj_t self_in) {
173+
is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in;
174+
check_for_deinit(self);
175+
common_hal_is31fl3741_FrameBuffer_refresh(self, 0);
176+
return mp_const_none;
177+
}
178+
MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_FrameBuffer_refresh_obj, is31fl3741_FrameBuffer_refresh);
179+
180+
//| width: int
181+
//| """The width of the display, in pixels"""
182+
//|
183+
STATIC mp_obj_t is31fl3741_FrameBuffer_get_width(mp_obj_t self_in) {
184+
is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in;
185+
check_for_deinit(self);
186+
return MP_OBJ_NEW_SMALL_INT(common_hal_is31fl3741_FrameBuffer_get_width(self));
187+
}
188+
MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_FrameBuffer_get_width_obj, is31fl3741_FrameBuffer_get_width);
189+
const mp_obj_property_t is31fl3741_FrameBuffer_width_obj = {
190+
.base.type = &mp_type_property,
191+
.proxy = {(mp_obj_t)&is31fl3741_FrameBuffer_get_width_obj,
192+
MP_ROM_NONE,
193+
MP_ROM_NONE},
194+
};
195+
196+
//| height: int
197+
//| """The height of the display, in pixels"""
198+
//|
199+
STATIC mp_obj_t is31fl3741_FrameBuffer_get_height(mp_obj_t self_in) {
200+
is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in;
201+
check_for_deinit(self);
202+
return MP_OBJ_NEW_SMALL_INT(common_hal_is31fl3741_FrameBuffer_get_height(self));
203+
}
204+
MP_DEFINE_CONST_FUN_OBJ_1(is31fl3741_FrameBuffer_get_height_obj, is31fl3741_FrameBuffer_get_height);
205+
const mp_obj_property_t is31fl3741_FrameBuffer_height_obj = {
206+
.base.type = &mp_type_property,
207+
.proxy = {(mp_obj_t)&is31fl3741_FrameBuffer_get_height_obj,
208+
MP_ROM_NONE,
209+
MP_ROM_NONE},
210+
};
211+
212+
STATIC const mp_rom_map_elem_t is31fl3741_FrameBuffer_locals_dict_table[] = {
213+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&is31fl3741_FrameBuffer_deinit_obj) },
214+
{ MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&is31fl3741_FrameBuffer_brightness_obj) },
215+
{ MP_ROM_QSTR(MP_QSTR_refresh), MP_ROM_PTR(&is31fl3741_FrameBuffer_refresh_obj) },
216+
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&is31fl3741_FrameBuffer_width_obj) },
217+
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&is31fl3741_FrameBuffer_height_obj) },
218+
};
219+
STATIC MP_DEFINE_CONST_DICT(is31fl3741_FrameBuffer_locals_dict, is31fl3741_FrameBuffer_locals_dict_table);
220+
221+
STATIC void is31fl3741_FrameBuffer_get_bufinfo(mp_obj_t self_in, mp_buffer_info_t *bufinfo) {
222+
is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in;
223+
check_for_deinit(self);
224+
225+
*bufinfo = self->bufinfo;
226+
}
227+
228+
STATIC void is31fl3741_FrameBuffer_swapbuffers(mp_obj_t self_in, uint8_t *dirty_row_bitmap) {
229+
common_hal_is31fl3741_FrameBuffer_refresh(self_in, dirty_row_bitmap);
230+
}
231+
232+
STATIC void is31fl3741_FrameBuffer_deinit_proto(mp_obj_t self_in) {
233+
common_hal_is31fl3741_FrameBuffer_deinit(self_in);
234+
}
235+
236+
STATIC float is31fl3741_FrameBuffer_get_brightness_proto(mp_obj_t self_in) {
237+
return common_hal_is31fl3741_FrameBuffer_get_paused(self_in) ? 0.0f : 1.0f;
238+
}
239+
240+
STATIC bool is31fl3741_FrameBuffer_set_brightness_proto(mp_obj_t self_in, mp_float_t value) {
241+
common_hal_is31fl3741_FrameBuffer_set_paused(self_in, value <= 0);
242+
return true;
243+
}
244+
245+
STATIC int is31fl3741_FrameBuffer_get_width_proto(mp_obj_t self_in) {
246+
return common_hal_is31fl3741_FrameBuffer_get_width(self_in);
247+
}
248+
249+
STATIC int is31fl3741_FrameBuffer_get_height_proto(mp_obj_t self_in) {
250+
return common_hal_is31fl3741_FrameBuffer_get_height(self_in);
251+
}
252+
253+
STATIC int is31fl3741_FrameBuffer_get_color_depth_proto(mp_obj_t self_in) {
254+
// The way displayio works depth is used to calculate bytes
255+
// We use an uint32_t for color already so setting to 24 causes
256+
// more changes required
257+
return 32;
258+
}
259+
260+
STATIC int is31fl3741_FrameBuffer_get_bytes_per_cell_proto(mp_obj_t self_in) {
261+
return 1;
262+
}
263+
264+
STATIC int is31fl3741_FrameBuffer_get_native_frames_per_second_proto(mp_obj_t self_in) {
265+
return 60; // This was just chosen may vary based on LEDs used?
266+
}
267+
268+
STATIC const framebuffer_p_t is31fl3741_FrameBuffer_proto = {
269+
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_framebuffer)
270+
.get_bufinfo = is31fl3741_FrameBuffer_get_bufinfo,
271+
.set_brightness = is31fl3741_FrameBuffer_set_brightness_proto,
272+
.get_brightness = is31fl3741_FrameBuffer_get_brightness_proto,
273+
.get_width = is31fl3741_FrameBuffer_get_width_proto,
274+
.get_height = is31fl3741_FrameBuffer_get_height_proto,
275+
.get_color_depth = is31fl3741_FrameBuffer_get_color_depth_proto,
276+
.get_bytes_per_cell = is31fl3741_FrameBuffer_get_bytes_per_cell_proto,
277+
.get_native_frames_per_second = is31fl3741_FrameBuffer_get_native_frames_per_second_proto,
278+
.swapbuffers = is31fl3741_FrameBuffer_swapbuffers,
279+
.deinit = is31fl3741_FrameBuffer_deinit_proto,
280+
};
281+
282+
STATIC mp_int_t is31fl3741_FrameBuffer_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
283+
is31fl3741_FrameBuffer_obj_t *self = (is31fl3741_FrameBuffer_obj_t *)self_in;
284+
// a readonly framebuffer would be unusual but not impossible
285+
if ((flags & MP_BUFFER_WRITE) && !(self->bufinfo.typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW)) {
286+
return 1;
287+
}
288+
*bufinfo = self->bufinfo;
289+
bufinfo->typecode = 'H';
290+
return 0;
291+
}
292+
293+
const mp_obj_type_t is31fl3741_FrameBuffer_type = {
294+
{ &mp_type_type },
295+
.flags = MP_TYPE_FLAG_EXTENDED,
296+
.name = MP_QSTR_is31fl3741,
297+
.locals_dict = (mp_obj_dict_t *)&is31fl3741_FrameBuffer_locals_dict,
298+
.make_new = is31fl3741_FrameBuffer_make_new,
299+
MP_TYPE_EXTENDED_FIELDS(
300+
.buffer_p = { .get_buffer = is31fl3741_FrameBuffer_get_buffer, },
301+
.protocol = &is31fl3741_FrameBuffer_proto,
302+
),
303+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
#pragma once
28+
29+
#include "shared-module/is31fl3741/FrameBuffer.h"
30+
#include "shared-module/is31fl3741/IS31FL3741.h"
31+
32+
extern const mp_obj_type_t is31fl3741_FrameBuffer_type;
33+
34+
void common_hal_is31fl3741_FrameBuffer_construct(is31fl3741_FrameBuffer_obj_t *self, int width, int height, mp_obj_t framebuffer, is31fl3741_IS31FL3741_obj_t *is31, mp_obj_t mapping);
35+
36+
void common_hal_is31fl3741_FrameBuffer_deinit(is31fl3741_FrameBuffer_obj_t *);
37+
38+
int common_hal_is31fl3741_FrameBuffer_get_width(is31fl3741_FrameBuffer_obj_t *self);
39+
int common_hal_is31fl3741_FrameBuffer_get_height(is31fl3741_FrameBuffer_obj_t *self);
40+
41+
void common_hal_is31fl3741_FrameBuffer_set_global_current(is31fl3741_FrameBuffer_obj_t *self, uint8_t current);
42+
uint8_t common_hal_is31fl3741_FrameBuffer_get_global_current(is31fl3741_FrameBuffer_obj_t *self);
43+
44+
void common_hal_is31fl3741_FrameBuffer_set_paused(is31fl3741_FrameBuffer_obj_t *self, bool paused);
45+
bool common_hal_is31fl3741_FrameBuffer_get_paused(is31fl3741_FrameBuffer_obj_t *self);
46+
void common_hal_is31fl3741_FrameBuffer_refresh(is31fl3741_FrameBuffer_obj_t *self, uint8_t *dirtyrows);
47+
48+
void common_hal_is31fl3741_FrameBuffer_reconstruct(is31fl3741_FrameBuffer_obj_t *self, mp_obj_t framebuffer);
49+
50+
void is31fl3741_FrameBuffer_collect_ptrs(is31fl3741_FrameBuffer_obj_t *self);

0 commit comments

Comments
 (0)