#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
#define SCREEN_ADDRESS 0x3C

#define LOGO_HEIGHT 32
#define LOGO_WIDTH 32

const char BP_DWS = 3;
const char BP_UPS = 4;
const char BP_GE = 5;
const char BP_ALIM = 6;
const char BP_CHR = 7;
const char BP_RPM = 8;

char EtatBP_DWS = HIGH;
char EtatBP_GE = HIGH;
char EtatBP_UPS = HIGH;
char EtatBP_ALIM = HIGH;
char EtatBP_CHR = HIGH;
char EtatBP_RPM = HIGH;

int alimentation;
float Gear_Engaged;
int Alim = 0;
int RPM = 0;
int PBR_CHR = 0;

const char LEDR = 12;
const char LEDB = 10;
const char LEDG = 11;

String stopwatch = "stop";
String start_pause = "low";
long start_time = 0;
long seconds = 0;

const unsigned char PROGMEM logo[] = {
  //mettre logo en binaire
};

Adafruit_SSD1306 MonEcran(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void TestFonctionOled(void) {
  MonEcran.clearDisplay();
  MonEcran.drawBitmap(
    (MonEcran.width() - LOGO_WIDTH) / 2,
    (MonEcran.height() - LOGO_HEIGHT) / 2,
    logo, LOGO_WIDTH, LOGO_HEIGHT, 1
  );
  MonEcran.display();
  delay(2000);

  MonEcran.setTextSize(2);
  MonEcran.setTextColor(SSD1306_WHITE);
}

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

  if (!MonEcran.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Boucle infinie en cas d'échec
  }
  MonEcran.clearDisplay();
  MonEcran.setTextSize(1);
  MonEcran.setTextColor(SSD1306_WHITE);
  MonEcran.setCursor(0, 0);
  MonEcran.println(F("Initialisation..."));
  MonEcran.display();
  delay(2000);

  pinMode(BP_DWS, INPUT);
  pinMode(BP_UPS, INPUT);
  pinMode(BP_GE, INPUT);
  pinMode(BP_ALIM, INPUT);
  pinMode(BP_CHR, INPUT);
  pinMode(BP_RPM, INPUT);

  pinMode(LEDR, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LEDG, OUTPUT);

  EtatBP_DWS = digitalRead(BP_DWS);
  EtatBP_GE = digitalRead(BP_GE);
  EtatBP_UPS = digitalRead(BP_UPS);
  EtatBP_CHR = digitalRead(BP_CHR);
  EtatBP_RPM = digitalRead(BP_RPM);
  EtatBP_ALIM = digitalRead(BP_ALIM);
}

void loop() {
  EtatBP_DWS = digitalRead(BP_DWS);
  EtatBP_GE = digitalRead(BP_GE);
  EtatBP_UPS = digitalRead(BP_UPS);
  EtatBP_CHR = digitalRead(BP_CHR);
  EtatBP_RPM = digitalRead(BP_RPM);
  EtatBP_ALIM = digitalRead(BP_ALIM);

  if (EtatBP_UPS == LOW && Gear_Engaged != 5 && Gear_Engaged <=5) {
    Gear_Engaged = Gear_Engaged + 1;
    delay(200);
  }
  if (EtatBP_DWS == LOW && Gear_Engaged > 0) {
    Gear_Engaged = Gear_Engaged - 1;
    delay(200);
  }

  if (EtatBP_RPM == LOW) { // RPM
    MonEcran.clearDisplay();
    MonEcran.setCursor(0, 0);
    MonEcran.setTextSize(2);
    MonEcran.print(F("RPM: "));
    MonEcran.println("valeur-RPM");
    MonEcran.display();
    // Afficher la valeur RPM sur le moniteur série pour le débogage
    Serial.print(F("RPM: "));
    Serial.println(RPM);
    delay(100);
  }

  if (EtatBP_ALIM == LOW) { // alimentation
    MonEcran.clearDisplay();
    MonEcran.setCursor(0, 0);
    MonEcran.setTextSize(2);
    MonEcran.print(F("ALIMENTATION: "));
    if (alimentation == 1) {
      MonEcran.println("ON");
    } else {
      MonEcran.println("OFF");
    }
    MonEcran.display();
    delay(100);
  }

  if (EtatBP_GE == LOW) { // Gear engaged
      MonEcran.clearDisplay();
      MonEcran.setCursor(0, 0);
      MonEcran.setTextSize(1);
      MonEcran.print(("Gear Engaged: "));
      if (Gear_Engaged != 0) {
        MonEcran.println(Gear_Engaged);
      } 
      else if(Gear_Engaged == 0){
        MonEcran.println("N");
      }
      MonEcran.display();
      Serial.print(F("Gear Engaged: "));
      Serial.println(Gear_Engaged);
      delay(1000);
  }

  if (EtatBP_CHR == LOW) { // chrono
    MonEcran.clearDisplay();
    MonEcran.setCursor(0, 0);
    MonEcran.setTextSize(2);
    MonEcran.print(F("TIME"));
    MonEcran.println("N");
    MonEcran.display();
    seconds = (millis() - start_time) / 1000;
    long centisecond = ((millis() - start_time) % 1000) % 100;
    MonEcran.setCursor(0, 20);
    MonEcran.print(seconds);
    MonEcran.setCursor(20, 20);
    MonEcran.print(":");
    MonEcran.setCursor(30, 20);
    MonEcran.print(centisecond);
    MonEcran.display();
    Serial.println(start_time);
    Serial.println(millis());
    PBR_CHR++;

  if(PBR_CHR == 1){
    MonEcran.clearDisplay();
    stopwatch = "stop";
    start_time = 0;
    MonEcran.setCursor(0, 0);
    MonEcran.print("Press");
    MonEcran.setCursor(0, 20);
    MonEcran.print("Start");
    MonEcran.display(); 
    digitalWrite(LEDR, HIGH);
  }
  else if(PBR_CHR == 2 && stopwatch =="stop"){
    MonEcran.clearDisplay();
    start_time = millis();
    MonEcran.print("");
    stopwatch = "run";
    start_pause = "high";
    digitalWrite(LEDB, HIGH);
  }
  else if(PBR_CHR == 3 && stopwatch =="run" && start_pause == "low"){
    MonEcran.clearDisplay();
    start_time = millis() - (seconds * 1000);
    stopwatch = "run";
    start_pause = "high";
    MonEcran.print("");
    PBR_CHR = 0;
    digitalWrite(LEDG, HIGH);
  }
  delay(10);
}
}
$abcdeabcde151015202530354045505560fghijfghij