diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index d9055f13c1f6247c256ad16e4b12c286ccbec356..48f7d4d6a7728dfdc813088fb9a642d1ce870c76 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -1677,7 +1677,6 @@ API(API_DISP_BLIT, int epic_disp_blit( int16_t y, int16_t w, int16_t h, - size_t size, uint16_t *img, uint8_t *alpha )); diff --git a/epicardium/modules/display.c b/epicardium/modules/display.c index 21be30a3b4e911201376776fc92f2204dee2ddaa..d7607a2027735db1813c86fc49c8a1718756d0b3 100644 --- a/epicardium/modules/display.c +++ b/epicardium/modules/display.c @@ -92,7 +92,6 @@ int epic_disp_blit( int16_t pos_y, int16_t width, int16_t height, - size_t size, uint16_t *img, uint8_t *alpha ) { @@ -107,10 +106,10 @@ int epic_disp_blit( int16_t count_y = height - offset_y; if (pos_x + width >= 160) { - count_x -= (pos_x + width) - 160; + count_x -= (pos_x + width) % 160; } if (pos_y + height >= 80) { - count_y -= (pos_y + height) - 80; + count_y -= (pos_y + height) % 80; } if (offset_x == 0 && offset_y == 0 && count_x == width && @@ -123,7 +122,7 @@ int epic_disp_blit( width, height, GFX_RAW, - size, + width * height * 2, img ); } else { @@ -140,7 +139,7 @@ int epic_disp_blit( count_x, 1, GFX_RAW, - size, + count_x * 2, curr_img ); } diff --git a/pycardium/modules/py/display.py b/pycardium/modules/py/display.py index d6bc0dc8a57e628772936528190194d4a17d9f4a..20616e14757fb03b5e51fd9ffe462803150ac4cd 100644 --- a/pycardium/modules/py/display.py +++ b/pycardium/modules/py/display.py @@ -120,7 +120,7 @@ class Display: sys_display.pixel(x, y, col) return self - def blit(self, x, y, w, h, img, alpha=None): + def blit(self, x, y, w, h, img, rgb565=False, alpha=None): """ Blit an image to the display @@ -128,8 +128,9 @@ class Display: :param y: top left Y coordinate :param w: image width :param h: image height - :param img: a byte buffer with pixel data (rgb565) + :param img: a byte buffer with pixel data (RGB) :param alpha: alpha mask for `img` + :param rgb565: Set to `True` if the data supplied is in rgb565 format .. note:: The alpha channel is not implemented yet. @@ -137,12 +138,13 @@ class Display: """ # TODO: alpha is not yet supported by epicardium - assert alpha == None + if alpha is not None: + raise ValueError("alpha not yet supported") if alpha is None: - sys_display.blit(x, y, w, h, img) + sys_display.blit(x, y, w, h, img, rgb565) else: - sys_display.blit(x, y, w, h, img, alpha) + sys_display.blit(x, y, w, h, img, rgb565, alpha) return self diff --git a/pycardium/modules/sys_display.c b/pycardium/modules/sys_display.c index a4713c0a18065d662991b9fbf6570967958a8576..fa416dc66cb6f45e43828a89246f1c3408faab64 100644 --- a/pycardium/modules/sys_display.c +++ b/pycardium/modules/sys_display.c @@ -100,10 +100,11 @@ static mp_obj_t mp_display_blit(size_t n_args, const mp_obj_t *args) /* Required arguments: posx, posy (on display), width, height (of image), buffer (rgb data of image) */ - int pos_x = mp_obj_get_int(args[0]); - int pos_y = mp_obj_get_int(args[1]); - int width = mp_obj_get_int(args[2]); - int height = mp_obj_get_int(args[3]); + int pos_x = mp_obj_get_int(args[0]); + int pos_y = mp_obj_get_int(args[1]); + int width = mp_obj_get_int(args[2]); + int height = mp_obj_get_int(args[3]); + bool rgb565 = mp_obj_is_true(args[5]); mp_buffer_info_t img; int res = 0; @@ -112,15 +113,28 @@ static mp_obj_t mp_display_blit(size_t n_args, const mp_obj_t *args) if (!mp_get_buffer(args[4], &img, MP_BUFFER_READ)) { mp_raise_TypeError("'img' does not support buffer protocol."); } - if ((int)img.len < width * height * 2) { + + int bpp = rgb565 ? 2 : 3; + if ((int)img.len < width * height * bpp) { mp_raise_ValueError("'img' is too small."); } - if (n_args > 5) { + uint16_t *buf; + if (rgb565) { + buf = (uint16_t *)img.buf; + } else { + /* Will raise an exception if out of memory: */ + buf = m_malloc(width * height * bpp); + for (int i = 0; i < width * height; i++) { + buf[i] = rgb888_to_rgb565(((uint8_t *)img.buf) + 3 * i); + } + } + + if (n_args > 6) { mp_buffer_info_t alpha; /* Load alpha buffer and check size */ - if (!mp_get_buffer(args[5], &alpha, MP_BUFFER_READ)) { + if (!mp_get_buffer(args[6], &alpha, MP_BUFFER_READ)) { mp_raise_TypeError( "'alpha' does not support buffer protocol." ); @@ -130,24 +144,10 @@ static mp_obj_t mp_display_blit(size_t n_args, const mp_obj_t *args) } res = epic_disp_blit( - pos_x, - pos_y, - width, - height, - img.len, - (uint16_t *)img.buf, - (uint8_t *)alpha.buf + pos_x, pos_y, width, height, buf, (uint8_t *)alpha.buf ); } else { - res = epic_disp_blit( - pos_x, - pos_y, - width, - height, - img.len, - (uint16_t *)img.buf, - NULL - ); + res = epic_disp_blit(pos_x, pos_y, width, height, buf, NULL); } if (res < 0) {