Files
esp32vga/vga_test/main/display.h
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

86 lines
2.8 KiB
C

#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "font_data.h"
// Pick one. Both pixel clocks divide 240 MHz exactly, so there's no jitter.
#define MODE_800x600 1
#if MODE_800x600
// VESA 800x600@60: 40 MHz, 37.9 kHz, 60.3 Hz, positive syncs
#define W 800
#define H 600
#define PCLK_HZ 40000000
#define H_FRONT 40
#define H_SYNC 128
#define H_BACK 88
#define V_FRONT 1
#define V_SYNC 4
#define V_BACK 23
#define SYNC_POS 1
#else
// 640x480 at 24 MHz (VGA timing, slightly slow): 30.0 kHz, 57.1 Hz, negative syncs
#define W 640
#define H 480
#define PCLK_HZ 24000000
#define H_FRONT 16
#define H_SYNC 96
#define H_BACK 48
#define V_FRONT 10
#define V_SYNC 2
#define V_BACK 33
#define SYNC_POS 0
#endif
#define REFRESH_HZ ((double)PCLK_HZ / ((W + H_FRONT + H_SYNC + H_BACK) * (H + V_FRONT + V_SYNC + V_BACK)))
// r, g: 0-7, b: 0-3
static inline uint8_t rgb332(int r, int g, int b)
{
return (r << 5) | (g << 2) | b;
}
// From 8-bit-per-channel colour, rounded to the nearest DAC level
static inline uint8_t rgb888_to_332(uint8_t r, uint8_t g, uint8_t b)
{
return rgb332((r * 7 + 127) / 255, (g * 7 + 127) / 255, (b * 3 + 127) / 255);
}
// Static content is drawn into the canvas; the display loop copies changed
// areas into each frame buffer. Animated content draws into the frame buffer
// directly and uses the canvas as the background to erase with.
extern uint8_t *canvas;
// Must be called from the task that will drive the display; the LCD
// interrupt runs on that task's core.
void display_init(void);
uint8_t *display_fb(int buf);
// Mark part of the canvas as changed, so both frame buffers pick it up
void canvas_dirty(int x, int y, int w, int h);
void canvas_dirty_all(void);
// Copy any changed canvas areas into frame buffer `buf`. Returns true if
// anything was copied.
bool display_sync(int buf);
// Show frame buffer `buf` from the next frame, and return once that frame
// has started (the other buffer is then free to draw into).
void display_present(int buf);
// Wait for the next frame to start without changing buffers
void display_wait_frame(void);
// Copy what's on screen into dst (W*H bytes). Any task may call this; the
// display task does the copy between frames. False if it isn't running.
bool display_snapshot(uint8_t *dst);
// Drawing on any W x H surface (canvas or frame buffer); all calls clip
void gfx_fill(uint8_t *s, int x, int y, int w, int h, uint8_t c);
void gfx_restore(uint8_t *dst, int x, int y, int w, int h); // copy from canvas
// bg < 0 draws transparent. Returns the x just past the text.
int gfx_text(uint8_t *s, const font_t *f, int scale, int x, int y, const char *str, uint8_t fg, int bg);
int gfx_text_width(const font_t *f, int scale, const char *str);