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:03:52 2018 +0000
Revision:
3:46a9b9c1e1c0
Parent:
2:4fc77e5b08e9
Child:
4:642635655630
fixed error non copyable

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