co5300.c

C source for Emulating a round-screen ESP32 board, 88 lines. Raw file

#include <stdio.h>
#include <string.h>
#include "co5300.h"

// Frame memory: byte-swapped RGB565, exactly the 16-bit words the app DMAs out
// (hud pack565 returns (c<<8)|(c>>8)). Reads back out through co5300_rgba().
static uint16_t s_fb[CO5300_W * CO5300_H];

// Decode state. The CASET/RASET window; a running pixel cursor so a RAMWR whose
// pixel data is split across several queued SPI transactions keeps filling where
// it left off (the esp_lcd driver chunks large blits with CS held active).
static int s_x0, s_x1, s_y0, s_y1;      // window, inclusive (xe/ye = end-1 from driver)
static int s_cur_x, s_cur_y;            // RAMWR write cursor
static int s_in_color;                  // inside a RAMWR colour stream
static uint8_t s_cmd;                   // pending command awaiting its parameters

static void begin_ramwr(void) { s_in_color = 1; s_cur_x = s_x0; s_cur_y = s_y0; }

void co5300_spi2_xfer(const uint8_t *data, uint32_t len)
{
    // A 4-byte transfer beginning with a QSPI opcode is a command header.
    if (len == 4 && (data[0] == 0x02 || data[0] == 0x32)) {
        s_cmd = data[2];
        s_in_color = 0;
        if (data[0] == 0x32 && s_cmd == 0x2C) begin_ramwr();   // RAMWR: pixels follow
        return;
    }

    // Otherwise this is parameter / pixel data for the pending command.
    switch (s_cmd) {
    case 0x2A:                           // CASET: xs, xe (big-endian, inclusive)
        if (len >= 4) { s_x0 = (data[0] << 8) | data[1]; s_x1 = (data[2] << 8) | data[3]; }
        break;
    case 0x2B:                           // RASET: ys, ye
        if (len >= 4) { s_y0 = (data[0] << 8) | data[1]; s_y1 = (data[2] << 8) | data[3]; }
        break;
    case 0x2C:                           // RAMWR colour stream (may arrive in chunks)
        if (!s_in_color) begin_ramwr();
        for (uint32_t i = 0; i + 1 < len; i += 2) {
            if (s_cur_y > s_y1 || s_cur_y >= CO5300_H) break;
            if (s_cur_x >= 0 && s_cur_x < CO5300_W && s_cur_y >= 0)
                s_fb[s_cur_y * CO5300_W + s_cur_x] = (uint16_t)(data[i] | (data[i + 1] << 8));
            if (++s_cur_x > s_x1) { s_cur_x = s_x0; s_cur_y++; }
        }
        break;
    default:                             // MADCTL/COLMOD/brightness/init: no pixels
        break;
    }
}

const uint16_t *co5300_fb(void) { return s_fb; }

void co5300_rgba(uint8_t *out)
{
    const int cx = 233, cy = 233, r2 = 233 * 233;
    for (int y = 0; y < CO5300_H; y++)
        for (int x = 0; x < CO5300_W; x++) {
            int o = (y * CO5300_W + x) * 4, dx = x - cx, dy = y - cy;
            if (dx * dx + dy * dy > r2) { out[o] = out[o+1] = out[o+2] = out[o+3] = 0; continue; }
            uint16_t v = s_fb[y * CO5300_W + x];
            uint16_t c = (uint16_t)((v << 8) | (v >> 8));
            uint8_t r5 = (c >> 11) & 0x1F, g6 = (c >> 5) & 0x3F, b5 = c & 0x1F;
            out[o]   = (r5 << 3) | (r5 >> 2);
            out[o+1] = (g6 << 2) | (g6 >> 4);
            out[o+2] = (b5 << 3) | (b5 >> 2);
            out[o+3] = 255;
        }
}

void co5300_dump_ppm(const char *path)
{
    FILE *f = fopen(path, "wb");
    if (!f) return;
    fprintf(f, "P6\n%d %d\n255\n", CO5300_W, CO5300_H);
    const int cx = 233, cy = 233, r2 = 233 * 233;
    for (int y = 0; y < CO5300_H; y++)
        for (int x = 0; x < CO5300_W; x++) {
            int dx = x - cx, dy = y - cy; uint8_t rgb[3] = {0, 0, 0};
            if (dx * dx + dy * dy <= r2) {
                uint16_t v = s_fb[y * CO5300_W + x];
                uint16_t c = (uint16_t)((v << 8) | (v >> 8));
                uint8_t r5 = (c >> 11) & 0x1F, g6 = (c >> 5) & 0x3F, b5 = c & 0x1F;
                rgb[0] = (r5 << 3) | (r5 >> 2); rgb[1] = (g6 << 2) | (g6 >> 4); rgb[2] = (b5 << 3) | (b5 >> 2);
            }
            fwrite(rgb, 1, 3, f);
        }
    fclose(f);
}