#include "pin.h" #include "logo.h" #include #include #include #include #include #include #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; 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() { Serial.begin(115200); setupMenu(); } void loop() { readButton(); if (!isPlay) { menu(); } else { if (!local) { if(!isWebSet){ setupWeb(); isWebSet = true; }else{ loopWeb(); } } else { // 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; } }