lvgl / lv_binding_micropython

LVGL binding for MicroPython
MIT License
250 stars 161 forks source link

please expose active buffer index when double-buffering #194

Closed eudoxos closed 2 years ago

eudoxos commented 2 years ago

lv_disp_draw_buf_t has the buf_act pointer which is swapped as buffers are used in alternation when double-buffering. Please consider making the information about which buffer is active available to Python (perhaps a a bool, returning buf_act==buf1). Otherwise pure-python display drivers have to track that in python-side variable independently, as e.g. here.

I have a tentative fix which looks like this (in src/hal/lv_hal_disp.c) but perhaps there is some way for this already?

/**
 * Return index of the buffer being active. The result is undefined when draw_buf is not initialized.
 * @param pointer to the buffer
 * @return always 0 for single-buffering, 0 or 1 for double-buffering (depending on the buffer being active).
*/
uint8_t lv_disp_draw_buf_get_active_buffer_index(lv_disp_draw_buf_t * draw_buf){
    if(draw_buf->buf_act == draw_buf->buf1) return 0;
    return 1;
}
amirgon commented 2 years ago

In general issues/PRs to LVGL C API should be opened under https://github.com/lvgl/lvgl.

But are you sure this is needed in this case? Why not compare buf_act in Python?

buf1_active = (draw_buf.buf_act == draw_buf.buf1)

I've just added a very simple fix to allow that.

eudoxos commented 2 years ago

In general issues/PRs to LVGL C API should be opened under https://github.com/lvgl/lvgl.

Right, I just was not sure if this was not more python-related. And it was actually. Your solution is perfect. I need to check better the API for Blob.

Actually, I can just use __dereference__ to get memoryview on the current buffer (based on this post of yours), without keeping the buffers around as python objects... (as you see, I love the := operator in python ;) )


disp_draw_buf=lv.disp_draw_buf_t()
disp_draw_buf.init(fb1:=bytearray(HRES*2*32),bytearray(HRES*2*32),len(fb1)//lv.color_t.__SIZE__)
def disp_drv_flush_cb(disp_drv,area,color):
    lcd.blit(area.x1,area.y1,w:=(area.x2-area.x1+1),h:=(area.y2-area.y1+1),disp_drv.draw_buf.buf_act.__dereference__(2*w*h),is_blocking=False)
    disp_drv.flush_ready()
...
amirgon commented 2 years ago

Actually, I can just use __dereference__ to get memoryview on the current buffer

Right. Blob and Struct in lv_bindings context are really nothing more than thin wrappers around a C pointer.

amirgon commented 2 years ago

Closing - please reopen if there are any further problems.