Skip to content

Commit 24b3096

Browse files
committed
Refresh ePaper displays once code.py is done running
1 parent 36a23e0 commit 24b3096

File tree

6 files changed

+52
-13
lines changed

6 files changed

+52
-13
lines changed

main.c

+8
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ bool run_code_py(safe_mode_t safe_mode) {
253253
}
254254

255255
bool serial_connected_before_animation = false;
256+
bool refreshed_epaper_display = false;
256257
rgb_status_animation_t animation;
257258
prep_rgb_status_animation(&result, found_main, safe_mode, &animation);
258259
while (true) {
@@ -290,6 +291,13 @@ bool run_code_py(safe_mode_t safe_mode) {
290291
}
291292
serial_connected_before_animation = serial_connected();
292293

294+
// Refresh the ePaper display if we have one. That way it'll show an error message.
295+
#if CIRCUITPY_DISPLAYIO
296+
if (!refreshed_epaper_display) {
297+
refreshed_epaper_display = maybe_refresh_epaperdisplay();
298+
}
299+
#endif
300+
293301
tick_rgb_status_animation(&animation);
294302
}
295303
}

shared-bindings/displayio/__init__.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
//|
7878
//| Releases any actively used displays so their busses and pins can be used again. This will also
7979
//| release the builtin display on boards that have one. You will need to reinitialize it yourself
80-
//| afterwards.
80+
//| afterwards. This may take seconds to complete if an active EPaperDisplay is refreshing.
8181
//|
8282
//| Use this once in your code.py if you initialize a display. Place it right before the
8383
//| initialization so the display is active as long as possible.

shared-module/displayio/EPaperDisplay.c

+27
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ void displayio_epaperdisplay_start_refresh(displayio_epaperdisplay_obj_t* self)
170170
}
171171

172172
uint32_t common_hal_displayio_epaperdisplay_get_time_to_refresh(displayio_epaperdisplay_obj_t* self) {
173+
if (self->core.last_refresh == 0) {
174+
return 0;
175+
}
173176
// Refresh at seconds per frame rate.
174177
uint32_t elapsed_time = ticks_ms - self->core.last_refresh;
175178
if (elapsed_time > self->milliseconds_per_frame) {
@@ -343,6 +346,13 @@ void displayio_epaperdisplay_background(displayio_epaperdisplay_obj_t* self) {
343346
}
344347

345348
void release_epaperdisplay(displayio_epaperdisplay_obj_t* self) {
349+
if (self->refreshing) {
350+
wait_for_busy(self);
351+
self->refreshing = false;
352+
// Run stop sequence but don't wait for busy because busy is set when sleeping.
353+
send_command_sequence(self, false, self->stop_sequence, self->stop_sequence_len);
354+
}
355+
346356
release_display_core(&self->core);
347357
if (self->busy.base.type == &digitalio_digitalinout_type) {
348358
common_hal_digitalio_digitalinout_deinit(&self->busy);
@@ -352,3 +362,20 @@ void release_epaperdisplay(displayio_epaperdisplay_obj_t* self) {
352362
void displayio_epaperdisplay_collect_ptrs(displayio_epaperdisplay_obj_t* self) {
353363
displayio_display_core_collect_ptrs(&self->core);
354364
}
365+
366+
bool maybe_refresh_epaperdisplay(void) {
367+
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
368+
if (displays[i].epaper_display.base.type != &displayio_epaperdisplay_type ||
369+
displays[i].epaper_display.core.current_group != &circuitpython_splash) {
370+
// Skip regular displays and those not showing the splash.
371+
continue;
372+
}
373+
displayio_epaperdisplay_obj_t* display = &displays[i].epaper_display;
374+
if (common_hal_displayio_epaperdisplay_get_time_to_refresh(display) != 0) {
375+
return false;
376+
}
377+
return common_hal_displayio_epaperdisplay_refresh(display);
378+
}
379+
// Return true if no ePaper displays are available to pretend it was updated.
380+
return true;
381+
}

shared-module/displayio/EPaperDisplay.h

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ typedef struct {
6060

6161
void displayio_epaperdisplay_background(displayio_epaperdisplay_obj_t* self);
6262
void release_epaperdisplay(displayio_epaperdisplay_obj_t* self);
63+
bool maybe_refresh_epaperdisplay(void);
6364

6465
void displayio_epaperdisplay_collect_ptrs(displayio_epaperdisplay_obj_t* self);
6566

shared-module/displayio/__init__.c

+13-11
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ void displayio_background(void) {
5454
}
5555

5656
void common_hal_displayio_release_displays(void) {
57+
// Release displays before busses so that they can send any final commands to turn the display
58+
// off properly.
59+
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
60+
mp_const_obj_t display_type = displays[i].display.base.type;
61+
if (display_type == NULL || display_type == &mp_type_NoneType) {
62+
continue;
63+
} else if (display_type == &displayio_display_type) {
64+
release_display(&displays[i].display);
65+
} else if (display_type == &displayio_epaperdisplay_type) {
66+
release_epaperdisplay(&displays[i].epaper_display);
67+
}
68+
displays[i].display.base.type = &mp_type_NoneType;
69+
}
5770
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
5871
mp_const_obj_t bus_type = displays[i].fourwire_bus.base.type;
5972
if (bus_type == NULL || bus_type == &mp_type_NoneType) {
@@ -67,17 +80,6 @@ void common_hal_displayio_release_displays(void) {
6780
}
6881
displays[i].fourwire_bus.base.type = &mp_type_NoneType;
6982
}
70-
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
71-
mp_const_obj_t display_type = displays[i].display.base.type;
72-
if (display_type == NULL || display_type == &mp_type_NoneType) {
73-
continue;
74-
} else if (display_type == &displayio_display_type) {
75-
release_display(&displays[i].display);
76-
} else if (display_type == &displayio_epaperdisplay_type) {
77-
release_epaperdisplay(&displays[i].epaper_display);
78-
}
79-
displays[i].display.base.type = &mp_type_NoneType;
80-
}
8183

8284
supervisor_stop_terminal();
8385
}

shared-module/displayio/display_core.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ void displayio_display_core_construct(displayio_display_core_t* self,
5252
self->current_group = NULL;
5353
self->colstart = colstart;
5454
self->rowstart = rowstart;
55+
self->last_refresh = 0;
5556

5657
if (MP_OBJ_IS_TYPE(bus, &displayio_parallelbus_type)) {
5758
self->bus_reset = common_hal_displayio_parallelbus_reset;
@@ -140,7 +141,7 @@ bool displayio_display_core_show(displayio_display_core_t* self, displayio_group
140141
if (!circuitpython_splash.in_group) {
141142
root_group = &circuitpython_splash;
142143
} else if (self->current_group == &circuitpython_splash) {
143-
return false;
144+
return true;
144145
}
145146
}
146147
if (root_group == self->current_group) {

0 commit comments

Comments
 (0)