liaison bluetooth entre gros robot -> panneau ou gros robot -> petit robot

Fork of liaison_Bluetooth by j d

Committer:
duperoux_j
Date:
Wed Apr 11 15:10:53 2018 +0000
Revision:
4:642635655630
Parent:
3:46a9b9c1e1c0
Child:
5:be0c602be047
fixed memory leak in example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
duperoux_j 1:ea044def4e89 1 #include "LiaisonBluetooth/LiaisonBluetooth.h"
duperoux_j 0:f39d89bfe442 2
duperoux_j 2:4fc77e5b08e9 3 /*
duperoux_j 2:4fc77e5b08e9 4 if (action_robot_2) {
duperoux_j 2:4fc77e5b08e9 5 char *score_char_array = convertir_int_en_4char(score_total);
duperoux_j 3:46a9b9c1e1c0 6 paquet_domotique_envoyer(0x30, 4, score_char_array);
duperoux_j 4:642635655630 7 delete []score_char_array;
duperoux_j 2:4fc77e5b08e9 8 }
duperoux_j 2:4fc77e5b08e9 9 */
duperoux_j 2:4fc77e5b08e9 10
duperoux_j 3:46a9b9c1e1c0 11 LiaisonBluetooth::LiaisonBluetooth(Serial *bluetooth, DigitalIn *state) : m_bluetooth(bluetooth), m_state(state)
duperoux_j 2:4fc77e5b08e9 12 {
duperoux_j 0:f39d89bfe442 13 }
duperoux_j 0:f39d89bfe442 14
duperoux_j 2:4fc77e5b08e9 15 bool LiaisonBluetooth::paquet_en_attente()
duperoux_j 2:4fc77e5b08e9 16 {
duperoux_j 3:46a9b9c1e1c0 17 while (m_bluetooth->readable() && m_state) {
duperoux_j 3:46a9b9c1e1c0 18 if (m_bluetooth->getc() == MARQUEUR_DEBUT_TRAME) {
duperoux_j 0:f39d89bfe442 19 return true;
duperoux_j 0:f39d89bfe442 20 }
duperoux_j 0:f39d89bfe442 21 }
duperoux_j 0:f39d89bfe442 22
duperoux_j 0:f39d89bfe442 23 return false;
duperoux_j 0:f39d89bfe442 24 }
duperoux_j 0:f39d89bfe442 25
duperoux_j 2:4fc77e5b08e9 26 PaquetDomotique *LiaisonBluetooth::lire()
duperoux_j 2:4fc77e5b08e9 27 {
duperoux_j 0:f39d89bfe442 28 char identifiant = 0;
duperoux_j 3:46a9b9c1e1c0 29 if (m_bluetooth->readable() && m_state) {
duperoux_j 3:46a9b9c1e1c0 30 identifiant = m_bluetooth->getc();
duperoux_j 0:f39d89bfe442 31 } else return NULL;
duperoux_j 2:4fc77e5b08e9 32
duperoux_j 0:f39d89bfe442 33 char buffer_longueur_paquet[8];
duperoux_j 0:f39d89bfe442 34 for (int i = 0 ; i < 8 ; i++) {
duperoux_j 3:46a9b9c1e1c0 35 if (m_bluetooth->readable() && m_state) {
duperoux_j 3:46a9b9c1e1c0 36 buffer_longueur_paquet[i] = m_bluetooth->getc();
duperoux_j 0:f39d89bfe442 37 } else return NULL;
duperoux_j 0:f39d89bfe442 38 }
duperoux_j 0:f39d89bfe442 39 int longueur_paquet = convertir_4char_en_int(buffer_longueur_paquet);
duperoux_j 2:4fc77e5b08e9 40
duperoux_j 0:f39d89bfe442 41 char buffer_data[longueur_paquet];
duperoux_j 0:f39d89bfe442 42 int index = 0;
duperoux_j 0:f39d89bfe442 43 while (index < longueur_paquet-1) {
duperoux_j 3:46a9b9c1e1c0 44 if (m_bluetooth->readable() && m_state) {
duperoux_j 3:46a9b9c1e1c0 45 char c = m_bluetooth->getc();
duperoux_j 2:4fc77e5b08e9 46
duperoux_j 0:f39d89bfe442 47 if (c == MARQUEUR_FIN_TRAME)
duperoux_j 0:f39d89bfe442 48 return NULL;
duperoux_j 2:4fc77e5b08e9 49
duperoux_j 0:f39d89bfe442 50 buffer_data[index++] = c;
duperoux_j 0:f39d89bfe442 51 } else return NULL;
duperoux_j 0:f39d89bfe442 52 }
duperoux_j 2:4fc77e5b08e9 53
duperoux_j 0:f39d89bfe442 54 return creer_paquetdomotique(identifiant, buffer_data);
duperoux_j 0:f39d89bfe442 55 }
duperoux_j 0:f39d89bfe442 56
duperoux_j 2:4fc77e5b08e9 57 void LiaisonBluetooth::envoyer(char idenfitiant, int longueur_data, char *data)
duperoux_j 2:4fc77e5b08e9 58 {
duperoux_j 3:46a9b9c1e1c0 59 if (m_state && m_bluetooth->writeable()) {
duperoux_j 3:46a9b9c1e1c0 60 m_bluetooth->putc(idenfitiant);
duperoux_j 0:f39d89bfe442 61 }
duperoux_j 2:4fc77e5b08e9 62
duperoux_j 0:f39d89bfe442 63 char *longueur_buffer = convertir_int_en_4char(longueur_data);
duperoux_j 0:f39d89bfe442 64 for (int i = 0 ; i < 4 ; i++) {
duperoux_j 3:46a9b9c1e1c0 65 if (m_state && m_bluetooth->writeable()) {
duperoux_j 3:46a9b9c1e1c0 66 m_bluetooth->putc(longueur_buffer[i]);
duperoux_j 0:f39d89bfe442 67 }
duperoux_j 0:f39d89bfe442 68 }
duperoux_j 2:4fc77e5b08e9 69
duperoux_j 0:f39d89bfe442 70 for (int i = 0 ; i < longueur_data ; i++) {
duperoux_j 3:46a9b9c1e1c0 71 if (m_state && m_bluetooth->writeable()) {
duperoux_j 3:46a9b9c1e1c0 72 m_bluetooth->putc(data[i]);
duperoux_j 0:f39d89bfe442 73 }
duperoux_j 0:f39d89bfe442 74 }
duperoux_j 0:f39d89bfe442 75 }
duperoux_j 0:f39d89bfe442 76
duperoux_j 0:f39d89bfe442 77
duperoux_j 2:4fc77e5b08e9 78 PaquetDomotique *creer_paquetdomotique(int identifiant, char *data)
duperoux_j 2:4fc77e5b08e9 79 {
duperoux_j 0:f39d89bfe442 80 PaquetDomotique *paquet = (PaquetDomotique*)malloc(sizeof(PaquetDomotique));
duperoux_j 0:f39d89bfe442 81 paquet->identifiant = identifiant;
duperoux_j 2:4fc77e5b08e9 82
duperoux_j 0:f39d89bfe442 83 paquet->data = (char*)malloc(sizeof(char)*strlen(data));
duperoux_j 0:f39d89bfe442 84 strcpy(paquet->data, data);
duperoux_j 2:4fc77e5b08e9 85
duperoux_j 0:f39d89bfe442 86 return paquet;
duperoux_j 0:f39d89bfe442 87 }
duperoux_j 2:4fc77e5b08e9 88 void detruire_paquetdomotique(PaquetDomotique *paquet)
duperoux_j 2:4fc77e5b08e9 89 {
duperoux_j 0:f39d89bfe442 90 free(paquet->data);
duperoux_j 0:f39d89bfe442 91 free(paquet);
duperoux_j 0:f39d89bfe442 92 }
duperoux_j 0:f39d89bfe442 93
duperoux_j 2:4fc77e5b08e9 94 char *convertir_int_en_4char(int integer)
duperoux_j 2:4fc77e5b08e9 95 {
duperoux_j 3:46a9b9c1e1c0 96 char *data = new char[4];
duperoux_j 2:4fc77e5b08e9 97
duperoux_j 0:f39d89bfe442 98 data[3] = (integer>>24) & 0xFF;
duperoux_j 0:f39d89bfe442 99 data[2] = (integer>>16) & 0xFF;
duperoux_j 0:f39d89bfe442 100 data[1] = (integer>>8) & 0xFF;
duperoux_j 0:f39d89bfe442 101 data[0] = integer & 0xFF;
duperoux_j 2:4fc77e5b08e9 102
duperoux_j 0:f39d89bfe442 103 return data;
duperoux_j 0:f39d89bfe442 104 }
duperoux_j 2:4fc77e5b08e9 105 int convertir_4char_en_int(char data[4])
duperoux_j 2:4fc77e5b08e9 106 {
duperoux_j 0:f39d89bfe442 107 return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
duperoux_j 0:f39d89bfe442 108 }