// AnalogClock_P4.ino
// Analog clock for the ESP32-P4 Function EV Board V1.4 with a 7" 1024x600 MIPI DSI panel.
#include <Arduino.h>
#include <WiFi.h>
#include <math.h>
#include <time.h>
#include "esp_lcd_panel_ops.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.
// It stays nullptr until that init runs, and flushFrame() skips drawing while it is null.
esp_lcd_panel_handle_t panel_handle = nullptr;
static uint16_t *fb = nullptr; // RGB565 frame buffer in PSRAM
// ---------- Network / time ----------
#define WIFI_SSID "Airtel_arvi_6035"
#define WIFI_PASS "air26289"
#define NTP_SERVER "pool.ntp.org"
#define GMT_OFFSET_SEC 19800 // India (UTC+5:30); change for your zone
#define DST_OFFSET_SEC 0
// ---------- 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_FACE rgb565(22, 28, 48)
#define C_RIM rgb565(212, 175, 55)
#define C_TICK rgb565(230, 230, 230)
#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)
// ---------- Drawing helpers ----------
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 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);
}
}
}
// Thick line from (x0,y0) to (x1,y1)
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);
}
}
// Hand from center. deg: 0 = 12 o'clock, clockwise.
void drawHand(float deg, int len, int tail, int w, uint16_t c) {
float a = (deg - 90.0f) * DEG_TO_RAD;
float x0 = CX - cosf(a) * tail, y0 = CY - sinf(a) * tail;
float x1 = CX + cosf(a) * len, y1 = CY + sinf(a) * len;
thickLine(x0, y0, x1, y1, w, c);
}
void drawFace() {
for (int i = 0; i < LCD_W * LCD_H; i++) fb[i] = C_BG;
fillCircle(CX, CY, CLOCK_R, C_FACE);
fillRing(CX, CY, CLOCK_R - 8, CLOCK_R, C_RIM);
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);
}
}
void drawClock(int hh, int mm, int ss) {
drawFace();
float secDeg = ss * 6.0f;
float minDeg = mm * 6.0f + ss * 0.1f;
float hourDeg = (hh % 12) * 30.0f + mm * 0.5f;
drawHand(hourDeg, 150, 16, 9, C_HOUR);
drawHand(minDeg, 220, 20, 6, C_MIN);
drawHand(secDeg, 245, 50, 2, C_SEC);
fillCircle(CX, CY, 12, C_CENTER);
fillCircle(CX, CY, 5, C_BG);
}
// Push the frame buffer to the panel over MIPI DSI
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 getNow(int &hh, int &mm, int &ss) {
struct tm t;
if (getLocalTime(&t, 100)) {
hh = t.tm_hour; mm = t.tm_min; ss = t.tm_sec;
return true;
}
// No network: count from 12:00:00
unsigned long s = millis() / 1000UL;
hh = 12 + (s / 3600) % 12;
mm = (s / 60) % 60;
ss = s % 60;
return false;
}
// ---------- Setup / loop ----------
void setup() {
Serial.begin(115200);
// 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);
}
// 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() {
int hh, mm, ss;
getNow(hh, mm, ss);
drawClock(hh, mm, ss);
flushFrame();
delay(1000);
}Loading
esp32-p4-function-ev
esp32-p4-function-ev