Skip to content

Commit

Permalink
PicoGraphics: Non-blocking Inky update for #936.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Oct 31, 2024
1 parent f2d84fa commit 96beb08
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
6 changes: 6 additions & 0 deletions micropython/modules/picographics/picographics.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(ModPicoGraphics_update_obj, ModPicoGraphics_update);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ModPicoGraphics_partial_update_obj, 5, 5, ModPicoGraphics_partial_update);
MP_DEFINE_CONST_FUN_OBJ_2(ModPicoGraphics_set_backlight_obj, ModPicoGraphics_set_backlight);
MP_DEFINE_CONST_FUN_OBJ_2(ModPicoGraphics_set_update_speed_obj, ModPicoGraphics_set_update_speed);
MP_DEFINE_CONST_FUN_OBJ_2(ModPicoGraphics_set_blocking_obj, ModPicoGraphics_set_blocking);
MP_DEFINE_CONST_FUN_OBJ_1(ModPicoGraphics_is_busy_obj, ModPicoGraphics_is_busy);

// Palette management
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ModPicoGraphics_update_pen_obj, 5, 5, ModPicoGraphics_update_pen);
Expand Down Expand Up @@ -69,6 +71,10 @@ static const mp_rom_map_elem_t ModPicoGraphics_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&ModPicoGraphics_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_partial_update), MP_ROM_PTR(&ModPicoGraphics_partial_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_update_speed), MP_ROM_PTR(&ModPicoGraphics_set_update_speed_obj) },

{ MP_ROM_QSTR(MP_QSTR_set_blocking), MP_ROM_PTR(&ModPicoGraphics_set_blocking_obj) },
{ MP_ROM_QSTR(MP_QSTR_is_busy), MP_ROM_PTR(&ModPicoGraphics_is_busy_obj) },

{ MP_ROM_QSTR(MP_QSTR_set_clip), MP_ROM_PTR(&ModPicoGraphics_set_clip_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_clip), MP_ROM_PTR(&ModPicoGraphics_get_clip_obj) },
{ MP_ROM_QSTR(MP_QSTR_remove_clip), MP_ROM_PTR(&ModPicoGraphics_remove_clip_obj) },
Expand Down
36 changes: 26 additions & 10 deletions micropython/modules/picographics/picographics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ typedef struct _ModPicoGraphics_obj_t {
void *buffer;
void *fontdata;
_PimoroniI2C_obj_t *i2c;
bool blocking = true;
//mp_obj_t scanline_callback; // Not really feasible in MicroPython
} ModPicoGraphics_obj_t;

Expand Down Expand Up @@ -643,6 +644,17 @@ mp_obj_t ModPicoGraphics_set_scanline_callback(mp_obj_t self_in, mp_obj_t cb_in)
}
*/

mp_obj_t ModPicoGraphics_set_blocking(mp_obj_t self_in, mp_obj_t blocking_in) {
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
self->blocking = blocking_in == mp_const_true;
return mp_const_none;
}

mp_obj_t ModPicoGraphics_is_busy(mp_obj_t self_in) {
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
return self->display->is_busy() ? mp_const_true : mp_const_false;
}

mp_obj_t ModPicoGraphics_update(mp_obj_t self_in) {
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
/*
Expand All @@ -666,13 +678,15 @@ mp_obj_t ModPicoGraphics_update(mp_obj_t self_in) {

self->display->update(self->graphics);

while(self->display->is_busy()) {
#ifdef mp_event_handle_nowait
mp_event_handle_nowait();
#endif
}
if(self->blocking) {
while(self->display->is_busy()) {
#ifdef mp_event_handle_nowait
mp_event_handle_nowait();
#endif
}

self->display->power_off();
self->display->power_off();
}

return mp_const_none;
}
Expand All @@ -695,10 +709,12 @@ mp_obj_t ModPicoGraphics_partial_update(size_t n_args, const mp_obj_t *args) {
mp_obj_get_int(args[ARG_h])
});

while(self->display->is_busy()) {
#ifdef mp_event_handle_nowait
mp_event_handle_nowait();
#endif
if(self->blocking) {
while(self->display->is_busy()) {
#ifdef mp_event_handle_nowait
mp_event_handle_nowait();
#endif
}
}

return mp_const_none;
Expand Down
3 changes: 3 additions & 0 deletions micropython/modules/picographics/picographics.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ extern mp_obj_t ModPicoGraphics_partial_update(size_t n_args, const mp_obj_t *ar
extern mp_obj_t ModPicoGraphics_set_backlight(mp_obj_t self_in, mp_obj_t brightness);
extern mp_obj_t ModPicoGraphics_set_update_speed(mp_obj_t self_in, mp_obj_t update_speed);

extern mp_obj_t ModPicoGraphics_set_blocking(mp_obj_t self_in, mp_obj_t blocking_in);
extern mp_obj_t ModPicoGraphics_is_busy(mp_obj_t self_in);

// Palette management
extern mp_obj_t ModPicoGraphics_update_pen(size_t n_args, const mp_obj_t *args);
extern mp_obj_t ModPicoGraphics_reset_pen(mp_obj_t self_in, mp_obj_t pen);
Expand Down

0 comments on commit 96beb08

Please sign in to comment.