#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Keypad.h>
// Configuration LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Configuration TFT
#define TFT_CS 10
#define TFT_DC 9
#define TFT_MOSI 11
#define TFT_SCK 13
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK);

// Configuration Clavier
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {1, 2, 3, 4};
byte colPins[COLS] = {5, 6, 7, 8};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

float xMin = -10;
float xMax = 10;
float yMin = -10000;
float yMax = 10000;

void setup() {
  Serial.begin(9600);

  // Initialisation des composants
  lcd.begin(16, 2);
  tft.begin();
}

void loop() {
  // Lire les coefficients de l'utilisateur
  float a = getUserInput("Entrez a: ");
  float b = getUserInput("Entrez b: ");
  float c = getUserInput("Entrez c: ");

  // Résolution analytique
  float discriminant = b * b - 4 * a * c;
  if (discriminant > 0) {
    float x1 = (-b + sqrt(discriminant)) / (2 * a);
    float x2 = (-b - sqrt(discriminant)) / (2 * a);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Solutions:");
    lcd.setCursor(0, 1);
    lcd.print("x1 = ");
    lcd.print(x1);
    lcd.setCursor(8, 1);
    lcd.print("x2 = ");
    lcd.print(x2);

    // Affichage graphique
    tft.fillScreen(ILI9341_BLACK);
    drawAxes();
    drawGraph(a, b, c, x1, x2);
  } else if (discriminant == 0) {
    float x = -b / (2 * a);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Solution unique:");
    lcd.setCursor(0, 1);
    lcd.print("x = ");
    lcd.print(x);

    // Affichage graphique
    tft.fillScreen(ILI9341_BLACK);
    drawAxes();
    drawGraph(a, b, c, x, x);
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Pas de solution reelle");

    // Affichage graphique
    tft.fillScreen(ILI9341_BLACK);
    tft.setCursor(10, 10);
    tft.setTextColor(ILI9341_WHITE);
    tft.setTextSize(2);
    tft.print("Pas de solution reelle");
  }

  delay(5000);  // Attendez 5 secondes avant de recommencer
}

float getUserInput(const char *prompt) {
  float value = 0.0;
  char buffer[16];

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(prompt);
  lcd.setCursor(0, 1);

  int i = 0;
  while (true) {
    char key = keypad.getKey();
    if (key) {
      if (key == '#') {
        buffer[i] = '\0';
        value = atof(buffer);
        break;
      } else {
        buffer[i++] = key;
        lcd.print(key);
      }
    }
  }

  return value;
}

void drawAxes() {
  tft.drawFastHLine(map(xMin, xMin, xMax, 0, 320), map(0, yMin, yMax, 240, 0), map(xMax - xMin, xMin, xMax, 0, 320), ILI9341_WHITE);
  tft.drawFastVLine(map(0, xMin, xMax, 0, 320), map(yMax - yMin, yMin, yMax, 240, 0), map(yMax - yMin, yMin, yMax, 0, 240), ILI9341_WHITE);
  tft.drawPixel(map(0, xMin, xMax, 0, 320), map(0, yMin, yMax, 240, 0), ILI9341_WHITE);

  // Ajouter les valeurs numériques sur les axes
  for (int i = -5; i <= 5; i++) {
    int xPos = map(i, xMin, xMax, 0, 320);
    int yPos = map(i, yMin, yMax, 240, 0);

    // Valeurs sur l'axe x
    tft.setCursor(xPos - 3, map(0, yMin, yMax, 240, 0) + 5);
    tft.setTextSize(1);
    tft.print(i);

    // Valeurs sur l'axe y
    tft.setCursor(map(0, xMin, xMax, 0, 320) + 5, yPos - 5);
    tft.setTextSize(1);
    tft.print(-i);
  }
}

void drawGraph(float a, float b, float c, float x1, float x2) {
  tft.setCursor(10, 10);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  tft.print("Graphique:");

  for (int x = map(xMin, xMin, xMax, 0, 320); x <= map(xMax, xMin, xMax, 0, 320); x++) {
    float xValue = map(x, 0, 320, xMin, xMax);
    float y = a * xValue * xValue + b * xValue + c;
    int yPos = map(y, yMin, yMax, 240, 0);
    tft.drawPixel(x, yPos, ILI9341_YELLOW);
  }

  float discriminant = b * b - 4 * a * c;
  if (discriminant > 0) {
    float x1_pos = map(x1, xMin, xMax, 0, 320);
    float x2_pos = map(x2, xMin, xMax, 0, 320);

    tft.fillCircle(x1_pos, map(0, yMin, yMax, 240, 0), 5, ILI9341_RED);
    tft.fillCircle(x2_pos, map(0, yMin, yMax, 240, 0), 5, ILI9341_BLUE);
  } else if (discriminant == 0) {
    float x_pos = map(x1, xMin, xMax, 0, 320);
    tft.fillCircle(x_pos, map(0, yMin, yMax, 240, 0), 5, ILI9341_GREEN);
  }

  delay(5000);
}




uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
keypad1:R1
keypad1:R2
keypad1:R3
keypad1:R4
keypad1:C1
keypad1:C2
keypad1:C3
keypad1:C4
lcd2:VCC
lcd2:GND
lcd2:CS
lcd2:RST
lcd2:D/C
lcd2:MOSI
lcd2:SCK
lcd2:LED
lcd2:MISO