#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
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;
tft.fillScreen(ILI9341_BLACK);
switch (requestedSignalNumber) {
case 1:
drawCombinedSignal(ILI9341_YELLOW, calculateCombinedSignal1);
break;
case 2:
drawCombinedSignal(ILI9341_GREEN, calculateCombinedSignal2);
break;
case 3:
drawCombinedSignal(ILI9341_RED, calculateCombinedSignal3);
break;
case 4:
drawCombinedSignal(ILI9341_CYAN, calculateCombinedSignal4);
break;
default:
break;
}
// Ne pas réinitialiser signalRequested ici
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) {
signalRequested = true;
requestedSignalNumber = button;
}
}
void drawCombinedSignal(uint16_t color, float (*calculateSignal)(int)) {
// Calculer l'amplitude maximale et minimale
float maxAmplitude = -INFINITY; // Initialiser à une valeur très petite
float minAmplitude = INFINITY; // Initialiser à une valeur très grande
for (int x = 0; x < TFT_WIDTH; x++) {
float signalValue = calculateSignal(x);
if (signalValue > maxAmplitude) {
maxAmplitude = signalValue;
}
if (signalValue < minAmplitude) {
minAmplitude = signalValue;
}
}
// Calculer l'amplitude maximale de la courbe (positif ou négatif)
float maxAbsoluteAmplitude = abs(maxAmplitude)+abs(minAmplitude);
// Calculer l'espace vide au-dessus et en dessous de la courbe
float verticalEmptySpace = (TFT_HEIGHT/ 2 + maxAbsoluteAmplitude/2);
// Dessiner le signal
tft.fillScreen(ILI9341_BLACK);
for (int x = 0; x < TFT_WIDTH - 1; x++) {
float y = calculateSignal(x) +verticalEmptySpace;
float y_next = calculateSignal(x + 1)+verticalEmptySpace;
tft.drawLine(x, y, x + 1, y_next, color);
// Vérifier si une nouvelle demande de signal est intervenue pendant le dessin
if (signalRequested && requestedSignalNumber != (calculateSignal == calculateCombinedSignal1 ? 1 :
calculateSignal == calculateCombinedSignal2 ? 2 :
calculateSignal == calculateCombinedSignal3 ? 3 : 4)) {
return;
}
}
}
float calculateCombinedSignal1(int x) {
float s = 0;
int offset = 1;
while (true) { // Infinite loop
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;
// Break out of the loop if necessary
if (offset > 1000) {
break; // Exit the infinite loop when offset exceeds 900
}
}
// Retourner la valeur du signal
return s * 6;
}
float calculateCombinedSignal2(int x) {
float s2 = 0;
int offset = 1;
while (true) { // Infinite loop
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;
// Break out of the loop if necessary
if (offset > 1000) {
break; // Exit the infinite loop when offset exceeds 900
}
}
// Return the signal value
return s2 * 0.4 * 10;
}
float calculateCombinedSignal3(int x) {
float s3 = 0;
int offset = 1;
while (true) { // Infinite loop
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;
// Break out of the loop if necessary
if (offset > 1000) {
break; // Exit the infinite loop when offset exceeds 900
}
}
// Return the signal value
return s3 * 15;
}
float calculateCombinedSignal4(int x) {
float sum_y34 = 0;
for (int n = 1; n <= 400; n += 15) {
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 the signal value
return sum_y34 * 5;
}