Created
March 16, 2017 19:01
-
-
Save Hydeo/49fb3f1ea4604e65396c3863a036b3d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
#include <vector> | |
#include <time.h> | |
#include "pitches.h" | |
//Variables Globales | |
enum CouleurBouton {rougeBouton=D6, bleuBouton=D5, jauneBouton=D7}; | |
enum Couleur { rouge=D2, jaune=D3, bleu=D1 }; | |
std::vector<int> sequence; | |
int indiceCourant; | |
String nbCoupsDefaut = "5"; | |
String vitesseDefaut = "1000"; | |
bool partieEnCours = false; | |
int vitesse; | |
const int pinMusique = D8; | |
int noteJaune = NOTE_E6; | |
int noteRouge = NOTE_G6; | |
int noteBleue = NOTE_B6; | |
// http://192.168.4.1 | |
const char *ssid = "Simon"; | |
const char *password = "SimonBOGOS"; | |
ESP8266WebServer server(80); | |
//Page Web | |
String baseHTML = "<!doctype html> <html> <head> <title>Fais péter le Simon </title> <style>body{background-color: #F5F3EB;}h1{color: blue;}p{color: red;}.center{margin: auto; width: 70%; padding: 10px;}input{float: right;}#aieaie{}#field1{height: 300px;}#field2{height: 300px;}</style> </head> <body> <br><div> <h1> The Bip-Bip Game </h1> </div><table style='width:100%'> <tr> <td style='vertical-align:top'> <fieldset style='height: 300px;' id='fieldset1'> <legend>Configuration Partie</legend> <br><br><form method='get' action='/init'> Difficultée : <input style='align:left' type='text' name='nbCoups' value='[VALUENBCOUPS]'/> <br><br>Vitesse : <input type='text' name='vitesse' value='[VALUEVITESSE]'/> <br><br><div style='margin: auto; width: 30%'> <button type='submit' value='Red'>Initialiser Partie</button> </div></form> <br></fieldset> </td><td style='vertical-align:top'> <fieldset style='height: 300px;' id='fieldset2'> <legend>Statistiques : </legend> <br>Nombre de Parties jouées : <span style='float: right;'>[PARTIEJOUEES]</span> <br><br>Difficultées Max atteinte : <span style='float: right;'>[DIFFMAX]</span> <br><br>Moyenne tours avant défaite : <span style='float: right;'>[MOYDEFAITE]</span> </fieldset> </tr></table> <div style='margin: auto; width: 100%'> <fieldset> <legend>Solution</legend> <table style='width:100%'> <tr> [TABLESOLUTION] </tr></table> </fieldset> </div></body></html>"; | |
//Méthodes des I/O | |
void jouerMusique(int nombreNotes, int vitesseNote, int noteDurations[], int melodie[]){ | |
for (int thisNote = 0; thisNote < 11; thisNote++) { | |
int noteDuration = vitesseNote / noteDurations[thisNote]; | |
tone(pinMusique, melodie[thisNote], noteDuration); | |
int pauseBetweenNotes = noteDuration * 1.30; | |
delay(pauseBetweenNotes); | |
noTone(pinMusique); | |
} | |
} | |
void jouerMusiqueVictoire(){ | |
int notesVictoire[] = { NOTE_ZERO, NOTE_C6 ,NOTE_C6,NOTE_C6,NOTE_C6, NOTE_GS5 , NOTE_AS5 , NOTE_C6, NOTE_ZERO , NOTE_AS5 , NOTE_C6}; | |
int rythmeVictoire[] = { 2,12,12,12,4,4,4,12,16,12,1 }; | |
int vitesseNoteVictoire = 1500; | |
jouerMusique(11, vitesseNoteVictoire, rythmeVictoire, notesVictoire); | |
} | |
void jouerMusiqueDefaite(){ | |
int notesDefaite[] = { NOTE_B4, NOTE_F5,NOTE_ZERO ,NOTE_F5,NOTE_F5, NOTE_E5, NOTE_D5, NOTE_C5,NOTE_E4,NOTE_ZERO, NOTE_E4 , NOTE_C4 }; | |
int noteDurations[] = { 4,4,4,4,3,3,3,4,4,4,4,4 }; | |
int vitesseNote = 500; | |
jouerMusique(12, vitesseNote, noteDurations, notesDefaite); | |
} | |
void jouerMusiqueInit(){ | |
int notesInit[] = { NOTE_G6, NOTE_FS6, NOTE_DS6, NOTE_A5 ,NOTE_GS5, NOTE_E6, NOTE_GS6, NOTE_C7 }; | |
int noteDurations[] = { 4,4,4,4,4,4,4,2 }; | |
int vitesseNote = 500; | |
jouerMusique(8, vitesseNote, noteDurations, notesInit); | |
delay(1000); | |
} | |
int boutonEcouteur(){ | |
//while (digitalRead(rougeBouton) == HIGH && digitalRead(bleuBouton) == HIGH && digitalRead(jauneBouton) == HIGH ) // While pin 12 is HIGH (not activated) | |
//yield(); // Do (almost) nothing -- yield to allow ESP8266 background functions | |
if(digitalRead(rougeBouton) == LOW && digitalRead(bleuBouton) == LOW && digitalRead(jauneBouton) == LOW) | |
return -2; | |
if(digitalRead(rougeBouton) == LOW){ | |
if(partieEnCours) | |
tone(pinMusique, noteRouge, 200); | |
return rouge; | |
} | |
if(digitalRead(bleuBouton) == LOW){ | |
if(partieEnCours) | |
tone(pinMusique, noteBleue, 200); | |
return bleu; | |
} | |
if(digitalRead(jauneBouton) == LOW){ | |
if(partieEnCours) | |
tone(pinMusique, noteJaune, 200); | |
return jaune; | |
} | |
return -1; | |
} | |
void allumerLeFeu(int couleur){ | |
//On eteint tout | |
digitalWrite(rouge, LOW); | |
digitalWrite(bleu, LOW); | |
digitalWrite(jaune, LOW); | |
//On allume celle qu'on veut | |
switch(couleur){ | |
case rouge: | |
digitalWrite(rouge, HIGH); | |
Serial.println("Rouge On"); | |
break; | |
case bleu: | |
digitalWrite(bleu, HIGH); | |
Serial.println("Bleu On"); | |
break; | |
case jaune: | |
digitalWrite(jaune, HIGH); | |
Serial.println("Jaune On"); | |
break; | |
} | |
} | |
//Méthodes de début de partie | |
void jouerSequence(){ | |
int tempsPause = vitesse * 0.10; | |
int laVitesse = vitesse * 0.90; | |
Serial.println("Jouer Sequence : "); | |
for(int i=0;i<sequence.size();i++){ | |
allumerLeFeu(sequence.at(i)); | |
if(rouge == sequence.at(i)) | |
tone(pinMusique, noteRouge, 200); | |
else if(bleu == sequence.at(i)) | |
tone(pinMusique, noteBleue, 200); | |
else if(jaune == sequence.at(i)) | |
tone(pinMusique, noteJaune, 200); | |
delay(laVitesse); | |
allumerLeFeu(-1); | |
delay(tempsPause); | |
} | |
} | |
void initialiserTableau(int coups){ | |
Serial.print("Initialiser tableau : "); | |
sequence.clear(); | |
for(int i=0; i < coups; i++){ | |
int test = rand() % 3; | |
if(0 == test) | |
sequence.push_back(rouge); | |
else if(1 == test) | |
sequence.push_back(jaune); | |
else | |
sequence.push_back(bleu); | |
} | |
Serial.println(sequence.size()); | |
indiceCourant = 0; | |
} | |
void incrementerTableau(){ | |
indiceCourant = 0; | |
int test = rand() % 3; | |
if(0 == test) | |
sequence.push_back(rouge); | |
else if(1 == test) | |
sequence.push_back(jaune); | |
else | |
sequence.push_back(bleu); | |
} | |
//Méthodes de jeu | |
void finPartie(bool reussite){ | |
if(reussite){ | |
//"Good Buzzer"? | |
jouerMusiqueVictoire(); | |
Serial.println("Gagné!"); | |
} | |
else{ | |
//"Bad Buzzer" | |
jouerMusiqueDefaite(); | |
Serial.println("Perdu!"); | |
} | |
} | |
void jouerCoups(int couleur){ | |
Serial.print("Jouer coups : "); | |
if(sequence.at(indiceCourant++) == couleur){ | |
Serial.println("Bon coups!"); | |
if(indiceCourant == sequence.size()){ | |
jouerMusiqueVictoire(); | |
incrementerTableau(); | |
jouerSequence(); | |
//finPartie(true); | |
} | |
partieEnCours = true; | |
} | |
else{ | |
Serial.println("Mauvais Coups!"); | |
finPartie(false); | |
} | |
} | |
//Méthodes des URLs | |
void tenirRacine() { | |
String resultatHTML = baseHTML; | |
resultatHTML.replace("[VALUENBCOUPS]", nbCoupsDefaut); | |
resultatHTML.replace("[VALUEVITESSE]", vitesseDefaut); | |
resultatHTML.replace("[MESSAGE]", ""); | |
server.send(200, "text/html", resultatHTML); | |
} | |
void initialiserPartieDefaut(){ | |
Serial.println("Initialiser Partie"); | |
int nbCoups = atoi(nbCoupsDefaut.c_str()); | |
int laVitesse = atoi(vitesseDefaut.c_str()); | |
Serial.print("Nombre Coups = "); | |
Serial.println(nbCoups); | |
Serial.print("Vitesse = "); | |
Serial.println(laVitesse); | |
initialiserTableau(nbCoups); | |
vitesse = laVitesse; | |
jouerMusiqueInit(); | |
jouerSequence(); | |
partieEnCours = true; | |
} | |
void initialiserPartie(){ | |
Serial.println("Initialiser Partie"); | |
int nbCoups = atoi(server.arg("nbCoups").c_str()); | |
int laVitesse = atoi(server.arg("vitesse").c_str()); | |
Serial.print("Nombre Coups = "); | |
Serial.println(nbCoups); | |
Serial.print("Vitesse = "); | |
Serial.println(laVitesse); | |
String resultatHTML = baseHTML; | |
resultatHTML.replace("[VALUENBCOUPS]", server.arg("nbCoups")); | |
resultatHTML.replace("[VALUEVITESSE]", server.arg("vitesse")); | |
resultatHTML.replace("[MESSAGE]", "Partie lancée!"); | |
server.send(200, "text/html", resultatHTML); | |
initialiserTableau(nbCoups); | |
vitesse = laVitesse; | |
jouerMusiqueInit(); | |
jouerSequence(); | |
partieEnCours = true; | |
} | |
void setupServer(){ | |
server.on("/", tenirRacine); | |
server.on("/init", initialiserPartie); | |
server.begin(); | |
} | |
// the setup routine runs once when you press reset: | |
void setup() { | |
srand((unsigned)time(NULL)); | |
delay(1000); | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.print("Configuring access point..."); | |
/* You can remove the password parameter if you want the AP to be open. */ | |
WiFi.softAP(ssid, password); | |
IPAddress myIP = WiFi.softAPIP(); | |
Serial.print("AP IP address: "); | |
Serial.println(myIP); | |
setupServer(); | |
Serial.println("HTTP server started"); | |
//Init des LED | |
pinMode(rouge, OUTPUT); | |
pinMode(bleu, OUTPUT); | |
pinMode(jaune, OUTPUT); | |
// Init des boutons | |
pinMode(rougeBouton, INPUT_PULLUP); | |
pinMode(bleuBouton, INPUT_PULLUP); | |
pinMode(jauneBouton, INPUT_PULLUP); | |
} | |
// the loop routine runs over and over again forever: | |
void loop() { | |
server.handleClient(); | |
if(partieEnCours){ | |
int couleur = boutonEcouteur(); | |
if(couleur != -1){ | |
partieEnCours = false; | |
allumerLeFeu(couleur); | |
jouerCoups(couleur); | |
while(couleur == boutonEcouteur()); | |
allumerLeFeu(-1); | |
} | |
} | |
else{ | |
if(-2 == boutonEcouteur()){ | |
while(-2 == boutonEcouteur()); | |
delay(500); | |
initialiserPartieDefaut(); | |
} | |
} | |
delay(100); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment