liaison bluetooth entre gros robot -> panneau ou gros robot -> petit robot
Fork of liaison_Bluetooth by
Diff: LiaisonBluetooth.cpp
- Revision:
- 5:be0c602be047
- Parent:
- 4:642635655630
- Child:
- 6:91cf94ff46ed
diff -r 642635655630 -r be0c602be047 LiaisonBluetooth.cpp --- a/LiaisonBluetooth.cpp Wed Apr 11 15:10:53 2018 +0000 +++ b/LiaisonBluetooth.cpp Fri Apr 13 07:39:01 2018 +0000 @@ -8,16 +8,19 @@ } */ -LiaisonBluetooth::LiaisonBluetooth(Serial *bluetooth, DigitalIn *state) : m_bluetooth(bluetooth), m_state(state) +LiaisonBluetooth::LiaisonBluetooth(Serial *bluetooth) : m_bluetooth(bluetooth), m_state(true) { } bool LiaisonBluetooth::paquet_en_attente() { + char c; while (m_bluetooth->readable() && m_state) { - if (m_bluetooth->getc() == MARQUEUR_DEBUT_TRAME) { + c = m_bluetooth->getc(); + if (c == MARQUEUR_DEBUT_TRAME) { return true; } + } return false; @@ -26,37 +29,51 @@ PaquetDomotique *LiaisonBluetooth::lire() { char identifiant = 0; + + wait_ms(10); + if (m_bluetooth->readable() && m_state) { identifiant = m_bluetooth->getc(); - } else return NULL; + } else { return NULL; } - char buffer_longueur_paquet[8]; - for (int i = 0 ; i < 8 ; i++) { + char buffer_longueur_paquet[4]; + for (int i = 0 ; i < 4 ; i++) { if (m_bluetooth->readable() && m_state) { buffer_longueur_paquet[i] = m_bluetooth->getc(); - } else return NULL; + } else { return NULL; } } - int longueur_paquet = convertir_4char_en_int(buffer_longueur_paquet); - + unsigned short longueur_paquet = atoi(buffer_longueur_paquet); + + m_bluetooth->printf("longueur %d\n", longueur_paquet); + + wait_ms(10); + char buffer_data[longueur_paquet]; int index = 0; - while (index < longueur_paquet-1) { + while (index < longueur_paquet) { if (m_bluetooth->readable() && m_state) { char c = m_bluetooth->getc(); - - if (c == MARQUEUR_FIN_TRAME) + + m_bluetooth->printf("%x\n", c); + + if (c == MARQUEUR_FIN_TRAME) { return NULL; + } buffer_data[index++] = c; - } else return NULL; + } else { m_bluetooth->printf("buffer_data\n"); return NULL; } } - return creer_paquetdomotique(identifiant, buffer_data); + return creer_paquetdomotique(identifiant, longueur_paquet, buffer_data); } void LiaisonBluetooth::envoyer(char idenfitiant, int longueur_data, char *data) { if (m_state && m_bluetooth->writeable()) { + m_bluetooth->putc(MARQUEUR_DEBUT_TRAME); + } + + if (m_state && m_bluetooth->writeable()) { m_bluetooth->putc(idenfitiant); } @@ -72,16 +89,23 @@ m_bluetooth->putc(data[i]); } } + + if (m_state && m_bluetooth->writeable()) { + m_bluetooth->putc(MARQUEUR_FIN_TRAME); + } } -PaquetDomotique *creer_paquetdomotique(int identifiant, char *data) +PaquetDomotique *creer_paquetdomotique(int identifiant, unsigned int longueur_data, char *data) { PaquetDomotique *paquet = (PaquetDomotique*)malloc(sizeof(PaquetDomotique)); paquet->identifiant = identifiant; - paquet->data = (char*)malloc(sizeof(char)*strlen(data)); + paquet->data = (char*)malloc(sizeof(char)*longueur_data); + memset(paquet->data, 0, longueur_data); strcpy(paquet->data, data); + + paquet->longueur = longueur_data; return paquet; } @@ -91,7 +115,7 @@ free(paquet); } -char *convertir_int_en_4char(int integer) +char *convertir_int_en_4char(unsigned int integer) { char *data = new char[4]; @@ -102,7 +126,7 @@ return data; } -int convertir_4char_en_int(char data[4]) +unsigned 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 + return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0]; +}