From 2062f238ca4d7f5f74257f0eac03f4647ed55a11 Mon Sep 17 00:00:00 2001 From: "UNIV-LYON1\\p2405212" Date: Tue, 17 Jun 2025 10:17:15 +0200 Subject: [PATCH] add of setBtn function --- gra_21_morpion_lumineux_code.ino | 13 ++++--- localMode.h | 61 +++++++++++++++++++++----------- menu.h | 1 + 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/gra_21_morpion_lumineux_code.ino b/gra_21_morpion_lumineux_code.ino index 9a38996..1f35bb1 100644 --- a/gra_21_morpion_lumineux_code.ino +++ b/gra_21_morpion_lumineux_code.ino @@ -18,13 +18,13 @@ uint16_t currtouched = 0; void setup() { Serial.begin(115200); -/* + if (!cap.begin(0x5B)) { Serial.println("MPR121 not found, check wiring?"); while (1); } Serial.println("MPR121 found!"); - */ + setupMenu(); } @@ -33,7 +33,6 @@ void loop() { readButton(); if (!isPlay) { menu(); - Serial.println("ok"); } else { if (!local) { if(!isWebSet){ @@ -43,8 +42,12 @@ void loop() { loopWeb(); } } else { - // Passe cap, lasttouched, currtouched par référence ! - localMode(cap, lasttouched, currtouched); + if(!isBtnSet){ + setBtn(cap, lasttouched, currtouched); + }else{ + // Passe cap, lasttouched, currtouched par référence ! + localMode(cap, lasttouched, currtouched); + } } } } diff --git a/localMode.h b/localMode.h index cd9c396..55ec639 100644 --- a/localMode.h +++ b/localMode.h @@ -52,9 +52,33 @@ bool allInit(); void localMode(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched); +bool isBtnSet = false; + #endif // LOCALMODE_H +void setBtn(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched){ + display.clearDisplay(); + display.setCursor(0, 0); + display.print("Veuillez rester\nappuye sur chaqu'un\ndes boutons pendant\n3 secondes avant de\nles relacher pour\nles initialiser"); + display.display(); + if(allInit()){ + isBtnSet = true; + }else{ + 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; + } + } + lasttouched = currtouched; + } +} + Coord getCo(int key) { switch(key) { case 0: return { 0, 0 }; @@ -107,31 +131,28 @@ void localMode(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouche 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; - if(victoire(grille, 1)) { - Serial.println("Joueur 1 a gagne"); - } - } else { - grille[c.y][c.x] = 2; - if(victoire(grille, 2)) { - Serial.println("Joueur 2 a gagne"); - } + Coord c = getCo(i); + if(grille[c.y][c.x] == 0) { + if(joueur1) { + grille[c.y][c.x] = 1; + if(victoire(grille, 1)) { + Serial.println("Joueur 1 a gagne"); + } + } else { + grille[c.y][c.x] = 2; + if(victoire(grille, 2)) { + Serial.println("Joueur 2 a gagne"); } - joueur1 = !joueur1; } + joueur1 = !joueur1; + } - for(int i = 0; i < 3; i++) { - for(int j = 0; j < 3; j++) { - Serial.print(grille[i][j]); - } - Serial.println(); + for(int i = 0; i < 3; i++) { + for(int j = 0; j < 3; j++) { + Serial.print(grille[i][j]); } + Serial.println(); } } } diff --git a/menu.h b/menu.h index fe6ff1b..a521aa3 100644 --- a/menu.h +++ b/menu.h @@ -17,6 +17,7 @@ bool isDifficulty = false; bool isPlay = false; bool isWebSet = false; + #define BUTTON_A 15 #define BUTTON_B 32 Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);