Skip to content

Commit e877449

Browse files
committed
Cleanup
1 parent aa92d3a commit e877449

File tree

5 files changed

+143
-120
lines changed

5 files changed

+143
-120
lines changed

shared-bindings/is31fl3741/is31fl3741.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,15 @@
8585
//|
8686

8787
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) {
88-
enum { ARG_width, ARG_height, ARG_i2c, ARG_addr, ARG_framebuffer, ARG_scale };
88+
enum { ARG_width, ARG_height, ARG_i2c, ARG_addr, ARG_framebuffer, ARG_scale, ARG_gamma };
8989
static const mp_arg_t allowed_args[] = {
9090
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
9191
{ MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
9292
{ MP_QSTR_i2c, MP_ARG_OBJ | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
9393
{ MP_QSTR_addr, MP_ARG_INT | MP_ARG_KW_ONLY, { .u_int = 0x30 } },
9494
{ MP_QSTR_framebuffer, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } },
9595
{ MP_QSTR_scale, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
96+
{ MP_QSTR_gamma, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
9697
};
9798
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
9899
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -108,6 +109,12 @@ STATIC mp_obj_t is31fl3741_is31fl3741_make_new(const mp_obj_type_t *type, size_t
108109

109110
// TODO make sure height/width divisible by 3
110111
self->scale = args[ARG_scale].u_bool;
112+
if (self->scale) {
113+
self->scale_width = args[ARG_width].u_int / 3;
114+
self->scale_height = args[ARG_height].u_int / 3;
115+
}
116+
117+
self->auto_gamma = args[ARG_gamma].u_bool;
111118

112119
mp_obj_t framebuffer = args[ARG_framebuffer].u_obj;
113120
if (framebuffer == mp_const_none) {

shared-bindings/is31fl3741/is31fl3741.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,12 @@ void common_hal_is31fl3741_is31fl3741_refresh(is31fl3741_is31fl3741_obj_t *self,
5151
void common_hal_is31fl3741_is31fl3741_reconstruct(is31fl3741_is31fl3741_obj_t *self, mp_obj_t framebuffer);
5252

5353
void is31fl3741_is31fl3741_collect_ptrs(is31fl3741_is31fl3741_obj_t *self);
54+
55+
void is31fl3741_send_unlock(busio_i2c_obj_t *i2c, uint8_t addr);
56+
void is31fl3741_set_page(busio_i2c_obj_t *i2c, uint8_t addr, uint8_t p);
57+
void is31fl3741_send_enable(busio_i2c_obj_t *i2c, uint8_t addr);
58+
void is31fl3741_send_reset(busio_i2c_obj_t *i2c, uint8_t addr);
59+
void is31fl3741_set_current(busio_i2c_obj_t *i2c, uint8_t addr, uint8_t current);
60+
uint8_t is31fl3741_get_current(busio_i2c_obj_t *i2c, uint8_t addr);
61+
void is31fl3741_set_led(busio_i2c_obj_t *i2c, uint8_t addr, uint16_t led, uint8_t level, uint8_t page);
62+
void is31fl3741_draw_pixel(busio_i2c_obj_t *i2c, uint8_t addr, int16_t x, int16_t y, uint32_t color);

shared-module/is31fl3741/allocator.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,5 @@
3131
#include "py/misc.h"
3232
#include "supervisor/memory.h"
3333

34-
// #define _PM_allocate common_hal_rgbmatrix_allocator_impl
35-
// #define _PM_free(x) (common_hal_rgbmatrix_free_impl((x)), (x) = NULL, (void)0)
3634
extern void *common_hal_is31fl3741_allocator_impl(size_t sz);
3735
extern void common_hal_is31fl3741_free_impl(void *);

shared-module/is31fl3741/is31fl3741.c

Lines changed: 123 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "py/objproperty.h"
3333
#include "py/runtime.h"
3434

35-
// #include "common-hal/is31fl3741/Is31fl3741.h"
3635
#include "shared-module/is31fl3741/allocator.h"
3736
#include "shared-bindings/is31fl3741/is31fl3741.h"
3837
#include "shared-bindings/microcontroller/Pin.h"
@@ -41,95 +40,11 @@
4140
#include "shared-module/framebufferio/FramebufferDisplay.h"
4241
#include "shared-bindings/busio/I2C.h"
4342

44-
extern Protomatter_core *_PM_protoPtr;
45-
46-
uint8_t cur_page = 99;
47-
48-
void send_unlock(busio_i2c_obj_t *i2c, uint8_t addr) {
49-
uint8_t unlock[2] = { 0xFE, 0xC5 }; // unlock command
50-
common_hal_busio_i2c_write(i2c, addr, unlock, 2, true);
51-
}
52-
53-
void set_page(busio_i2c_obj_t *i2c, uint8_t addr, uint8_t p) {
54-
if (p == cur_page) {
55-
return;
56-
}
57-
58-
cur_page = p;
59-
send_unlock(i2c, addr);
60-
61-
uint8_t page[2] = { 0xFD, 0x00 }; // page command
62-
page[1] = p;
63-
common_hal_busio_i2c_write(i2c, addr, page, 2, true);
64-
}
65-
66-
void send_enable(busio_i2c_obj_t *i2c, uint8_t addr) {
67-
set_page(i2c, addr, 4);
68-
uint8_t enable[2] = { 0x00, 0x01 }; // enable command
69-
common_hal_busio_i2c_write(i2c, addr, enable, 2, true);
70-
}
71-
72-
void send_reset(busio_i2c_obj_t *i2c, uint8_t addr) {
73-
set_page(i2c, addr, 4);
74-
uint8_t rst[2] = { 0x3F, 0xAE }; // reset command
75-
common_hal_busio_i2c_write(i2c, addr, rst, 2, true);
76-
}
77-
78-
void set_current(busio_i2c_obj_t *i2c, uint8_t addr, uint8_t current) {
79-
set_page(i2c, addr, 4);
80-
uint8_t gcur[2] = { 0x01, 0x00 }; // global current command
81-
gcur[1] = current;
82-
common_hal_busio_i2c_write(i2c, addr, gcur, 2, true);
83-
}
84-
85-
uint8_t get_current(busio_i2c_obj_t *i2c, uint8_t addr) {
86-
set_page(i2c, addr, 4);
87-
uint8_t gcur = 0x01; // global current command
88-
common_hal_busio_i2c_write(i2c, addr, &gcur, 1, true);
89-
90-
uint8_t data = 0;
91-
common_hal_busio_i2c_read(i2c, addr, &data, 1);
92-
return data;
93-
}
94-
95-
96-
void set_led(busio_i2c_obj_t *i2c, uint8_t addr, uint16_t led, uint8_t level, uint8_t page) {
97-
uint8_t cmd[2] = { 0x00, 0x00 };
98-
99-
if (led < 180) {
100-
set_page(i2c, addr, page);
101-
cmd[0] = (uint8_t)led;
102-
} else {
103-
set_page(i2c, addr, page + 1);
104-
cmd[0] = (uint8_t)(led - 180);
105-
}
106-
107-
cmd[1] = level;
108-
109-
common_hal_busio_i2c_write(i2c, addr, cmd, 2, true);
110-
}
111-
112-
void drawPixel(busio_i2c_obj_t *i2c, uint8_t addr, int16_t x, int16_t y, uint32_t color) {
113-
uint8_t r = color >> 16 & 0xFF;
114-
uint8_t g = color >> 8 & 0xFF;
115-
uint8_t b = color & 0xFF;
116-
117-
int16_t x1 = (x * 5 + y) * 3;
118-
uint16_t ridx = glassesmatrix_ledmap[x1 + 2];
119-
if (ridx != 65535) {
120-
uint16_t gidx = glassesmatrix_ledmap[x1 + 1];
121-
uint16_t bidx = glassesmatrix_ledmap[x1 + 0];
122-
set_led(i2c, addr, ridx, r, 0);
123-
set_led(i2c, addr, gidx, g, 0);
124-
set_led(i2c, addr, bidx, b, 0);
125-
}
126-
}
127-
12843
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) {
12944
self->width = width;
13045
self->height = height;
13146

132-
self->bufsize = 4 * width * height;
47+
self->bufsize = sizeof(uint32_t) * width * height;
13348

13449
// Probe the bus to see if a device acknowledges the given address.
13550
if (!common_hal_busio_i2c_probe(i2c, addr)) {
@@ -141,24 +56,6 @@ void common_hal_is31fl3741_is31fl3741_construct(is31fl3741_is31fl3741_obj_t *sel
14156
self->device_address = addr;
14257

14358
common_hal_is31fl3741_is31fl3741_reconstruct(self, framebuffer);
144-
145-
common_hal_displayio_is31fl3741_begin_transaction(self);
146-
147-
uint8_t command = 0xFC;
148-
common_hal_busio_i2c_write(i2c, addr, &command, 1, false);
149-
uint8_t data = 0;
150-
common_hal_busio_i2c_read(i2c, addr, &data, 1);
151-
152-
send_reset(i2c, addr);
153-
send_enable(i2c, addr);
154-
set_current(i2c, addr, 0x08);
155-
156-
// set scale to max for all
157-
for (int i; i < 351; i++) {
158-
set_led(i2c, addr, i, 0xFF, 2);
159-
}
160-
161-
common_hal_displayio_is31fl3741_end_transaction(self);
16259
}
16360

16461
void common_hal_is31fl3741_is31fl3741_reconstruct(is31fl3741_is31fl3741_obj_t *self, mp_obj_t framebuffer) {
@@ -184,7 +81,23 @@ void common_hal_is31fl3741_is31fl3741_reconstruct(is31fl3741_is31fl3741_obj_t *s
18481
self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW;
18582
}
18683

187-
// initialize LEDs here
84+
common_hal_displayio_is31fl3741_begin_transaction(self);
85+
86+
uint8_t command = 0xFC;
87+
common_hal_busio_i2c_write(self->i2c, self->device_address, &command, 1, false);
88+
uint8_t data = 0;
89+
common_hal_busio_i2c_read(self->i2c, self->device_address, &data, 1);
90+
91+
is31fl3741_send_reset(self->i2c, self->device_address);
92+
is31fl3741_send_enable(self->i2c, self->device_address);
93+
is31fl3741_set_current(self->i2c, self->device_address, 0x08);
94+
95+
// set scale to max for all
96+
for (int i; i < 351; i++) {
97+
is31fl3741_set_led(self->i2c, self->device_address, i, 0xFF, 2);
98+
}
99+
100+
common_hal_displayio_is31fl3741_end_transaction(self);
188101

189102
self->paused = 0;
190103
}
@@ -207,13 +120,13 @@ bool common_hal_is31fl3741_is31fl3741_get_paused(is31fl3741_is31fl3741_obj_t *se
207120

208121
void common_hal_is31fl3741_is31fl3741_set_global_current(is31fl3741_is31fl3741_obj_t *self, uint8_t current) {
209122
common_hal_displayio_is31fl3741_begin_transaction(self);
210-
set_current(self->i2c, self->device_address, current);
123+
is31fl3741_set_current(self->i2c, self->device_address, current);
211124
common_hal_displayio_is31fl3741_end_transaction(self);
212125
}
213126

214127
uint8_t common_hal_is31fl3741_is31fl3741_get_global_current(is31fl3741_is31fl3741_obj_t *self) {
215128
common_hal_displayio_is31fl3741_begin_transaction(self);
216-
uint8_t current = get_current(self->i2c, self->device_address);
129+
uint8_t current = is31fl3741_get_current(self->i2c, self->device_address);
217130
common_hal_displayio_is31fl3741_end_transaction(self);
218131
return current;
219132
}
@@ -230,9 +143,9 @@ void common_hal_is31fl3741_is31fl3741_refresh(is31fl3741_is31fl3741_obj_t *self,
230143
if (self->scale) {
231144
uint32_t *buffer = self->bufinfo.buf;
232145

233-
for (int x = 0; x < 18; x++) {
146+
for (int x = 0; x < self->scale_width; x++) {
234147
uint32_t *ptr = &buffer[x * 3]; // Entry along top scan line w/x offset
235-
for (int y = 0; y < 5; y++) {
148+
for (int y = 0; y < self->scale_height; y++) {
236149
uint16_t rsum = 0, gsum = 0, bsum = 0;
237150
// Inner x/y loops are row-major on purpose (less pointer math)
238151
for (uint8_t yy = 0; yy < 3; yy++) {
@@ -242,23 +155,37 @@ void common_hal_is31fl3741_is31fl3741_refresh(is31fl3741_is31fl3741_obj_t *self,
242155
gsum += (rgb >> 8) & 0xFF;
243156
bsum += rgb & 0xFF;
244157
}
245-
ptr += 54; // canvas->width(); // Advance one scan line
158+
ptr += self->width; // canvas->width(); // Advance one scan line
246159
}
247160
rsum = rsum / 9;
248161
gsum = gsum / 9;
249162
bsum = bsum / 9;
250-
uint32_t color = (IS31GammaTable[rsum] << 16) +
251-
(IS31GammaTable[gsum] << 8) +
252-
(IS31GammaTable[bsum] / 9);
253-
drawPixel(self->i2c, self->device_address, x, y, color);
163+
uint32_t color = 0;
164+
if (self->auto_gamma) {
165+
color = (IS31GammaTable[rsum] << 16) +
166+
(IS31GammaTable[gsum] << 8) +
167+
IS31GammaTable[bsum];
168+
} else {
169+
color = (rsum << 16) + (gsum << 8) + bsum;
170+
}
171+
is31fl3741_draw_pixel(self->i2c, self->device_address, x, y, color);
254172
}
255173
}
256174
} else {
257175
uint32_t *buffer = self->bufinfo.buf;
258176
for (int y = 0; y < self->height; y++) {
259177
if ((dirty_row_flags >> y) & 0x1) {
178+
uint32_t color = 0;
179+
if (self->auto_gamma) {
180+
color = IS31GammaTable[((*buffer) >> 16 & 0xFF)] +
181+
IS31GammaTable[((*buffer) >> 8 & 0xFF)] +
182+
IS31GammaTable[((*buffer) & 0xFF)];
183+
} else {
184+
color = *buffer;
185+
}
186+
260187
for (int x = 0; x < self->width; x++) {
261-
drawPixel(self->i2c, self->device_address, x, y, *buffer);
188+
is31fl3741_draw_pixel(self->i2c, self->device_address, x, y, color);
262189
buffer++;
263190
}
264191
}
@@ -299,3 +226,84 @@ void common_hal_is31fl3741_free_impl(void *ptr_in) {
299226
void is31fl3741_is31fl3741_collect_ptrs(is31fl3741_is31fl3741_obj_t *self) {
300227
gc_collect_ptr(self->framebuffer);
301228
}
229+
230+
uint8_t cur_page = 99; // set to invalid page to start
231+
232+
void is31fl3741_send_unlock(busio_i2c_obj_t *i2c, uint8_t addr) {
233+
uint8_t unlock[2] = { 0xFE, 0xC5 }; // unlock command
234+
common_hal_busio_i2c_write(i2c, addr, unlock, 2, true);
235+
}
236+
237+
void is31fl3741_set_page(busio_i2c_obj_t *i2c, uint8_t addr, uint8_t p) {
238+
if (p == cur_page) {
239+
return;
240+
}
241+
242+
cur_page = p;
243+
is31fl3741_send_unlock(i2c, addr);
244+
245+
uint8_t page[2] = { 0xFD, 0x00 }; // page command
246+
page[1] = p;
247+
common_hal_busio_i2c_write(i2c, addr, page, 2, true);
248+
}
249+
250+
void is31fl3741_send_enable(busio_i2c_obj_t *i2c, uint8_t addr) {
251+
is31fl3741_set_page(i2c, addr, 4);
252+
uint8_t enable[2] = { 0x00, 0x01 }; // enable command
253+
common_hal_busio_i2c_write(i2c, addr, enable, 2, true);
254+
}
255+
256+
void is31fl3741_send_reset(busio_i2c_obj_t *i2c, uint8_t addr) {
257+
is31fl3741_set_page(i2c, addr, 4);
258+
uint8_t rst[2] = { 0x3F, 0xAE }; // reset command
259+
common_hal_busio_i2c_write(i2c, addr, rst, 2, true);
260+
}
261+
262+
void is31fl3741_set_current(busio_i2c_obj_t *i2c, uint8_t addr, uint8_t current) {
263+
is31fl3741_set_page(i2c, addr, 4);
264+
uint8_t gcur[2] = { 0x01, 0x00 }; // global current command
265+
gcur[1] = current;
266+
common_hal_busio_i2c_write(i2c, addr, gcur, 2, true);
267+
}
268+
269+
uint8_t is31fl3741_get_current(busio_i2c_obj_t *i2c, uint8_t addr) {
270+
is31fl3741_set_page(i2c, addr, 4);
271+
uint8_t gcur = 0x01; // global current command
272+
common_hal_busio_i2c_write(i2c, addr, &gcur, 1, true);
273+
274+
uint8_t data = 0;
275+
common_hal_busio_i2c_read(i2c, addr, &data, 1);
276+
return data;
277+
}
278+
279+
void is31fl3741_set_led(busio_i2c_obj_t *i2c, uint8_t addr, uint16_t led, uint8_t level, uint8_t page) {
280+
uint8_t cmd[2] = { 0x00, 0x00 };
281+
282+
if (led < 180) {
283+
is31fl3741_set_page(i2c, addr, page);
284+
cmd[0] = (uint8_t)led;
285+
} else {
286+
is31fl3741_set_page(i2c, addr, page + 1);
287+
cmd[0] = (uint8_t)(led - 180);
288+
}
289+
290+
cmd[1] = level;
291+
292+
common_hal_busio_i2c_write(i2c, addr, cmd, 2, true);
293+
}
294+
295+
void is31fl3741_draw_pixel(busio_i2c_obj_t *i2c, uint8_t addr, int16_t x, int16_t y, uint32_t color) {
296+
uint8_t r = color >> 16 & 0xFF;
297+
uint8_t g = color >> 8 & 0xFF;
298+
uint8_t b = color & 0xFF;
299+
300+
int16_t x1 = (x * 5 + y) * 3;
301+
uint16_t ridx = glassesmatrix_ledmap[x1 + 2];
302+
if (ridx != 65535) {
303+
uint16_t gidx = glassesmatrix_ledmap[x1 + 1];
304+
uint16_t bidx = glassesmatrix_ledmap[x1 + 0];
305+
is31fl3741_set_led(i2c, addr, ridx, r, 0);
306+
is31fl3741_set_led(i2c, addr, gidx, g, 0);
307+
is31fl3741_set_led(i2c, addr, bidx, b, 0);
308+
}
309+
}

shared-module/is31fl3741/is31fl3741.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ typedef struct {
3535
mp_obj_base_t base;
3636
mp_obj_t framebuffer;
3737
mp_buffer_info_t bufinfo;
38-
uint16_t bufsize, width, height;
38+
uint16_t bufsize, width, height, scale_width, scale_height;
3939
busio_i2c_obj_t *i2c;
4040
uint8_t device_address;
4141
uint8_t bit_depth;
4242
bool paused;
43-
bool doublebuffer;
4443
bool scale;
44+
bool auto_gamma;
4545
} is31fl3741_is31fl3741_obj_t;
4646

4747
static const uint16_t glassesmatrix_ledmap[18 * 5 * 3] = {
@@ -137,6 +137,7 @@ static const uint16_t glassesmatrix_ledmap[18 * 5 * 3] = {
137137
276, 22, 277, // (17,4) / 7
138138
};
139139

140+
// Gamma correction table
140141
static const uint8_t IS31GammaTable[256] = {
141142
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142143
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,

0 commit comments

Comments
 (0)