coba tft — ESP32-S3 simulation

An interactive ESP32-S3 circuit simulation you can run free in your browser on Velxio, by itsmebenn.

Components

Sketch code

#include <SPI.h>
#include <XPT2046_Touchscreen.h>

// --- KONFIGURASI LAYAR TFT ILI9488 ---
#define TFT_CS   5
#define TFT_DC   2
#define TFT_RST  4
#define TFT_W    480   
#define TFT_H    320

// --- KONFIGURASI TOUCH SCREEN XPT2046 ---
#define TOUCH_CS  40
#define TOUCH_IRQ 41
XPT2046_Touchscreen ts(TOUCH_CS, TOUCH_IRQ);

// --- STATE MACHINE (HALAMAN) ---
enum ScreenState { DASHBOARD, AIR_DETAIL, ROOM_LIST, SETTING_MENU };
ScreenState currentState = DASHBOARD;
bool screenNeedsUpdate = true; 

// ==========================================
// PENGGERAK TFT (DENGAN ATURAN LALU LINTAS SPI)
// ==========================================
static void cmd(uint8_t c) {
  SPI.beginTransaction(SPISettings(40000000, MSBFIRST, SPI_MODE0));
  digitalWrite(TFT_DC, LOW);
  digitalWrite(TFT_CS, LOW);
  SPI.transfer(c);
  digitalWrite(TFT_CS, HIGH);
  SPI.endTransaction();
}

static void data(const uint8_t *d, size_t n) {
  SPI.beginTransaction(SPISettings(40000000, MSBFIRST, SPI_MODE0));
  digitalWrite(TFT_DC, HIGH);
  digitalWrite(TFT_CS, LOW);
  SPI.writeBytes((uint8_t*)d, n);
  digitalWrite(TFT_CS, HIGH);
  SPI.endTransaction();
}

static void data8(uint8_t d) { data(&d, 1); }

static void setWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
  uint8_t c[4] = { (uint8_t)(x0 >> 8), (uint8_t)x0, (uint8_t)(x1 >> 8), (uint8_t)x1 };
  uint8_t p[4] = { (uint8_t)(y0 >> 8), (uint8_t)y0, (uint8_t)(y1 >> 8), (uint8_t)y1 };
  cmd(0x2A); data(c, 4);   
  cmd(0x2B); data(p, 4);   
  cmd(0x2C);               
}

static void fillRect(int x, int y, int w, int h, uint8_t r, uint8_t g, uint8_t b) {
  static uint8_t line[TFT_W * 3]; 
  for (int i = 0; i < w; i++) {
    line[i * 3] = r & 0xFC; line[i * 3 + 1] = g & 0xFC; line[i * 3 + 2] = b & 0xFC;
  }
  setWindow(x, y, x + w - 1, y + h - 1);
  
  SPI.beginTransaction(SPISettings(40000000, MSBFIRST, SPI_MODE0));
  digitalWrite(TFT_DC, HIGH);
  digitalWrite(TFT_CS, LOW);
  for (int row = 0; row < h; row++) SPI.writeBytes(line, w * 3);
  digitalWrite(TFT_CS, HIGH);
  SPI.endTransaction();
}

static void drawRect(int x, int y, int w, int h, uint8_t thickness, uint8_t r, uint8_t g, uint8_t b) {
  fillRect(x, y, w, thickness, r, g, b); 
  fillRect(x, y + h - thickness, w, thickness, r, g, b); 
  fillRect(x, y, thickness, h, r, g, b); 
  fillRect(x + w - thickness, y, thickness, h, r, g, b); 
}

// Kamus Font Ekstra Lengkap
struct Glyph { char ch; uint8_t col[5]; };
static const Glyph FONT[] = {
  { '0', { 0x3E, 0x51, 0x49, 0x45, 0x3E } }, { '1', { 0x00, 0x42, 0x7F, 0x40, 0x00 } },
  { '2', { 0x42, 0x61, 0x51, 0x49, 0x46 } }, { '3', { 0x21, 0x41, 0x45, 0x4B, 0x31 } },
  { '4', { 0x18, 0x14, 0x12, 0x7F, 0x10 } }, { '5', { 0x27, 0x45, 0x45, 0x45, 0x39 } },
  { '6', { 0x3C, 0x4A, 0x49, 0x49, 0x30 } }, { '7', { 0x01, 0x71, 0x09, 0x05, 0x03 } },
  { '8', { 0x36, 0x49, 0x49, 0x49, 0x36 } }, { '9', { 0x06, 0x49, 0x49, 0x29, 0x1E } },
  
  { 'A', { 0x7E, 0x09, 0x09, 0x09, 0x7E } }, { 'B', { 0x7F, 0x49, 0x49, 0x49, 0x36 } },
  { 'C

More projects by itsmebenn