Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 9 additions & 64 deletions ext_mod/lcd_bus/esp32_include/rgb565_dither.h
Original file line number Diff line number Diff line change
@@ -1,77 +1,22 @@
#ifndef __RGB565_DITHER_H__
#define __RGB565_DITHER_H__

#include <string.h>
#include <stdint.h>
#include <stdbool.h>

#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
#endif
63 changes: 63 additions & 0 deletions ext_mod/lcd_bus/esp32_src/rgb565_dither.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,66 @@

#include "rgb565_dither.h"
#include <string.h>

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;
}



1 change: 1 addition & 0 deletions ext_mod/lcd_bus/micropython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading