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:00:25 2018 +0000
Revision:
7:543d5548dbb9
Parent:
6:91cf94ff46ed
Child:
8:4b5a472a8f94
debug test

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 3:46a9b9c1e1c0 74 if (m_state && m_bluetooth->writeable()) {
duperoux_j 5:be0c602be047 75 m_bluetooth->putc(MARQUEUR_DEBUT_TRAME);
duperoux_j 7:543d5548dbb9 76 } else { m_pc->printf("MARQUEUR_DEBUT_TRAME\n"); }
duperoux_j 5:be0c602be047 77
duperoux_j 5:be0c602be047 78 if (m_state && m_bluetooth->writeable()) {
duperoux_j 3:46a9b9c1e1c0 79 m_bluetooth->putc(idenfitiant);
duperoux_j 7:543d5548dbb9 80 } else { m_pc->printf("idenfitiant\n"); }
duperoux_j 2:4fc77e5b08e9 81
duperoux_j 6:91cf94ff46ed 82 //char *longueur_buffer = convertir_int_en_4char(longueur_data);
duperoux_j 6:91cf94ff46ed 83 char str[4];
duperoux_j 6:91cf94ff46ed 84 sprintf(str,"%d", longueur_data);
duperoux_j 0:f39d89bfe442 85 for (int i = 0 ; i < 4 ; i++) {
duperoux_j 3:46a9b9c1e1c0 86 if (m_state && m_bluetooth->writeable()) {
duperoux_j 6:91cf94ff46ed 87 m_bluetooth->putc(str[i]);
duperoux_j 7:543d5548dbb9 88 } else { m_pc->printf("longueur_data %d\n", i); }
duperoux_j 0:f39d89bfe442 89 }
duperoux_j 2:4fc77e5b08e9 90
duperoux_j 0:f39d89bfe442 91 for (int i = 0 ; i < longueur_data ; i++) {
duperoux_j 3:46a9b9c1e1c0 92 if (m_state && m_bluetooth->writeable()) {
duperoux_j 3:46a9b9c1e1c0 93 m_bluetooth->putc(data[i]);
duperoux_j 7:543d5548dbb9 94 } else { m_pc->printf("data %d\n", i); }
duperoux_j 0:f39d89bfe442 95 }
duperoux_j 5:be0c602be047 96
duperoux_j 5:be0c602be047 97 if (m_state && m_bluetooth->writeable()) {
duperoux_j 5:be0c602be047 98 m_bluetooth->putc(MARQUEUR_FIN_TRAME);
duperoux_j 7:543d5548dbb9 99 } else { m_pc->printf("MARQUEUR_FIN_TRAME\n"); }
duperoux_j 0:f39d89bfe442 100 }
duperoux_j 0:f39d89bfe442 101
duperoux_j 0:f39d89bfe442 102
duperoux_j 5:be0c602be047 103 PaquetDomotique *creer_paquetdomotique(int identifiant, unsigned int longueur_data, char *data)
duperoux_j 2:4fc77e5b08e9 104 {
duperoux_j 0:f39d89bfe442 105 PaquetDomotique *paquet = (PaquetDomotique*)malloc(sizeof(PaquetDomotique));
duperoux_j 0:f39d89bfe442 106 paquet->identifiant = identifiant;
duperoux_j 2:4fc77e5b08e9 107
duperoux_j 5:be0c602be047 108 paquet->data = (char*)malloc(sizeof(char)*longueur_data);
duperoux_j 5:be0c602be047 109 memset(paquet->data, 0, longueur_data);
duperoux_j 0:f39d89bfe442 110 strcpy(paquet->data, data);
duperoux_j 5:be0c602be047 111
duperoux_j 5:be0c602be047 112 paquet->longueur = longueur_data;
duperoux_j 2:4fc77e5b08e9 113
duperoux_j 0:f39d89bfe442 114 return paquet;
duperoux_j 0:f39d89bfe442 115 }
duperoux_j 2:4fc77e5b08e9 116 void detruire_paquetdomotique(PaquetDomotique *paquet)
duperoux_j 2:4fc77e5b08e9 117 {
duperoux_j 0:f39d89bfe442 118 free(paquet->data);
duperoux_j 0:f39d89bfe442 119 free(paquet);
duperoux_j 0:f39d89bfe442 120 }
duperoux_j 0:f39d89bfe442 121
duperoux_j 5:be0c602be047 122 char *convertir_int_en_4char(unsigned int integer)
duperoux_j 2:4fc77e5b08e9 123 {
duperoux_j 3:46a9b9c1e1c0 124 char *data = new char[4];
duperoux_j 2:4fc77e5b08e9 125
duperoux_j 0:f39d89bfe442 126 data[3] = (integer>>24) & 0xFF;
duperoux_j 0:f39d89bfe442 127 data[2] = (integer>>16) & 0xFF;
duperoux_j 0:f39d89bfe442 128 data[1] = (integer>>8) & 0xFF;
duperoux_j 0:f39d89bfe442 129 data[0] = integer & 0xFF;
duperoux_j 2:4fc77e5b08e9 130
duperoux_j 0:f39d89bfe442 131 return data;
duperoux_j 0:f39d89bfe442 132 }
duperoux_j 5:be0c602be047 133 unsigned int convertir_4char_en_int(char data[4])
duperoux_j 2:4fc77e5b08e9 134 {
duperoux_j 5:be0c602be047 135 return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
duperoux_j 5:be0c602be047 136 }