/*
ESP32 + ILI9341 LCD Basic Example
https://wokwi.com/projects/325324981270479442
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int fontSizeW = 5;
int fontSizeH = 8;
void setup() {
tft.begin();
// km/h
printElement("25", 8, 240, 76, 0, "km/h");
// left
printElement("45", 5, 120, 250, 0, "km");
// tft.setCursor((120 - (2 * 5 * fontSizeW)) / 2, 250);
// tft.setTextColor(ILI9341_WHITE);
// tft.setTextSize(5);
// tft.println("78");
// tft.setCursor((120 - (2 * 1 * fontSizeW)) / 2, 250 + (5 * fontSizeH) + 5);
// tft.setTextColor(0x8C51);
// tft.setTextSize(1);
// tft.println("km");
// right
printElement("1:00", 3, 120, 260, 120, "time");
// tft.setCursor(((120 - (4 * 3 * fontSizeW)) / 2) + 120, 260);
// tft.setTextColor(ILI9341_WHITE);
// tft.setTextSize(3);
// tft.println("1:00");
// tft.setCursor(((120 - (4 * 1 * fontSizeW)) / 2) + 120, 260 + (3 * fontSizeH) + 5);
// tft.setTextColor(0x8C51);
// tft.setTextSize(1);
// tft.println("time");
tft.drawFastHLine(0,225, 240, 0x8C51);
tft.drawFastVLine(120,225, 95, 0x8C51);
}
void loop() { delay(10); }
void printElement(String text, int textSize, int xTotal, int y, int padding, String legendText) {
int stringLength = text.length();
int pixelSize = stringLength * textSize * fontSizeW;
int legendTextSize = 2;
int paddingTop = 10;
if(textSize < 6) {
legendTextSize = 1;
paddingTop = 5;
}
tft.setCursor(((xTotal - pixelSize) / 2) + padding, y);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(textSize);
tft.println(text);
tft.setCursor(((xTotal - (legendText.length() * legendTextSize * fontSizeW)) / 2) + padding, y + (textSize * fontSizeH) + paddingTop);
tft.setTextColor(0x8C51);
tft.setTextSize(legendTextSize);
tft.println(legendText);
}