#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_SIN 0 // Broche GPIO 0 pour le bouton sin
#define BUTTON_COS 5 // Broche GPIO 5 pour le bouton cos
#define TFT_WIDTH 320
#define TFT_HEIGHT 240
#define NUM_PERIODS 4 // Nombre de périodes à afficher
volatile bool sinSelected = false;
volatile bool cosSelected = false;
volatile bool signalDisplayed = false;
int drawDelay = 10; // Délai initial entre les itérations de dessin
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Button buttonSin(BUTTON_SIN);
Button buttonCos(BUTTON_COS);
void setup() {
Serial.begin(9600);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
buttonSin.begin();
buttonCos.begin();
attachInterrupt(BUTTON_SIN, sinSelectedInterrupt, FALLING);
attachInterrupt(BUTTON_COS, cosSelectedInterrupt, FALLING);
}
void loop() {
if (sinSelected && !signalDisplayed) {
Serial.println("Bouton sin sélectionné");
sinSelected = false;
signalDisplayed = true;
displaySignal(sinWave);
}
if (cosSelected && !signalDisplayed) {
Serial.println("Bouton cos sélectionné");
cosSelected = false;
signalDisplayed = true;
displaySignal(cosWave);
}
}
void sinSelectedInterrupt() {
sinSelected = true;
}
void cosSelectedInterrupt() {
cosSelected = true;
}
void displaySignal(float (*waveFunction)(float)) {
for (int period = 0; period < NUM_PERIODS; period++) {
float periodStart = 2 * PI * period;
float periodEnd = 2 * PI * (period + 1);
tft.fillScreen(ILI9341_BLACK);
drawAxes();
float xPrev = periodStart;
float yPrev = (*waveFunction)(xPrev) * (TFT_HEIGHT / 4) + TFT_HEIGHT / 2;
for (float x = periodStart + 0.1; x < periodEnd; x += 0.1) {
float y = (*waveFunction)(x) * (TFT_HEIGHT / 4) + TFT_HEIGHT / 2;
tft.drawLine((xPrev - periodStart) * (TFT_WIDTH / (2 * PI)), yPrev, (x - periodStart) * (TFT_WIDTH / (2 * PI)), y, ILI9341_GREEN);
xPrev = x;
yPrev = y;
delay(drawDelay);
}
}
}
float sinWave(float x) {
return sin(x);
}
float cosWave(float x) {
return cos(x);
}
void drawAxes() {
tft.drawLine(0, TFT_HEIGHT / 2, TFT_WIDTH - 1, TFT_HEIGHT / 2, ILI9341_WHITE);
tft.drawLine(TFT_WIDTH / 2, 0, TFT_WIDTH / 2, TFT_HEIGHT - 1, ILI9341_WHITE);
}