From 08de0a245c0b047de796cff5749b498ea656feae Mon Sep 17 00:00:00 2001 From: Pyramond Date: Mon, 16 Jun 2025 17:01:54 +0200 Subject: [PATCH 1/9] Add local solo mode --- gra_21_morpion_lumineux_code.ino | 10 ++++-- localMode.h | 61 ++++++++++++++++++++++++++++---- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/gra_21_morpion_lumineux_code.ino b/gra_21_morpion_lumineux_code.ino index aba373e..fc63cde 100644 --- a/gra_21_morpion_lumineux_code.ino +++ b/gra_21_morpion_lumineux_code.ino @@ -10,13 +10,13 @@ #include // --- Capteur capacitif --- -// Attention : Une SEULE instance globale ! Adafruit_MPR121 cap = Adafruit_MPR121(); uint16_t lasttouched = 0; uint16_t currtouched = 0; void setup() { Serial.begin(115200); + randomSeed(analogRead(0)); if (!cap.begin(0x5B)) { Serial.println("MPR121 not found, check wiring?"); @@ -40,8 +40,12 @@ void loop() { loopWeb(); } } else { - // Passe cap, lasttouched, currtouched par référence ! - localMode(cap, lasttouched, currtouched); + if(solo) { + localModeSolo(cap, lasttouched, currtouched); + } + else { + localModeDuo(cap, lasttouched, currtouched); + } } } } diff --git a/localMode.h b/localMode.h index cd9c396..5907918 100644 --- a/localMode.h +++ b/localMode.h @@ -49,8 +49,10 @@ bool joueur1 = true; Coord getCo(int key); bool victoire(int grille[3][3], int joueur); bool allInit(); +void afficheGrille(); -void localMode(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched); +void localModeDuo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched); +void localModeSolo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched); #endif // LOCALMODE_H @@ -97,7 +99,17 @@ bool allInit() { return true; } -void localMode(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) { +void afficheGrille() { + Serial.println("=============="); + for(int i = 0; i < 3; i++) { + for(int j = 0; j < 3; j++) { + Serial.print(grille[i][j]); + } + Serial.println(); + } +} + +void localModeDuo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) { currtouched = cap.touched(); for (int i = 0; i < numKeys; i++) { @@ -114,23 +126,60 @@ void localMode(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouche if(grille[c.y][c.x] == 0) { if(joueur1) { grille[c.y][c.x] = 1; + afficheGrille(); if(victoire(grille, 1)) { Serial.println("Joueur 1 a gagne"); } } else { grille[c.y][c.x] = 2; + afficheGrille(); if(victoire(grille, 2)) { Serial.println("Joueur 2 a gagne"); } } joueur1 = !joueur1; } + } + } + } + lasttouched = currtouched; +} - for(int i = 0; i < 3; i++) { - for(int j = 0; j < 3; j++) { - Serial.print(grille[i][j]); +void localModeSolo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) { + + //Serial.println("Mode Solo Local"); + + currtouched = cap.touched(); + + for (int i = 0; i < numKeys; i++) { + uint8_t t = keys[i].touchID; + keys[i].led.setPixelColor(0, 255); // Bleu + keys[i].led.show(); + + if ((currtouched & _BV(t)) && !(lasttouched & _BV(t))) { + Serial.print("Touch "); Serial.print(t); Serial.println(" pressed"); + btns[i] = true; + + if(allInit()) { + Coord c = getCo(i); + if(grille[c.y][c.x] == 0) { + grille[c.y][c.x] = 1; + afficheGrille(); + if(victoire(grille, 1)) { + Serial.println("Joueur 1 a gagne"); + } + + int x, y; + do { + x = random(0, 3); + y = random(0, 3); + } while(grille[y][x] != 0); + + grille[y][x] = 2; + afficheGrille(); + if(victoire(grille, 2)) { + Serial.println("Joueur 2 a gagne"); } - Serial.println(); } } } From 8489a7dfed029c0d5dd801e24fc3e8a7df2c408a Mon Sep 17 00:00:00 2001 From: Pyramond Date: Tue, 17 Jun 2025 09:11:18 +0200 Subject: [PATCH 2/9] Uncomment setup --- gra_21_morpion_lumineux_code.ino | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gra_21_morpion_lumineux_code.ino b/gra_21_morpion_lumineux_code.ino index dec6512..2f10c13 100644 --- a/gra_21_morpion_lumineux_code.ino +++ b/gra_21_morpion_lumineux_code.ino @@ -18,13 +18,12 @@ void setup() { Serial.begin(115200); randomSeed(analogRead(0)); -/* + if (!cap.begin(0x5B)) { Serial.println("MPR121 not found, check wiring?"); while (1); } Serial.println("MPR121 found!"); - */ setupMenu(); } From 1f685ec62670701387c6bbf083c49c74236936d1 Mon Sep 17 00:00:00 2001 From: Pyramond Date: Tue, 17 Jun 2025 09:32:17 +0200 Subject: [PATCH 3/9] Split into header/cpp --- menu.cpp | 130 +++++++++++++++++++++++++++++++++++++++++++ menu.h | 166 ++++++++++--------------------------------------------- 2 files changed, 159 insertions(+), 137 deletions(-) create mode 100644 menu.cpp diff --git a/menu.cpp b/menu.cpp new file mode 100644 index 0000000..e8293b8 --- /dev/null +++ b/menu.cpp @@ -0,0 +1,130 @@ +#include "menu.h" +#include + +// Variables de configuration du menu +bool local = true; +bool solo = true; +int difficulty = 1; + +uint8_t btnPrevA; +uint8_t btnPrevB; +uint8_t btnA; +uint8_t btnB; + +bool isMod = false; +bool isModJeu = false; +bool isDifficulty = false; +bool isPlay = false; +bool isWebSet = false; + +Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire); + +unsigned long lastDebounceTimeA = 0; +unsigned long lastDebounceTimeB = 0; +const unsigned long debounceDelay = 200; + +// Assure-toi que epd_bitmap_logo soit déclaré ailleurs ou inclus ici +//extern const unsigned char epd_bitmap_logo[]; + +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 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 menu() { + if (!isMod) { + choixMode(); + } else if (local && !isModJeu) { + choixModeJeu(); + } else if (local && solo && !isDifficulty) { + choixDifficulty(); + } else { + isPlay = true; + } +} + +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; + } +} + +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; +} diff --git a/menu.h b/menu.h index fe6ff1b..63ce57e 100644 --- a/menu.h +++ b/menu.h @@ -1,150 +1,42 @@ +#ifndef MENU_H +#define MENU_H + #include #include +// Variables de configuration du menu +extern bool local; +extern bool solo; +extern int difficulty; -bool local = true; -bool solo = true; -int difficulty = 1; +extern uint8_t btnPrevA; +extern uint8_t btnPrevB; +extern uint8_t btnA; +extern uint8_t btnB; -uint8_t btnPrevA; -uint8_t btnPrevB; -uint8_t btnA; -uint8_t btnB; - -bool isMod = false; -bool isModJeu = false; -bool isDifficulty = false; -bool isPlay = false; -bool isWebSet = false; +extern bool isMod; +extern bool isModJeu; +extern bool isDifficulty; +extern bool isPlay; +extern bool isWebSet; #define BUTTON_A 15 #define BUTTON_B 32 -Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire); -unsigned long lastDebounceTimeA = 0; -unsigned long lastDebounceTimeB = 0; -const unsigned long debounceDelay = 200; +extern Adafruit_SH1107 display; -void setupMenu() { - pinMode(BUTTON_A, INPUT_PULLUP); - pinMode(BUTTON_B, INPUT_PULLUP); +extern unsigned long lastDebounceTimeA; +extern unsigned long lastDebounceTimeB; +extern const unsigned long debounceDelay; - btnPrevA = digitalRead(BUTTON_A); - btnPrevB = digitalRead(BUTTON_B); +void setupMenu(); +void choixMode(); +void choixModeJeu(); +void choixDifficulty(); +void menu(); - 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(); +void handleButtonA(); +void handleButtonB(); +void readButton(); - display.setRotation(1); - display.setTextColor(SH110X_WHITE); - display.setCursor(0, 0); - display.display(); -} - - - - - - - - - - - - -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 menu() { - if (!isMod) { - choixMode(); - } else if (local && !isModJeu) { - choixModeJeu(); - } else if (local && solo && !isDifficulty) { - choixDifficulty(); - } else { - isPlay = true; - } -} - - - - - - - - - -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; - } -} - - -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; -} \ No newline at end of file +#endif // MENU_H From aab65787b385e991d36638c4be57ff8ae7669c8b Mon Sep 17 00:00:00 2001 From: Pyramond Date: Tue, 17 Jun 2025 09:32:30 +0200 Subject: [PATCH 4/9] Split into header/cpp --- localMode.cpp | 150 +++++++++++++++++++++++++++++++++++++++++++ localMode.h | 172 ++++---------------------------------------------- 2 files changed, 161 insertions(+), 161 deletions(-) create mode 100644 localMode.cpp diff --git a/localMode.cpp b/localMode.cpp new file mode 100644 index 0000000..e664ee9 --- /dev/null +++ b/localMode.cpp @@ -0,0 +1,150 @@ +#include "localMode.h" +#include + +uint8_t color_pos = 16; + +MechKey keys[] = { + {0, A0, false, Adafruit_NeoPixel(1, A0, NEO_GRB + NEO_KHZ800)}, + {1, 25, false, Adafruit_NeoPixel(1, 25, NEO_GRB + NEO_KHZ800)}, + {2, 34, false, Adafruit_NeoPixel(1, 34, NEO_GRB + NEO_KHZ800)}, + {3, 39, false, Adafruit_NeoPixel(1, 39, NEO_GRB + NEO_KHZ800)}, + {4, 36, false, Adafruit_NeoPixel(1, 36, NEO_GRB + NEO_KHZ800)}, + {5, 4, false, Adafruit_NeoPixel(1, 4, NEO_GRB + NEO_KHZ800)}, + {6, 14, false, Adafruit_NeoPixel(1, 14, NEO_GRB + NEO_KHZ800)}, + {7, 33, false, Adafruit_NeoPixel(1, 33, NEO_GRB + NEO_KHZ800)}, + {8, 15, false, Adafruit_NeoPixel(1, 15, NEO_GRB + NEO_KHZ800)}, +}; +const int numKeys = 9; + +int grille[3][3] = { + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0} +}; + +bool btns[9] = { false, false, false, false, false, false, false, false, false }; + +bool joueur1 = true; + +Coord getCo(int key) { + switch(key) { + case 0: return { 0, 0 }; + case 1: return { 1, 0 }; + case 2: return { 2, 0 }; + case 3: return { 0, 1 }; + case 4: return { 1, 1 }; + case 5: return { 2, 1 }; + case 6: return { 0, 2 }; + case 7: return { 1, 2 }; + case 8: return { 2, 2 }; + default: return { -1, -1 }; + } +} + +bool victoire(int grille[3][3], int joueur) { + // Vérification des lignes et colonnes + for (int i = 0; i < 3; ++i) { + if (grille[i][0] == joueur && grille[i][1] == joueur && grille[i][2] == joueur) + return true; + if (grille[0][i] == joueur && grille[1][i] == joueur && grille[2][i] == joueur) + return true; + } + // Vérification des deux diagonales + if (grille[0][0] == joueur && grille[1][1] == joueur && grille[2][2] == joueur) + return true; + if (grille[0][2] == joueur && grille[1][1] == joueur && grille[2][0] == joueur) + return true; + return false; +} + +bool allInit() { + for(int i = 0; i < 9; i++) { + if (!btns[i]) return false; + } + return true; +} + +void afficheGrille() { + Serial.println("=============="); + for(int i = 0; i < 3; i++) { + for(int j = 0; j < 3; j++) { + Serial.print(grille[i][j]); + } + Serial.println(); + } +} + +void localModeDuo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) { + currtouched = cap.touched(); + + for (int i = 0; i < numKeys; i++) { + uint8_t t = keys[i].touchID; + keys[i].led.setPixelColor(0, 255); // Bleu + keys[i].led.show(); + + if ((currtouched & _BV(t)) && !(lasttouched & _BV(t))) { + Serial.print("Touch "); Serial.print(t); Serial.println(" pressed"); + btns[i] = true; + + if(allInit()) { + Coord c = getCo(i); + if(grille[c.y][c.x] == 0) { + if(joueur1) { + grille[c.y][c.x] = 1; + afficheGrille(); + if(victoire(grille, 1)) { + Serial.println("Joueur 1 a gagne"); + } + } else { + grille[c.y][c.x] = 2; + afficheGrille(); + if(victoire(grille, 2)) { + Serial.println("Joueur 2 a gagne"); + } + } + joueur1 = !joueur1; + } + } + } + } + lasttouched = currtouched; +} + +void localModeSolo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) { + currtouched = cap.touched(); + + for (int i = 0; i < numKeys; i++) { + uint8_t t = keys[i].touchID; + keys[i].led.setPixelColor(0, 255); // Bleu + keys[i].led.show(); + + if ((currtouched & _BV(t)) && !(lasttouched & _BV(t))) { + Serial.print("Touch "); Serial.print(t); Serial.println(" pressed"); + btns[i] = true; + + if(allInit()) { + Coord c = getCo(i); + if(grille[c.y][c.x] == 0) { + grille[c.y][c.x] = 1; + afficheGrille(); + if(victoire(grille, 1)) { + Serial.println("Joueur 1 a gagne"); + } + + int x, y; + do { + x = random(0, 3); + y = random(0, 3); + } while(grille[y][x] != 0); + + grille[y][x] = 2; + afficheGrille(); + if(victoire(grille, 2)) { + Serial.println("Joueur 2 a gagne"); + } + } + } + } + } + lasttouched = currtouched; +} diff --git a/localMode.h b/localMode.h index 5907918..5c929ef 100644 --- a/localMode.h +++ b/localMode.h @@ -9,10 +9,10 @@ #endif struct MechKey { - uint8_t touchID; // Index sur le MPR121 - uint8_t dataPin; // Broche de l'ESP32 vers NeoPixel - bool state; // État (toggle) - Adafruit_NeoPixel led; // Instance NeoPixel + uint8_t touchID; + uint8_t dataPin; + bool state; + Adafruit_NeoPixel led; }; struct Coord { @@ -20,31 +20,12 @@ struct Coord { int y; }; -uint8_t color_pos = 16; - -// Création tableau global, mais pas de capteurs ici ! -MechKey keys[] = { - {0, A0, false, Adafruit_NeoPixel(1, A0, NEO_GRB + NEO_KHZ800)}, - {1, 25, false, Adafruit_NeoPixel(1, 25, NEO_GRB + NEO_KHZ800)}, - {2, 34, false, Adafruit_NeoPixel(1, 34, NEO_GRB + NEO_KHZ800)}, - {3, 39, false, Adafruit_NeoPixel(1, 39, NEO_GRB + NEO_KHZ800)}, - {4, 36, false, Adafruit_NeoPixel(1, 36, NEO_GRB + NEO_KHZ800)}, - {5, 4, false, Adafruit_NeoPixel(1, 4, NEO_GRB + NEO_KHZ800)}, - {6, 14, false, Adafruit_NeoPixel(1, 14, NEO_GRB + NEO_KHZ800)}, - {7, 33, false, Adafruit_NeoPixel(1, 33, NEO_GRB + NEO_KHZ800)}, - {8, 15, false, Adafruit_NeoPixel(1, 15, NEO_GRB + NEO_KHZ800)}, -}; -const int numKeys = 9; - -int grille[3][3] = { - {0, 0, 0}, - {0, 0, 0}, - {0, 0, 0} -}; - -bool btns[9] = { false, false, false, false, false, false, false, false, false }; - -bool joueur1 = true; +extern uint8_t color_pos; +extern MechKey keys[]; +extern const int numKeys; +extern int grille[3][3]; +extern bool btns[9]; +extern bool joueur1; Coord getCo(int key); bool victoire(int grille[3][3], int joueur); @@ -54,135 +35,4 @@ void afficheGrille(); void localModeDuo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched); void localModeSolo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched); -#endif // LOCALMODE_H - - -Coord getCo(int key) { - switch(key) { - case 0: return { 0, 0 }; - case 1: return { 1, 0 }; - case 2: return { 2, 0 }; - case 3: return { 0, 1 }; - case 4: return { 1, 1 }; - case 5: return { 2, 1 }; - case 6: return { 0, 2 }; - case 7: return { 1, 2 }; - case 8: return { 2, 2 }; - default: return { -1, -1 }; - } -} - -bool victoire(int grille[3][3], int joueur) { - // Vérification des lignes et colonnes - for (int i = 0; i < 3; ++i) { - // Ligne - if (grille[i][0] == joueur && grille[i][1] == joueur && grille[i][2] == joueur) - return true; - // Colonne - if (grille[0][i] == joueur && grille[1][i] == joueur && grille[2][i] == joueur) - return true; - } - // Vérification des deux diagonales - if (grille[0][0] == joueur && grille[1][1] == joueur && grille[2][2] == joueur) - return true; - if (grille[0][2] == joueur && grille[1][1] == joueur && grille[2][0] == joueur) - return true; - - // Aucun alignement trouvé - return false; -} - -bool allInit() { - for(int i = 0; i < 9; i++) { - if (!btns[i]) return false; - } - return true; -} - -void afficheGrille() { - Serial.println("=============="); - for(int i = 0; i < 3; i++) { - for(int j = 0; j < 3; j++) { - Serial.print(grille[i][j]); - } - Serial.println(); - } -} - -void localModeDuo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) { - currtouched = cap.touched(); - - for (int i = 0; i < numKeys; i++) { - uint8_t t = keys[i].touchID; - keys[i].led.setPixelColor(0, 255); // Bleu - keys[i].led.show(); - - if ((currtouched & _BV(t)) && !(lasttouched & _BV(t))) { - Serial.print("Touch "); Serial.print(t); Serial.println(" pressed"); - btns[i] = true; - - if(allInit()) { - Coord c = getCo(i); - if(grille[c.y][c.x] == 0) { - if(joueur1) { - grille[c.y][c.x] = 1; - afficheGrille(); - if(victoire(grille, 1)) { - Serial.println("Joueur 1 a gagne"); - } - } else { - grille[c.y][c.x] = 2; - afficheGrille(); - if(victoire(grille, 2)) { - Serial.println("Joueur 2 a gagne"); - } - } - joueur1 = !joueur1; - } - } - } - } - lasttouched = currtouched; -} - -void localModeSolo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) { - - //Serial.println("Mode Solo Local"); - - currtouched = cap.touched(); - - for (int i = 0; i < numKeys; i++) { - uint8_t t = keys[i].touchID; - keys[i].led.setPixelColor(0, 255); // Bleu - keys[i].led.show(); - - if ((currtouched & _BV(t)) && !(lasttouched & _BV(t))) { - Serial.print("Touch "); Serial.print(t); Serial.println(" pressed"); - btns[i] = true; - - if(allInit()) { - Coord c = getCo(i); - if(grille[c.y][c.x] == 0) { - grille[c.y][c.x] = 1; - afficheGrille(); - if(victoire(grille, 1)) { - Serial.println("Joueur 1 a gagne"); - } - - int x, y; - do { - x = random(0, 3); - y = random(0, 3); - } while(grille[y][x] != 0); - - grille[y][x] = 2; - afficheGrille(); - if(victoire(grille, 2)) { - Serial.println("Joueur 2 a gagne"); - } - } - } - } - } - lasttouched = currtouched; -} \ No newline at end of file +#endif // LOCALMODE_H \ No newline at end of file From 513a4c505665d83418dc191d9ee906c6557fcbed Mon Sep 17 00:00:00 2001 From: Pyramond Date: Tue, 17 Jun 2025 09:42:43 +0200 Subject: [PATCH 5/9] Split into header/cpp --- web.cpp | 328 ++++++++++++++++++++++++++++++++++++++++++++++++++++ web.h | 350 +++----------------------------------------------------- 2 files changed, 341 insertions(+), 337 deletions(-) create mode 100644 web.cpp diff --git a/web.cpp b/web.cpp new file mode 100644 index 0000000..25dff81 --- /dev/null +++ b/web.cpp @@ -0,0 +1,328 @@ +#include "web.h" +#include +#include +#include + +// Utilisateurs du réseau +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.println("Connectez vous a :"); + display.print("Nom : "); + display.println(ssid); + display.print("Mdp : "); + display.println(password); + display.println(); + display.print("Puis accedez a \nl'adresse "); + display.print(myIP); + display.display(); +} + +void webGame(NetworkClient &c){ + c.println(R"rawliteral( + + + + + + + Morpion Duo/Solo + + + +

Morpion

+ +
Joueur 1 (Bleu) à jouer
+
+
+
+
+
+
+
+
+
+
+
+ + + + +)rawliteral"); +} + +void webSolo(NetworkClient &c) { + c.println(""); + c.println(""); + c.println(""); + c.println(""); + c.println(""); + c.println(""); + c.println("
"); + c.println("Pas encore commence !"); + c.println("
"); + c.println(""); + c.println(""); +} + +void loopWeb() { + NetworkClient client = server.accept(); + if (client) { + Serial.println("New Client."); + String currentLine = ""; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + Serial.write(c); + if (c == '\n') { + if (currentLine.length() == 0) { + client.println("HTTP/1.1 200 OK"); + client.println("Content-type:text/html"); + client.println(); + webGame(client); + client.println(); + break; + } else { + currentLine = ""; + } + } else if (c != '\r') { + currentLine += c; + } + } + } + client.stop(); + Serial.println("Client Disconnected."); + } +} diff --git a/web.h b/web.h index 95bf6c2..8d008d8 100644 --- a/web.h +++ b/web.h @@ -1,344 +1,20 @@ +#ifndef WEBSERVER_H +#define WEBSERVER_H + #include #include #include -const char *ssid = "morpion-SAE"; -const char *password = "12345678"; -NetworkServer server(80); -NetworkClient client = server.accept(); +// SSID & mot de passe du réseau WiFi +extern const char *ssid; +extern const char *password; -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(); +// Serveur et client web +extern NetworkServer server; - Serial.println("Server started"); +void setupWeb(); +void loopWeb(); +void webGame(NetworkClient &c); +void webSolo(NetworkClient &c); - display.clearDisplay(); - display.setCursor(0, 0); - display.println("Connectez vous a :"); - display.print("Nom : "); - display.println(ssid); - display.print("Mdp : "); - display.println(password); - display.println(); - display.print("Puis accedez a \nl'adresse "); - display.print(myIP); - display.display(); -} - - - - - -void webGame(NetworkClient &c){ - c.println(R"rawliteral( - - - - - - - Morpion Duo/Solo - - - -

Morpion

- -
Joueur 1 (Bleu) à jouer
-
-
-
-
-
-
-
-
-
-
-
- - - - -)rawliteral"); -} - - - - -void webSolo(NetworkClient &c){ - c.println(""); - c.println(""); - c.println(""); - c.println(""); - c.println(""); - - c.println(""); - c.println("
"); - c.println("Pas encore commence !"); - c.println("
"); - - c.println(""); - c.println(""); -} - - - - -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: - webGame(client); - 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 - } - } - } - client.stop(); - Serial.println("Client Disconnected."); - } -} \ No newline at end of file +#endif // WEBSERVER_H From ebccbc87c788cecce1c8efa4d8e521ba2b0eb4d3 Mon Sep 17 00:00:00 2001 From: Pyramond Date: Tue, 17 Jun 2025 09:43:11 +0200 Subject: [PATCH 6/9] Remove Serial.println --- gra_21_morpion_lumineux_code.ino | 1 - 1 file changed, 1 deletion(-) diff --git a/gra_21_morpion_lumineux_code.ino b/gra_21_morpion_lumineux_code.ino index 2f10c13..3ee0fcf 100644 --- a/gra_21_morpion_lumineux_code.ino +++ b/gra_21_morpion_lumineux_code.ino @@ -32,7 +32,6 @@ void loop() { readButton(); if (!isPlay) { menu(); - Serial.println("ok"); } else { if (!local) { if(!isWebSet){ From 95fd4461b2977d36ec631062f178c79ae4cf530a Mon Sep 17 00:00:00 2001 From: Pyramond Date: Tue, 17 Jun 2025 09:46:40 +0200 Subject: [PATCH 7/9] update readme.md --- README.md | 106 +++++++++--------------------------------------------- 1 file changed, 16 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index 7a59eeb..0ee42e1 100644 --- a/README.md +++ b/README.md @@ -1,93 +1,19 @@ -# grA_21_Morpion_lumineux_code +# Morpion lumineux + +## SAE 203 + +

+ Quentin T'JAMPENS
+ Giovanni JOSSERAND
+

+### Bibliothèques utilisées -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/topics/git/add_files/#add-files-to-a-git-repository) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://iutbg-gitlab.iutbourg.univ-lyon1.fr/sae_203_2025/grA_21_Morpion_lumineaux/gra_21_morpion_lumineux_code.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://iutbg-gitlab.iutbourg.univ-lyon1.fr/sae_203_2025/grA_21_Morpion_lumineaux/gra_21_morpion_lumineux_code/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Set auto-merge](https://docs.gitlab.com/user/project/merge_requests/auto_merge/) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README - -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +
    +
  • Adafruit NeoPixel v1.15.1
  • +
  • TTP229
  • +
  • Adafruit_MPR121
  • +
  • Adafruit GFX Library
  • +
  • Adafruit_SH110X
  • +
\ No newline at end of file From 05469e9b816fb6e075f9849d6fc73b459d005a3c Mon Sep 17 00:00:00 2001 From: Pyramond Date: Tue, 17 Jun 2025 10:20:32 +0200 Subject: [PATCH 8/9] Fix display error --- web.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/web.cpp b/web.cpp index 25dff81..c6dfdb8 100644 --- a/web.cpp +++ b/web.cpp @@ -3,6 +3,8 @@ #include #include +extern Adafruit_SH1107 display; + // Utilisateurs du réseau const char *ssid = "morpion-SAE"; const char *password = "12345678"; From 16d812f924c2851a65b6dc93404b831bd520eab8 Mon Sep 17 00:00:00 2001 From: Pyramond Date: Tue, 17 Jun 2025 10:20:44 +0200 Subject: [PATCH 9/9] add reset btn --- gra_21_morpion_lumineux_code.ino | 1 + localMode.cpp | 22 ++++++++++++++++++---- localMode.h | 2 ++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/gra_21_morpion_lumineux_code.ino b/gra_21_morpion_lumineux_code.ino index 3ee0fcf..607bbda 100644 --- a/gra_21_morpion_lumineux_code.ino +++ b/gra_21_morpion_lumineux_code.ino @@ -17,6 +17,7 @@ uint16_t currtouched = 0; void setup() { Serial.begin(115200); randomSeed(analogRead(0)); + pinMode(A2, INPUT); if (!cap.begin(0x5B)) { diff --git a/localMode.cpp b/localMode.cpp index e664ee9..f1917b9 100644 --- a/localMode.cpp +++ b/localMode.cpp @@ -1,8 +1,6 @@ #include "localMode.h" #include -uint8_t color_pos = 16; - MechKey keys[] = { {0, A0, false, Adafruit_NeoPixel(1, A0, NEO_GRB + NEO_KHZ800)}, {1, 25, false, Adafruit_NeoPixel(1, 25, NEO_GRB + NEO_KHZ800)}, @@ -74,13 +72,28 @@ void afficheGrille() { } } +void reset() { + if(digitalRead(A2) == HIGH) { + Serial.println("Reset"); + + for(int i = 0; i < 3; i++) { + for(int j = 0; j < 3; j++) { + grille[i][j] = 0; + } + } + + joueur1 = true; + + afficheGrille(); + } +} + void localModeDuo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) { currtouched = cap.touched(); + reset(); for (int i = 0; i < numKeys; i++) { uint8_t t = keys[i].touchID; - keys[i].led.setPixelColor(0, 255); // Bleu - keys[i].led.show(); if ((currtouched & _BV(t)) && !(lasttouched & _BV(t))) { Serial.print("Touch "); Serial.print(t); Serial.println(" pressed"); @@ -112,6 +125,7 @@ void localModeDuo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtou void localModeSolo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) { currtouched = cap.touched(); + reset(); for (int i = 0; i < numKeys; i++) { uint8_t t = keys[i].touchID; diff --git a/localMode.h b/localMode.h index 5c929ef..675f210 100644 --- a/localMode.h +++ b/localMode.h @@ -3,6 +3,7 @@ #include #include +#include "pin.h" #ifndef _BV #define _BV(bit) (1 << (bit)) @@ -31,6 +32,7 @@ Coord getCo(int key); bool victoire(int grille[3][3], int joueur); bool allInit(); void afficheGrille(); +void reset(); void localModeDuo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched); void localModeSolo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched);