#include <TFT_eSPI.h>
#include <SPI.h> // this is needed for display
#include <ArduinoTrace.h>
#include "Led.h"
//This display 320 x 240 pixels; TTGO display 240 x 135
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library with default width and height
// const int centerX[6]={60,120,180,60,120,180};
// const int centerY[6]={30,30,30,90,90,90};
// const int color[6]={TFT_RED,TFT_WHITE,TFT_BLUE,TFT_GREEN,TFT_MAGENTA,TFT_ORANGE};
unsigned long currentMillis;
int ledNumbers=3;
const int centerX[3]={60,120,180};
const int centerY= 62;
const char name[3]={'R','G','B'};
const int color[3]={TFT_RED, TFT_GREEN, TFT_BLUE};
Led myLed[3]={ Led(tft), Led(tft), Led(tft) };
//---- led1: red led -----------
// LOW -> HIGH
bool led1State=LOW;
bool led2State=LOW;
bool led3State=LOW;
unsigned long previousMillisLed1, previousMillisLed2, previousMillisLed3;
const int LED1_T_ON=2000;
const int LED1_T_OFF=2000;
const int LED2_T_ON=1500;
const int LED2_T_OFF=500;
const int LED3_T_ON=200;
const int LED3_T_OFF=800;
void setup(void) {
Serial.begin(115200);
Serial.println("INICIO:");
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
// tft.setTextColor(TFT_YELLOW);
// tft.setTextSize(3);
// tft.setCursor(10,10);
// tft.println("Hello:");
// tft.drawCircle(120,67,25,TFT_RED);
// tft.setTextColor(TFT_WHITE);
// tft.setTextSize(2);
// tft.setCursor(115,95);
// tft.println("R");
tft.drawRect(0,0,240,135,TFT_WHITE);
// for(int i=0;i<6;i++)
// {
// tft.drawCircle(centerX[i],centerY[i],18,color[i]);
// tft.setTextColor(TFT_WHITE);
// tft.setTextSize(2);
// tft.setCursor(centerX[i]-5,centerY[i]+28);
// tft.println("R");
// }
tft.setTextSize(2);
tft.setTextColor(TFT_WHITE);
for(int i=0;i<ledNumbers;i++){
myLed[i].centerX=centerX[i];
myLed[i].centerY=centerY;
myLed[i].name=name[i];
myLed[i].color=color[i];
myLed[i].Init();
}
//----led1:red led: ON-----------------
// tft.setCursor(19,30);
// tft.print("R");
// tft.drawCircle(21, 16, 10, TFT_WHITE);
// tft.fillCircle(21, 16, 7, TFT_WHITE);
// //------------------------------------
// // LED2:green LED:ON
// tft.setCursor(64,30);
// tft.print("G");
// tft.drawCircle(64, 16, 10, TFT_WHITE);
// tft.fillCircle(64, 16, 7, TFT_WHITE);
// // update display with all of the above graphics
// // tft.display();
// // LED3:green LED:ON
// tft.setCursor(105,30);
// tft.print("B");
// tft.drawCircle(107, 16, 10, TFT_WHITE);
// tft.fillCircle(107, 16, 7, TFT_WHITE);
}
void loop() {
// for(int i=3;i<6;i++)
// {
// tft.fillCircle(centerX[i],centerY[i],11,color[i]);
// delay(500);
// tft.fillCircle(centerX[i],centerY[i],11,TFT_BLACK);
// delay(500);
// }
// tft.fillCircle(120,67,18,TFT_RED);
// delay(500);
// tft.fillCircle(120,67,18,TFT_BLACK);
// delay(500);
currentMillis=millis();
Led1Handle();
Led2Handle();
Led3Handle();
}
void Led1Handle()
{
if(led1State==HIGH)
{
if((currentMillis-previousMillisLed1)>=LED1_T_ON)
{
previousMillisLed1=currentMillis;
led1State=LOW;
// tft.fillCircle(21, 16, 7, TFT_BLACK);
myLed[0].LedOff();
}
}
else
{
if((currentMillis-previousMillisLed1)>=LED1_T_OFF)
{
previousMillisLed1=currentMillis;
led1State=HIGH;
// tft.fillCircle(21, 16, 7, TFT_WHITE);
myLed[0].LedOn();
}
}
}
void Led2Handle()
{
if(led2State==HIGH)
{
if((currentMillis-previousMillisLed2)>=LED2_T_ON)
{
previousMillisLed2=currentMillis;
led2State=LOW;
// tft.fillCircle(64, 16, 7, TFT_BLACK);
myLed[1].LedOff();
}
}
else
{
if((currentMillis-previousMillisLed2)>=LED2_T_OFF)
{
previousMillisLed2=currentMillis;
led2State=HIGH;
// tft.fillCircle(64, 16, 7, TFT_WHITE);
myLed[1].LedOn();
}
}
}
void Led3Handle()
{
if(led3State==HIGH)
{
if((currentMillis-previousMillisLed3)>=LED3_T_ON)
{
previousMillisLed3=currentMillis;
led3State=LOW;
// tft.fillCircle(107, 16, 7, TFT_BLACK);
myLed[2].LedOff();
}
}
else
{
if((currentMillis-previousMillisLed3)>=LED3_T_OFF)
{
previousMillisLed3=currentMillis;
led3State=HIGH;
// tft.fillCircle(107, 16, 7, TFT_WHITE);
myLed[2].LedOn();
}
}
}