nrf5/lcd: Updating framebuffer with double buffer for epaper displays. Moving statics into instance struct. Adding new function to refresh using old buffer, such that epaper can get a cleaner image after update.
This commit is contained in:
parent
11fc46ca6f
commit
70198b07a5
|
@ -26,6 +26,7 @@
|
|||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "py/nlr.h"
|
||||
#include "py/obj.h"
|
||||
|
@ -42,18 +43,18 @@
|
|||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
fb_byte_t * fb_bytes;
|
||||
fb_byte_t * fb_old;
|
||||
fb_byte_t * fb_dirty;
|
||||
uint16_t height;
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
uint16_t width;
|
||||
mp_uint_t bytes_stride;
|
||||
mp_uint_t dirty_stride;
|
||||
mp_obj_t line_update_cb;
|
||||
mp_obj_t line_update_cb;
|
||||
mp_uint_t bg_color;
|
||||
mp_uint_t fg_color;
|
||||
mp_uint_t font_size;
|
||||
} mp_obj_framebuf_t;
|
||||
|
||||
static uint8_t m_bg_color;
|
||||
static uint8_t m_fg_color;
|
||||
static uint8_t m_font_size;
|
||||
|
||||
STATIC void lcd_enable_pixel(mp_obj_framebuf_t * p_framebuffer, uint16_t x, uint16_t y) {
|
||||
uint16_t column = (x / 8);
|
||||
uint16_t line = y;
|
||||
|
@ -73,35 +74,35 @@ STATIC void lcd_disable_pixel(mp_obj_framebuf_t * p_framebuffer, uint16_t x, uin
|
|||
}
|
||||
|
||||
STATIC void lcd_init(mp_obj_framebuf_t * p_framebuffer) {
|
||||
m_fg_color = LCD_BLACK;
|
||||
m_bg_color = LCD_WHITE;
|
||||
p_framebuffer->fg_color = LCD_BLACK;
|
||||
p_framebuffer->bg_color = LCD_WHITE;
|
||||
|
||||
memset(p_framebuffer->fb_bytes, 0x00, p_framebuffer->bytes_stride * p_framebuffer->height);
|
||||
memset(p_framebuffer->fb_dirty, 0x00, p_framebuffer->dirty_stride);
|
||||
}
|
||||
|
||||
STATIC void lcd_fg_color_set(mp_obj_framebuf_t * p_framebuffer, uint16_t color) {
|
||||
m_fg_color = (color == 0) ? LCD_BLACK : LCD_WHITE;
|
||||
p_framebuffer->fg_color = (color == 0) ? LCD_BLACK : LCD_WHITE;
|
||||
}
|
||||
|
||||
#if 0
|
||||
STATIC uint16_t lcd_fg_color_get(mp_obj_framebuf_t * p_framebuffer) {
|
||||
return m_fg_color;
|
||||
return p_framebuffer->fg_color;
|
||||
}
|
||||
#endif
|
||||
|
||||
STATIC void lcd_bg_color_set(mp_obj_framebuf_t * p_framebuffer, uint16_t color) {
|
||||
m_bg_color = (color == 0) ? LCD_BLACK : LCD_WHITE;
|
||||
p_framebuffer->bg_color = (color == 0) ? LCD_BLACK : LCD_WHITE;
|
||||
}
|
||||
|
||||
#if 0
|
||||
STATIC uint16_t lcd_bg_color_get(mp_obj_framebuf_t * p_framebuffer) {
|
||||
return m_bg_color;
|
||||
return p_framebuffer->bg_color;
|
||||
}
|
||||
#endif
|
||||
|
||||
STATIC void lcd_clear_screen(mp_obj_framebuf_t * p_framebuffer) {
|
||||
if (m_bg_color == LCD_BLACK) {
|
||||
if (p_framebuffer->bg_color == LCD_BLACK) {
|
||||
memset(p_framebuffer->fb_bytes, 0x00, p_framebuffer->bytes_stride * p_framebuffer->height);
|
||||
} else {
|
||||
memset(p_framebuffer->fb_bytes, 0xFF, p_framebuffer->bytes_stride * p_framebuffer->height);
|
||||
|
@ -113,34 +114,34 @@ STATIC void lcd_print_char(mp_obj_framebuf_t * p_framebuffer, uint16_t x, uint16
|
|||
uint16_t col = x;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
|
||||
uint16_t current_col = col + (i * m_font_size);
|
||||
uint16_t current_col = col + (i * p_framebuffer->font_size);
|
||||
|
||||
for (uint8_t y_pos = 0; y_pos < 8; y_pos++) {
|
||||
if ((((uint8_t)font_petme128_8x8[((ch - 32) * 8) + i]) >> y_pos) & 0x01) {
|
||||
for (uint8_t s_w = 0; s_w < m_font_size; s_w++) {
|
||||
for (uint8_t s_h = 0; s_h < m_font_size; s_h++) {
|
||||
if (m_fg_color < LCD_WHITE) {
|
||||
for (uint8_t s_w = 0; s_w < p_framebuffer->font_size; s_w++) {
|
||||
for (uint8_t s_h = 0; s_h < p_framebuffer->font_size; s_h++) {
|
||||
if (p_framebuffer->fg_color < LCD_WHITE) {
|
||||
lcd_disable_pixel(p_framebuffer,
|
||||
current_col + s_w,
|
||||
y + (y_pos * m_font_size) + s_h);
|
||||
y + (y_pos * p_framebuffer->font_size) + s_h);
|
||||
} else {
|
||||
lcd_enable_pixel(p_framebuffer,
|
||||
current_col + s_w,
|
||||
y + (y_pos * m_font_size) + s_h);
|
||||
y + (y_pos * p_framebuffer->font_size) + s_h);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (uint8_t s_w = 0; s_w < m_font_size; s_w++) {
|
||||
for (uint8_t s_h = 0; s_h < m_font_size; s_h++) {
|
||||
if (m_bg_color < LCD_WHITE) {
|
||||
for (uint8_t s_w = 0; s_w < p_framebuffer->font_size; s_w++) {
|
||||
for (uint8_t s_h = 0; s_h < p_framebuffer->font_size; s_h++) {
|
||||
if (p_framebuffer->bg_color < LCD_WHITE) {
|
||||
lcd_disable_pixel(p_framebuffer,
|
||||
current_col + s_w,
|
||||
y + (y_pos * m_font_size) + s_h);
|
||||
y + (y_pos * p_framebuffer->font_size) + s_h);
|
||||
} else {
|
||||
lcd_enable_pixel(p_framebuffer,
|
||||
current_col + s_w,
|
||||
y + (y_pos * m_font_size) + s_h);
|
||||
y + (y_pos * p_framebuffer->font_size) + s_h);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -162,7 +163,7 @@ STATIC uint8_t lcd_font_size_get(mp_obj_framebuf_t * p_framebuffer) {
|
|||
STATIC void lcd_print_string(mp_obj_framebuf_t * p_framebuffer, uint16_t x, uint16_t y, const char * p_str) {
|
||||
uint16_t str_len = strlen(p_str);
|
||||
for (uint16_t i = 0; i < str_len; i++) {
|
||||
lcd_print_char(p_framebuffer, x + (i * 8 * m_font_size), y, p_str[i]);
|
||||
lcd_print_char(p_framebuffer, x + (i * 8 * p_framebuffer->font_size), y, p_str[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -174,22 +175,44 @@ STATIC void lcd_pixel_draw(mp_obj_framebuf_t * p_framebuffer, uint16_t x, uint16
|
|||
}
|
||||
}
|
||||
|
||||
STATIC void lcd_update(mp_obj_framebuf_t * p_framebuffer) {
|
||||
STATIC void lcd_update(mp_obj_framebuf_t * p_framebuffer, bool refresh) {
|
||||
for (uint16_t i = 0; i < p_framebuffer->dirty_stride; i++) {
|
||||
if (p_framebuffer->fb_dirty[i].byte != 0) {
|
||||
if (p_framebuffer->fb_dirty[i].byte != 0 || refresh) {
|
||||
for (uint16_t b = 0; b < 8; b++) {
|
||||
if (((p_framebuffer->fb_dirty[i].byte >> b) & 0x01) == 1) {
|
||||
if ((((p_framebuffer->fb_dirty[i].byte >> b) & 0x01) == 1) || refresh) {
|
||||
uint16_t line_num = (i * 8) + b;
|
||||
mp_obj_t args[3];
|
||||
mp_obj_t args[4];
|
||||
mp_uint_t num_of_args = 3;
|
||||
args[0] = p_framebuffer;
|
||||
args[1] = MP_OBJ_NEW_SMALL_INT(line_num);
|
||||
args[2] = mp_obj_new_bytearray_by_ref(p_framebuffer->bytes_stride,
|
||||
&p_framebuffer->fb_bytes[line_num * p_framebuffer->bytes_stride]);
|
||||
mp_call_function_n_kw(p_framebuffer->line_update_cb, 3, 0, args);
|
||||
|
||||
if (refresh == false) {
|
||||
args[2] = mp_obj_new_bytearray_by_ref(p_framebuffer->bytes_stride,
|
||||
&p_framebuffer->fb_bytes[line_num * p_framebuffer->bytes_stride]);
|
||||
} else {
|
||||
args[2] = mp_const_none;
|
||||
}
|
||||
|
||||
if (p_framebuffer->fb_old != NULL) {
|
||||
args[3] = mp_obj_new_bytearray_by_ref(p_framebuffer->bytes_stride,
|
||||
&p_framebuffer->fb_bytes[line_num * p_framebuffer->bytes_stride]);
|
||||
num_of_args = 4;
|
||||
}
|
||||
|
||||
mp_call_function_n_kw(p_framebuffer->line_update_cb, num_of_args, 0, args);
|
||||
|
||||
// update old buffer
|
||||
if (p_framebuffer->fb_old != NULL) {
|
||||
memcpy(&p_framebuffer->fb_old[line_num * p_framebuffer->bytes_stride],
|
||||
&p_framebuffer->fb_bytes[line_num * p_framebuffer->bytes_stride],
|
||||
p_framebuffer->dirty_stride);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p_framebuffer->fb_dirty[i].byte = 0x00;
|
||||
if (refresh == false) {
|
||||
p_framebuffer->fb_dirty[i].byte = 0x00;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -211,7 +234,16 @@ STATIC mp_obj_t lcd_mono_fb_make_new(const mp_obj_type_t *type, size_t n_args, s
|
|||
o->fb_bytes = m_new(fb_byte_t, (o->bytes_stride) * o->height);
|
||||
o->fb_dirty = m_new(fb_byte_t, o->dirty_stride);
|
||||
|
||||
m_font_size = 1;
|
||||
// default to not use double buffer
|
||||
o->fb_old = NULL;
|
||||
|
||||
o->font_size = 1;
|
||||
|
||||
if (n_args >= 4) {
|
||||
if (mp_obj_is_true(args[3])) {
|
||||
o->fb_old = m_new(fb_byte_t, (o->bytes_stride) * o->height);
|
||||
}
|
||||
}
|
||||
|
||||
lcd_init(o);
|
||||
|
||||
|
@ -270,18 +302,33 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lcd_mono_fb_text_obj, 4, 5, lcd_mono_
|
|||
STATIC mp_obj_t lcd_mono_fb_show(mp_obj_t self_in) {
|
||||
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
lcd_update(self);
|
||||
lcd_update(self, false);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lcd_mono_fb_show_obj, lcd_mono_fb_show);
|
||||
|
||||
STATIC mp_obj_t lcd_mono_fb_refresh(mp_obj_t self_in) {
|
||||
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
lcd_update(self, true);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lcd_mono_fb_refresh_obj, lcd_mono_fb_refresh);
|
||||
|
||||
|
||||
STATIC mp_obj_t lcd_mono_fb_del(mp_obj_t self_in) {
|
||||
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
m_free(self->fb_bytes);
|
||||
m_free(self->fb_dirty);
|
||||
|
||||
if (self->fb_old != NULL) {
|
||||
// free double buffer if used
|
||||
m_free(self->fb_old);
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lcd_mono_fb_del_obj, lcd_mono_fb_del);
|
||||
|
@ -293,6 +340,7 @@ STATIC const mp_rom_map_elem_t lcd_mono_fb_locals_dict_table[] = {
|
|||
{ MP_ROM_QSTR(MP_QSTR_scroll), MP_ROM_PTR(&lcd_mono_fb_scroll_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&lcd_mono_fb_text_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&lcd_mono_fb_show_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_refresh), MP_ROM_PTR(&lcd_mono_fb_refresh_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(lcd_mono_fb_locals_dict, lcd_mono_fb_locals_dict_table);
|
||||
|
||||
|
|
Loading…
Reference in New Issue