diff --git a/micropython/modules/pico_display/pico_display.cpp b/micropython/modules/pico_display/pico_display.cpp index 0d931ffc..89a96625 100644 --- a/micropython/modules/pico_display/pico_display.cpp +++ b/micropython/modules/pico_display/pico_display.cpp @@ -20,9 +20,16 @@ mp_obj_t picodisplay_init(mp_obj_t buf_obj) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_obj, &bufinfo, MP_BUFFER_RW); picodisplay_buf_obj = buf_obj; - if(display == nullptr) - display = new PicoDisplay((uint16_t *)bufinfo.buf); + + // If a display already exists, delete it + if(display != nullptr) { + delete display; + } + + // Create a new display pointing to the newly provided buffer + display = new PicoDisplay((uint16_t *)bufinfo.buf); display->init(); + return mp_const_none; } diff --git a/micropython/modules/pico_display_2/pico_display_2.cpp b/micropython/modules/pico_display_2/pico_display_2.cpp index 6268c8a4..2d92454a 100644 --- a/micropython/modules/pico_display_2/pico_display_2.cpp +++ b/micropython/modules/pico_display_2/pico_display_2.cpp @@ -20,9 +20,16 @@ mp_obj_t picodisplay2_init(mp_obj_t buf_obj) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_obj, &bufinfo, MP_BUFFER_RW); picodisplay2_buf_obj = buf_obj; - if(display2 == nullptr) - display2 = new PicoDisplay2((uint16_t *)bufinfo.buf); + + // If a display already exists, delete it + if(display2 != nullptr) { + delete display2; + } + + // Create a new display pointing to the newly provided buffer + display2 = new PicoDisplay2((uint16_t *)bufinfo.buf); display2->init(); + return mp_const_none; }