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:14:50 2018 +0000
Revision:
9:f69c70d1bd2d
Parent:
8:4b5a472a8f94
fixed 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 9:f69c70d1bd2d 35 identifiant = m_bluetooth->getc();
duperoux_j 9:f69c70d1bd2d 36 m_pc->printf("identifiant %x\n", identifiant);
duperoux_j 9:f69c70d1bd2d 37
duperoux_j 5:be0c602be047 38 char buffer_longueur_paquet[4];
duperoux_j 5:be0c602be047 39 for (int i = 0 ; i < 4 ; i++) {
duperoux_j 9:f69c70d1bd2d 40 buffer_longueur_paquet[i] = m_bluetooth->getc();
duperoux_j 0:f39d89bfe442 41 }
duperoux_j 5:be0c602be047 42 unsigned short longueur_paquet = atoi(buffer_longueur_paquet);
duperoux_j 5:be0c602be047 43
duperoux_j 7:543d5548dbb9 44 m_pc->printf("longueur %d\n", longueur_paquet);
duperoux_j 5:be0c602be047 45
duperoux_j 5:be0c602be047 46 wait_ms(10);
duperoux_j 5:be0c602be047 47
duperoux_j 0:f39d89bfe442 48 char buffer_data[longueur_paquet];
duperoux_j 0:f39d89bfe442 49 int index = 0;
duperoux_j 5:be0c602be047 50 while (index < longueur_paquet) {
duperoux_j 9:f69c70d1bd2d 51 if (m_bluetooth->readable() && m_state) { //wtf readable() not always working ?! removed almost all
duperoux_j 3:46a9b9c1e1c0 52 char c = m_bluetooth->getc();
duperoux_j 5:be0c602be047 53
duperoux_j 7:543d5548dbb9 54 m_pc->printf("%x\n", c);
duperoux_j 5:be0c602be047 55
duperoux_j 5:be0c602be047 56 if (c == MARQUEUR_FIN_TRAME) {
duperoux_j 7:543d5548dbb9 57 m_pc->printf("error MARQUEUR_FIN_TRAME %d\n", index);
duperoux_j 0:f39d89bfe442 58 return NULL;
duperoux_j 5:be0c602be047 59 }
duperoux_j 2:4fc77e5b08e9 60
duperoux_j 0:f39d89bfe442 61 buffer_data[index++] = c;
duperoux_j 7:543d5548dbb9 62 } else { m_pc->printf("buffer_data\n"); return NULL; }
duperoux_j 0:f39d89bfe442 63 }
duperoux_j 7:543d5548dbb9 64
duperoux_j 7:543d5548dbb9 65
duperoux_j 5:be0c602be047 66 return creer_paquetdomotique(identifiant, longueur_paquet, buffer_data);
duperoux_j 0:f39d89bfe442 67 }
duperoux_j 0:f39d89bfe442 68
duperoux_j 2:4fc77e5b08e9 69 void LiaisonBluetooth::envoyer(char idenfitiant, int longueur_data, char *data)
duperoux_j 2:4fc77e5b08e9 70 {
duperoux_j 8:4b5a472a8f94 71 m_bluetooth->putc(MARQUEUR_DEBUT_TRAME);
duperoux_j 8:4b5a472a8f94 72 m_bluetooth->putc(idenfitiant);
duperoux_j 2:4fc77e5b08e9 73
duperoux_j 6:91cf94ff46ed 74 //char *longueur_buffer = convertir_int_en_4char(longueur_data);
duperoux_j 9:f69c70d1bd2d 75 char str[4] = {0, 0, 0, 0};
duperoux_j 6:91cf94ff46ed 76 sprintf(str,"%d", longueur_data);
duperoux_j 0:f39d89bfe442 77 for (int i = 0 ; i < 4 ; i++) {
duperoux_j 8:4b5a472a8f94 78 m_bluetooth->putc(str[i]);
duperoux_j 0:f39d89bfe442 79 }
duperoux_j 2:4fc77e5b08e9 80
duperoux_j 0:f39d89bfe442 81 for (int i = 0 ; i < longueur_data ; i++) {
duperoux_j 8:4b5a472a8f94 82 m_bluetooth->putc(data[i]);
duperoux_j 0:f39d89bfe442 83 }
duperoux_j 5:be0c602be047 84
duperoux_j 8:4b5a472a8f94 85 m_bluetooth->putc(MARQUEUR_FIN_TRAME);
duperoux_j 0:f39d89bfe442 86 }
duperoux_j 0:f39d89bfe442 87
duperoux_j 0:f39d89bfe442 88
duperoux_j 5:be0c602be047 89 PaquetDomotique *creer_paquetdomotique(int identifiant, unsigned int longueur_data, char *data)
duperoux_j 2:4fc77e5b08e9 90 {
duperoux_j 0:f39d89bfe442 91 PaquetDomotique *paquet = (PaquetDomotique*)malloc(sizeof(PaquetDomotique));
duperoux_j 0:f39d89bfe442 92 paquet->identifiant = identifiant;
duperoux_j 2:4fc77e5b08e9 93
duperoux_j 5:be0c602be047 94 paquet->data = (char*)malloc(sizeof(char)*longueur_data);
duperoux_j 5:be0c602be047 95 memset(paquet->data, 0, longueur_data);
duperoux_j 0:f39d89bfe442 96 strcpy(paquet->data, data);
duperoux_j 5:be0c602be047 97
duperoux_j 5:be0c602be047 98 paquet->longueur = longueur_data;
duperoux_j 2:4fc77e5b08e9 99
duperoux_j 0:f39d89bfe442 100 return paquet;
duperoux_j 0:f39d89bfe442 101 }
duperoux_j 2:4fc77e5b08e9 102 void detruire_paquetdomotique(PaquetDomotique *paquet)
duperoux_j 2:4fc77e5b08e9 103 {
duperoux_j 0:f39d89bfe442 104 free(paquet->data);
duperoux_j 0:f39d89bfe442 105 free(paquet);
duperoux_j 0:f39d89bfe442 106 }
duperoux_j 0:f39d89bfe442 107
duperoux_j 5:be0c602be047 108 char *convertir_int_en_4char(unsigned int integer)
duperoux_j 2:4fc77e5b08e9 109 {
duperoux_j 3:46a9b9c1e1c0 110 char *data = new char[4];
duperoux_j 2:4fc77e5b08e9 111
duperoux_j 0:f39d89bfe442 112 data[3] = (integer>>24) & 0xFF;
duperoux_j 0:f39d89bfe442 113 data[2] = (integer>>16) & 0xFF;
duperoux_j 0:f39d89bfe442 114 data[1] = (integer>>8) & 0xFF;
duperoux_j 0:f39d89bfe442 115 data[0] = integer & 0xFF;
duperoux_j 2:4fc77e5b08e9 116
duperoux_j 0:f39d89bfe442 117 return data;
duperoux_j 0:f39d89bfe442 118 }
duperoux_j 5:be0c602be047 119 unsigned int convertir_4char_en_int(char data[4])
duperoux_j 2:4fc77e5b08e9 120 {
duperoux_j 5:be0c602be047 121 return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
duperoux_j 5:be0c602be047 122 }