Files
esp32vga/vga_test/main/main.c
T
jimmyandClaude Opus 5.5 c307f08de8 Add ESP32-S3 VGA firmware with web interface
800x600 RGB332 VGA output for the XIAO ESP32S3 Plus, double buffered in
PSRAM. Includes content modes (test pattern, plasma, stars, clock,
Mandelbrot, message, images, Halloween, Haunting), a flash image gallery,
WiFi AP+STA web UI and OTA updates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 22:05:32 +13:00

182 lines
4.2 KiB
C

// VGA display with web interface for XIAO ESP32S3 Plus.
// Display and content run on core 1; WiFi and the web server on core 0.
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "app.h"
#include "content.h"
#include "display.h"
static const char *TAG = "app";
uint8_t *live_image;
static SemaphoreHandle_t live_lock;
static SemaphoreHandle_t show_lock;
static show_t show;
static uint32_t show_seq; // bumped on every change, so the display task notices
void live_image_lock(void)
{
xSemaphoreTake(live_lock, portMAX_DELAY);
}
void live_image_unlock(void)
{
xSemaphoreGive(live_lock);
}
void app_get_show(show_t *out)
{
xSemaphoreTake(show_lock, portMAX_DELAY);
*out = show;
xSemaphoreGive(show_lock);
}
static uint32_t get_seq(void)
{
xSemaphoreTake(show_lock, portMAX_DELAY);
uint32_t s = show_seq;
xSemaphoreGive(show_lock);
return s;
}
static void save_show(void)
{
nvs_handle_t h;
if (nvs_open("vga", NVS_READWRITE, &h) != ESP_OK) {
return;
}
nvs_set_u8(h, "content", show.content);
nvs_set_i8(h, "slot", show.slot);
nvs_set_str(h, "msg_text", show.msg.text);
nvs_set_u8(h, "msg_fg", show.msg.fg);
nvs_set_u8(h, "msg_bg", show.msg.bg);
nvs_set_u8(h, "msg_size", show.msg.size);
nvs_commit(h);
nvs_close(h);
}
static void load_show(void)
{
show = (show_t){
.content = CONTENT_TEST,
.slot = 0,
.msg = {.text = "Hello from the ESP32-S3!", .fg = rgb332(7, 7, 3), .bg = rgb332(0, 0, 2), .size = MSG_SIZE_FILL},
};
nvs_handle_t h;
if (nvs_open("vga", NVS_READONLY, &h) != ESP_OK) {
return;
}
uint8_t u;
int8_t i;
size_t len = sizeof(show.msg.text);
if (nvs_get_u8(h, "content", &u) == ESP_OK && u < CONTENT_COUNT) {
show.content = u;
}
if (nvs_get_i8(h, "slot", &i) == ESP_OK) {
show.slot = i;
}
nvs_get_str(h, "msg_text", show.msg.text, &len);
nvs_get_u8(h, "msg_fg", &show.msg.fg);
nvs_get_u8(h, "msg_bg", &show.msg.bg);
nvs_get_u8(h, "msg_size", &show.msg.size);
nvs_close(h);
// The live image is lost on reboot
if (show.content == CONTENT_IMAGE && show.slot == SLOT_LIVE) {
show.content = CONTENT_TEST;
}
}
void app_show(content_id_t c, int slot)
{
xSemaphoreTake(show_lock, portMAX_DELAY);
show.content = c;
show.slot = slot;
show_seq++;
save_show();
xSemaphoreGive(show_lock);
ESP_LOGI(TAG, "show %s (slot %d)", content_key(c), slot);
}
void app_set_message(const message_t *m)
{
xSemaphoreTake(show_lock, portMAX_DELAY);
show.msg = *m;
show.content = CONTENT_MESSAGE;
show_seq++;
save_show();
xSemaphoreGive(show_lock);
}
void app_refresh(void)
{
xSemaphoreTake(show_lock, portMAX_DELAY);
show_seq++;
xSemaphoreGive(show_lock);
}
static void display_task(void *arg)
{
display_init();
uint32_t seq = get_seq() - 1; // force the first enter()
const content_ops_t *c = NULL;
show_t cur;
int back = 1;
while (1) {
uint32_t now = get_seq();
if (now != seq) {
seq = now;
app_get_show(&cur);
c = content_ops(cur.content);
if (c->enter) {
c->enter(&cur);
}
canvas_dirty_all();
}
if (c->tick) {
c->tick();
}
uint8_t *fb = display_fb(back);
bool changed = display_sync(back);
if (c->draw) {
changed |= c->draw(fb, back);
}
if (changed) {
display_present(back);
back ^= 1;
} else {
display_wait_frame();
}
}
}
void app_main(void)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
show_lock = xSemaphoreCreateMutex();
live_lock = xSemaphoreCreateMutex();
load_show();
gallery_init();
xTaskCreatePinnedToCore(display_task, "display", 8192, NULL, 10, NULL, 1);
web_start();
}