tic tac toe works on touch and eink

This commit is contained in:
Adolfo Reyna
2026-01-29 17:16:59 -05:00
parent 372895fa08
commit 435a5caa56
7 changed files with 450 additions and 145 deletions

View File

@@ -1,15 +1,19 @@
#include "low_level_display_st7789.h"
#include <stdio.h>
#include <stdlib.h>
// Note: This is a placeholder implementation
// You'll need to add the actual ST7789 driver code to lib/st7789/
LowLevelDisplayST7789::LowLevelDisplayST7789(const st7789_config* cfg, int w, int h)
: config(cfg), width(w), height(h), initialized(false) {
: config(cfg), width(w), height(h), initialized(false), rgb_buffer(nullptr) {
}
LowLevelDisplayST7789::~LowLevelDisplayST7789() {
// Cleanup if needed
if (rgb_buffer) {
free(rgb_buffer);
rgb_buffer = nullptr;
}
}
bool LowLevelDisplayST7789::init() {
@@ -19,6 +23,15 @@ bool LowLevelDisplayST7789::init() {
// TODO: Implement ST7789 initialization
// st7789_init(config, width, height);
//
// TODO: Allocate RGB565 buffer once (reused for all draw operations)
// size_t buffer_size = width * height * sizeof(uint16_t);
// rgb_buffer = (uint16_t *)malloc(buffer_size);
// if (!rgb_buffer) {
// printf("Error: Failed to allocate %zu bytes for RGB buffer\n", buffer_size);
// return false;
// }
printf("ST7789 display initialized: %dx%d (stub)\n", width, height);
initialized = true;
return false; // Return false until actual driver is implemented
@@ -36,6 +49,18 @@ void LowLevelDisplayST7789::draw_pixel(int x, int y, bool white) {
void LowLevelDisplayST7789::draw_buffer(const uint8_t* bit_buffer) {
// TODO: Implement - convert 1-bit to RGB565 and write
// IMPORTANT: Use the persistent rgb_buffer member, NOT malloc/free!
// Example:
// if (!bit_buffer || !rgb_buffer) return;
// for (int y = 0; y < height; y++) {
// for (int x = 0; x < width; x++) {
// int byte_index = (y * width + x) / 8;
// int bit_index = 7 - (x % 8);
// bool pixel_white = (bit_buffer[byte_index] >> bit_index) & 0x01;
// rgb_buffer[y * width + x] = pixel_white ? COLOR_WHITE : COLOR_BLACK;
// }
// }
// st7789_write(rgb_buffer, width * height);
(void)bit_buffer;
}

View File

@@ -20,6 +20,7 @@ private:
int width;
int height;
bool initialized;
uint16_t* rgb_buffer; // Persistent buffer for 1-bit to RGB565 conversion
public:
LowLevelDisplayST7789(const st7789_config* cfg, int w, int h);

View File

@@ -7,11 +7,14 @@
#define COLOR_WHITE 0xFFFF
LowLevelDisplayST7796::LowLevelDisplayST7796(const st7796_config* cfg, int w, int h)
: config(cfg), width(w), height(h), initialized(false) {
: config(cfg), width(w), height(h), initialized(false), rgb_buffer(nullptr) {
}
LowLevelDisplayST7796::~LowLevelDisplayST7796() {
// Cleanup if needed
if (rgb_buffer) {
free(rgb_buffer);
rgb_buffer = nullptr;
}
}
bool LowLevelDisplayST7796::init() {
@@ -20,8 +23,17 @@ bool LowLevelDisplayST7796::init() {
}
st7796_init(config, width, height);
// Allocate RGB565 buffer once (reused for all draw operations)
size_t buffer_size = width * height * sizeof(uint16_t);
rgb_buffer = (uint16_t *)malloc(buffer_size);
if (!rgb_buffer) {
printf("Error: Failed to allocate %zu bytes for RGB buffer\n", buffer_size);
return false;
}
printf("ST7796 display initialized: %dx%d (RGB buffer: %zu bytes)\n", width, height, buffer_size);
initialized = true;
printf("ST7796 display initialized: %dx%d\n", width, height);
return true;
}
@@ -34,16 +46,9 @@ void LowLevelDisplayST7796::draw_pixel(int x, int y, bool white) {
}
void LowLevelDisplayST7796::draw_buffer(const uint8_t* bit_buffer) {
if (!bit_buffer) return;
if (!bit_buffer || !rgb_buffer) return;
// Allocate RGB565 buffer for entire screen
uint16_t *rgb_buffer = (uint16_t *)malloc(width * height * sizeof(uint16_t));
if (!rgb_buffer) {
printf("Error: Failed to allocate RGB buffer\n");
return;
}
// Convert 1-bit buffer to RGB565
// Convert 1-bit buffer to RGB565 using persistent buffer
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int byte_index = (y * width + x) / 8;
@@ -56,8 +61,6 @@ void LowLevelDisplayST7796::draw_buffer(const uint8_t* bit_buffer) {
// Draw entire buffer at once
st7796_set_cursor(0, 0);
st7796_write(rgb_buffer, width * height);
free(rgb_buffer);
}
void LowLevelDisplayST7796::refresh() {

View File

@@ -10,6 +10,7 @@ private:
int width;
int height;
bool initialized;
uint16_t* rgb_buffer; // Persistent buffer for 1-bit to RGB565 conversion
public:
LowLevelDisplayST7796(const st7796_config* cfg, int w, int h);