liaison bluetooth entre gros robot -> panneau ou gros robot -> petit robot
Fork of liaison_Bluetooth by
Diff: LiaisonBluetooth.cpp
- Revision:
- 2:4fc77e5b08e9
- Parent:
- 1:ea044def4e89
- Child:
- 3:46a9b9c1e1c0
diff -r ea044def4e89 -r 4fc77e5b08e9 LiaisonBluetooth.cpp --- a/LiaisonBluetooth.cpp Wed Apr 11 12:33:54 2018 +0000 +++ b/LiaisonBluetooth.cpp Wed Apr 11 12:59:45 2018 +0000 @@ -1,9 +1,29 @@ #include "LiaisonBluetooth/LiaisonBluetooth.h" -LiaisonBluetooth::LiaisonBluetooth(Serial bluetooth, DigitalIn state) : m_bluetooth(bluetooth), m_state(state) { +/* +LiaisonBluetooth liaison(bluetooth, bluetooth_state); + +if (action_robot) { + char *score_char_array = convertir_int_en_4char(score_a_ajouter); + paquet_domotique_envoyer_ajouterscore(bluetooth, score_char_array); +} +if (action_robot1) { + char *score_char_array = convertir_int_en_4char(score_total); + paquet_domotique_envoyer_rafraichirscore(bluetooth, score_char_array); +} +if (action_robot_2) { + char *score_char_array = convertir_int_en_4char(score_total); + paquet_domotique_envoyer_finmatch(bluetooth, score_char_array); +} +*/ + + +LiaisonBluetooth::LiaisonBluetooth(Serial bluetooth, DigitalIn state) : m_bluetooth(bluetooth), m_state(state) +{ } -bool LiaisonBluetooth::paquet_en_attente() { +bool LiaisonBluetooth::paquet_en_attente() +{ while (m_bluetooth.readable() && m_state) { if (m_bluetooth.getc() == MARQUEUR_DEBUT_TRAME) { return true; @@ -13,12 +33,13 @@ return false; } -PaquetDomotique *LiaisonBluetooth::lire() { +PaquetDomotique *LiaisonBluetooth::lire() +{ char identifiant = 0; if (m_bluetooth.readable() && m_state) { identifiant = m_bluetooth.getc(); } else return NULL; - + char buffer_longueur_paquet[8]; for (int i = 0 ; i < 8 ; i++) { if (m_bluetooth.readable() && m_state) { @@ -26,67 +47,72 @@ } else return NULL; } int longueur_paquet = convertir_4char_en_int(buffer_longueur_paquet); - + char buffer_data[longueur_paquet]; int index = 0; while (index < longueur_paquet-1) { if (m_bluetooth.readable() && m_state) { char c = m_bluetooth.getc(); - + if (c == MARQUEUR_FIN_TRAME) return NULL; - + buffer_data[index++] = c; } else return NULL; } - + return creer_paquetdomotique(identifiant, buffer_data); } -void LiaisonBluetooth::envoyer(char idenfitiant, int longueur_data, char *data) { +void LiaisonBluetooth::envoyer(char idenfitiant, int longueur_data, char *data) +{ if (m_state && m_bluetooth.writeable()) { - m_bluetooth.putc(idenfitiant); + m_bluetooth.putc(idenfitiant); } - + char *longueur_buffer = convertir_int_en_4char(longueur_data); for (int i = 0 ; i < 4 ; i++) { if (m_state && m_bluetooth.writeable()) { - m_bluetooth.putc(longueur_buffer[i]); + m_bluetooth.putc(longueur_buffer[i]); } } - + for (int i = 0 ; i < longueur_data ; i++) { if (m_state && m_bluetooth.writeable()) { - m_bluetooth.putc(data[i]); + m_bluetooth.putc(data[i]); } } } -PaquetDomotique *creer_paquetdomotique(int identifiant, char *data) { +PaquetDomotique *creer_paquetdomotique(int identifiant, char *data) +{ PaquetDomotique *paquet = (PaquetDomotique*)malloc(sizeof(PaquetDomotique)); paquet->identifiant = identifiant; - + paquet->data = (char*)malloc(sizeof(char)*strlen(data)); strcpy(paquet->data, data); - + return paquet; } -void detruire_paquetdomotique(PaquetDomotique *paquet) { +void detruire_paquetdomotique(PaquetDomotique *paquet) +{ free(paquet->data); free(paquet); } -char *convertir_int_en_4char(int integer) { +char *convertir_int_en_4char(int integer) +{ char data[4]; - + data[3] = (integer>>24) & 0xFF; data[2] = (integer>>16) & 0xFF; data[1] = (integer>>8) & 0xFF; data[0] = integer & 0xFF; - + return data; } -int convertir_4char_en_int(char data[4]) { +int convertir_4char_en_int(char data[4]) +{ return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; } \ No newline at end of file