From 4e5b545de3489fcd292a286d3900c2c3c09cb0f3 Mon Sep 17 00:00:00 2001 From: Robert Lukierski Date: Sat, 18 Jan 2025 10:17:25 +0100 Subject: [PATCH] fixes-for-esp32 --- ext_mod/lcd_bus/esp32_include/rgb565_dither.h | 73 +++---------------- ext_mod/lcd_bus/esp32_src/rgb565_dither.c | 63 ++++++++++++++++ ext_mod/lcd_bus/micropython.cmake | 1 + 3 files changed, 73 insertions(+), 64 deletions(-) diff --git a/ext_mod/lcd_bus/esp32_include/rgb565_dither.h b/ext_mod/lcd_bus/esp32_include/rgb565_dither.h index 9356e40c..fd19b9b6 100644 --- a/ext_mod/lcd_bus/esp32_include/rgb565_dither.h +++ b/ext_mod/lcd_bus/esp32_include/rgb565_dither.h @@ -1,77 +1,22 @@ #ifndef __RGB565_DITHER_H__ #define __RGB565_DITHER_H__ - #include +#include +#include - #define CALC_THRESHOLD(x, y) (uint8_t)((y & 7) << 3) + (x & 7) + #define CALC_THRESHOLD(x, y) (uint8_t)(((y & 7) << 3) + (x & 7)) - static uint8_t *red_thresh; - static uint8_t *green_thresh; - static uint8_t *blue_thresh; + extern uint8_t *red_thresh; + extern uint8_t *green_thresh; + extern uint8_t *blue_thresh; + bool rgb565_dither_init(void); static inline void rgb565_dither_pixel(uint8_t treshold_id, uint16_t *pixel) { *pixel = (((((*pixel >> 8) & 0xF8) + red_thresh[treshold_id]) << 8) | ((((*pixel >> 3) & 0xFC) + green_thresh[treshold_id]) << 3) | - ((((*pixel & 0x1F) << 3) + blue_thresh[treshold_id]) >> 3)); + ((((*pixel & 0x1F) << 3) + blue_thresh[treshold_id]) >> 3)) ; } - - static inline bool rgb565_dither_init(void) - { - if (red_thresh == NULL) { - red_thresh = (uint8_t *)malloc(64); - if (red_thresh != NULL) { - memcpy(red_thresh, - (uint8_t []){ - 1, 7, 3, 5, 0, 8, 2, 6, - 7, 1, 5, 3, 8, 0, 6, 2, - 3, 5, 0, 8, 2, 6, 1, 7, - 5, 3, 8, 0, 6, 2, 7, 1, - 0, 8, 2, 6, 1, 7, 3, 5, - 8, 0, 6, 2, 7, 1, 5, 3, - 2, 6, 1, 7, 3, 5, 0, 8, - 6, 2, 7, 1, 5, 3, 8, 0 - }, 64); - } - } - - if (green_thresh == NULL) { - green_thresh = (uint8_t *)malloc(64); - if (green_thresh != NULL) { - memcpy(green_thresh, - (uint8_t []){ - 1, 3, 2, 2, 3, 1, 2, 2, - 2, 2, 0, 4, 2, 2, 4, 0, - 3, 1, 2, 2, 1, 3, 2, 2, - 2, 2, 4, 0, 2, 2, 0, 4, - 1, 3, 2, 2, 3, 1, 2, 2, - 2, 2, 0, 4, 2, 2, 4, 0, - 3, 1, 2, 2, 1, 3, 2, 2, - 2, 2, 4, 0, 2, 2, 0, 4 - }, 64); - } - } - if (blue_thresh == NULL) { - blue_thresh = (uint8_t *)malloc(64); - if (blue_thresh != NULL) { - memcpy(blue_thresh, - (uint8_t []){ - 5, 3, 8, 0, 6, 2, 7, 1, - 3, 5, 0, 8, 2, 6, 1, 7, - 8, 0, 6, 2, 7, 1, 5, 3, - 0, 8, 2, 6, 1, 7, 3, 5, - 6, 2, 7, 1, 5, 3, 8, 0, - 2, 6, 1, 7, 3, 5, 0, 8, - 7, 1, 5, 3, 8, 0, 6, 2, - 1, 7, 3, 5, 0, 8, 2, 6 - }, 64); - } - } - - if (red_thresh == NULL || blue_thresh == NULL || green_thresh == NULL) return false; - else return true; - } - -#endif \ No newline at end of file +#endif diff --git a/ext_mod/lcd_bus/esp32_src/rgb565_dither.c b/ext_mod/lcd_bus/esp32_src/rgb565_dither.c index b28b04f6..8c013b0c 100644 --- a/ext_mod/lcd_bus/esp32_src/rgb565_dither.c +++ b/ext_mod/lcd_bus/esp32_src/rgb565_dither.c @@ -1,3 +1,66 @@ +#include "rgb565_dither.h" +#include + +uint8_t* red_thresh = NULL; +uint8_t* green_thresh = NULL; +uint8_t* blue_thresh = NULL; + +bool rgb565_dither_init(void) +{ + if (red_thresh == NULL) { + red_thresh = (uint8_t *)malloc(64); + if (red_thresh != NULL) { + memcpy(red_thresh, + (uint8_t []){ + 1, 7, 3, 5, 0, 8, 2, 6, + 7, 1, 5, 3, 8, 0, 6, 2, + 3, 5, 0, 8, 2, 6, 1, 7, + 5, 3, 8, 0, 6, 2, 7, 1, + 0, 8, 2, 6, 1, 7, 3, 5, + 8, 0, 6, 2, 7, 1, 5, 3, + 2, 6, 1, 7, 3, 5, 0, 8, + 6, 2, 7, 1, 5, 3, 8, 0 + }, 64); + } + } + + if (green_thresh == NULL) { + green_thresh = (uint8_t *)malloc(64); + if (green_thresh != NULL) { + memcpy(green_thresh, + (uint8_t []){ + 1, 3, 2, 2, 3, 1, 2, 2, + 2, 2, 0, 4, 2, 2, 4, 0, + 3, 1, 2, 2, 1, 3, 2, 2, + 2, 2, 4, 0, 2, 2, 0, 4, + 1, 3, 2, 2, 3, 1, 2, 2, + 2, 2, 0, 4, 2, 2, 4, 0, + 3, 1, 2, 2, 1, 3, 2, 2, + 2, 2, 4, 0, 2, 2, 0, 4 + }, 64); + } + } + if (blue_thresh == NULL) { + blue_thresh = (uint8_t *)malloc(64); + if (blue_thresh != NULL) { + memcpy(blue_thresh, + (uint8_t []){ + 5, 3, 8, 0, 6, 2, 7, 1, + 3, 5, 0, 8, 2, 6, 1, 7, + 8, 0, 6, 2, 7, 1, 5, 3, + 0, 8, 2, 6, 1, 7, 3, 5, + 6, 2, 7, 1, 5, 3, 8, 0, + 2, 6, 1, 7, 3, 5, 0, 8, + 7, 1, 5, 3, 8, 0, 6, 2, + 1, 7, 3, 5, 0, 8, 2, 6 + }, 64); + } + } + + if (red_thresh == NULL || blue_thresh == NULL || green_thresh == NULL) return false; + else return true; +} + diff --git a/ext_mod/lcd_bus/micropython.cmake b/ext_mod/lcd_bus/micropython.cmake index fc24fa88..8feb1ff2 100644 --- a/ext_mod/lcd_bus/micropython.cmake +++ b/ext_mod/lcd_bus/micropython.cmake @@ -18,6 +18,7 @@ if(ESP_PLATFORM) ${CMAKE_CURRENT_LIST_DIR}/esp32_src/i80_bus.c ${CMAKE_CURRENT_LIST_DIR}/esp32_src/rgb_bus.c ${CMAKE_CURRENT_LIST_DIR}/esp32_src/rgb_bus_rotation.c + ${CMAKE_CURRENT_LIST_DIR}/esp32_src/rgb565_dither.c ) # gets esp_lcd include paths