main.ino
Arduino (C++) · 212 lines
#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>
#include <Adafruit_GFX.h>
#include <DIYables_TFT_SPI.h>
// ===== TFT PINS =====
#define TFT_CS 5
#define TFT_DC 2
#define TFT_RST 4
// ESP32 VSPI pins (hardware SPI - used implicitly, no need to pass them)
#define TFT_MOSI 23
#define TFT_SCK 18
#define TFT_MISO 19
// ILI9488 native resolution is 320x480 (portrait); rotation is applied in setup()
DIYables_ILI9488_SPI tft(320, 480, TFT_CS, TFT_DC, TFT_RST);
// This library has no color constants - plain RGB565 values
#define ILI9488_BLACK 0x0000
#define ILI9488_WHITE 0xFFFF
#define ILI9488_GREEN 0x07E0
// ===== WIFI / CAPTIVE PORTAL =====
const char* ssid = "ESP32_Display";
DNSServer dnsServer;
WebServer server(80);
String displayText = "mikrobot.pl"; // default text
// ===== SCREEN / MATRIX SETTINGS =====
static const int SCREEN_WIDTH = 480; // after setRotation(1)
static const int SCREEN_HEIGHT = 320;
static const int FONT_W = 6; // default Adafruit_GFX 5x7 + 1 spacing
static const int FONT_H = 8;
static const int MAX_COLS = 80; // 480 / 6
int columns = 0;
int dropY[MAX_COLS];
uint8_t speedFrames[MAX_COLS];
uint8_t frameCounter[MAX_COLS];
uint8_t tailLen[MAX_COLS];
bool activeCol[MAX_COLS];
// Density tuning (more black background without patterns)
static const int ACTIVE_PERCENT = 55; // percent of columns active at a time (lower => more black)
static const int REACTIVATE_CHANCE = 3; // chance per frame (per inactive col) to wake up (0..100)
// ===== HTML PAGE =====
String htmlPage() {
String page = "<!doctype html><html><head>"
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
"</head><body>"
"<h2>ESP32 Matrix Display</h2>"
"<form action='/set' method='POST'>"
"<input name='text' type='text' style='width:220px;height:34px;font-size:18px;'>"
"<br><br><input type='submit' value='Display' style='width:220px;height:40px;font-size:18px;'>"
"</form>"
"</body></html>";
return page;
}
void handleRoot() {
server.send(200, "text/html", htmlPage());
}
void handleSet() {
if (server.hasArg("text")) {
displayText = server.arg("text");
displayText.trim();
if (displayText.length() == 0) displayText = "mikrobot.pl";
}
server.sendHeader("Location", "/");
server.send(303);
}
// ===== DRAW CENTERED TEXT =====
void drawCenteredText() {
tft.setTextSize(3);
tft.setTextColor(ILI9488_WHITE, ILI9488_BLACK);
int16_t x1, y1;
uint16_t w, h;
tft.getTextBounds(displayText, 0, 0, &x1, &y1, &w, &h);
int x = (SCREEN_WIDTH - (int)w) / 2;
int y = (SCREEN_HEIGHT - (int)h) / 2;
tft.setCursor(x, y);
tft.print(displayText);
}
// ===== RESET / START A COLUMN =====
void startColumn(int i) {
dropY[i] = random(-SCREEN_HEIGHT, 0);
speedFrames[i] = random(2, 6); // slower -> less “green fill”
frameCounter[i] = 0;
tailLen[i] = random(6, 16); // shorter tails -> more black visible
activeCol[i] = true;
}
void stopColumn(int i) {
activeCol[i] = false;
}
// ===== MATRIX STEP =====
void matrixStep() {
for (int i = 0; i < columns; i++) {
// If inactive, randomly reactivate sometimes (no pattern)
if (!activeCol[i]) {
if (random(0, 100) < REACTIVATE_CHANCE) {
startColumn(i);
}
continue;
}
frameCounter[i]++;
if (frameCounter[i] < speedFrames[i]) continue;
frameCounter[i] = 0;
int x = i * FONT_W;
// Erase tail (black-on-black clears)
int tailY = dropY[i] - (int)tailLen[i] * FONT_H;
if (tailY >= 0 && tailY < SCREEN_HEIGHT) {
tft.setTextSize(1);
tft.setTextColor(ILI9488_BLACK, ILI9488_BLACK);
tft.setCursor(x, tailY);
tft.write((char)random(33, 126));
}
// Draw head
if (dropY[i] >= 0 && dropY[i] < SCREEN_HEIGHT) {
tft.setTextSize(1);
tft.setTextColor(ILI9488_GREEN, ILI9488_BLACK);
tft.setCursor(x, dropY[i]);
tft.write((char)random(33, 126));
}
// Advance
dropY[i] += FONT_H;
// Finished run? Reset or stop randomly to keep more black background
if (dropY[i] - (int)tailLen[i] * FONT_H > SCREEN_HEIGHT) {
// Chance to stop this column (adds black gaps, random)
if (random(0, 100) > ACTIVE_PERCENT) {
stopColumn(i);
} else {
startColumn(i);
}
}
}
}
void setup() {
Serial.begin(115200);
tft.begin(); // tft.begin(24000000) to pin the SPI clock (ILI9488 max)
tft.setRotation(1);
tft.fillScreen(ILI9488_BLACK);
// If your module has a LED/BL pin wired to a GPIO, uncomment:
// tft.initBacklight(15);
// tft.backlightOn();
randomSeed((uint32_t)esp_random());
columns = SCREEN_WIDTH / FONT_W;
if (columns > MAX_COLS) columns = MAX_COLS;
// Random initial activity per column (no pattern)
for (int i = 0; i < columns; i++) {
activeCol[i] = (random(0, 100) < ACTIVE_PERCENT);
if (activeCol[i]) startColumn(i);
}
// Start open AP
WiFi.softAP(ssid);
IPAddress myIP = WiFi.softAPIP();
// DNS catch-all for captive portal
dnsServer.start(53, "*", myIP);
server.on("/", handleRoot);
server.on("/set", HTTP_POST, handleSet);
// Redirect unknown URLs to portal
server.onNotFound([]() {
server.sendHeader("Location", "http://" + WiFi.softAPIP().toString(), true);
server.send(302, "text/plain", "");
});
server.begin();
drawCenteredText();
}
void loop() {
dnsServer.processNextRequest();
server.handleClient();
matrixStep();
drawCenteredText();
delay(20);
}