#include <Adafruit_GFX.h> // https://github.com/adafruit/Adafruit-GFX-Library
#include <Adafruit_ILI9341.h> // https://github.com/adafruit/Adafruit_ILI9341
// #include <Adafruit_BusIO.h> //https://github.com/adafruit/Adafruit_BusIO
// ansi
// https://gist.github.com/ConnerWill/d4b6c776b509add763e17f9f113fd25b
// https://forum.arduino.cc/t/ansi-output-in-serial-monitor/1443596
#define TFT_MISO 15
#define TFT_CLK 18
#define TFT_MOSI 23
#define TFT_DC 2 // incorrect DC to screen pin gives white screen
#define TFT_RST 4
#define TFT_CS 5
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#define wht 0xFFFF
#define blk 0x0000
#define red 0xF800
#define amb 0xFEA0
#define yel 0xFFE0
#define grn 0x07E0
#define cyn 0x07FF
#define blu 0x001F
#define redoff 0x7800
#define yeloff 0x9A60
#define grnoff 0x03E0
#define KEY1 14 // button
bool toggle = 0;
unsigned long blinktimer, blinktimeout = 500;
void setup() {
Serial.begin(115200);
delay(2000);
pinMode(KEY1, INPUT_PULLUP); // PRESSED = 0, RELEASED = 1
tft.begin();
tft.setRotation(1);
tft.setTextSize(1);
tft.setTextColor(wht, blk);
nessoN1outline();
drawTrafficLight();
for (byte i = 0; i < 8; i++) // scroll Serial Monitor to clear
Serial.println();
delay(3000);
}
void loop() {
circlefill(50, 30, 13, redoff);
circlefill(50, 65, 13, yeloff);
circlefill(50, 100, 13, grn); // green light OH
while (digitalRead(KEY1) == 1) // wait for button
walkdontwalk(1); // flashing "DONT WALK"
walkdontwalk(0); // steady "WALK"
circlefill(50, 30, 13, redoff);
circlefill(50, 65, 13, yel); // yellow light ON
circlefill(50, 100, 13, grnoff);
delay(3000);
circlefill(50, 30, 13, red); // red light ON
circlefill(50, 65, 13, yeloff);
circlefill(50, 100, 13, grnoff);
delay(5000);
}
// https://doc-tft-espi.readthedocs.io/tft_espi/methods/constructor/
// https://doc-tft-espi.readthedocs.io/tft_espi/colors/
void walkdontwalk(bool state) {
if (state == 1) { // DONT WALK
if (millis() - blinktimer >= blinktimeout) {
blinktimer = millis();
toggle = !toggle;
if (toggle) {
text(100, 25, 5, redoff, "DONT"); // x, y, pitch, text
text(100, 80, 5, redoff, "WALK");
} else {
text(100, 25, 5, red, "DONT");
text(100, 80, 5, red, "WALK");
}
}
} else {
text(100, 25, 5, redoff, "DONT");
text(100, 80, 5, red, "WALK");
}
}
void drawTrafficLight() {
for (byte i = 0; i < 4; i++) // border thickness
roundrectdraw(90 + i, 10 + i, 135 - i * 2, 115 - i * 2, 7, red, 5);
text(100, 25, 5, redoff, "DONT");
text(100, 80, 5, red, "WALK");
roundrectfill(20, 10, 60, 115, 7, yel); // x, y, w, h, color
circlefill(50, 30, 15, blk);
circlefill(50, 30, 13, red); // x, y, radius, color
circlefill(50, 65, 15, blk);
circlefill(50, 65, 13, amb); // x, y, radius, color (gold)
circlefill(50, 100, 15, blk);
circlefill(50, 100, 13, grn); // x, y, radius, color
}
void nessoN1outline() {
int nessoW = 240;
int nessoH = 135;
int startX = ((tft.width() - nessoW + 2) / 2); // inside border is full Nesso N1 screen
int startY = ((tft.height() - nessoH + 2) / 2); // inside border is full Nesso N1 screen
tft.drawRect(startX, startY, nessoW, nessoH, wht);
tft.setCursor(0, 0); tft.print("Arduino Nesso N1");
tft.setCursor(0, 10); tft.print("startX: "); tft.print(startX);
tft.setCursor(0, 20); tft.print("startY: "); tft.print(startY);
}
void text(int x, int y, byte pitch, int color, char text[]) {
x += 41;
y += 53;
tft.setTextSize(pitch);
tft.setTextColor(color);
tft.setCursor(x, y);
tft.print(text);
tft.setTextSize(1);
}
void pixel() {
int y = 180, color = yel;
for (int x = 80; x < 180; x += 5) // increase left to right
tft.drawPixel(x, y, color);
}
void line() {
int xStart = 0, yStart = 0, xEnd = 240, yEnd = 320, color = red;
tft.drawLine(xStart, yStart, xEnd, yEnd, color);
}
void rect() {
int x = 40, y = 150, width = 160, height = 40, color = cyn;
tft.drawRect(x, y, width, height, color);
// https://doc-tft-espi.readthedocs.io/tft_espi/methods/drawrect/
}
void roundrectdraw(int x, int y, int width, int height, int innerradius, int color, int outterradius) {
x += 41;
y += 53;
tft.drawRoundRect(x, y, width, height, outterradius, color);
}
void roundrectfill(int x, int y, int width, int height, int radius, int color) {
x += 41;
y += 53;
tft.fillRoundRect(x, y, width, height, radius, color);
// https://doc-tft-espi.readthedocs.io/tft_espi/methods/fillrect/
}
void circle(int x, int y, int radius, int color) {
x += 41;
y += 53;
tft.drawCircle(x, y, radius, color);
}
void circlefill(int x, int y, int radius, int color) {
x += 41;
y += 53;
tft.fillCircle(x, y, radius, color);
}
void test() {
uint8_t x = tft.readcommand8(ILI9341_RDMODE);
Serial.println();
Serial.print(F("Display Power Mode: 0x")); Serial.println(x, HEX); x = tft.readcommand8(ILI9341_RDMADCTL);
Serial.print(F("MADCTL Mode : 0x")); Serial.println(x, HEX); x = tft.readcommand8(ILI9341_RDPIXFMT);
Serial.print(F("Pixel Format : 0x")); Serial.println(x, HEX); x = tft.readcommand8(ILI9341_RDIMGFMT);
Serial.print(F("Image Format : 0x")); Serial.println(x, HEX); x = tft.readcommand8(ILI9341_RDSELFDIAG);
Serial.print(F("Self Diagnostic : 0x")); Serial.println(x, HEX);
while (1);
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4