#include <FastLED.h>
#include "SPI.h"
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
#define LED_PIN 22
#define NUM_LEDS 6
CRGB leds[NUM_LEDS];
const int dipSwitchPin1 = 25;
const int dipSwitchPin2 = 26;
const int dipSwitchPin3 = 32;
const int dipSwitchPin4 = 33;
int TALLYCAM = 0;
int previousTALLYCAM = 0;
unsigned long previousMillis = 0;
const unsigned long interval = 1000; // Interval d'une seconde
uint16_t ColorTFT = ILI9341_BLACK;
uint16_t oldColor = ILI9341_BLACK;
int AtemAdressIP[4] = {192, 168, 1, 100};
void setup() {
Serial.begin(115200);
pinMode(dipSwitchPin1, INPUT);
pinMode(dipSwitchPin2, INPUT);
pinMode(dipSwitchPin3, INPUT);
pinMode(dipSwitchPin4, INPUT);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
}
void loop() {
dipswitch_loop();
if (TALLYCAM == 4) {
fill_solid(leds, NUM_LEDS, CRGB::Red);
tft.setTextColor(ILI9341_RED);
// ColorTFT = ILI9341_RED;
// if (ColorTFT != oldColor) {
// tft.fillScreen(ILI9341_RED);
// oldColor = ColorTFT;
// }
} else if (TALLYCAM == 3) {
fill_solid(leds, NUM_LEDS, CRGB::Green);
tft.setTextColor(ILI9341_GREEN);
// ColorTFT = ILI9341_GREEN;
// if (ColorTFT != oldColor) {
// tft.fillScreen(ILI9341_GREEN);
// oldColor = ColorTFT;
// }
} else {
fill_solid(leds, NUM_LEDS, CRGB::Blue);
tft.setTextColor(ILI9341_BLUE);
// ColorTFT = ILI9341_BLUE;
// if (ColorTFT != oldColor) {
// tft.fillScreen(ILI9341_BLUE);
// oldColor = ColorTFT;
// }
}
FastLED.show();
// tft.setTextColor(ILI9341_WHITE); // Couleur du texte blanc
displayCenteredText("CAM", -4, - 45, 2, 2);
displayCenteredText(String(TALLYCAM), 0, 0, 8, 2);
tft.setTextColor(ILI9341_WHITE); // Couleur du texte blanc
displayTopRightText("IP:" + String(AtemAdressIP[0]) + "." + String(AtemAdressIP[1]) + "." + String(AtemAdressIP[2]) + "." + String(AtemAdressIP[3]), -35, 5, 1, 2);
tft.setTextColor(ILI9341_WHITE); // Couleur du texte blanc
displayBottomLeftText("PROGRAM:CAM" + String(TALLYCAM), 5, -30, 1, 2);
displayBottomLeftText("PREVIEW:CAM" + String(TALLYCAM), 5, -5, 1, 2);
}
void dipswitch_loop() {
unsigned long currentMillis = millis(); // Obtient le temps actuel
// Vérifie si l'intervalle d'une seconde s'est écoulé
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Enregistre le temps actuel
TALLYCAM = 0; // Réinitialise la valeur à chaque boucle
if (digitalRead(dipSwitchPin1) == LOW) {
TALLYCAM += 1;
}
if (digitalRead(dipSwitchPin2) == LOW) {
TALLYCAM += 2;
}
if (digitalRead(dipSwitchPin3) == LOW) {
TALLYCAM += 3;
}
if (digitalRead(dipSwitchPin4) == LOW) {
TALLYCAM += 4;
}
if (TALLYCAM != previousTALLYCAM) {
// tft.fillScreen(ColorTFT);
tft.fillRect((tft.width() / 2) - 45, (tft.height() / 2) - 35, 90, 65, ColorTFT);
oldColor = ColorTFT;
tft.fillRect(92, tft.height() - 39, 15, 9, ColorTFT);
tft.fillRect(92, tft.height() - 14, 15, 9, ColorTFT);
tft.fillRect(tft.width() - 140, 4, 140, 10, ColorTFT);
// yield();
Serial.print("TALLYCAM: ");
Serial.print("Cam ");
Serial.println(TALLYCAM);
// ESPUI.print(tallycam_label, "Cam " + String(TALLYCAM));
previousTALLYCAM = TALLYCAM;
}
}
}
void displayText(String text, int xOffset, int yOffset, int textSize, int spacing) {
tft.setTextSize(textSize);
int charWidth = 6 * textSize; // Largeur d'un caractère
int charHeight = 8 * textSize; // Hauteur d'un caractère
int x = xOffset;
int y = yOffset;
for (char &c : text) {
tft.setCursor(x, y);
tft.print(c);
x += charWidth + spacing; // Déplace le curseur pour le prochain caractère
if (x + charWidth > tft.width()) { // Vérifie si le prochain caractère dépasse la largeur de l'écran
x = xOffset; // Retourne au début de la ligne
y += charHeight + spacing; // Passe à la ligne suivante
}
}
}
void displayCenteredText(String text, int xOffset, int yOffset, int textSize, int spacing) {
tft.setTextSize(textSize);
int16_t w = tft.textWidth(text);
int16_t h = tft.fontHeight();
int x = (tft.width() - w) / 2 + xOffset;
int y = (tft.height() - h) / 2 + yOffset;
displayText(text, x, y, textSize, spacing);
}
void displayBottomLeftText(String text, int xOffset, int yOffset, int textSize, int spacing) {
tft.setTextSize(textSize);
int x = xOffset;
int y = tft.height() - (8 * textSize) + yOffset;
displayText(text, x, y, textSize, spacing);
}
void displayTopLeftText(String text, int xOffset, int yOffset, int textSize, int spacing) {
int x = xOffset;
int y = yOffset;
displayText(text, x, y, textSize, spacing);
}
void displayTopRightText(String text, int xOffset, int yOffset, int textSize, int spacing) {
tft.setTextSize(textSize);
int16_t w = tft.textWidth(text);
int16_t h = tft.fontHeight();
int x = tft.width() - w + xOffset;
int y = yOffset;
displayText(text, x, y, textSize, spacing);
}