/* ESP32 Loading Screen with WiFi and Lights
Displays a top bar with the current time and a mocked battery, connects to WIFI to get the time
*/
#include "User_Setup.h"
#include <TFT_eSPI.h>
#include <SPI.h> // this is needed for display
#include "Free_Fonts.h"
#include <WiFi.h>
#include <NTPClient.h>
#include <FastLED.h>
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 15
#define TFT_DC 2
#define TFT_MOSI 23
#define TFT_SCLK 18
// For LEDs
#define DATA_PIN 26
//#define CLOCK_PIN 13
#define ledCount 9
CRGB leds[ledCount];
#define color1 0xF79E
#define color2 0xB1B1
#define color3 0x4A69
#define TFT_GREY 0x5AEB
#define TFT_ORANGE 0xFD20 /* 255, 165, 0 */
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library with default width and height
TFT_eSprite sprite = TFT_eSprite(&tft);
#define SCREEN_WIDTH tft.width()
#define SCREEN_HEIGHT tft.height()
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
void setup(void) {
Serial.begin(115200);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, ledCount); // GRB ordering is assumed
tft.begin();
tft.setRotation(1);
Serial.println("TFT set up");
tft.drawString("Connecting to WiFi", SCREEN_WIDTH / 4, SCREEN_HEIGHT / 2, FONT4);
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
tft.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, TFT_BLACK);
tft.drawString("WiFi connected", SCREEN_WIDTH / 6, SCREEN_HEIGHT / 2, FONT4);
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
timeClient.begin();
timeClient.setTimeOffset(0);
tft.fillRect(2, 25, SCREEN_WIDTH, 2, TFT_WHITE);
tft.fillRect(0, 0, 2, 25, TFT_WHITE);
sprite.createSprite(SCREEN_WIDTH - 1, 25 - 1);
}
int timer = 0;
void loop() {
delay(100);
timeClient.update();
timer++;
if (timer > 100) {
timer = 0;
}
sprite.setTextDatum(TL_DATUM);
sprite.drawString(timeClient.getFormattedTime(), 16, 0, FONT4);
// Battery Level
int batteryLevel = 100 - timer;
int x = SCREEN_WIDTH - (16 + 8 + 8 + 8 + 8 + 2 + 2 + 2);
for (int i=0; i <= 3; i++) {
int color = TFT_DARKGREY;
if (batteryLevel > 90) {
color = TFT_GREEN;
} else if (batteryLevel > 50 && i < 3) {
color = TFT_GREEN;
} else if (batteryLevel > 25 && i < 2) {
color = TFT_ORANGE;
} else if (batteryLevel > 0 && i < 1) {
color = TFT_RED;
}
sprite.fillRect(x, 2, 8, 12, color);
x += 8 + 2;
}
int color = TFT_DARKGREY;
if (batteryLevel > 95) {
color = TFT_GREEN;
}
sprite.fillRect(x, 4, 4, 8, color);
sprite.pushSprite(1, 0);
for (int i=0; i <= ledCount; i++) {
leds[i] = CRGB::Blue;
FastLED.show();
}
}
Loading
ili9341-cap-touch
ili9341-cap-touch