Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399// Live waveform + entity stack for the CO5300 AMOLED (368x448 portrait).//// Layout:// - entity stack fills the full height of the screen. The newest entity is// wrapped into 1-3 lines at the TOP, everything older is pushed down, and// lines that fall past the bottom edge are dropped.// - oscilloscope trace is centred vertically and drawn first; each// text line paints a black backdrop so it stays readable over the trace.//// Landscape (448x368) needs a buffer transpose during the flush, not MADCTL's// MV bit: the driver adds x_gap to every draw_bitmap window regardless of the// axis swap, so the panel's 0x10 offset lands on the wrong axis and the image// corrupts.//// The framebuffer is pushed in horizontal bands because a single full-frame// draw_bitmap cannot obtain an internal DMA bounce buffer and fails silently.
#include "display.h"#include "orientation.h"#include "battery_status.h"#include "display_activity.h"#include <freertos/FreeRTOS.h>#include <freertos/semphr.h>
#include <Arduino.h>
#include <cmath>#include <cstring>#include <esp_heap_caps.h>#include <esp_lcd_panel_io.h>#include <esp_lcd_panel_ops.h>#include <esp_log.h>
#include "font8x8_basic.h"#include <esp_lcd_co5300.h>
#define LCD_CS 12#define LCD_PCLK 11#define LCD_D0 4#define LCD_D1 5#define LCD_D2 6#define LCD_D3 7
#define LCD_W 368#define LCD_H 448#define X_GAP 0x10
#define TEXT_SCALE 3// The AMOLED has rounded corners: keep text out of them or the first and last// glyph of a full-width line gets clipped (seen with "Diego in Madrid").#define SAFE_X 16#define SAFE_TOP 24#define PER_LINE ((LCD_W - 2 * SAFE_X) / (8 * TEXT_SCALE)) // 14 chars#define LINE_H 30#define STACK_TOP SAFE_TOP#define MAX_LINES ((LCD_H - STACK_TOP) / LINE_H)
#define SCOPE_AMP 56
static const uint16_t kTrace = 0x05FF;static const uint16_t kNew = 0xFFFF; // newest entitystatic const uint16_t kOld = 0xB5B6; // older entries: bright enough to seestatic const uint16_t kDim = 0x4208;static const uint16_t kBattery = 0x7BEF; // muted gray, below entity textstatic constexpr uint8_t kBrightnessPercent = 40;
static esp_lcd_panel_handle_t s_panel;static uint16_t *s_fb;static const int kBands = 40;static uint16_t *s_band;static SemaphoreHandle_t s_transfer_done;static unsigned s_rotation = 0;static int s_width = LCD_W;static int s_height = LCD_H;static float s_gain = 1.0f;
static char s_lines[MAX_LINES][PER_LINE + 1];static int s_nlines = 0;static int s_newest_lines = 0; // how many top lines belong to the newest entitystatic DisplayActivity s_activity;static bool s_processing = false;static int s_battery_percent = battery_status::unknown;static bool s_battery_charging = false;
static const co5300_lcd_init_cmd_t kInitCmds[] = { {0xFE, (uint8_t[]){0x00}, 1, 0}, {0xC4, (uint8_t[]){0x80}, 1, 0}, {0x3A, (uint8_t[]){0x55}, 1, 0}, {0x35, (uint8_t[]){0x00}, 1, 0}, {0x53, (uint8_t[]){0x20}, 1, 0}, {0x51, (uint8_t[]){static_cast<uint8_t>(kBrightnessPercent * 255 / 100)}, 1, 0}, {0x63, (uint8_t[]){0xFF}, 1, 0}, {0x2A, (uint8_t[]){0x00, 0x00, 0x01, 0x6F}, 4, 0}, {0x2B, (uint8_t[]){0x00, 0x00, 0x01, 0xBF}, 4, 0}, {0x11, (uint8_t[]){0x00}, 0, 100}, {0x29, (uint8_t[]){0x00}, 0, 0},};
static inline void px(int x, int y, uint16_t c) { if (x >= 0 && x < s_width && y >= 0 && y < s_height) s_fb[y * s_width + x] = c;}
static bool IRAM_ATTR transfer_done(esp_lcd_panel_io_handle_t, esp_lcd_panel_io_event_data_t *, void *) { BaseType_t wake = pdFALSE; xSemaphoreGiveFromISR(s_transfer_done, &wake); return wake == pdTRUE;}
static void flush_fb() { // Keep the panel in native portrait; transpose/reverse only pixel data. // Wait for DMA completion before reusing the band (or the framebuffer). for (int y = 0; y < LCD_H; y += kBands) { int h = (y + kBands <= LCD_H) ? kBands : LCD_H - y; for (int row = 0; row < h; ++row) for (int x = 0; x < LCD_W; ++x) s_band[row * LCD_W + x] = s_fb[orientation::source_index(s_rotation, x, y + row)]; esp_err_t err = esp_lcd_panel_draw_bitmap(s_panel, 0, y, LCD_W, y + h, s_band); if (err != ESP_OK) { ESP_LOGE("display", "band transfer failed: %s", esp_err_to_name(err)); s_activity.invalidate(); return; } xSemaphoreTake(s_transfer_done, portMAX_DELAY); }}
static orientation::Rect dot_region() { return {22, s_height - 34, 12, 12};}
static void draw_activity_dot() { const auto r = dot_region(); for (int y = 0; y < r.h; ++y) for (int x = 0; x < r.w; ++x) { const int dx = 2 * (x - 2) - 7, dy = 2 * (y - 2) - 7; px(r.x + x, r.y + y, s_activity.dot_on() && dx * dx + dy * dy <= 64 ? 0xFFFF : 0); }}
static void flush_activity_dot() { const auto r = orientation::native_rect(s_rotation, dot_region()); for (int y = 0; y < r.h; ++y) for (int x = 0; x < r.w; ++x) s_band[y * r.w + x] = s_fb[orientation::source_index(s_rotation, r.x + x, r.y + y)]; const esp_err_t err = esp_lcd_panel_draw_bitmap(s_panel, r.x, r.y, r.x + r.w, r.y + r.h, s_band); if (err != ESP_OK) { ESP_LOGE("display", "dot transfer failed: %s", esp_err_to_name(err)); s_activity.invalidate(); return; } xSemaphoreTake(s_transfer_done, portMAX_DELAY);}
void display_set_rotation(unsigned rotation) { rotation &= 3; if (rotation != s_rotation) s_activity.invalidate(); s_rotation = rotation; s_width = orientation::width(s_rotation); s_height = orientation::height(s_rotation);}
static void draw_char(int x, int y, char c, uint16_t col, int scale) { if (c < 32 || c > 126) return; const uint8_t *g = (const uint8_t *)font8x8_basic[(uint8_t)c]; for (int row = 0; row < 8; row++) for (int bit = 0; bit < 8; bit++) if (g[row] & (1 << bit)) for (int dy = 0; dy < scale; dy++) for (int dx = 0; dx < scale; dx++) px(x + bit * scale + dx, y + row * scale + dy, col);}
// Clears only the glyph box, so the oscilloscope trace stays continuous either// side of the text instead of being cut across the full width of the panel.static void draw_centered(int y, const char *s, uint16_t col, int scale) { int w = (int)strlen(s) * 8 * scale; int x = (s_width - w) / 2; if (x < SAFE_X) x = SAFE_X; if (x + w > s_width - SAFE_X) x = s_width - SAFE_X - w; if (x < 0) x = 0; for (int yy = y - 2; yy < y + 8 * scale + 2; yy++) for (int xx = x - 2; xx < x + w + 2; xx++) px(xx, yy, 0x0000); for (int i = 0; s[i]; i++) draw_char(x + i * 8 * scale, y, s[i], col, scale);}
// Wraps `src` into up to `maxlines` lines of PER_LINE, breaking at spaces.static int wrap_lines(char dst[][PER_LINE + 1], int maxlines, const char *src) { int line = 0; const char *p = src; while (*p && line < maxlines) { while (*p == ' ') p++; if (!*p) break; int len = (int)strlen(p); int take = len < PER_LINE ? len : PER_LINE; if (len > PER_LINE) { int br = -1; for (int i = take; i > 0; i--) if (p[i] == ' ') { br = i; break; } if (br > 0) take = br; } memcpy(dst[line], p, take); dst[line][take] = 0; line++; p += take; } return line;}
void display_set_processing(bool on) { s_processing = on; }
void display_set_battery(int percent, bool charging) { if (s_battery_percent != percent || s_battery_charging != charging) s_activity.invalidate(); s_battery_percent = percent; s_battery_charging = charging;}
void display_set_utterance(const char *text, const char *entities) { (void)text; if (!entities || !entities[0]) return; // keep the existing stack
char fresh[3][PER_LINE + 1]; int k = wrap_lines(fresh, 3, entities); if (k == 0) return;
// push the old lines down by k, dropping what falls off the bottom int keep = s_nlines + k; if (keep > MAX_LINES) { // shift only the survivors int survivors = MAX_LINES - k; if (survivors < 0) survivors = 0; for (int i = survivors - 1; i >= 0; i--) memcpy(s_lines[i + k], s_lines[i], sizeof(s_lines[0])); s_nlines = survivors + k; } else { for (int i = keep - k - 1; i >= 0; i--) memcpy(s_lines[i + k], s_lines[i], sizeof(s_lines[0])); s_nlines = keep; }
for (int i = 0; i < k; i++) memcpy(s_lines[i], fresh[i], sizeof(s_lines[0]));
s_newest_lines = k; s_activity.invalidate(); Serial.printf("ENT lines=%d newest=%d top=%s\n", s_nlines, s_newest_lines, s_lines[0]);}
bool display_init() { s_fb = (uint16_t *)heap_caps_malloc(LCD_W * LCD_H * 2, MALLOC_CAP_SPIRAM); if (!s_fb) { ESP_LOGE("display", "framebuffer alloc failed"); return false; } memset(s_fb, 0, LCD_W * LCD_H * 2); s_band = (uint16_t *)heap_caps_malloc(LCD_W * kBands * 2, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); s_transfer_done = xSemaphoreCreateBinary(); if (!s_band || !s_transfer_done) { ESP_LOGE("display", "DMA band / semaphore alloc failed"); return false; }
spi_bus_config_t buscfg = {}; buscfg.sclk_io_num = LCD_PCLK; buscfg.data0_io_num = LCD_D0; buscfg.data1_io_num = LCD_D1; buscfg.data2_io_num = LCD_D2; buscfg.data3_io_num = LCD_D3; buscfg.max_transfer_sz = LCD_W * kBands * 2; if (spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO) != ESP_OK) { ESP_LOGE("display", "spi_bus_initialize failed"); return false; }
esp_lcd_panel_io_handle_t io = NULL; esp_lcd_panel_io_spi_config_t io_config = {}; io_config.cs_gpio_num = LCD_CS; io_config.dc_gpio_num = -1; io_config.spi_mode = 0; io_config.pclk_hz = 40 * 1000 * 1000; io_config.trans_queue_depth = 1; io_config.on_color_trans_done = transfer_done; io_config.lcd_cmd_bits = 32; io_config.lcd_param_bits = 8; io_config.flags.quad_mode = true; if (esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SPI2_HOST, &io_config, &io) != ESP_OK) { ESP_LOGE("display", "panel_io failed"); return false; }
co5300_vendor_config_t vendor = { .init_cmds = kInitCmds, .init_cmds_size = sizeof(kInitCmds) / sizeof(kInitCmds[0]), .flags = {.use_qspi_interface = 1}, }; esp_lcd_panel_dev_config_t panel_cfg = { .reset_gpio_num = -1, .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB, .bits_per_pixel = 16, .vendor_config = &vendor, }; if (esp_lcd_new_panel_co5300(io, &panel_cfg, &s_panel) != ESP_OK) { ESP_LOGE("display", "new_panel_co5300 failed"); return false; } esp_lcd_panel_reset(s_panel); esp_lcd_panel_init(s_panel); esp_lcd_panel_set_gap(s_panel, X_GAP, 0); const esp_err_t brightness = esp_lcd_panel_co5300_set_brightness(s_panel, kBrightnessPercent); Serial.printf("POWER display_brightness=%u%% ok=%d\n", kBrightnessPercent, brightness == ESP_OK); esp_lcd_panel_disp_on_off(s_panel, true);
// Start quietly: no full-screen colour test flashes. flush_fb();
ESP_LOGI("display", "co5300 ready %dx%d", LCD_W, LCD_H); return true;}
void display_update(const int16_t *samples, int n, bool voiced, bool animate_wave) { if (!s_panel || !samples || n <= 0) return; const auto update = s_activity.update(millis(), voiced || s_processing, animate_wave); if (update == DisplayActivity::None) return; if (update == DisplayActivity::Dot) { draw_activity_dot(); flush_activity_dot(); return; }
memset(s_fb, 0, LCD_W * LCD_H * 2);
// ---- oscilloscope, vertically centred int peak = 1; for (int i = 0; i < n; i++) { int a = samples[i] < 0 ? -samples[i] : samples[i]; if (a > peak) peak = a; } float want = 32768.0f / (float)peak; if (want > 40.0f) want = 40.0f; if (animate_wave) s_gain = s_gain * 0.85f + want * 0.15f;
int scope_mid = s_height / 2; int prevY = scope_mid; for (int x = 0; x < s_width; x++) { int idx = (int)((long)x * n / s_width); float v = animate_wave ? (float)samples[idx] * s_gain / 32768.0f : 0.0f; if (v > 1.0f) v = 1.0f; if (v < -1.0f) v = -1.0f; int y = scope_mid - (int)(v * SCOPE_AMP); int y0 = prevY < y ? prevY : y; int y1 = prevY < y ? y : prevY; for (int yy = y0; yy <= y1; yy++) px(x, yy, kTrace); prevY = y; }
draw_activity_dot();
// Static entity stack: no ellipsis, row shifting, or flashing text. if (s_nlines == 0) { draw_centered(STACK_TOP, "listening", kDim, TEXT_SCALE); } else { for (int i = 0; i < s_nlines; i++) { const uint16_t col = (i < s_newest_lines) ? kNew : kOld; const int y = STACK_TOP + i * LINE_H; // Hide overflow in landscape without deleting it; turning back // to portrait restores the entire stack. Keep 14-char wrapping // in both orientations so rotation never loses/splits old lines. if (y + 8 * TEXT_SCALE > s_height - battery_status::footer_height) break; draw_centered(y, s_lines[i], col, TEXT_SCALE); } }
// Logical bottom-right follows rotation; inset avoids the rounded glass. char battery[8]; battery_status::format(battery, sizeof(battery), s_battery_percent); const auto box = battery_status::bounds(s_width, s_height, strlen(battery)); for (int yy = box.y - 3; yy < box.y + box.height + 3; ++yy) for (int xx = box.x - 3; xx < box.x + box.width + 3; ++xx) px(xx, yy, 0); for (int i = 0; battery[i]; ++i) draw_char(box.x + i * 8 * battery_status::scale, box.y, battery[i], kBattery, battery_status::scale);
if (s_battery_charging) { static const uint16_t bolt[16] = { 0x100, 0x180, 0x1C0, 0x0E0, 0x0F0, 0x078, 0x7FC, 0x3FE, 0x1FF, 0x0E0, 0x070, 0x038, 0x01C, 0x00E, 0x007, 0x003 }; const auto icon = battery_status::charging_bounds(s_width, s_height, strlen(battery)); for (int y = 0; y < icon.height; ++y) for (int x = 0; x < icon.width; ++x) if (bolt[y] & (1u << x)) px(icon.x + x, icon.y + y, 0xFFFF); }
flush_fb();}