@@ -47,10 +47,7 @@ uint8_t cur_page = 99;
47
47
48
48
void send_unlock (busio_i2c_obj_t * i2c , uint8_t addr ) {
49
49
uint8_t unlock [2 ] = { 0xFE , 0xC5 }; // unlock command
50
- uint8_t result = common_hal_busio_i2c_write (i2c , addr , unlock , 2 , true);
51
- if (result != 0 ) {
52
- mp_printf (& mp_plat_print , "Unlock error %x\n" , result );
53
- }
50
+ common_hal_busio_i2c_write (i2c , addr , unlock , 2 , true);
54
51
}
55
52
56
53
void set_page (busio_i2c_obj_t * i2c , uint8_t addr , uint8_t p ) {
@@ -63,55 +60,35 @@ void set_page(busio_i2c_obj_t *i2c, uint8_t addr, uint8_t p) {
63
60
64
61
uint8_t page [2 ] = { 0xFD , 0x00 }; // page command
65
62
page [1 ] = p ;
66
- uint8_t result = common_hal_busio_i2c_write (i2c , addr , page , 2 , true);
67
- if (result != 0 ) {
68
- mp_printf (& mp_plat_print , "Set Page error %x\n" , result );
69
- }
63
+ common_hal_busio_i2c_write (i2c , addr , page , 2 , true);
70
64
}
71
65
72
66
void send_enable (busio_i2c_obj_t * i2c , uint8_t addr ) {
73
67
set_page (i2c , addr , 4 );
74
68
uint8_t enable [2 ] = { 0x00 , 0x01 }; // enable command
75
- uint8_t result = common_hal_busio_i2c_write (i2c , addr , enable , 2 , true);
76
- if (result != 0 ) {
77
- mp_printf (& mp_plat_print , "Enable error %x\n" , result );
78
- }
69
+ common_hal_busio_i2c_write (i2c , addr , enable , 2 , true);
79
70
}
80
71
81
72
void send_reset (busio_i2c_obj_t * i2c , uint8_t addr ) {
82
73
set_page (i2c , addr , 4 );
83
74
uint8_t rst [2 ] = { 0x3F , 0xAE }; // reset command
84
- uint8_t result = common_hal_busio_i2c_write (i2c , addr , rst , 2 , true);
85
- if (result != 0 ) {
86
- mp_printf (& mp_plat_print , "reset error %x\n" , result );
87
- }
75
+ common_hal_busio_i2c_write (i2c , addr , rst , 2 , true);
88
76
}
89
77
90
78
void set_current (busio_i2c_obj_t * i2c , uint8_t addr , uint8_t current ) {
91
79
set_page (i2c , addr , 4 );
92
80
uint8_t gcur [2 ] = { 0x01 , 0x00 }; // global current command
93
81
gcur [1 ] = current ;
94
- uint8_t result = common_hal_busio_i2c_write (i2c , addr , gcur , 2 , true);
95
- if (result != 0 ) {
96
- mp_printf (& mp_plat_print , "set current error %x\n" , result );
97
- }
82
+ common_hal_busio_i2c_write (i2c , addr , gcur , 2 , true);
98
83
}
99
84
100
85
uint8_t get_current (busio_i2c_obj_t * i2c , uint8_t addr ) {
101
86
set_page (i2c , addr , 4 );
102
87
uint8_t gcur = 0x01 ; // global current command
103
-
104
- uint8_t result = common_hal_busio_i2c_write (i2c , addr , & gcur , 1 , true);
105
- if (result != 0 ) {
106
- mp_printf (& mp_plat_print , "get current error %x\n" , result );
107
- }
88
+ common_hal_busio_i2c_write (i2c , addr , & gcur , 1 , true);
108
89
109
90
uint8_t data = 0 ;
110
- result = common_hal_busio_i2c_read (i2c , addr , & data , 1 );
111
- if (result != 0 ) {
112
- mp_printf (& mp_plat_print , "get current error %x\n" , result );
113
- }
114
-
91
+ common_hal_busio_i2c_read (i2c , addr , & data , 1 );
115
92
return data ;
116
93
}
117
94
@@ -129,10 +106,7 @@ void set_led(busio_i2c_obj_t *i2c, uint8_t addr, uint16_t led, uint8_t level, ui
129
106
130
107
cmd [1 ] = level ;
131
108
132
- uint8_t result = common_hal_busio_i2c_write (i2c , addr , cmd , 2 , true);
133
- if (result != 0 ) {
134
- mp_printf (& mp_plat_print , "set led error %x\n" , result );
135
- }
109
+ common_hal_busio_i2c_write (i2c , addr , cmd , 2 , true);
136
110
}
137
111
138
112
void drawPixel (busio_i2c_obj_t * i2c , uint8_t addr , int16_t x , int16_t y , uint32_t color ) {
@@ -168,7 +142,7 @@ void common_hal_is31fl3741_is31fl3741_construct(is31fl3741_is31fl3741_obj_t *sel
168
142
169
143
common_hal_is31fl3741_is31fl3741_reconstruct (self , framebuffer );
170
144
171
- common_hal_busio_i2c_try_lock ( i2c );
145
+ common_hal_displayio_is31fl3741_begin_transaction ( self );
172
146
173
147
uint8_t command = 0xFC ;
174
148
common_hal_busio_i2c_write (i2c , addr , & command , 1 , false);
@@ -184,7 +158,7 @@ void common_hal_is31fl3741_is31fl3741_construct(is31fl3741_is31fl3741_obj_t *sel
184
158
set_led (i2c , addr , i , 0xFF , 2 );
185
159
}
186
160
187
- common_hal_busio_i2c_unlock ( i2c );
161
+ common_hal_displayio_is31fl3741_end_transaction ( self );
188
162
}
189
163
190
164
void common_hal_is31fl3741_is31fl3741_reconstruct (is31fl3741_is31fl3741_obj_t * self , mp_obj_t framebuffer ) {
@@ -210,69 +184,12 @@ void common_hal_is31fl3741_is31fl3741_reconstruct(is31fl3741_is31fl3741_obj_t *s
210
184
self -> bufinfo .typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW ;
211
185
}
212
186
213
- /*
214
- memset(&self->protomatter, 0, sizeof(self->protomatter));
215
- ProtomatterStatus stat = _PM_init(&self->protomatter,
216
- self->width, self->bit_depth,
217
- self->rgb_count / 6, self->rgb_pins,
218
- self->addr_count, self->addr_pins,
219
- self->clock_pin, self->latch_pin, self->oe_pin,
220
- self->doublebuffer, self->serpentine ? -self->tile : self->tile,
221
- self->timer);
222
-
223
- if (stat == PROTOMATTER_OK) {
224
- _PM_protoPtr = &self->protomatter;
225
- common_hal_is31fl3741_timer_enable(self->timer);
226
- stat = _PM_begin(&self->protomatter);
227
-
228
- if (stat == PROTOMATTER_OK) {
229
- _PM_convert_565(&self->protomatter, self->bufinfo.buf, self->width);
230
- _PM_swapbuffer_maybe(&self->protomatter);
231
- }
232
- }
187
+ // initialize LEDs here
233
188
234
- if (stat != PROTOMATTER_OK) {
235
- common_hal_is31fl3741_is31fl3741_deinit(self);
236
- switch (stat) {
237
- case PROTOMATTER_ERR_PINS:
238
- mp_raise_ValueError(translate("Invalid pin"));
239
- break;
240
- case PROTOMATTER_ERR_ARG:
241
- mp_raise_ValueError(translate("Invalid argument"));
242
- break;
243
- case PROTOMATTER_ERR_MALLOC:
244
- mp_raise_msg(&mp_type_MemoryError, NULL);
245
- break;
246
- default:
247
- mp_raise_msg_varg(&mp_type_RuntimeError,
248
- translate("Internal error #%d"), (int)stat);
249
- break;
250
- }
251
- }
252
- */
253
189
self -> paused = 0 ;
254
190
}
255
191
256
192
void common_hal_is31fl3741_is31fl3741_deinit (is31fl3741_is31fl3741_obj_t * self ) {
257
- /*
258
- if (self->timer) {
259
- common_hal_is31fl3741_timer_free(self->timer);
260
- self->timer = 0;
261
- }
262
-
263
- if (_PM_protoPtr == &self->protomatter) {
264
- _PM_protoPtr = NULL;
265
- }
266
-
267
- if (self->protomatter.rgbPins) {
268
- _PM_deallocate(&self->protomatter);
269
- }
270
- memset(&self->protomatter, 0, sizeof(self->protomatter));
271
-
272
- // If it was supervisor-allocated, it is supervisor-freed and the pointer
273
- // is zeroed, otherwise the pointer is just zeroed
274
- _PM_free(self->bufinfo.buf);
275
- */
276
193
self -> base .type = NULL ;
277
194
278
195
// If a framebuffer was passed in to the constructor, NULL the reference
@@ -289,30 +206,67 @@ bool common_hal_is31fl3741_is31fl3741_get_paused(is31fl3741_is31fl3741_obj_t *se
289
206
}
290
207
291
208
void common_hal_is31fl3741_is31fl3741_set_global_current (is31fl3741_is31fl3741_obj_t * self , uint8_t current ) {
209
+ common_hal_displayio_is31fl3741_begin_transaction (self );
292
210
set_current (self -> i2c , self -> device_address , current );
211
+ common_hal_displayio_is31fl3741_end_transaction (self );
293
212
}
294
213
295
214
uint8_t common_hal_is31fl3741_is31fl3741_get_global_current (is31fl3741_is31fl3741_obj_t * self ) {
296
- return get_current (self -> i2c , self -> device_address );
215
+ common_hal_displayio_is31fl3741_begin_transaction (self );
216
+ uint8_t current = get_current (self -> i2c , self -> device_address );
217
+ common_hal_displayio_is31fl3741_end_transaction (self );
218
+ return current ;
297
219
}
298
220
299
221
void common_hal_is31fl3741_is31fl3741_refresh (is31fl3741_is31fl3741_obj_t * self , uint8_t * dirtyrows ) {
300
- uint8_t dirty_row_flags = 0xFF ;
222
+ common_hal_displayio_is31fl3741_begin_transaction (self );
223
+
224
+ uint8_t dirty_row_flags = 0xFF ; // only supports 8 rows gotta fix
301
225
if (dirtyrows != 0 ) {
302
226
dirty_row_flags = * dirtyrows ;
303
227
}
304
228
305
229
if (!self -> paused ) {
306
- uint32_t * buffer = self -> bufinfo .buf ;
307
- for (int y = 0 ; y < 5 ; y ++ ) {
308
- if ((dirty_row_flags >> y ) & 0x1 ) {
309
- for (int x = 0 ; x < 18 ; x ++ ) {
310
- drawPixel (self -> i2c , self -> device_address , x , y , * buffer );
311
- buffer ++ ;
230
+ if (self -> scale ) {
231
+ uint32_t * buffer = self -> bufinfo .buf ;
232
+
233
+ for (int x = 0 ; x < 18 ; x ++ ) {
234
+ uint32_t * ptr = & buffer [x * 3 ]; // Entry along top scan line w/x offset
235
+ for (int y = 0 ; y < 5 ; y ++ ) {
236
+ uint16_t rsum = 0 , gsum = 0 , bsum = 0 ;
237
+ // Inner x/y loops are row-major on purpose (less pointer math)
238
+ for (uint8_t yy = 0 ; yy < 3 ; yy ++ ) {
239
+ for (uint8_t xx = 0 ; xx < 3 ; xx ++ ) {
240
+ uint32_t rgb = ptr [xx ];
241
+ rsum += rgb >> 16 & 0xFF ;
242
+ gsum += (rgb >> 8 ) & 0xFF ;
243
+ bsum += rgb & 0xFF ;
244
+ }
245
+ ptr += 54 ; // canvas->width(); // Advance one scan line
246
+ }
247
+ rsum = rsum / 9 ;
248
+ gsum = gsum / 9 ;
249
+ 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 );
254
+ }
255
+ }
256
+ } else {
257
+ uint32_t * buffer = self -> bufinfo .buf ;
258
+ for (int y = 0 ; y < self -> height ; y ++ ) {
259
+ if ((dirty_row_flags >> y ) & 0x1 ) {
260
+ for (int x = 0 ; x < self -> width ; x ++ ) {
261
+ drawPixel (self -> i2c , self -> device_address , x , y , * buffer );
262
+ buffer ++ ;
263
+ }
312
264
}
313
265
}
314
266
}
315
267
}
268
+
269
+ common_hal_displayio_is31fl3741_end_transaction (self );
316
270
}
317
271
318
272
int common_hal_is31fl3741_is31fl3741_get_width (is31fl3741_is31fl3741_obj_t * self ) {
@@ -323,6 +277,16 @@ int common_hal_is31fl3741_is31fl3741_get_height(is31fl3741_is31fl3741_obj_t *sel
323
277
return self -> height ;
324
278
}
325
279
280
+ void common_hal_displayio_is31fl3741_begin_transaction (is31fl3741_is31fl3741_obj_t * self ) {
281
+ while (!common_hal_busio_i2c_try_lock (self -> i2c )) {
282
+ RUN_BACKGROUND_TASKS ;
283
+ }
284
+ }
285
+
286
+ void common_hal_displayio_is31fl3741_end_transaction (is31fl3741_is31fl3741_obj_t * self ) {
287
+ common_hal_busio_i2c_unlock (self -> i2c );
288
+ }
289
+
326
290
void * common_hal_is31fl3741_allocator_impl (size_t sz ) {
327
291
supervisor_allocation * allocation = allocate_memory (align32_size (sz ), false, true);
328
292
return allocation ? allocation -> ptr : NULL ;
@@ -331,3 +295,7 @@ void *common_hal_is31fl3741_allocator_impl(size_t sz) {
331
295
void common_hal_is31fl3741_free_impl (void * ptr_in ) {
332
296
free_memory (allocation_from_ptr (ptr_in ));
333
297
}
298
+
299
+ void is31fl3741_is31fl3741_collect_ptrs (is31fl3741_is31fl3741_obj_t * self ) {
300
+ gc_collect_ptr (self -> framebuffer );
301
+ }
0 commit comments