diff --git a/SAE.ino b/SAE.ino index 2734723..47d699b 100644 --- a/SAE.ino +++ b/SAE.ino @@ -1,5 +1,8 @@ #include "pin.h" #include "logo.h" +#include "menu.h" +#include "web.h" + #include #include #include @@ -9,18 +12,9 @@ #include Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire); -const char *ssid = "morpion-SAE"; -const char *password = "12345678"; -NetworkServer server(80); - #define BUTTON_A 15 #define BUTTON_B 32 -int tour=0; - -bool local = true; -bool solo = true; -int difficulty = 1; bool isMod = false; bool isModJeu = false; @@ -28,14 +22,6 @@ bool isDifficulty = false; bool isPlay = false; bool isWebSet = false; -uint8_t btnPrevA; -uint8_t btnPrevB; -uint8_t btnA; -uint8_t btnB; - -unsigned long lastDebounceTimeA = 0; -unsigned long lastDebounceTimeB = 0; -const unsigned long debounceDelay = 200; // délai de rebond (200 ms) void setup() { @@ -59,253 +45,4 @@ void loop() { // Mode local } } -} - -void loopWeb(){ - NetworkClient client = server.accept(); - - if (client) { - Serial.println("New Client."); - String currentLine = ""; - while (client.connected()) { - if (client.available()) { // if there's bytes to read from the client, - char c = client.read(); // read a byte, then - Serial.write(c); - if (c == '\n') { // if the byte is a newline character - // if the current line is blank, you got two newline characters in a row. - // that's the end of the client HTTP request, so send a response: - if (currentLine.length() == 0) { - // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) and a content-type so the client knows what's coming, then a blank line: - client.println("HTTP/1.1 200 OK"); - client.println("Content-type:text/html"); - client.println(); - - // the content of the HTTP response follows the header: - //client.print("Click here to turn ON the LED.
"); - //client.print("Click here to turn OFF the LED.
"); - - client.println(""); - client.println(""); - client.println(""); - client.println(""); - client.println(""); - client.println(""); - - client.println(""); - client.println("
"); - for (int i = 0; i < 9; i++) { - client.print("
"); - client.print(i + 1); - client.println("
"); - } - client.println("
"); - - client.println(""); - - client.println(""); - client.println(""); - - client.println(); - break; - } else { // if you got a newline, then clear currentLine: - currentLine = ""; - } - } else if (c != '\r') { // if you got anything else but a carriage return character, - currentLine += c; // add it to the end of the currentLine - } - - if (currentLine.endsWith("GET /H")) { - //digitalWrite(LED_BUILTIN, HIGH); - } - if (currentLine.endsWith("GET /L")) { - //digitalWrite(LED_BUILTIN, LOW); - } - } - } - client.stop(); - Serial.println("Client Disconnected."); - } -} - - - - - - - - - - -void readButton(){ - btnA = digitalRead(BUTTON_A); - if (btnA == LOW && btnPrevA == HIGH && (millis() - lastDebounceTimeA > debounceDelay)) { - lastDebounceTimeA = millis(); - handleButtonA(); - } - btnPrevA = btnA; - btnB = digitalRead(BUTTON_B); - if (btnB == LOW && btnPrevB == HIGH && (millis() - lastDebounceTimeB > debounceDelay)) { - lastDebounceTimeB = millis(); - handleButtonB(); - } - btnPrevB = btnB; -} - -void setupMenu() { - pinMode(BUTTON_A, INPUT_PULLUP); - pinMode(BUTTON_B, INPUT_PULLUP); - - btnPrevA = digitalRead(BUTTON_A); - btnPrevB = digitalRead(BUTTON_B); - - display.begin(0x3C, true); - display.clearDisplay(); - display.drawBitmap(0, 0, epd_bitmap_logo, 128, 64, SH110X_WHITE); - display.display(); - delay(1000); - display.clearDisplay(); - display.display(); - - display.setRotation(1); - display.setTextColor(SH110X_WHITE); - display.setCursor(0, 0); - display.display(); -} - -void setupWeb(){ - Serial.println(); - Serial.println("Configuring access point..."); - if (!WiFi.softAP(ssid, password)) { - log_e("Soft AP creation failed."); - while (1); - } - IPAddress myIP = WiFi.softAPIP(); - Serial.print("AP IP address: "); - Serial.println(myIP); - server.begin(); - - Serial.println("Server started"); - - display.clearDisplay(); - display.setCursor(0, 0); - display.print("Connectez vous au\nreseau "); - display.println(ssid); - display.println(); - display.print("Puis accedez a \nl'adresse "); - display.print(myIP); - display.display(); -} - -void menu() { - if (!isMod) { - choixMode(); - } else if (!isModJeu) { - choixModeJeu(); - } else if (solo && !isDifficulty) { - choixDifficulty(); - } else { - isPlay = true; // mode duo sans choix difficulté - } -} - -void choixMode() { - display.clearDisplay(); - display.setCursor(0, 0); - display.print("Choisissez le mode :\n"); - display.print("[A] Local\n"); - display.print("[B] Web\n"); - display.display(); -} - -void choixModeJeu() { - display.clearDisplay(); - display.setCursor(0, 0); - display.print("Choisissez le type :\n"); - display.print("[A] Solo\n"); - display.print("[B] Duo\n"); - display.display(); -} - -void choixDifficulty() { - display.clearDisplay(); - display.setCursor(0, 0); - display.print("Choisissez la \ndifficulte :\n"); - display.print("[A] Debutant\n"); - display.print("[B] Expert\n"); - display.display(); -} - -void handleButtonA() { - if (!isMod) { - local = true; - isMod = true; - } else if (!isModJeu) { - solo = true; - isModJeu = true; - } else if (solo && !isDifficulty) { - difficulty = 1; - isDifficulty = true; - isPlay = true; - } -} - -void handleButtonB() { - if (!isMod) { - local = false; - isMod = true; - } else if (!isModJeu) { - solo = false; - isModJeu = true; - } else if (solo && !isDifficulty) { - difficulty = 2; - isDifficulty = true; - isPlay = true; - } -} +} \ No newline at end of file diff --git a/menu.h b/menu.h new file mode 100644 index 0000000..b456c71 --- /dev/null +++ b/menu.h @@ -0,0 +1,131 @@ +bool local = true; +bool solo = true; +int difficulty = 1; + +uint8_t btnPrevA; +uint8_t btnPrevB; +uint8_t btnA; +uint8_t btnB; + +unsigned long lastDebounceTimeA = 0; +unsigned long lastDebounceTimeB = 0; +const unsigned long debounceDelay = 200; // délai de rebond (200 ms) + +void setupMenu() { + pinMode(BUTTON_A, INPUT_PULLUP); + pinMode(BUTTON_B, INPUT_PULLUP); + + btnPrevA = digitalRead(BUTTON_A); + btnPrevB = digitalRead(BUTTON_B); + + display.begin(0x3C, true); + display.clearDisplay(); + display.drawBitmap(0, 0, epd_bitmap_logo, 128, 64, SH110X_WHITE); + display.display(); + delay(1000); + display.clearDisplay(); + display.display(); + + display.setRotation(1); + display.setTextColor(SH110X_WHITE); + display.setCursor(0, 0); + display.display(); +} + + + + + +void menu() { + if (!isMod) { + choixMode(); + } else if (!isModJeu) { + choixModeJeu(); + } else if (solo && !isDifficulty) { + choixDifficulty(); + } else { + isPlay = true; // mode duo sans choix difficulté + } +} + + + + + + +void choixMode() { + display.clearDisplay(); + display.setCursor(0, 0); + display.print("Choisissez le mode :\n"); + display.print("[A] Local\n"); + display.print("[B] Web\n"); + display.display(); +} + +void choixModeJeu() { + display.clearDisplay(); + display.setCursor(0, 0); + display.print("Choisissez le type :\n"); + display.print("[A] Solo\n"); + display.print("[B] Duo\n"); + display.display(); +} + +void choixDifficulty() { + display.clearDisplay(); + display.setCursor(0, 0); + display.print("Choisissez la \ndifficulte :\n"); + display.print("[A] Debutant\n"); + display.print("[B] Expert\n"); + display.display(); +} + + + + + +void readButton(){ + btnA = digitalRead(BUTTON_A); + if (btnA == LOW && btnPrevA == HIGH && (millis() - lastDebounceTimeA > debounceDelay)) { + lastDebounceTimeA = millis(); + handleButtonA(); + } + btnPrevA = btnA; + btnB = digitalRead(BUTTON_B); + if (btnB == LOW && btnPrevB == HIGH && (millis() - lastDebounceTimeB > debounceDelay)) { + lastDebounceTimeB = millis(); + handleButtonB(); + } + btnPrevB = btnB; +} + + + + +void handleButtonA() { + if (!isMod) { + local = true; + isMod = true; + } else if (!isModJeu) { + solo = true; + isModJeu = true; + } else if (solo && !isDifficulty) { + difficulty = 1; + isDifficulty = true; + isPlay = true; + } +} + +void handleButtonB() { + if (!isMod) { + local = false; + isMod = true; + } else if (!isModJeu) { + solo = false; + isModJeu = true; + } else if (solo && !isDifficulty) { + difficulty = 2; + isDifficulty = true; + isPlay = true; + } +} \ No newline at end of file diff --git a/web.h b/web.h new file mode 100644 index 0000000..6492e23 --- /dev/null +++ b/web.h @@ -0,0 +1,170 @@ +const char *ssid = "morpion-SAE"; +const char *password = "12345678"; +NetworkServer server(80); + +void setupWeb(){ + Serial.println(); + Serial.println("Configuring access point..."); + if (!WiFi.softAP(ssid, password)) { + log_e("Soft AP creation failed."); + while (1); + } + IPAddress myIP = WiFi.softAPIP(); + Serial.print("AP IP address: "); + Serial.println(myIP); + server.begin(); + + Serial.println("Server started"); + + display.clearDisplay(); + display.setCursor(0, 0); + display.print("Connectez vous au\nreseau "); + display.println(ssid); + display.println(); + display.print("Puis accedez a \nl'adresse "); + display.print(myIP); + display.display(); +} + + + + + + +void loopWeb(){ + NetworkClient client = server.accept(); + + if (client) { + Serial.println("New Client."); + String currentLine = ""; + while (client.connected()) { + if (client.available()) { // if there's bytes to read from the client, + char c = client.read(); // read a byte, then + Serial.write(c); + if (c == '\n') { // if the byte is a newline character + // if the current line is blank, you got two newline characters in a row. + // that's the end of the client HTTP request, so send a response: + if (currentLine.length() == 0) { + // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) and a content-type so the client knows what's coming, then a blank line: + client.println("HTTP/1.1 200 OK"); + client.println("Content-type:text/html"); + client.println(); + + // the content of the HTTP response follows the header: + if(solo){ + webSolo(); + }else{ + webDuo(); + } + client.println(); + break; + } else { // if you got a newline, then clear currentLine: + currentLine = ""; + } + } else if (c != '\r') { // if you got anything else but a carriage return character, + currentLine += c; // add it to the end of the currentLine + } + + if (currentLine.endsWith("GET /H")) { + //digitalWrite(LED_BUILTIN, HIGH); + } + if (currentLine.endsWith("GET /L")) { + //digitalWrite(LED_BUILTIN, LOW); + } + } + } + client.stop(); + Serial.println("Client Disconnected."); + } +} + + + + + +void webDuo(){ + client.println(""); + client.println(""); + client.println(""); + client.println(""); + client.println(""); + client.println(""); + + client.println(""); + client.println("
"); + for (int i = 0; i < 9; i++) { + client.print("
"); + client.print(i + 1); + client.println("
"); + } + client.println("
"); + + client.println(""); + + client.println(""); + client.println(""); +} + + +void webSolo(){ + client.println(""); + client.println(""); + client.println(""); + client.println(""); + client.println(""); + + client.println(""); + client.println("
"); + client.println("Pas encore commence !") + client.println("
"); + + client.println(""); + client.println(""); +} \ No newline at end of file