From a034e011c50a4aca37c881416fed8d4568c8a079 Mon Sep 17 00:00:00 2001 From: Simon Gagne Date: Mon, 20 Mar 2023 10:16:10 -0400 Subject: Merge du code de communication --- chordNote.h | 2 +- main.cpp | 472 +++++++++++++++++++++++++++++++++++++++--------------------- song.h | 2 +- 3 files changed, 310 insertions(+), 166 deletions(-) diff --git a/chordNote.h b/chordNote.h index 3ac7f71..33a7010 100644 --- a/chordNote.h +++ b/chordNote.h @@ -41,5 +41,5 @@ class ChordNote { // the expected chord }; -#include "chordNote.cpp" +//#include "chordNote.cpp" #endif // __CHORDNOTE_H__ diff --git a/main.cpp b/main.cpp index 2929525..8eebaab 100644 --- a/main.cpp +++ b/main.cpp @@ -2,28 +2,46 @@ #include #include #include +#include -//#include "include/serial/SerialPort.hpp" -//#include "include/json.hpp" +#include "include/serial/SerialPort.hpp" +#include "include/json.hpp" #include "chordNote.h" #include "song.h" -//using json = nlohmann::json; +using json = nlohmann::json; using namespace std; /*------------------------------ Constantes ---------------------------------*/ #define BAUD 115200 // Frequence de transmission serielle +//#define BAUD 9600 // Frequence de transmission serielle #define MSG_MAX_SIZE 1024 // Longueur maximale d'un message +//Already defined in ChordNote.h +//#define FRET_1 = 0 +//#define FRET_2 = 1 +//#define FRET_3 = 2 +//#define FRET_4 = 3 +//#define FRET_5 = 4 + +#define STRUM_UP 5 +#define STRUM_DN 6 +#define JOY_DIR 7 + + /*------------------------- Prototypes de fonctions -------------------------*/ -//bool SendToSerial(SerialPort *arduino, json j_msg); -//bool RcvFromSerial(SerialPort *arduino, string &msg); +bool SendToSerial(SerialPort *arduino, json j_msg); +bool RcvFromSerial(SerialPort *arduino, string &msg); +void RcvJsonThread(); +void testThread(); +bool ComparePlayedNotes(ChordNote note, bool strumNeeded); +bool CompareIndividualButton(bool wantedValue, int buttonState); /*---------------------------- Variables globales ---------------------------*/ -//SerialPort * arduino; //doit etre un objet global! +SerialPort * arduino; //doit etre un objet global! int led_state = 0; -//bool Fret1 = 0; +int Fret1 = 0; int Fret2 = 0; int Fret3 = 0; int Fret4 = 0; @@ -32,6 +50,9 @@ int JoyDir = 0; int StrumUp = 0; int StrumDown = 0; +int testInt = 0; +bool isThreadOver = false; + int main() { string raw_msg; @@ -40,35 +61,33 @@ int main() //string com; //cout <<"Entrer le port de communication du Arduino: "; //cin >> com; - - const int NB_SQUARES = 20; - ChordNote note1(0, 5000, 6000); - ChordNote note2(1, 6200, 6200); + ChordNote note1(0, 5000, 5400); + ChordNote note2(1, 5800, 5800); ChordNote note3(2, 6600, 6600); - ChordNote note4(3, 6800, 6800); - ChordNote note5(0, 7000, 9000); - ChordNote note6(4, 7200, 7200); + ChordNote note4(3, 8000, 8000); + ChordNote note5(0, 9000, 9600); + /*ChordNote note6(4, 7200, 7200); ChordNote note7(1, 7400, 7400); ChordNote note8(2, 8000, 8000); ChordNote note9(3, 8600, 8600); - ChordNote note10(4, 9000, 9000); + ChordNote note10(4, 9000, 9000);*/ - note1.change(1); + note2.change(0); - const int FRAMERATE = 200; + const int FRAMERATE = 100; int renderStart = FRAMERATE * NB_SQUARES; note1.setRenderStart(renderStart); note2.setRenderStart(renderStart); note3.setRenderStart(renderStart); note4.setRenderStart(renderStart); note5.setRenderStart(renderStart); - note6.setRenderStart(renderStart); + /*note6.setRenderStart(renderStart); note7.setRenderStart(renderStart); note8.setRenderStart(renderStart); note9.setRenderStart(renderStart); - note10.setRenderStart(renderStart); + note10.setRenderStart(renderStart);*/ string displayArray[NB_SQUARES + 1][5]; for (int i = 0; i < NB_SQUARES + 1; i++) @@ -82,40 +101,38 @@ int main() const int NB_NOTES = 5; ChordNote song[NB_NOTES] = {note1, note2, note3, note4, note5}; - //string com = "COM7"; - //arduino = new SerialPort(com.c_str(), BAUD); + string com = "com7"; + arduino = new SerialPort(com.c_str(), BAUD); - // if(!arduino->isConnected()){ - // cerr << "Impossible de se connecter au port "<< string(com.c_str()) <<". Fermeture du programme!" <isConnected()){ + cerr << "Impossible de se connecter au port "<< string(com.c_str()) <<". Fermeture du programme!" <(currentTime - startTime).count()); if(totalDiff > 10000)//End of song { - isNotDone = false; + isThreadOver = true; + worker.join(); + SongNotDone = false; } else { @@ -127,97 +144,37 @@ int main() double diffSinceBeginning = double(std::chrono::duration_cast (newCheckTime - startTime).count()); //TO ADD : CHECK NOTES INPUT - // Envoie message Arduino - // j_msg_send["led"] = led_state; - // if(!SendToSerial(arduino, j_msg_send)){ - // cerr << "Erreur lors de l'envoie du message. " << endl; - // } - // // Reception message Arduino - // j_msg_rcv.clear(); // effacer le message precedent - // if(!RcvFromSerial(arduino, raw_msg)){ - // cerr << "Erreur lors de la reception du message. " << endl; - // } + int nextNoteStart = song[nextNoteToPlay].getStart(); + int previousNoteEnd = 0; + bool isNotePlayedCorrectly = false; + if (nextNoteToPlay > 0) + { + previousNoteEnd = song[nextNoteToPlay - 1].getEnd(); + } + if (abs(nextNoteStart - diffSinceBeginning) < 25) + { + isNotePlayedCorrectly = ComparePlayedNotes(song[nextNoteToPlay], false); + nextNoteToPlay++; + if (isNotePlayedCorrectly) + { + nbNotesCorrect++; + } + } + //if (diffLastPrint <= previousNoteEnd) - // // Impression du message de l'Arduino si valide - // if(raw_msg.size()>0){ - // //cout << "raw_msg: " << raw_msg << endl; // debug - // // Transfert du message en json - // j_msg_rcv = json::parse(raw_msg); - // cout << "Message de l'Arduino: " << j_msg_rcv << endl; - // } - // if(j_msg_rcv==json::value_t::null) - // { - // cout << "DATA_NULL\n"; - // } - // else - // { - // //cout << "longueur " << j_msg_rcv.size() <<"\n"; - // //cout << "7 " << j_msg_rcv[7] <<"\n"; - // Fret1 = j_msg_rcv[0]; - // Fret2 = j_msg_rcv[1]; - // Fret3 = j_msg_rcv[2]; - // Fret4 = j_msg_rcv[3]; - // Fret5 = j_msg_rcv[4]; - // StrumUp = j_msg_rcv[5]; - // StrumDown = j_msg_rcv[6]; - // JoyDir = j_msg_rcv[7]; - - - // /*if(led_state == 10) //lorsque la dixieme led est allumee - // { - // start = std::chrono::steady_clock::now(); // debute le chrono - // } - - // if(Fret1 != 0) //verifie la valeur de fret1 - // { - // now = std::chrono::steady_clock::now(); - - // double elapsed_time_ms = double(std::chrono::duration_cast (now - start).count()); - // std::cout << "Temps ecouleyy : " << elapsed_time_ms/1e3 << " secondes" << std::endl; - // }*/ - // if(JoyDir == 4) - // { - // led_state+=1; - // } - - // if(JoyDir == 3) - // { - // led_state-=1; - // } - - // if(JoyDir == 2) - // { - // led_state=0; - // } - - // if(JoyDir == 1) - // { - // led_state=10; - // } - - // if (led_state > 10) - // { - // led_state = 0; - // } - - // if (led_state < 0) - // { - // led_state = 10; - // } - // } //Gestion affichage if(diffLastPrint >= FRAMERATE) { system("cls"); lastPrintTime = newCheckTime; - if (nextNoteIndex > 0) + if (nextRenderNoteIndex > 0) { - int previousNoteRenderStart = song[nextNoteIndex-1].getRenderStart(); - int previousNoteLength = song[nextNoteIndex-1].getEnd() - song[nextNoteIndex-1].getStart(); + int previousNoteRenderStart = song[nextRenderNoteIndex -1].getRenderStart(); + int previousNoteLength = song[nextRenderNoteIndex -1].getEnd() - song[nextRenderNoteIndex -1].getStart(); if ((diffSinceBeginning - previousNoteRenderStart) < previousNoteLength) { - bool* notes = song[nextNoteIndex - 1].getNotes(); + bool* notes = song[nextRenderNoteIndex - 1].getNotes(); for (int j = 0; j < 5; j++) { if (notes[j]) @@ -228,9 +185,9 @@ int main() } } - if (abs(song[nextNoteIndex].getRenderStart() - diffSinceBeginning) < 25) + if (abs(song[nextRenderNoteIndex].getRenderStart() - diffSinceBeginning) < 25) { - bool* notes = song[nextNoteIndex].getNotes(); + bool* notes = song[nextRenderNoteIndex].getNotes(); for (int j = 0; j < 5; j++) { if (notes[j]) @@ -238,7 +195,7 @@ int main() displayArray[0][j] = "X"; } } - nextNoteIndex++; + nextRenderNoteIndex++; } //printing @@ -282,52 +239,239 @@ int main() cout << endl; cout << "Timestamp " << diffSinceBeginning << " ms" << endl; + cout << "Nb notes reussies : " << nbNotesCorrect << endl; } } } - + cout << endl << testInt << endl; return 0; } -// /*---------------------------Definition de fonctions ------------------------*/ -// bool SendToSerial(SerialPort *arduino, json j_msg){ -// // Return 0 if error -// string msg = j_msg.dump(); -// bool ret = arduino->writeSerialPort(msg.c_str(), msg.length()); -// return ret; -// } - - -// bool RcvFromSerial(SerialPort *arduino, string &msg){ -// // Return 0 if error -// // Message output in msg -// string str_buffer; -// char char_buffer[MSG_MAX_SIZE]; -// int buffer_size; - -// msg.clear(); // clear string -// // Read serialport until '\n' character (Blocking) - -// // Version fonctionnel dans VScode, mais non fonctionnel avec Visual Studio -// /* -// while(msg.back()!='\n'){ -// if(msg.size()>MSG_MAX_SIZE){ -// return false; -// } - -// buffer_size = arduino->readSerialPort(char_buffer, MSG_MAX_SIZE); -// str_buffer.assign(char_buffer, buffer_size); -// msg.append(str_buffer); -// } -// */ - -// // Version fonctionnelle dans VScode et Visual Studio -// buffer_size = arduino->readSerialPort(char_buffer, MSG_MAX_SIZE); -// str_buffer.assign(char_buffer, buffer_size); -// msg.append(str_buffer); - -// //msg.pop_back(); //remove '/n' from string - -// return true; -// } \ No newline at end of file + /*---------------------------Definition de fonctions ------------------------*/ + bool SendToSerial(SerialPort *arduino, json j_msg){ + // Return 0 if error + string msg = j_msg.dump(); + bool ret = arduino->writeSerialPort(msg.c_str(), msg.length()); + return ret; + } + + + bool RcvFromSerial(SerialPort *arduino, string &msg){ + // Return 0 if error + // Message output in msg + string str_buffer; + char char_buffer[MSG_MAX_SIZE]; + int buffer_size; + + msg.clear(); // clear string + // Read serialport until '\n' character (Blocking) + + // Version fonctionnel dans VScode, mais non fonctionnel avec Visual Studio + /* + while(msg.back()!='\n'){ + if(msg.size()>MSG_MAX_SIZE){ + return false; + } + + buffer_size = arduino->readSerialPort(char_buffer, MSG_MAX_SIZE); + str_buffer.assign(char_buffer, buffer_size); + msg.append(str_buffer); + } + */ + + // Version fonctionnelle dans VScode et Visual Studio + buffer_size = arduino->readSerialPort(char_buffer, MSG_MAX_SIZE); + str_buffer.assign(char_buffer, buffer_size); + msg.append(str_buffer); + + //msg.pop_back(); //remove '/n' from string + + return true; + } + + void RcvJsonThread() + { + string raw_msg; + json json_recu; + json json_envoye; + while (!isThreadOver) + { + //Envoie message Arduino + json_envoye["led"] = led_state; + if (!SendToSerial(arduino, json_envoye)) { + cerr << "Erreur lors de l'envoie du message. " << endl; + } + // Reception message Arduino + json_recu.clear(); // effacer le message precedent + if (!RcvFromSerial(arduino, raw_msg)) { + cerr << "Erreur lors de la reception du message. " << endl; + } + //cout << "Message : " << raw_msg << endl; + + // Impression du message de l'Arduino si valide + if (raw_msg.size() > 0) { + //cout << "raw_msg: " << raw_msg << endl; // debug + // Transfert du message en json + json_recu = json::parse(raw_msg); + //out << "Message de l'Arduino: " << json_recu << endl; + } + if (json_recu == json::value_t::null) + { + //cout << "DATA_NULL\n"; + } + else + { + //cout << "longueur " << j_msg_rcv.size() <<"\n"; + //cout << "7 " << j_msg_rcv[7] <<"\n"; + Fret1 = json_recu[FRET1]; + Fret2 = json_recu[FRET2]; + Fret3 = json_recu[FRET3]; + Fret4 = json_recu[FRET4]; + Fret5 = json_recu[FRET5]; + StrumUp = json_recu[STRUM_UP]; + StrumDown = json_recu[STRUM_DN]; + JoyDir = json_recu[JOY_DIR]; + + + /*if(led_state == 10) //lorsque la dixieme led est allumee + { + start = std::chrono::steady_clock::now(); // debute le chrono + } + + if(Fret1 != 0) //verifie la valeur de fret1 + { + now = std::chrono::steady_clock::now(); + + double elapsed_time_ms = double(std::chrono::duration_cast (now - start).count()); + std::cout << "Temps ecouleyy : " << elapsed_time_ms/1e3 << " secondes" << std::endl; + }*/ + if (JoyDir == 4) + { + led_state += 1; + } + + if (JoyDir == 3) + { + led_state -= 1; + } + + if (JoyDir == 2) + { + led_state = 0; + } + + if (JoyDir == 1) + { + led_state = 10; + } + + if (led_state > 10) + { + led_state = 0; + } + + if (led_state < 0) + { + led_state = 10; + } + } + Sleep(200); + } + } + + void testThread() + { + while (!isThreadOver) + { + testInt++; + } + } + + bool ComparePlayedNotes(ChordNote note, bool strumNeeded) + { + bool correctlyPlayed = false; + bool* notes = note.getNotes(); + + bool comp1 = CompareIndividualButton(notes[0], Fret1); + bool comp2 = CompareIndividualButton(notes[1], Fret2); + bool comp3 = CompareIndividualButton(notes[2], Fret3); + bool comp4 = CompareIndividualButton(notes[3], Fret4); + bool comp5 = CompareIndividualButton(notes[4], Fret5); + + if (comp1 && comp2 && comp3 && comp4 && comp5) + { + if (strumNeeded) + { + bool compDown = CompareIndividualButton(true, StrumDown); + bool compUp = CompareIndividualButton(true, StrumUp); + + if (compDown || compUp) + { + correctlyPlayed = true; + } + } + correctlyPlayed = true; + } + + return correctlyPlayed; + } + + bool CompareIndividualButton(bool wantedValue, int buttonState) + { + return wantedValue == buttonState; + } + + //bool ComparePlayedNotes(ChordNote note, bool strumNeeded) + //{ + // bool correctlyPlayed = true; + // bool* notes = note.getNotes(); + + // //cout << "Voulu : " << notes[0]; + // if (notes[0]) + // { + // if (!Fret1) + // { + // correctlyPlayed = false; + // } + // } + // //cout << " --- Output : " << correctlyPlayed << endl;; + // if (notes[1]) + // { + // if (!Fret2) + // { + // correctlyPlayed = false; + // } + // } + // if (notes[2]) + // { + // if (!Fret3) + // { + // correctlyPlayed = false; + // } + // } + // if (notes[3]) + // { + // if (!Fret4) + // { + // correctlyPlayed = false; + // } + // } + // if (notes[4]) + // { + // if (!Fret5) + // { + // correctlyPlayed = false; + // } + // } + // if (strumNeeded) + // { + // if (!StrumUp && !StrumDown) + // { + // correctlyPlayed = false; + // } + // } + + // return correctlyPlayed; + //} + diff --git a/song.h b/song.h index 3a33d84..c8f08a1 100644 --- a/song.h +++ b/song.h @@ -27,6 +27,6 @@ class Song { std::vector getChords(); }; -#include "song.cpp" +//#include "song.cpp" #endif // __SONG_H__ -- cgit v1.2.3