relog 1233 — Xiao Esp32S3 Sense simulation

Reloj analógico con manecillas y hora digital en la pantalla redonda GC9A01 del XIAO ESP32S3 Sense.

An interactive Xiao Esp32S3 Sense circuit simulation you can run free in your browser on Velxio, by dave.

Components

Sketch code

// Round clock for the Seeed Studio Round Display (GC9A01A, 240x240) on the
// XIAO ESP32S3 Sense.
//
// SPI wiring (display pin -> XIAO pin):
//   CS -> D1, DC -> D3, BL -> D6, SCK -> D8, MOSI -> D10, MISO -> D9
//   VCC -> 3V3, GND -> GND
//
// The clock is millis()-based and starts at 10:10:00.
// Serial: "SET hh:mm:ss" sets the current time.

#include <math.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_GC9A01A.h>

#define TFT_CS    D1
#define TFT_DC    D3
#define TFT_RST   -1      // no reset pin on the Round Display
#define TFT_BL    D6
#define PIN_SCK   D8
#define PIN_MISO  D9
#define PIN_MOSI  D10

SPIClass spi(FSPI);
Adafruit_GC9A01A tft(&spi, TFT_CS, TFT_DC, TFT_RST);

// Clock face geometry
const int CX = 120;   // center x
const int CY = 118;   // center y
const int R  = 96;    // face radius
const int HLEN = 44;  // hour hand length
const int MLEN = 66;  // minute hand length
const int SLEN = 78;  // second hand length

const uint16_t BG   = 0x0000;   // black
const uint16_t FG   = 0xFFFF;   // white
const uint16_t RED  = 0xF800;
const uint16_t GRAY = 0x8410;

unsigned long epoch = 10UL * 3600 + 10UL * 60;  // 10:10:00
uint32_t t0 = 0;
bool handsDrawn = false;
float prevH = 0, prevM = 0, prevS = 0;

void handAt(float angleDeg, int len, uint16_t color) {
  float rad = angleDeg * PI / 180.0f;
  int x = CX + (int)(len * sinf(rad));
  int y = CY - (int)(len * cosf(rad));
  tft.drawLine(CX, CY, x, y, color);
}

void drawFace() {
  tft.fillScreen(BG);
  tft.drawCircle(CX, CY, R, GRAY);

  // 60 minute ticks; hour ticks are longer and white
  for (int i = 0; i < 60; i++) {
    float rad = i * (PI / 30.0f);
    int rIn  = (i % 5 == 0) ? R - 14 : R - 6;
    int rOut = R - 2;
    int x1 = CX + (int)(rIn  * sinf(rad));
    int y1 = CY - (int)(rIn  * cosf(rad));
    int x2 = CX + (int)(rOut * sinf(rad));
    int y2 = CY - (int)(rOut * cosf(rad));
    tft.drawLine(x1, y1, x2, y2, (i % 5 == 0) ? FG : GRAY);
  }
}

void drawDigital(uint8_t h, uint8_t m, uint8_t s) {
  char buf[9];
  sprintf(buf, "%02d:%02d:%02d", h, m, s);
  tft.fillRect(60, 200, 120, 20, BG);  // erase previous text
  tft.setTextColor(FG);
  tft.setTextSize(2);
  tft.setCursor(72, 202);
  tft.print(buf);
}

void renderClock(uint8_t h, uint8_t m, uint8_t s) {
  float hAng = (h % 12) * 30.0f + m * 0.5f;
  float mAng = m * 6.0f + s * 0.1f;
  float sAng = s * 6.0f;

  if (handsDrawn) {  // erase previous hands in background color
    handAt(prevH, HLEN, BG);
    handAt(prevM, MLEN, BG);
    handAt(prevS, SLEN, BG);
  }
  handAt(hAng, HLEN, FG);
  handAt(mAng, MLEN, FG);
  handAt(sAng, SLEN, RED);
  tft.fillCircle(CX, CY, 4, FG);  // center cap
  drawDigital(h, m, s);

  prevH = hAng; prevM = mAng; prevS = sAng;
  handsDrawn = true;
}

void handleSerial() {
  static String line;
  while (Serial.available()) {
    char c = (char)Serial.read();
    if (c == '\n') {
      line.trim();
      if (line.startsWith("SET")) {
        int h, m, s;
        if (sscanf(line.c_str(

More projects by dave