#include <TFT_eSPI.h>
#include <SPI.h>
#include <Led.h>
TFT_eSPI tft = TFT_eSPI();
class LED {
private:
bool state;
unsigned long previousMillis;
int t_on;
int t_off;
public:
LED(int on, int off) : t_on(on), t_off(off) {
state = HIGH;
previousMillis = 0;
}
void update(unsigned long currentMillis) {
if (state == HIGH) {
if (currentMillis - previousMillis >= t_on) {
previousMillis = currentMillis;
state = LOW;
}
} else {
if (currentMillis - previousMillis >= t_off) {
previousMillis = currentMillis;
state = HIGH;
}
}
}
bool getState() {
return state;
}
};
Led led1(2000, 2000);
Led led2(1500, 500);
Led led3(800, 200);
// Arreglo de leds
Led led[3] = {
led1, led2, led3
};
// Arreglo de colores
uint32_t color[3] = {TFT_RED, TFT_GREEN, TFT_BLUE};
// Arreglo de letras
char* text[3] = {"R", "G", "B"};
void setup() {
Serial.begin(115200);
Serial.println("INICIO:");
// Configurando TFT
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
// Dibujando margenes y letras
for (int i = 0; i < 3; i++) {
// Dibujando el margen del LED
tft.drawCircle(40 + i * 60, 33, 25, color[i]);
// Dibujando la letra que representa el color del LED
tft.setTextColor(color[i]);
tft.setTextSize(2);
tft.setCursor(40 + i * 60, 70);
tft.println(text[i]);
}
}
void loop() {
// Actualizando millis
unsigned long currentMillis = millis();
for (int i = 0; i < 3; i++) {
led[i].update(currentMillis);
}
// Actualizando imagen
writeGraphMem();
//Delay opcional para reducir el uso de CPU
delay(20);
}
void writeGraphMem() {
for (int i = 0; i < 3; i++) {
// Encedido y apagado de LED's
if (led[i].getState() == HIGH) {
// Encendido
tft.fillCircle(40 + i * 60, 33, 18, color[i]);
} else {
// Apagado
tft.fillCircle(40 + i * 60, 33, 18, TFT_BLACK);
}
}
}