#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// ======================================================
// CYD ESP32-2432S028
// FOUR WAR QUOTES DISPLAY
// ======================================================
// TFT PINS
#define TFT_CS 15
#define TFT_DC 2
#define TFT_MOSI 13
#define TFT_MISO 12
#define TFT_SCK 14
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// RGB LED
#define LED_R 4
#define LED_G 16
#define LED_B 17
// DISPLAY SIZE
#define W 320
#define H 240
// ======================================================
// RGB CONTROL
// ======================================================
void setRGB(bool r, bool g, bool b) {
digitalWrite(LED_R, r ? LOW : HIGH);
digitalWrite(LED_G, g ? LOW : HIGH);
digitalWrite(LED_B, b ? LOW : HIGH);
}
// ======================================================
// QUOTES
// ======================================================
const char* quotes[] = {
"In war, truth is the first casualty.",
"Victory belongs to the most persevering.",
"Only the dead have seen the end of war.",
"Courage is resistance to fear."
};
const char* authors[] = {
"- Aeschylus",
"- Napoleon Bonaparte",
"- Plato",
"- Mark Twain"
};
// ======================================================
// COLORS
// ======================================================
uint16_t quoteColors[] = {
ILI9341_RED,
ILI9341_CYAN,
ILI9341_YELLOW,
ILI9341_GREEN
};
// ======================================================
// BACKGROUND GRADIENT
// ======================================================
void gradientBG(uint16_t topColor, uint16_t bottomColor) {
uint8_t r1 = ((topColor >> 11) & 0x1F) << 3;
uint8_t g1 = ((topColor >> 5) & 0x3F) << 2;
uint8_t b1 = (topColor & 0x1F) << 3;
uint8_t r2 = ((bottomColor >> 11) & 0x1F) << 3;
uint8_t g2 = ((bottomColor >> 5) & 0x3F) << 2;
uint8_t b2 = (bottomColor & 0x1F) << 3;
for (int y = 0; y < H; y++) {
uint8_t r = map(y, 0, H, r1, r2);
uint8_t g = map(y, 0, H, g1, g2);
uint8_t b = map(y, 0, H, b1, b2);
tft.drawFastHLine(
0,
y,
W,
tft.color565(r, g, b)
);
}
}
// ======================================================
// FRAME
// ======================================================
void drawFrame(uint16_t c) {
tft.drawRoundRect(6, 6, W-12, H-12, 12, c);
tft.drawRoundRect(12, 12, W-24, H-24, 10, c);
tft.drawFastHLine(25, 40, W-50, c);
tft.drawFastHLine(25, H-40, W-50, c);
}
// ======================================================
// TYPEWRITER EFFECT
// ======================================================
void typeWriter(
const char* txt,
int x,
int y,
uint16_t color
) {
String build = "";
tft.setTextColor(color);
for (int i = 0; txt[i] != '\0'; i++) {
build += txt[i];
tft.fillRect(x, y, 260, 70, ILI9341_BLACK);
tft.setCursor(x, y);
tft.print(build);
delay(35);
}
}
// ======================================================
// SHOW QUOTE
// ======================================================
void showQuote(
const char* quote,
const char* author,
uint16_t textColor,
uint16_t bg1,
uint16_t bg2
) {
gradientBG(bg1, bg2);
drawFrame(textColor);
// TITLE
tft.setTextColor(textColor);
tft.setTextSize(2);
tft.setCursor(95, 18);
tft.print("WAR QUOTES");
// QUOTE
tft.setTextSize(2);
typeWriter(
quote,
28,
90,
textColor
);
// AUTHOR
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(90, 180);
tft.print(author);
// DECORATION
for (int i = 0; i < 25; i++) {
tft.drawCircle(
W/2,
H/2,
30 + i * 3,
tft.color565(
random(50,255),
random(50,255),
random(50,255)
)
);
}
delay(8000);
}
// ======================================================
// SETUP
// ======================================================
void setup() {
SPI.begin(TFT_SCK, TFT_MISO, TFT_MOSI, TFT_CS);
tft.begin();
tft.setRotation(1);
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
setRGB(false, false, false);
randomSeed(analogRead(0));
tft.fillScreen(ILI9341_BLACK);
}
// ======================================================
// LOOP
// ======================================================
void loop() {
// =========================================
// QUOTE 1
// =========================================
setRGB(true, false, false);
showQuote(
quotes[0],
authors[0],
quoteColors[0],
ILI9341_BLACK,
ILI9341_MAROON
);
// =========================================
// QUOTE 2
// =========================================
setRGB(false, true, false);
showQuote(
quotes[1],
authors[1],
quoteColors[1],
ILI9341_NAVY,
ILI9341_BLACK
);
// =========================================
// QUOTE 3
// =========================================
setRGB(false, false, true);
showQuote(
quotes[2],
authors[2],
quoteColors[2],
ILI9341_DARKGREEN,
ILI9341_BLACK
);
// =========================================
// QUOTE 4
// =========================================
setRGB(true, true, false);
showQuote(
quotes[3],
authors[3],
quoteColors[3],
ILI9341_PURPLE,
ILI9341_BLACK
);
}https://wokwi.com/projects/464271800647048193