Add display color inversion flag and logic for Feather TFT

This commit is contained in:
Adolfo Reyna
2026-01-30 23:02:53 -05:00
parent 45cbcc8384
commit d2fd001e70
4 changed files with 28 additions and 14 deletions

View File

@@ -20,6 +20,9 @@
#define TOUCH_INVERT_X true #define TOUCH_INVERT_X true
#define TOUCH_INVERT_Y false #define TOUCH_INVERT_Y false
// Invert display color (black <-> white)
#define DISPLAY_INVERT_COLOR true
// SPI pins for display - Feather RP2350 with 4.0" TFT // SPI pins for display - Feather RP2350 with 4.0" TFT
#define DISPLAY_SPI_PORT spi1 #define DISPLAY_SPI_PORT spi1
#define DISPLAY_SCK_PIN 10 // D10 (SCK) #define DISPLAY_SCK_PIN 10 // D10 (SCK)

View File

@@ -22,7 +22,12 @@ LowLevelDisplay* LowLevelDisplay::create(DisplayType type, int width, int height
.gpio_bl = DISPLAY_BL_PIN, .gpio_bl = DISPLAY_BL_PIN,
}; };
printf("Creating ST7796 display (%dx%d)\n", width, height); printf("Creating ST7796 display (%dx%d)\n", width, height);
return new LowLevelDisplayST7796(&lcd_config, width, height); // Use board config flag for color inversion
#ifdef DISPLAY_INVERT_COLOR
return new LowLevelDisplayST7796(&lcd_config, width, height, DISPLAY_INVERT_COLOR);
#else
return new LowLevelDisplayST7796(&lcd_config, width, height, false);
#endif
} }
case DISPLAY_TYPE_ST7789: { case DISPLAY_TYPE_ST7789: {

View File

@@ -6,8 +6,8 @@
#define COLOR_BLACK 0x0000 #define COLOR_BLACK 0x0000
#define COLOR_WHITE 0xFFFF #define COLOR_WHITE 0xFFFF
LowLevelDisplayST7796::LowLevelDisplayST7796(const st7796_config* cfg, int w, int h) LowLevelDisplayST7796::LowLevelDisplayST7796(const st7796_config* cfg, int w, int h, bool invert)
: config(cfg), width(w), height(h), initialized(false), rgb_buffer(nullptr) { : config(cfg), width(w), height(h), initialized(false), rgb_buffer(nullptr), invert_color(invert) {
} }
LowLevelDisplayST7796::~LowLevelDisplayST7796() { LowLevelDisplayST7796::~LowLevelDisplayST7796() {
@@ -38,26 +38,27 @@ bool LowLevelDisplayST7796::init() {
} }
void LowLevelDisplayST7796::clear(bool white) { void LowLevelDisplayST7796::clear(bool white) {
st7796_fill(white ? COLOR_WHITE : COLOR_BLACK); bool out_white = invert_color ? !white : white;
st7796_fill(out_white ? COLOR_WHITE : COLOR_BLACK);
} }
void LowLevelDisplayST7796::draw_pixel(int x, int y, bool white) { void LowLevelDisplayST7796::draw_pixel(int x, int y, bool white) {
st7796_draw_pixel(x, y, white ? COLOR_WHITE : COLOR_BLACK); bool out_white = invert_color ? !white : white;
st7796_draw_pixel(x, y, out_white ? COLOR_WHITE : COLOR_BLACK);
} }
void LowLevelDisplayST7796::draw_buffer(const uint8_t* bit_buffer) { void LowLevelDisplayST7796::draw_buffer(const uint8_t* bit_buffer) {
if (!bit_buffer || !rgb_buffer) return; if (!bit_buffer || !rgb_buffer) return;
// Convert 1-bit buffer to RGB565 using persistent buffer // Convert 1-bit buffer to RGB565 using persistent buffer
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
int byte_index = (y * width + x) / 8; int byte_index = (y * width + x) / 8;
int bit_index = 7 - (x % 8); int bit_index = 7 - (x % 8);
bool pixel_white = (bit_buffer[byte_index] >> bit_index) & 0x01; bool pixel_white = (bit_buffer[byte_index] >> bit_index) & 0x01;
rgb_buffer[y * width + x] = pixel_white ? COLOR_WHITE : COLOR_BLACK; bool out_white = invert_color ? !pixel_white : pixel_white;
rgb_buffer[y * width + x] = out_white ? COLOR_WHITE : COLOR_BLACK;
} }
} }
// Draw entire buffer at once // Draw entire buffer at once
st7796_set_cursor(0, 0); st7796_set_cursor(0, 0);
st7796_write(rgb_buffer, width * height); st7796_write(rgb_buffer, width * height);

View File

@@ -11,9 +11,10 @@ private:
int height; int height;
bool initialized; bool initialized;
uint16_t* rgb_buffer; // Persistent buffer for 1-bit to RGB565 conversion uint16_t* rgb_buffer; // Persistent buffer for 1-bit to RGB565 conversion
bool invert_color; // If true, swap black/white
public: public:
LowLevelDisplayST7796(const st7796_config* cfg, int w, int h); LowLevelDisplayST7796(const st7796_config* cfg, int w, int h, bool invert = false);
~LowLevelDisplayST7796() override; ~LowLevelDisplayST7796() override;
// Core display operations - converts 1-bit to RGB565 internally // Core display operations - converts 1-bit to RGB565 internally
@@ -34,6 +35,10 @@ public:
// Orientation control // Orientation control
void set_rotation(uint8_t rotation) override; void set_rotation(uint8_t rotation) override;
// Color inversion control
void set_invert_color(bool inv) { invert_color = inv; }
bool get_invert_color() const { return invert_color; }
}; };
#endif // LOW_LEVEL_DISPLAY_ST7796_H #endif // LOW_LEVEL_DISPLAY_ST7796_H