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 13:04:27 2018 +0000
Revision:
8:4b5a472a8f94
Parent:
7:543d5548dbb9
Child:
9:f69c70d1bd2d
Child:
10:f5ad5e892d20
removed verifications

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 7:543d5548dbb9 11 LiaisonBluetooth::LiaisonBluetooth(Serial *bluetooth, Serial *pc) : m_bluetooth(bluetooth), m_state(true), m_pc(pc)
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 7:543d5548dbb9 37 } else { m_pc->printf("error identifiant %d\n", identifiant); 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 7:543d5548dbb9 43 } else { m_pc->printf("error buffer_longueur_paquet %d\n", i); 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 7:543d5548dbb9 47 m_pc->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 7:543d5548dbb9 57 m_pc->printf("%x\n", c);
duperoux_j 5:be0c602be047 58
duperoux_j 5:be0c602be047 59 if (c == MARQUEUR_FIN_TRAME) {
duperoux_j 7:543d5548dbb9 60 m_pc->printf("error MARQUEUR_FIN_TRAME %d\n", index);
duperoux_j 0:f39d89bfe442 61 return NULL;
duperoux_j 5:be0c602be047 62 }
duperoux_j 2:4fc77e5b08e9 63
duperoux_j 0:f39d89bfe442 64 buffer_data[index++] = c;
duperoux_j 7:543d5548dbb9 65 } else { m_pc->printf("buffer_data\n"); return NULL; }
duperoux_j 0:f39d89bfe442 66 }
duperoux_j 7:543d5548dbb9 67
duperoux_j 7:543d5548dbb9 68
duperoux_j 5:be0c602be047 69 return creer_paquetdomotique(identifiant, longueur_paquet, buffer_data);
duperoux_j 0:f39d89bfe442 70 }
duperoux_j 0:f39d89bfe442 71
duperoux_j 2:4fc77e5b08e9 72 void LiaisonBluetooth::envoyer(char idenfitiant, int longueur_data, char *data)
duperoux_j 2:4fc77e5b08e9 73 {
duperoux_j 8:4b5a472a8f94 74 m_bluetooth->putc(MARQUEUR_DEBUT_TRAME);
duperoux_j 8:4b5a472a8f94 75 m_bluetooth->putc(idenfitiant);
duperoux_j 2:4fc77e5b08e9 76
duperoux_j 6:91cf94ff46ed 77 //char *longueur_buffer = convertir_int_en_4char(longueur_data);
duperoux_j 6:91cf94ff46ed 78 char str[4];
duperoux_j 6:91cf94ff46ed 79 sprintf(str,"%d", longueur_data);
duperoux_j 0:f39d89bfe442 80 for (int i = 0 ; i < 4 ; i++) {
duperoux_j 8:4b5a472a8f94 81 m_bluetooth->putc(str[i]);
duperoux_j 0:f39d89bfe442 82 }
duperoux_j 2:4fc77e5b08e9 83
duperoux_j 0:f39d89bfe442 84 for (int i = 0 ; i < longueur_data ; i++) {
duperoux_j 8:4b5a472a8f94 85 m_bluetooth->putc(data[i]);
duperoux_j 0:f39d89bfe442 86 }
duperoux_j 5:be0c602be047 87
duperoux_j 8:4b5a472a8f94 88 m_bluetooth->putc(MARQUEUR_FIN_TRAME);
duperoux_j 0:f39d89bfe442 89 }
duperoux_j 0:f39d89bfe442 90
duperoux_j 0:f39d89bfe442 91
duperoux_j 5:be0c602be047 92 PaquetDomotique *creer_paquetdomotique(int identifiant, unsigned int longueur_data, char *data)
duperoux_j 2:4fc77e5b08e9 93 {
duperoux_j 0:f39d89bfe442 94 PaquetDomotique *paquet = (PaquetDomotique*)malloc(sizeof(PaquetDomotique));
duperoux_j 0:f39d89bfe442 95 paquet->identifiant = identifiant;
duperoux_j 2:4fc77e5b08e9 96
duperoux_j 5:be0c602be047 97 paquet->data = (char*)malloc(sizeof(char)*longueur_data);
duperoux_j 5:be0c602be047 98 memset(paquet->data, 0, longueur_data);
duperoux_j 0:f39d89bfe442 99 strcpy(paquet->data, data);
duperoux_j 5:be0c602be047 100
duperoux_j 5:be0c602be047 101 paquet->longueur = longueur_data;
duperoux_j 2:4fc77e5b08e9 102
duperoux_j 0:f39d89bfe442 103 return paquet;
duperoux_j 0:f39d89bfe442 104 }
duperoux_j 2:4fc77e5b08e9 105 void detruire_paquetdomotique(PaquetDomotique *paquet)
duperoux_j 2:4fc77e5b08e9 106 {
duperoux_j 0:f39d89bfe442 107 free(paquet->data);
duperoux_j 0:f39d89bfe442 108 free(paquet);
duperoux_j 0:f39d89bfe442 109 }
duperoux_j 0:f39d89bfe442 110
duperoux_j 5:be0c602be047 111 char *convertir_int_en_4char(unsigned int integer)
duperoux_j 2:4fc77e5b08e9 112 {
duperoux_j 3:46a9b9c1e1c0 113 char *data = new char[4];
duperoux_j 2:4fc77e5b08e9 114
duperoux_j 0:f39d89bfe442 115 data[3] = (integer>>24) & 0xFF;
duperoux_j 0:f39d89bfe442 116 data[2] = (integer>>16) & 0xFF;
duperoux_j 0:f39d89bfe442 117 data[1] = (integer>>8) & 0xFF;
duperoux_j 0:f39d89bfe442 118 data[0] = integer & 0xFF;
duperoux_j 2:4fc77e5b08e9 119
duperoux_j 0:f39d89bfe442 120 return data;
duperoux_j 0:f39d89bfe442 121 }
duperoux_j 5:be0c602be047 122 unsigned int convertir_4char_en_int(char data[4])
duperoux_j 2:4fc77e5b08e9 123 {
duperoux_j 5:be0c602be047 124 return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
duperoux_j 5:be0c602be047 125 }