Skip to content

Commit 6b2d43d

Browse files
committed
Mapping for LEDs to diplay
1 parent 9fa3fef commit 6b2d43d

File tree

6 files changed

+56
-23
lines changed

6 files changed

+56
-23
lines changed

locale/circuitpython.pot

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ msgstr ""
574574

575575
#: shared-bindings/displayio/Display.c
576576
#: shared-bindings/framebufferio/FramebufferDisplay.c
577+
#: shared-bindings/is31fl3741/is31fl3741.c
577578
#: shared-bindings/rgbmatrix/RGBMatrix.c
578579
msgid "Brightness must be 0-1.0"
579580
msgstr ""
@@ -2081,6 +2082,10 @@ msgstr ""
20812082
msgid "Sample rate too high. It must be less than %d"
20822083
msgstr ""
20832084

2085+
#: shared-bindings/is31fl3741/is31fl3741.c
2086+
msgid "Scale dimensions must divide by 3"
2087+
msgstr ""
2088+
20842089
#: ports/nrf/common-hal/_bleio/Adapter.c
20852090
msgid "Scan already in progess. Stop with stop_scan."
20862091
msgstr ""
@@ -2342,7 +2347,7 @@ msgstr ""
23422347
msgid "Unable to create lock"
23432348
msgstr ""
23442349

2345-
#: shared-module/displayio/I2CDisplay.c
2350+
#: shared-module/displayio/I2CDisplay.c shared-module/is31fl3741/is31fl3741.c
23462351
#, c-format
23472352
msgid "Unable to find I2C Display at %x"
23482353
msgstr ""
@@ -4453,6 +4458,7 @@ msgstr ""
44534458
msgid "width must be from 2 to 8 (inclusive), not %d"
44544459
msgstr ""
44554460

4461+
#: shared-bindings/is31fl3741/is31fl3741.c
44564462
#: shared-bindings/rgbmatrix/RGBMatrix.c
44574463
msgid "width must be greater than zero"
44584464
msgstr ""

shared-bindings/is31fl3741/__init__.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131

3232
#include "shared-bindings/is31fl3741/is31fl3741.h"
3333

34-
//| """Low-level routines for bitbanged LED matrices"""
35-
//|
36-
3734
STATIC const mp_rom_map_elem_t is31fl3741_module_globals_table[] = {
3835
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_is31fl3741) },
3936
{ MP_ROM_QSTR(MP_QSTR_is31fl3741), MP_ROM_PTR(&is31fl3741_is31fl3741_type) },

shared-bindings/is31fl3741/is31fl3741.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@
5757
//|
5858

5959
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) {
60-
enum { ARG_width, ARG_height, ARG_i2c, ARG_addr, ARG_framebuffer, ARG_scale, ARG_gamma };
60+
enum { ARG_width, ARG_height, ARG_i2c, ARG_addr, ARG_framebuffer, ARG_mapping, ARG_scale, ARG_gamma };
6161
static const mp_arg_t allowed_args[] = {
6262
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
6363
{ MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
6464
{ MP_QSTR_i2c, MP_ARG_OBJ | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
6565
{ MP_QSTR_addr, MP_ARG_INT | MP_ARG_KW_ONLY, { .u_int = 0x30 } },
6666
{ MP_QSTR_framebuffer, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } },
67+
{ MP_QSTR_mapping, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
6768
{ MP_QSTR_scale, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
6869
{ MP_QSTR_gamma, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
6970
};
@@ -79,9 +80,12 @@ STATIC mp_obj_t is31fl3741_is31fl3741_make_new(const mp_obj_type_t *type, size_t
7980
mp_raise_ValueError(translate("width must be greater than zero"));
8081
}
8182

82-
// TODO make sure height/width divisible by 3
8383
self->scale = args[ARG_scale].u_bool;
8484
if (self->scale) {
85+
if (((args[ARG_height].u_int % 3) != 0) || ((args[ARG_width].u_int % 3) != 0)) {
86+
mp_raise_ValueError(translate("Scale dimensions must divide by 3"));
87+
}
88+
8589
self->scale_width = args[ARG_width].u_int / 3;
8690
self->scale_height = args[ARG_height].u_int / 3;
8791
}
@@ -101,7 +105,8 @@ STATIC mp_obj_t is31fl3741_is31fl3741_make_new(const mp_obj_type_t *type, size_t
101105
args[ARG_height].u_int,
102106
framebuffer,
103107
MP_OBJ_TO_PTR(i2c),
104-
args[ARG_addr].u_int
108+
args[ARG_addr].u_int,
109+
args[ARG_mapping].u_obj
105110
);
106111

107112
return MP_OBJ_FROM_PTR(self);

shared-bindings/is31fl3741/is31fl3741.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
extern const mp_obj_type_t is31fl3741_is31fl3741_type;
3232

33-
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);
33+
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_obj_t mapping);
3434

3535
void common_hal_is31fl3741_is31fl3741_deinit(is31fl3741_is31fl3741_obj_t *);
3636

@@ -58,4 +58,4 @@ void is31fl3741_send_reset(busio_i2c_obj_t *i2c, uint8_t addr);
5858
void is31fl3741_set_current(busio_i2c_obj_t *i2c, uint8_t addr, uint8_t current);
5959
uint8_t is31fl3741_get_current(busio_i2c_obj_t *i2c, uint8_t addr);
6060
void is31fl3741_set_led(busio_i2c_obj_t *i2c, uint8_t addr, uint16_t led, uint8_t level, uint8_t page);
61-
void is31fl3741_draw_pixel(busio_i2c_obj_t *i2c, uint8_t addr, int16_t x, int16_t y, uint32_t color);
61+
void is31fl3741_draw_pixel(busio_i2c_obj_t *i2c, uint8_t addr, int16_t x, int16_t y, uint32_t color, uint16_t *mapping);

shared-module/is31fl3741/is31fl3741.c

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include "shared-module/framebufferio/FramebufferDisplay.h"
4141
#include "shared-bindings/busio/I2C.h"
4242

43-
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) {
43+
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_obj_t mapping) {
4444
self->width = width;
4545
self->height = height;
4646

@@ -59,6 +59,21 @@ void common_hal_is31fl3741_is31fl3741_construct(is31fl3741_is31fl3741_obj_t *sel
5959
// of the heap as well.
6060
gc_never_free(self->i2c);
6161

62+
// TODO mapping should be equal to height * width * 3
63+
mp_obj_t *items;
64+
size_t len;
65+
mp_obj_list_get(mapping, &len, &items);
66+
67+
self->mapping = common_hal_is31fl3741_allocator_impl(sizeof(uint16_t) * len);
68+
for (size_t i = 0; i < len; i++) {
69+
mp_int_t value = mp_obj_get_int(items[i]);
70+
// We only store up to 16 bits
71+
if (value > 0xFFFF) {
72+
value = 0xFFFF;
73+
}
74+
self->mapping[i] = (uint16_t)value;
75+
}
76+
6277
common_hal_is31fl3741_is31fl3741_reconstruct(self, framebuffer);
6378
}
6479

@@ -114,6 +129,11 @@ void common_hal_is31fl3741_is31fl3741_deinit(is31fl3741_is31fl3741_obj_t *self)
114129
self->i2c = NULL;
115130
}
116131

132+
if (self->mapping != 0) {
133+
common_hal_is31fl3741_free_impl(self->mapping);
134+
self->mapping = 0;
135+
}
136+
117137
self->base.type = NULL;
118138

119139
// If a framebuffer was passed in to the constructor, NULL the reference
@@ -144,15 +164,12 @@ uint8_t common_hal_is31fl3741_is31fl3741_get_global_current(is31fl3741_is31fl374
144164

145165
void common_hal_is31fl3741_is31fl3741_refresh(is31fl3741_is31fl3741_obj_t *self, uint8_t *dirtyrows) {
146166
common_hal_displayio_is31fl3741_begin_transaction(self);
147-
148-
uint8_t dirty_row_flags = 0xFF; // only supports 8 rows gotta fix
149-
if (dirtyrows != 0) {
150-
dirty_row_flags = *dirtyrows;
151-
}
152-
153167
if (!self->paused) {
168+
uint8_t dirty_row_flags = 0xFF; // only supports 8 rows gotta fix
169+
154170
if (self->scale) {
155171
// Based on the Arduino IS31FL3741 driver code
172+
// dirtyrows flag current not implemented for scaled displays
156173
uint32_t *buffer = self->bufinfo.buf;
157174

158175
for (int x = 0; x < self->scale_width; x++) {
@@ -180,13 +197,17 @@ void common_hal_is31fl3741_is31fl3741_refresh(is31fl3741_is31fl3741_obj_t *self,
180197
} else {
181198
color = (rsum << 16) + (gsum << 8) + bsum;
182199
}
183-
is31fl3741_draw_pixel(self->i2c, self->device_address, x, y, color);
200+
is31fl3741_draw_pixel(self->i2c, self->device_address, x, y, color, self->mapping);
184201
}
185202
}
186203
} else {
187204
uint32_t *buffer = self->bufinfo.buf;
188205
for (int y = 0; y < self->height; y++) {
189-
if ((dirty_row_flags >> y) & 0x1) {
206+
if ((dirtyrows != 0) && ((y % 8) == 0)) {
207+
dirty_row_flags = *dirtyrows++;
208+
}
209+
210+
if ((dirty_row_flags >> (y % 8)) & 0x1) {
190211
uint32_t color = 0;
191212
if (self->auto_gamma) {
192213
color = IS31GammaTable[((*buffer) >> 16 & 0xFF)] +
@@ -197,7 +218,7 @@ void common_hal_is31fl3741_is31fl3741_refresh(is31fl3741_is31fl3741_obj_t *self,
197218
}
198219

199220
for (int x = 0; x < self->width; x++) {
200-
is31fl3741_draw_pixel(self->i2c, self->device_address, x, y, color);
221+
is31fl3741_draw_pixel(self->i2c, self->device_address, x, y, color, self->mapping);
201222
buffer++;
202223
}
203224
}
@@ -240,6 +261,7 @@ void common_hal_is31fl3741_free_impl(void *ptr_in) {
240261

241262
void is31fl3741_is31fl3741_collect_ptrs(is31fl3741_is31fl3741_obj_t *self) {
242263
gc_collect_ptr(self->framebuffer);
264+
gc_collect_ptr(self->mapping);
243265
}
244266

245267
// The following are routines to manipulate the IS31FL3741 chip
@@ -311,16 +333,16 @@ void is31fl3741_set_led(busio_i2c_obj_t *i2c, uint8_t addr, uint16_t led, uint8_
311333
common_hal_busio_i2c_write(i2c, addr, cmd, 2, true);
312334
}
313335

314-
void is31fl3741_draw_pixel(busio_i2c_obj_t *i2c, uint8_t addr, int16_t x, int16_t y, uint32_t color) {
336+
void is31fl3741_draw_pixel(busio_i2c_obj_t *i2c, uint8_t addr, int16_t x, int16_t y, uint32_t color, uint16_t *mapping) {
315337
uint8_t r = color >> 16 & 0xFF;
316338
uint8_t g = color >> 8 & 0xFF;
317339
uint8_t b = color & 0xFF;
318340

319341
int16_t x1 = (x * 5 + y) * 3;
320-
uint16_t ridx = glassesmatrix_ledmap[x1 + 2];
342+
uint16_t ridx = mapping[x1 + 2];
321343
if (ridx != 65535) {
322-
uint16_t gidx = glassesmatrix_ledmap[x1 + 1];
323-
uint16_t bidx = glassesmatrix_ledmap[x1 + 0];
344+
uint16_t gidx = mapping[x1 + 1];
345+
uint16_t bidx = mapping[x1 + 0];
324346
is31fl3741_set_led(i2c, addr, ridx, r, 0);
325347
is31fl3741_set_led(i2c, addr, gidx, g, 0);
326348
is31fl3741_set_led(i2c, addr, bidx, b, 0);

shared-module/is31fl3741/is31fl3741.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ typedef struct {
3939
busio_i2c_obj_t *i2c;
4040
busio_i2c_obj_t inline_i2c;
4141
uint8_t device_address;
42+
uint16_t *mapping;
4243
uint8_t bit_depth;
4344
bool paused;
4445
bool scale;
4546
bool auto_gamma;
4647
} is31fl3741_is31fl3741_obj_t;
4748

49+
/*
4850
static const uint16_t glassesmatrix_ledmap[18 * 5 * 3] = {
4951
65535, 65535, 65535, // (0,0) (clipped, corner)
5052
10, 8, 9, // (0,1) / right ring pixel 20
@@ -137,6 +139,7 @@ static const uint16_t glassesmatrix_ledmap[18 * 5 * 3] = {
137139
23, 25, 24, // (17,3) / 6
138140
276, 22, 277, // (17,4) / 7
139141
};
142+
*/
140143

141144
// Gamma correction table
142145
static const uint8_t IS31GammaTable[256] = {

0 commit comments

Comments
 (0)