// AnalogClock_P4.ino
// Graphical analog clock for the ESP32-P4 Function EV Board V1.4 (7" 1024x600 MIPI DSI).
#include <Arduino.h>
#include <WiFi.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include "esp_lcd_panel_ops.h"
#include "secrets.h"
// ---------- Display ----------
#define LCD_W 1024
#define LCD_H 600
#define CX (LCD_W / 2)
#define CY (LCD_H / 2)
#define CLOCK_R 270
#define PIN_LCD_RST 27
#define PIN_LCD_BL 26
// Set this to the panel handle created by the board's display (BSP) init.
// flushFrame() does nothing while it is nullptr.
esp_lcd_panel_handle_t panel_handle = nullptr;
static uint16_t *fb = nullptr; // RGB565 frame buffer in PSRAM
// ---------- Network / time ----------
#define NTP_SERVER "pool.ntp.org"
#define GMT_OFFSET_SEC 19800 // India (UTC+5:30); change for your zone
#define DST_OFFSET_SEC 0
// ---------- Time state (declared before any function uses it) ----------
struct ClockTime {
int yy, mo, dd, hh, mm, ss;
bool synced;
};
// ---------- Colors (RGB565) ----------
static inline uint16_t rgb565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
#define C_BG rgb565(12, 16, 30)
#define C_SHADOW rgb565(4, 6, 12)
#define C_FACE rgb565(22, 28, 48)
#define C_FACE_HI rgb565(34, 42, 68)
#define C_RIM rgb565(212, 175, 55)
#define C_TICK rgb565(230, 230, 230)
#define C_NUM rgb565(235, 235, 240)
#define C_HOUR rgb565(240, 240, 240)
#define C_MIN rgb565(200, 200, 210)
#define C_SEC rgb565(255, 80, 60)
#define C_CENTER rgb565(212, 175, 55)
#define C_TIME rgb565(245, 245, 250)
#define C_DATE rgb565(212, 175, 55)
#define C_DIM rgb565(120, 128, 150)
// ---------- Pixel primitives ----------
static inline void px(int x, int y, uint16_t c) {
if (x < 0 || y < 0 || x >= LCD_W || y >= LCD_H) return;
fb[y * LCD_W + x] = c;
}
void fillRect(int x, int y, int w, int h, uint16_t c) {
for (int j = 0; j < h; j++)
for (int i = 0; i < w; i++) px(x + i, y + j, c);
}
void fillCircle(int cx, int cy, int r, uint16_t c) {
for (int y = -r; y <= r; y++) {
int dx = (int)sqrtf((float)(r * r - y * y));
for (int x = -dx; x <= dx; x++) px(cx + x, cy + y, c);
}
}
void fillRing(int cx, int cy, int r0, int r1, uint16_t c) {
for (int y = -r1; y <= r1; y++) {
for (int x = -r1; x <= r1; x++) {
int d2 = x * x + y * y;
if (d2 <= r1 * r1 && d2 >= r0 * r0) px(cx + x, cy + y, c);
}
}
}
void thickLine(float x0, float y0, float x1, float y1, int w, uint16_t c) {
float dx = x1 - x0, dy = y1 - y0;
int steps = (int)ceilf(sqrtf(dx * dx + dy * dy));
if (steps == 0) steps = 1;
for (int i = 0; i <= steps; i++) {
float t = (float)i / steps;
fillCircle((int)lroundf(x0 + dx * t), (int)lroundf(y0 + dy * t), w, c);
}
}
// ---------- 5x7 bitmap font (digits, ':', '/', ' ', '-') ----------
static const uint8_t FONT[14][7] = {
{0x0E,0x11,0x13,0x15,0x19,0x11,0x0E}, // 0
{0x04,0x0C,0x04,0x04,0x04,0x04,0x0E}, // 1
{0x0E,0x11,0x01,0x02,0x04,0x08,0x1F}, // 2
{0x1F,0x02,0x04,0x02,0x01,0x11,0x0E}, // 3
{0x02,0x06,0x0A,0x12,0x1F,0x02,0x02}, // 4
{0x1F,0x10,0x1E,0x01,0x01,0x11,0x0E}, // 5
{0x06,0x08,0x10,0x1E,0x11,0x11,0x0E}, // 6
{0x1F,0x01,0x02,0x04,0x08,0x08,0x08}, // 7
{0x0E,0x11,0x11,0x0E,0x11,0x11,0x0E}, // 8
{0x0E,0x11,0x11,0x0F,0x01,0x02,0x0C}, // 9
{0x00,0x04,0x04,0x00,0x04,0x04,0x00}, // :
{0x01,0x01,0x02,0x04,0x08,0x10,0x10}, // /
{0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // space
{0x00,0x00,0x00,0x1F,0x00,0x00,0x00}, // -
};
int glyphIndex(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c == ':') return 10;
if (c == '/') return 11;
if (c == '-') return 13;
return 12; // space for anything unsupported
}
void drawChar(int x, int y, char c, int s, uint16_t col) {
const uint8_t *g = FONT[glyphIndex(c)];
for (int row = 0; row < 7; row++)
for (int cc = 0; cc < 5; cc++)
if ((g[row] >> (4 - cc)) & 1) fillRect(x + cc * s, y + row * s, s, s, col);
}
int textWidth(const char *str, int s) {
int n = strlen(str);
return n == 0 ? 0 : n * 6 * s - s;
}
void drawTextCentered(int cx, int cy, const char *str, int s, uint16_t col) {
int x = cx - textWidth(str, s) / 2;
int y = cy - (7 * s) / 2;
for (int i = 0; str[i]; i++) {
drawChar(x, y, str[i], s, col);
x += 6 * s;
}
}
// ---------- Hands ----------
// deg: 0 = 12 o'clock, clockwise. dx, dy offset the hand (used for shadows).
void drawHand(float deg, int len, int tail, int w, uint16_t c, int dx = 0, int dy = 0) {
float a = (deg - 90.0f) * DEG_TO_RAD;
float x0 = CX - cosf(a) * tail + dx, y0 = CY - sinf(a) * tail + dy;
float x1 = CX + cosf(a) * len + dx, y1 = CY + sinf(a) * len + dy;
thickLine(x0, y0, x1, y1, w, c);
}
// ---------- Scene ----------
void drawDial() {
for (int i = 0; i < LCD_W * LCD_H; i++) fb[i] = C_BG;
// Drop shadow under the dial, then the dial face with a lighter inner ring
fillCircle(CX + 8, CY + 10, CLOCK_R + 4, C_SHADOW);
fillCircle(CX, CY, CLOCK_R, C_FACE);
fillCircle(CX, CY, CLOCK_R - 30, C_FACE_HI);
fillCircle(CX, CY, CLOCK_R - 32, C_FACE);
// Gold rim
fillRing(CX, CY, CLOCK_R - 8, CLOCK_R, C_RIM);
// 60 tick marks (thicker at each hour)
for (int i = 0; i < 60; i++) {
float a = (i * 6.0f - 90.0f) * DEG_TO_RAD;
bool hour = (i % 5 == 0);
float r0 = CLOCK_R - (hour ? 45 : 25);
float r1 = CLOCK_R - 16;
thickLine(CX + cosf(a) * r0, CY + sinf(a) * r0,
CX + cosf(a) * r1, CY + sinf(a) * r1,
hour ? 4 : 1, C_TICK);
}
// Numerals 1 to 12
char buf[4];
for (int i = 1; i <= 12; i++) {
float a = (i * 30.0f - 90.0f) * DEG_TO_RAD;
float r = CLOCK_R - 75;
snprintf(buf, sizeof(buf), "%d", i);
drawTextCentered(CX + cosf(a) * r, CY + sinf(a) * r, buf, 6, C_NUM);
}
}
void drawHands(int hh, int mm, int ss) {
float secDeg = ss * 6.0f;
float minDeg = mm * 6.0f + ss * 0.1f;
float hourDeg = (hh % 12) * 30.0f + mm * 0.5f;
// Shadows first (offset down-right), then the hands
drawHand(hourDeg, 150, 16, 9, C_SHADOW, 5, 6);
drawHand(minDeg, 220, 20, 6, C_SHADOW, 6, 7);
drawHand(secDeg, 245, 50, 2, C_SHADOW, 4, 5);
drawHand(hourDeg, 150, 16, 9, C_HOUR);
drawHand(minDeg, 220, 20, 6, C_MIN);
drawHand(secDeg, 245, 50, 2, C_SEC);
// Hub
fillCircle(CX, CY, 14, C_CENTER);
fillCircle(CX, CY, 6, C_BG);
}
// Side panels: date on the left, digital time on the right.
// The dial spans x = 242..782, so both panels sit outside it.
void drawPanels(const char *dm, const char *yr, const char *hm, const char *sec) {
drawTextCentered(120, CY - 30, dm, 6, C_DATE);
drawTextCentered(120, CY + 30, yr, 6, C_DATE);
drawTextCentered(900, CY - 30, hm, 7, C_TIME);
drawTextCentered(900, CY + 60, sec, 4, C_DIM);
}
void drawScene(const ClockTime &t) {
char dm[8], yr[8], hm[8], sec[4];
drawDial();
drawHands(t.hh, t.mm, t.ss);
if (t.synced) {
snprintf(dm, sizeof(dm), "%02d/%02d", t.dd, t.mo);
snprintf(yr, sizeof(yr), "%04d", t.yy);
} else {
snprintf(dm, sizeof(dm), "--/--");
snprintf(yr, sizeof(yr), "----");
}
snprintf(hm, sizeof(hm), "%02d:%02d", t.hh, t.mm);
snprintf(sec, sizeof(sec), "%02d", t.ss);
drawPanels(dm, yr, hm, sec);
}
void flushFrame() {
if (!panel_handle) return; // display not initialised yet
esp_lcd_panel_draw_bitmap(panel_handle, 0, 0, LCD_W, LCD_H, fb);
}
// ---------- Time ----------
bool readClock(ClockTime &t) {
struct tm tm_info;
if (getLocalTime(&tm_info, 100)) {
t.yy = tm_info.tm_year + 1900;
t.mo = tm_info.tm_mon + 1;
t.dd = tm_info.tm_mday;
t.hh = tm_info.tm_hour;
t.mm = tm_info.tm_min;
t.ss = tm_info.tm_sec;
t.synced = true;
return true;
}
// No network: count from 12:00:00, date unknown
unsigned long s = millis() / 1000UL;
t.yy = t.mo = t.dd = 0;
t.hh = 12 + (s / 3600) % 12;
t.mm = (s / 60) % 60;
t.ss = s % 60;
t.synced = false;
return false;
}
// ---------- Setup / loop ----------
void setup() {
Serial.begin(115200);
delay(500);
// Check PSRAM first: the frame buffer needs about 1.2 MB of it
Serial.printf("PSRAM size: %u bytes\n", ESP.getPsramSize());
Serial.printf("Free PSRAM: %u bytes\n", ESP.getFreePsram());
if (ESP.getPsramSize() == 0) {
Serial.println("PSRAM is not enabled. Turn it on in the board settings and reflash.");
while (true) delay(1000);
}
// Backlight on (PWM on GPIO26)
ledcAttach(PIN_LCD_BL, 5000, 8);
ledcWrite(PIN_LCD_BL, 255);
// Frame buffer in PSRAM
fb = (uint16_t *)ps_malloc(LCD_W * LCD_H * sizeof(uint16_t));
if (!fb) {
Serial.println("PSRAM allocation failed");
while (true) delay(1000);
}
Serial.println("Frame buffer allocated");
// Wi-Fi (on this board it is provided through the ESP32-C6)
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("Connecting Wi-Fi");
for (int i = 0; i < 30 && WiFi.status() != WL_CONNECTED; i++) {
delay(500);
Serial.print(".");
}
Serial.println(WiFi.status() == WL_CONNECTED ? " connected" : " failed");
if (WiFi.status() == WL_CONNECTED) {
configTime(GMT_OFFSET_SEC, DST_OFFSET_SEC, NTP_SERVER);
}
}
void loop() {
static unsigned long nextTick = 0;
unsigned long now = millis();
// Redraw at fixed one-second intervals
if ((long)(now - nextTick) >= 0) {
nextTick = now + 1000;
ClockTime t;
readClock(t);
drawScene(t);
flushFrame();
}
delay(10);
}Loading
esp32-p4-function-ev
esp32-p4-function-ev