Skip to content

Commit 2f277eb

Browse files
committed
Cleanup and i2c fix
1 parent e877449 commit 2f277eb

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

shared-module/displayio/__init__.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,33 @@ void reset_displays(void) {
223223
#endif
224224
#if CIRCUITPY_IS31FL3741
225225
} else if (displays[i].is31fl3741.base.type == &is31fl3741_is31fl3741_type) {
226-
is31fl3741_is31fl3741_obj_t *pm = &displays[i].is31fl3741;
227-
if (!any_display_uses_this_framebuffer(&pm->base)) {
228-
common_hal_is31fl3741_is31fl3741_deinit(pm);
226+
is31fl3741_is31fl3741_obj_t *is31 = &displays[i].is31fl3741;
227+
if (((uint32_t)is31->i2c) < ((uint32_t)&displays) ||
228+
((uint32_t)is31->i2c) > ((uint32_t)&displays + CIRCUITPY_DISPLAY_LIMIT)) {
229+
busio_i2c_obj_t *original_i2c = is31->i2c;
230+
#if BOARD_I2C
231+
// We don't need to move original_i2c if it is the board.I2C object because it is
232+
// statically allocated already. (Doing so would also make it impossible to reference in
233+
// a subsequent VM run.)
234+
if (original_i2c == common_hal_board_get_i2c()) {
235+
continue;
236+
}
237+
#endif
238+
memcpy(&is31->inline_i2c, original_i2c, sizeof(busio_i2c_obj_t));
239+
is31->i2c = &is31->inline_i2c;
240+
// Check for other displays that use the same i2c bus and swap them too.
241+
/*for (uint8_t j = i + 1; j < CIRCUITPY_DISPLAY_LIMIT; j++) {
242+
if (displays[i].i2cdisplay_bus.base.type == &displayio_i2cdisplay_type &&
243+
displays[i].i2cdisplay_bus.bus == original_i2c) {
244+
displays[i].i2cdisplay_bus.bus = &i2c->inline_bus;
245+
}
246+
}*/
247+
}
248+
249+
if (!any_display_uses_this_framebuffer(&is31->base)) {
250+
common_hal_is31fl3741_is31fl3741_deinit(is31);
229251
} else {
230-
common_hal_is31fl3741_is31fl3741_set_paused(pm, true);
252+
common_hal_is31fl3741_is31fl3741_set_paused(is31, true);
231253
}
232254
#endif
233255
#if CIRCUITPY_SHARPDISPLAY

shared-module/is31fl3741/is31fl3741.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ void common_hal_is31fl3741_is31fl3741_construct(is31fl3741_is31fl3741_obj_t *sel
5454

5555
self->i2c = i2c;
5656
self->device_address = addr;
57+
common_hal_busio_i2c_never_reset(self->i2c);
58+
// Our object is statically allocated off the heap so make sure the bus object lives to the end
59+
// of the heap as well.
60+
gc_never_free(self->i2c);
5761

5862
common_hal_is31fl3741_is31fl3741_reconstruct(self, framebuffer);
5963
}
@@ -83,14 +87,14 @@ void common_hal_is31fl3741_is31fl3741_reconstruct(is31fl3741_is31fl3741_obj_t *s
8387

8488
common_hal_displayio_is31fl3741_begin_transaction(self);
8589

86-
uint8_t command = 0xFC;
90+
uint8_t command = 0xFC; // device ID
8791
common_hal_busio_i2c_write(self->i2c, self->device_address, &command, 1, false);
8892
uint8_t data = 0;
8993
common_hal_busio_i2c_read(self->i2c, self->device_address, &data, 1);
9094

9195
is31fl3741_send_reset(self->i2c, self->device_address);
9296
is31fl3741_send_enable(self->i2c, self->device_address);
93-
is31fl3741_set_current(self->i2c, self->device_address, 0x08);
97+
is31fl3741_set_current(self->i2c, self->device_address, 0xFF);
9498

9599
// set scale to max for all
96100
for (int i; i < 351; i++) {
@@ -103,6 +107,12 @@ void common_hal_is31fl3741_is31fl3741_reconstruct(is31fl3741_is31fl3741_obj_t *s
103107
}
104108

105109
void common_hal_is31fl3741_is31fl3741_deinit(is31fl3741_is31fl3741_obj_t *self) {
110+
common_hal_displayio_is31fl3741_end_transaction(self); // in case we still had a lock
111+
112+
if (self->i2c == &self->inline_i2c) {
113+
common_hal_busio_i2c_deinit(self->i2c);
114+
}
115+
106116
self->base.type = NULL;
107117

108118
// If a framebuffer was passed in to the constructor, NULL the reference

shared-module/is31fl3741/is31fl3741.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef struct {
3737
mp_buffer_info_t bufinfo;
3838
uint16_t bufsize, width, height, scale_width, scale_height;
3939
busio_i2c_obj_t *i2c;
40+
busio_i2c_obj_t inline_i2c;
4041
uint8_t device_address;
4142
uint8_t bit_depth;
4243
bool paused;

0 commit comments

Comments
 (0)