Ramadan Project — Esp32 Devkit C V4 simulation

An interactive Esp32 Devkit C V4 circuit simulation you can run free in your browser on Velxio, by tareq-neoaz.

Components

Sketch code

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Wire.h>
#include <RTClib.h>
#include <math.h>

// ---------- Wiring ----------
#define TFT_CS   2
#define TFT_DC   4
#define TFT_RST  5
// MOSI=23, MISO=19, SCK=18 (VSPI defaults)
// I2C: SDA=21, SCL=22

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
RTC_DS1307 rtc;

#define SCREEN_W 240
#define SCREEN_H 320

#define STRIP_Y   0
#define STRIP_H   140
GFXcanvas16 *strip = nullptr;

// ---------- Colors ----------
#define C_NIGHT_TOP   0x10A4
#define C_NIGHT_MID   0x2967
#define C_NIGHT_BOT   0xCB26
#define C_MOON        0xFFE0
#define C_LANTERN     0xFD20
#define C_LANTERN_DARK 0x8000
#define C_MOSQUE      0x0000
#define C_TEXT        0xFFE0
#define C_CLOUD       0x7BEF

// ---------- Countdown ----------
#define EID_YEAR   2026
#define EID_MONTH  3
#define EID_DAY    20
#define FALLBACK_DAYS 5

// ---------- Lanterns ----------
struct Lantern {
  int16_t x, y, length, size;
  float   phase;
};
Lantern lanterns[3] = {
  { 50,  10, 40, 14, 0.0f },
  { 120, 10, 55, 16, 1.2f },
  { 190, 10, 45, 14, 2.4f },
};

// ---------- Stars ----------
#define NUM_STARS 45
struct Star { int16_t x, y; uint8_t phase, speed; };
Star stars[NUM_STARS];

// ---------- Clouds ----------
#define NUM_CLOUDS 4
struct Cloud { float x, y; int16_t w, h; };
Cloud clouds[NUM_CLOUDS];

int daysUntilEid = FALLBACK_DAYS;
unsigned long lastCountdownUpdate = 0;

const int MOON_CX = 120;
const int MOON_CY = 200;
const int MOON_R  = 45;

// =====================================================
// HELPERS
// =====================================================
uint16_t lerp565(uint16_t a, uint16_t b, float t) {
  uint8_t ar = (a >> 11) & 0x1F, ag = (a >> 5) & 0x3F, ab = a & 0x1F;
  uint8_t br = (b >> 11) & 0x1F, bg = (b >> 5) & 0x3F, bb = b & 0x1F;
  uint8_t rr = ar + (br - ar) * t;
  uint8_t gg = ag + (bg - ag) * t;
  uint8_t bl = ab + (bb - ab) * t;
  return (rr << 11) | (gg << 5) | bl;
}

uint16_t sampleSky(int y) {
  if (y < 0) y = 0;
  if (y >= SCREEN_H) y = SCREEN_H - 1;
  if (y < SCREEN_H * 0.55) {
    float t = (float)y / (SCREEN_H * 0.55f);
    return lerp565(C_NIGHT_TOP, C_NIGHT_MID, t);
  } else {
    float t = (y - SCREEN_H * 0.55f) / (SCREEN_H * 0.45f);
    return lerp565(C_NIGHT_MID, C_NIGHT_BOT, t);
  }
}

uint16_t dimWhite(uint8_t b) {
  uint8_t r = (b >> 3) & 0x1F;
  uint8_t g = (b >> 2) & 0x3F;
  uint8_t bl = (b >> 3) & 0x1F;
  return (r << 11) | (g << 5) | bl;
}

uint16_t blend565(uint16_t a, uint16_t b, uint8_t alpha) {
  uint8_t ar = (a >> 11) & 0x1F, ag = (a >> 5) & 0x3F, ab = a & 0x1F;
  uint8_t br = (b >> 11) & 0x1F, bg = (b >> 5) & 0x3F, bb = b & 0x1F;
  uint8_t rr = (ar * (255 - alpha) + br * alpha) / 255;
  uint8_t gg = (ag * (255 - alpha) + bg * alpha) / 255;
  uint8_t bl = (ab * (255 - alpha) + bb * alpha) / 255;
  return (rr << 11) | (gg << 5) | bl;
}

// =====================================================
// STAR RENDER
// ============================

More projects by tareq-neoaz