Initial commit: Base Project for Kode Dot (ESP32-S3)

This commit is contained in:
Lago
2026-04-03 13:21:23 +02:00
commit a3d9840a92
30 changed files with 59419 additions and 0 deletions
@@ -0,0 +1,10 @@
// src/custom_ota_override.cpp
// Strong override of the weak boot/OTA hook in the core.
// Returning true means: "do NOT auto-validate the running OTA image".
// After any reset, the bootloader will roll back to the last valid app (your factory).
extern "C" bool verifyRollbackLater(); // must use C linkage to match the weak symbol
extern "C" bool verifyRollbackLater() {
return true; // never auto-validate OTAs
}
+204
View File
@@ -0,0 +1,204 @@
#include <kodedot/display_manager.h>
#include <Preferences.h>
// Forward declarations for internal helpers
extern "C" void __wrap_esp_ota_mark_app_valid_cancel_rollback(void);
void init_nvs();
void lvgl_port_rounder_callback(lv_disp_drv_t *disp_drv, lv_area_t *area);
// Static instance used by LVGL callbacks
DisplayManager* DisplayManager::instance = nullptr;
// --- Internal system utilities ---
extern "C" void __wrap_esp_ota_mark_app_valid_cancel_rollback(void) {
// no-op: always disables OTA validation
}
static Preferences prefs;
void init_nvs() {
// Initialize NVS namespace for application storage
prefs.begin("kode_storage", false);
}
// --- LVGL helper ---
void lvgl_port_rounder_callback(lv_disp_drv_t *disp_drv, lv_area_t *area) {
// Efficiently align area to even/odd boundaries as required by the panel
area->x1 &= ~1; // round down to even
area->y1 &= ~1;
area->x2 = (area->x2 & ~1) + 1; // round up to odd
area->y2 = (area->y2 & ~1) + 1;
}
DisplayManager::DisplayManager() : bus(nullptr), gfx(nullptr), buf(nullptr), buf2(nullptr) {
instance = this;
}
DisplayManager::~DisplayManager() {
if (buf) free(buf);
if (buf2) free(buf2);
if (gfx) {
delete gfx;
}
if (bus) {
delete bus;
}
instance = nullptr;
}
bool DisplayManager::init() {
Serial.println("Bringing up display subsystem...");
// Initialize NVS (preferences)
init_nvs();
// Power on the panel if an enable pin is provided
if (LCD_EN >= 0) {
pinMode(LCD_EN, OUTPUT);
digitalWrite(LCD_EN, HIGH);
}
bus = new Arduino_ESP32QSPI(
LCD_CS, LCD_SCLK, LCD_SDIO0, LCD_SDIO1, LCD_SDIO2, LCD_SDIO3
);
gfx = new Arduino_CO5300(
bus, LCD_RST, 0, LCD_WIDTH, LCD_HEIGHT,
22, 0, 0, 0
);
if (!gfx->begin()) {
Serial.println("Error: failed to initialize panel");
return false;
}
gfx->setRotation(0);
// Load brightness percentage (0-100) from NVS and apply to panel (0-255)
{
uint8_t saved_pct = prefs.getUChar("brightness_pct", 100);
if (saved_pct > 100) saved_pct = 100;
uint8_t saved_brightness = (uint8_t)(((uint16_t)saved_pct * 255 + 50) / 100); // round
gfx->setBrightness(saved_brightness);
Serial.printf("Brightness (pct=%u) applied from NVS\n", (unsigned)saved_pct);
}
gfx->fillScreen(BLACK);
Serial.println("Panel initialized");
// Initialize LVGL core
lv_init();
// Allocate draw buffers (prefer PSRAM; fallback to internal SRAM)
size_t draw_buf_pixels = (size_t)LCD_WIDTH * (size_t)LCD_DRAW_BUFF_HEIGHT;
size_t draw_buf_bytes = draw_buf_pixels * sizeof(lv_color_t);
Serial.printf("Requesting LVGL draw buffer: %u bytes\n", (unsigned)draw_buf_bytes);
buf = (lv_color_t*)heap_caps_malloc(draw_buf_bytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (!buf) {
// Fallback to internal SRAM with reduced height window
size_t fallback_height = 100; // small window to avoid exhausting SRAM
draw_buf_pixels = (size_t)LCD_WIDTH * fallback_height;
draw_buf_bytes = draw_buf_pixels * sizeof(lv_color_t);
Serial.printf("PSRAM not available, using SRAM fallback: %u bytes\n", (unsigned)draw_buf_bytes);
buf = (lv_color_t*)heap_caps_malloc(draw_buf_bytes, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
if (!buf) {
Serial.println("Error: unable to allocate draw buffer in SRAM");
return false;
}
}
// Try double buffering in PSRAM if there is room
buf2 = (lv_color_t*)heap_caps_malloc(draw_buf_bytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (!buf2) {
// If not enough memory, keep single buffer
buf2 = NULL;
}
// Configure LVGL display driver
lv_disp_draw_buf_init(&draw_buf, buf, buf2, (uint32_t)draw_buf_pixels);
// Initialize the display driver
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = LCD_WIDTH;
disp_drv.ver_res = LCD_HEIGHT;
disp_drv.flush_cb = disp_flush_callback;
disp_drv.rounder_cb = lvgl_port_rounder_callback; // avoid visual artifacts on odd/even alignment
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register(&disp_drv);
Serial.println("LVGL initialized");
// Initialize capacitive touch
if(bbct.init(TOUCH_I2C_SDA, TOUCH_I2C_SCL, TOUCH_RST, TOUCH_INT) != CT_SUCCESS) {
Serial.println("Error: failed to initialize touch");
return false;
} else {
Serial.println("Touch initialized");
}
// Register LVGL input driver (touch)
static lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = touchpad_read_callback;
lv_indev_drv_register(&indev_drv);
Serial.println("Display subsystem ready");
return true;
}
void DisplayManager::update() {
// Advance LVGL tick by 5 ms per iteration
lv_tick_inc(5);
// Let LVGL process timers
lv_timer_handler();
}
void DisplayManager::setBrightness(uint8_t brightness) {
if (gfx) {
gfx->setBrightness(brightness);
}
// Persist brightness as percentage (0-100) in NVS
uint8_t pct = (uint8_t)(((uint16_t)brightness * 100 + 127) / 255); // round
if (pct > 100) pct = 100;
prefs.putUChar("brightness_pct", pct);
}
bool DisplayManager::getTouchCoordinates(int16_t &x, int16_t &y) {
TOUCHINFO ti;
if(bbct.getSamples(&ti) && ti.count > 0) {
x = ti.x[0];
y = ti.y[0];
return true;
}
return false;
}
// LVGL display flush callback
void DisplayManager::disp_flush_callback(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
if (!instance || !instance->gfx) return;
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);
instance->gfx->startWrite();
instance->gfx->writeAddrWindow(area->x1, area->y1, w, h);
instance->gfx->writePixels((uint16_t *)&color_p->full, w * h);
instance->gfx->endWrite();
lv_disp_flush_ready(disp);
}
// LVGL touch read callback
void DisplayManager::touchpad_read_callback(lv_indev_drv_t *indev_drv, lv_indev_data_t *data) {
if (!instance) return;
TOUCHINFO ti;
if(instance->bbct.getSamples(&ti) && ti.count > 0) {
data->state = LV_INDEV_STATE_PRESSED;
data->point.x = ti.x[0];
data->point.y = ti.y[0];
} else {
data->state = LV_INDEV_STATE_RELEASED;
}
}