#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
#include <Button.h>
#include <math.h>
#define TFT_DC 2
#define TFT_CS 15
#define BUTTON_SIGNAL1 0 // Broche GPIO pour le bouton du signal 1
#define BUTTON_SIGNAL2 5 // Broche GPIO pour le bouton du signal 2
#define BUTTON_SIGNAL3 14 // Broche GPIO pour le bouton du signal 3
#define BUTTON_SIGNAL4 12 // Broche GPIO pour le bouton du signal 4
#define TFT_WIDTH 320
#define TFT_HEIGHT 240
#define SIGNAL_CLEAR_DELAY 50 // Délai (en ms) entre chaque colonne effacée lors du nettoyage du signal précédent
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Button buttonSignal1(BUTTON_SIGNAL1);
Button buttonSignal2(BUTTON_SIGNAL2);
Button buttonSignal3(BUTTON_SIGNAL3);
Button buttonSignal4(BUTTON_SIGNAL4);
volatile bool signalRequested = false;
volatile int requestedSignalNumber = 0;
volatile bool signalInProgress = false;
void setup() {
Serial.begin(9600);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
buttonSignal1.begin();
buttonSignal2.begin();
buttonSignal3.begin();
buttonSignal4.begin();
attachInterrupt(digitalPinToInterrupt(BUTTON_SIGNAL1), signalInterrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(BUTTON_SIGNAL2), signalInterrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(BUTTON_SIGNAL3), signalInterrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(BUTTON_SIGNAL4), signalInterrupt, FALLING);
}
void loop() {
if (signalRequested && !signalInProgress) {
signalInProgress = true;
signalRequested = false; // Réinitialiser signalRequested ici pour éviter les demandes multiples
clearSignal(); // Effacer le signal précédent
switch (requestedSignalNumber) {
case 1:
drawSignal(ILI9341_YELLOW, calculateCombinedSignal1);
clearSignal();
break;
case 2:
drawSignal(ILI9341_GREEN, calculateCombinedSignal2);
clearSignal();
break;
case 3:
drawSignal(ILI9341_RED, calculateCombinedSignal3);
clearSignal();
break;
case 4:
drawSignal(ILI9341_CYAN, calculateCombinedSignal4);
clearSignal();
break;
default:
break;
}
signalInProgress = false;
}
}
void signalInterrupt() {
int button = digitalRead(BUTTON_SIGNAL1) == LOW ? 1 :
digitalRead(BUTTON_SIGNAL2) == LOW ? 2 :
digitalRead(BUTTON_SIGNAL3) == LOW ? 3 :
digitalRead(BUTTON_SIGNAL4) == LOW ? 4 : 0;
if (button != 0 && button != requestedSignalNumber) {
signalRequested = true;
requestedSignalNumber = button;
signalInProgress = false; // Reset signalInProgress to allow new signal to be drawn
}
}
void clearSignal() {
// Effacer l'écran progressivement
for (int x = 0; x < TFT_WIDTH; x++) {
for (int y = 0; y < TFT_HEIGHT; y++) {
tft.drawPixel(x, y, ILI9341_BLACK); // Effacer en noir
delay(SIGNAL_CLEAR_DELAY); // Délai entre chaque colonne effacée
}
}
}
void drawSignal(uint16_t color, float (*calculateSignal)(int)) {
for (int x = 0; x < TFT_WIDTH - 1; x++) {
float y = calculateSignal(x);
float y_next = calculateSignal(x + 1);
tft.drawLine(x, y, x + 1, y_next, color);
}
}
float calculateCombinedSignal1(int x) {
float s = 0;
int offset = 1;
while (offset <= 1000) {
float onde_P = -(1 / 0.4) * exp(-pow((x - 20 - offset), 2) / pow(8, 2));
float onde_Q = (1.4 / 0.4) * exp(-pow((x - 42 - offset), 2) / 1.5);
float onde_R = -(4 / 0.2) * exp(-pow((x - 47.35 - offset), 2) / pow(2.5, 2));
float onde_S = (1 / 0.2) * exp(-pow((x - 50.9 - offset), 2) / pow(1.5, 2));
float onde_T = -(1 / 0.3) * exp(-pow((x - 85 - offset), 2) / 80);
s += (onde_P + onde_Q + onde_R + onde_S + onde_T);
offset += 120;
}
return s * 4 + 80;
}
float calculateCombinedSignal2(int x) {
float s2 = 0;
int offset = 1;
while (offset <= 1000) {
float y1 = -(3.5 / 0.25) * exp(-pow((x - 15 - offset), 2) / pow(4, 2));
float y2 = 7 * exp(-pow((x - 20 - offset), 2) / pow(8, 2));
float y3 = -2 * exp(-pow((x - 20.2 - offset), 2) / pow(6, 2));
s2 += y1 + y2 + y3;
offset += 100;
}
return s2 * 0.6 * 7 + 50;
}
float calculateCombinedSignal3(int x) {
float s3 = 0;
int offset = 1;
while (offset <= 1000) {
float y1 = -(0.4 / 2.5) * exp(-pow((x - 12 - offset), 2) / pow(6, 2));
float y2 = -(0.5 / 5) * exp(-pow((x - 25.5 - offset), 2) / pow(3, 2));
float y3 = -(0.4 / 5) * exp(-pow((x - 35 - offset), 2) / pow(2, 2));
float y4 = (0.7 / 5) * exp(-pow((x - 40 - offset), 2) / pow(2, 2));
float y5 = -(0.6 / 9) * exp(-pow((x - 50 - offset), 2) / 15);
float y6 = -(0.7 / 3.0) * exp(-pow((x - 60 - offset), 2) / pow(12, 2));
s3 += y1 - y2 + y3 + y4 + y5 + y6;
offset += 90;
}
return s3 * 15 + 10;
}
float calculateCombinedSignal4(int x) {
float sum_y34 = 0;
for (int n = 1; n <= 400; n += 18) {
float y3 = -(3.0 / 0.2) * exp(-pow((x - (5 + n)), 2) / pow(2, 2));
float y4 = 0.8 * exp(-pow((x - (8 + n)), 2) / pow(9, 2));
sum_y34 += y3 + y4;
}
return sum_y34 * 5 + 70;
}