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

Fork of liaison_Bluetooth by j d

Committer:
duperoux_j
Date:
Fri Apr 13 12:42:43 2018 +0000
Revision:
6:91cf94ff46ed
Parent:
5:be0c602be047
Child:
7:543d5548dbb9
fix function envoyer

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 5:be0c602be047 11 LiaisonBluetooth::LiaisonBluetooth(Serial *bluetooth) : m_bluetooth(bluetooth), m_state(true)
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 5:be0c602be047 17 char c;
duperoux_j 3:46a9b9c1e1c0 18 while (m_bluetooth->readable() && m_state) {
duperoux_j 5:be0c602be047 19 c = m_bluetooth->getc();
duperoux_j 5:be0c602be047 20 if (c == MARQUEUR_DEBUT_TRAME) {
duperoux_j 0:f39d89bfe442 21 return true;
duperoux_j 0:f39d89bfe442 22 }
duperoux_j 5:be0c602be047 23
duperoux_j 0:f39d89bfe442 24 }
duperoux_j 0:f39d89bfe442 25
duperoux_j 0:f39d89bfe442 26 return false;
duperoux_j 0:f39d89bfe442 27 }
duperoux_j 0:f39d89bfe442 28
duperoux_j 2:4fc77e5b08e9 29 PaquetDomotique *LiaisonBluetooth::lire()
duperoux_j 2:4fc77e5b08e9 30 {
duperoux_j 0:f39d89bfe442 31 char identifiant = 0;
duperoux_j 5:be0c602be047 32
duperoux_j 5:be0c602be047 33 wait_ms(10);
duperoux_j 5:be0c602be047 34
duperoux_j 3:46a9b9c1e1c0 35 if (m_bluetooth->readable() && m_state) {
duperoux_j 3:46a9b9c1e1c0 36 identifiant = m_bluetooth->getc();
duperoux_j 5:be0c602be047 37 } else { return NULL; }
duperoux_j 2:4fc77e5b08e9 38
duperoux_j 5:be0c602be047 39 char buffer_longueur_paquet[4];
duperoux_j 5:be0c602be047 40 for (int i = 0 ; i < 4 ; i++) {
duperoux_j 3:46a9b9c1e1c0 41 if (m_bluetooth->readable() && m_state) {
duperoux_j 3:46a9b9c1e1c0 42 buffer_longueur_paquet[i] = m_bluetooth->getc();
duperoux_j 5:be0c602be047 43 } else { return NULL; }
duperoux_j 0:f39d89bfe442 44 }
duperoux_j 5:be0c602be047 45 unsigned short longueur_paquet = atoi(buffer_longueur_paquet);
duperoux_j 5:be0c602be047 46
duperoux_j 5:be0c602be047 47 m_bluetooth->printf("longueur %d\n", longueur_paquet);
duperoux_j 5:be0c602be047 48
duperoux_j 5:be0c602be047 49 wait_ms(10);
duperoux_j 5:be0c602be047 50
duperoux_j 0:f39d89bfe442 51 char buffer_data[longueur_paquet];
duperoux_j 0:f39d89bfe442 52 int index = 0;
duperoux_j 5:be0c602be047 53 while (index < longueur_paquet) {
duperoux_j 3:46a9b9c1e1c0 54 if (m_bluetooth->readable() && m_state) {
duperoux_j 3:46a9b9c1e1c0 55 char c = m_bluetooth->getc();
duperoux_j 5:be0c602be047 56
duperoux_j 5:be0c602be047 57 m_bluetooth->printf("%x\n", c);
duperoux_j 5:be0c602be047 58
duperoux_j 5:be0c602be047 59 if (c == MARQUEUR_FIN_TRAME) {
duperoux_j 0:f39d89bfe442 60 return NULL;
duperoux_j 5:be0c602be047 61 }
duperoux_j 2:4fc77e5b08e9 62
duperoux_j 0:f39d89bfe442 63 buffer_data[index++] = c;
duperoux_j 5:be0c602be047 64 } else { m_bluetooth->printf("buffer_data\n"); return NULL; }
duperoux_j 0:f39d89bfe442 65 }
duperoux_j 2:4fc77e5b08e9 66
duperoux_j 5:be0c602be047 67 return creer_paquetdomotique(identifiant, longueur_paquet, buffer_data);
duperoux_j 0:f39d89bfe442 68 }
duperoux_j 0:f39d89bfe442 69
duperoux_j 2:4fc77e5b08e9 70 void LiaisonBluetooth::envoyer(char idenfitiant, int longueur_data, char *data)
duperoux_j 2:4fc77e5b08e9 71 {
duperoux_j 3:46a9b9c1e1c0 72 if (m_state && m_bluetooth->writeable()) {
duperoux_j 5:be0c602be047 73 m_bluetooth->putc(MARQUEUR_DEBUT_TRAME);
duperoux_j 5:be0c602be047 74 }
duperoux_j 5:be0c602be047 75
duperoux_j 5:be0c602be047 76 if (m_state && m_bluetooth->writeable()) {
duperoux_j 3:46a9b9c1e1c0 77 m_bluetooth->putc(idenfitiant);
duperoux_j 0:f39d89bfe442 78 }
duperoux_j 2:4fc77e5b08e9 79
duperoux_j 6:91cf94ff46ed 80 //char *longueur_buffer = convertir_int_en_4char(longueur_data);
duperoux_j 6:91cf94ff46ed 81 char str[4];
duperoux_j 6:91cf94ff46ed 82 sprintf(str,"%d", longueur_data);
duperoux_j 0:f39d89bfe442 83 for (int i = 0 ; i < 4 ; i++) {
duperoux_j 3:46a9b9c1e1c0 84 if (m_state && m_bluetooth->writeable()) {
duperoux_j 6:91cf94ff46ed 85 m_bluetooth->putc(str[i]);
duperoux_j 0:f39d89bfe442 86 }
duperoux_j 0:f39d89bfe442 87 }
duperoux_j 2:4fc77e5b08e9 88
duperoux_j 0:f39d89bfe442 89 for (int i = 0 ; i < longueur_data ; i++) {
duperoux_j 3:46a9b9c1e1c0 90 if (m_state && m_bluetooth->writeable()) {
duperoux_j 3:46a9b9c1e1c0 91 m_bluetooth->putc(data[i]);
duperoux_j 0:f39d89bfe442 92 }
duperoux_j 0:f39d89bfe442 93 }
duperoux_j 5:be0c602be047 94
duperoux_j 5:be0c602be047 95 if (m_state && m_bluetooth->writeable()) {
duperoux_j 5:be0c602be047 96 m_bluetooth->putc(MARQUEUR_FIN_TRAME);
duperoux_j 5:be0c602be047 97 }
duperoux_j 0:f39d89bfe442 98 }
duperoux_j 0:f39d89bfe442 99
duperoux_j 0:f39d89bfe442 100
duperoux_j 5:be0c602be047 101 PaquetDomotique *creer_paquetdomotique(int identifiant, unsigned int longueur_data, char *data)
duperoux_j 2:4fc77e5b08e9 102 {
duperoux_j 0:f39d89bfe442 103 PaquetDomotique *paquet = (PaquetDomotique*)malloc(sizeof(PaquetDomotique));
duperoux_j 0:f39d89bfe442 104 paquet->identifiant = identifiant;
duperoux_j 2:4fc77e5b08e9 105
duperoux_j 5:be0c602be047 106 paquet->data = (char*)malloc(sizeof(char)*longueur_data);
duperoux_j 5:be0c602be047 107 memset(paquet->data, 0, longueur_data);
duperoux_j 0:f39d89bfe442 108 strcpy(paquet->data, data);
duperoux_j 5:be0c602be047 109
duperoux_j 5:be0c602be047 110 paquet->longueur = longueur_data;
duperoux_j 2:4fc77e5b08e9 111
duperoux_j 0:f39d89bfe442 112 return paquet;
duperoux_j 0:f39d89bfe442 113 }
duperoux_j 2:4fc77e5b08e9 114 void detruire_paquetdomotique(PaquetDomotique *paquet)
duperoux_j 2:4fc77e5b08e9 115 {
duperoux_j 0:f39d89bfe442 116 free(paquet->data);
duperoux_j 0:f39d89bfe442 117 free(paquet);
duperoux_j 0:f39d89bfe442 118 }
duperoux_j 0:f39d89bfe442 119
duperoux_j 5:be0c602be047 120 char *convertir_int_en_4char(unsigned int integer)
duperoux_j 2:4fc77e5b08e9 121 {
duperoux_j 3:46a9b9c1e1c0 122 char *data = new char[4];
duperoux_j 2:4fc77e5b08e9 123
duperoux_j 0:f39d89bfe442 124 data[3] = (integer>>24) & 0xFF;
duperoux_j 0:f39d89bfe442 125 data[2] = (integer>>16) & 0xFF;
duperoux_j 0:f39d89bfe442 126 data[1] = (integer>>8) & 0xFF;
duperoux_j 0:f39d89bfe442 127 data[0] = integer & 0xFF;
duperoux_j 2:4fc77e5b08e9 128
duperoux_j 0:f39d89bfe442 129 return data;
duperoux_j 0:f39d89bfe442 130 }
duperoux_j 5:be0c602be047 131 unsigned int convertir_4char_en_int(char data[4])
duperoux_j 2:4fc77e5b08e9 132 {
duperoux_j 5:be0c602be047 133 return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
duperoux_j 5:be0c602be047 134 }