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

Fork of liaison_Bluetooth by j d

Committer:
Sitkah
Date:
Wed May 09 19:36:00 2018 +0000
Revision:
16:1c3ff1ff1bc0
Parent:
15:7d159715eb14
pas en tenir compte;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sitkah 16:1c3ff1ff1bc0 1 #include "global.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 15:7d159715eb14 11 LiaisonBluetooth::LiaisonBluetooth(Serial *bluetooth, Serial *pc) : m_bluetooth(bluetooth), m_pc(pc), 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 0:f39d89bfe442 23 }
duperoux_j 0:f39d89bfe442 24
duperoux_j 0:f39d89bfe442 25 return false;
duperoux_j 0:f39d89bfe442 26 }
duperoux_j 0:f39d89bfe442 27
duperoux_j 2:4fc77e5b08e9 28 PaquetDomotique *LiaisonBluetooth::lire()
duperoux_j 2:4fc77e5b08e9 29 {
duperoux_j 0:f39d89bfe442 30 char identifiant = 0;
duperoux_j 5:be0c602be047 31
duperoux_j 5:be0c602be047 32 wait_ms(10);
duperoux_j 5:be0c602be047 33
duperoux_j 3:46a9b9c1e1c0 34 if (m_bluetooth->readable() && m_state) {
duperoux_j 3:46a9b9c1e1c0 35 identifiant = m_bluetooth->getc();
duperoux_j 7:543d5548dbb9 36 } else { m_pc->printf("error identifiant %d\n", identifiant); return NULL; }
duperoux_j 2:4fc77e5b08e9 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 3:46a9b9c1e1c0 40 if (m_bluetooth->readable() && m_state) {
duperoux_j 3:46a9b9c1e1c0 41 buffer_longueur_paquet[i] = m_bluetooth->getc();
duperoux_j 7:543d5548dbb9 42 } else { m_pc->printf("error buffer_longueur_paquet %d\n", i); return NULL; }
duperoux_j 0:f39d89bfe442 43 }
duperoux_j 5:be0c602be047 44 unsigned short longueur_paquet = atoi(buffer_longueur_paquet);
duperoux_j 5:be0c602be047 45
duperoux_j 13:20c2439438dd 46 //m_pc->printf("longueur %d\n", longueur_paquet);
duperoux_j 5:be0c602be047 47
duperoux_j 5:be0c602be047 48 wait_ms(10);
duperoux_j 5:be0c602be047 49
duperoux_j 0:f39d89bfe442 50 char buffer_data[longueur_paquet];
duperoux_j 0:f39d89bfe442 51 int index = 0;
duperoux_j 5:be0c602be047 52 while (index < longueur_paquet) {
duperoux_j 3:46a9b9c1e1c0 53 if (m_bluetooth->readable() && m_state) {
duperoux_j 3:46a9b9c1e1c0 54 char c = m_bluetooth->getc();
duperoux_j 5:be0c602be047 55
duperoux_j 13:20c2439438dd 56 //m_pc->printf("%x\n", c);
duperoux_j 5:be0c602be047 57
duperoux_j 5:be0c602be047 58 if (c == MARQUEUR_FIN_TRAME) {
duperoux_j 7:543d5548dbb9 59 m_pc->printf("error MARQUEUR_FIN_TRAME %d\n", index);
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 7:543d5548dbb9 64 } else { m_pc->printf("buffer_data\n"); return NULL; }
duperoux_j 0:f39d89bfe442 65 }
duperoux_j 7:543d5548dbb9 66
duperoux_j 7:543d5548dbb9 67
duperoux_j 5:be0c602be047 68 return creer_paquetdomotique(identifiant, longueur_paquet, buffer_data);
duperoux_j 0:f39d89bfe442 69 }
duperoux_j 0:f39d89bfe442 70
duperoux_j 2:4fc77e5b08e9 71 void LiaisonBluetooth::envoyer(char idenfitiant, int longueur_data, char *data)
duperoux_j 2:4fc77e5b08e9 72 {
duperoux_j 8:4b5a472a8f94 73 m_bluetooth->putc(MARQUEUR_DEBUT_TRAME);
duperoux_j 8:4b5a472a8f94 74 m_bluetooth->putc(idenfitiant);
duperoux_j 2:4fc77e5b08e9 75
duperoux_j 6:91cf94ff46ed 76 //char *longueur_buffer = convertir_int_en_4char(longueur_data);
Sitkah 10:f5ad5e892d20 77 char str[4]={0,0,0,0};
duperoux_j 6:91cf94ff46ed 78 sprintf(str,"%d", longueur_data);
duperoux_j 0:f39d89bfe442 79 for (int i = 0 ; i < 4 ; i++) {
duperoux_j 8:4b5a472a8f94 80 m_bluetooth->putc(str[i]);
duperoux_j 0:f39d89bfe442 81 }
duperoux_j 2:4fc77e5b08e9 82
duperoux_j 0:f39d89bfe442 83 for (int i = 0 ; i < longueur_data ; i++) {
duperoux_j 8:4b5a472a8f94 84 m_bluetooth->putc(data[i]);
duperoux_j 0:f39d89bfe442 85 }
duperoux_j 5:be0c602be047 86
duperoux_j 8:4b5a472a8f94 87 m_bluetooth->putc(MARQUEUR_FIN_TRAME);
duperoux_j 0:f39d89bfe442 88 }
duperoux_j 0:f39d89bfe442 89
duperoux_j 15:7d159715eb14 90 void LiaisonBluetooth::envoyer_short(char identifiant, short data) {
duperoux_j 14:7118ea22e01f 91 char buffer[7];
duperoux_j 14:7118ea22e01f 92 memset(buffer, '\0', 7);
duperoux_j 14:7118ea22e01f 93 sprintf(buffer, "%hd", data);
duperoux_j 14:7118ea22e01f 94 envoyer(identifiant, 7, buffer);
duperoux_j 14:7118ea22e01f 95 }
duperoux_j 0:f39d89bfe442 96
duperoux_j 5:be0c602be047 97 PaquetDomotique *creer_paquetdomotique(int identifiant, unsigned int longueur_data, char *data)
duperoux_j 2:4fc77e5b08e9 98 {
duperoux_j 0:f39d89bfe442 99 PaquetDomotique *paquet = (PaquetDomotique*)malloc(sizeof(PaquetDomotique));
duperoux_j 0:f39d89bfe442 100 paquet->identifiant = identifiant;
duperoux_j 2:4fc77e5b08e9 101
duperoux_j 5:be0c602be047 102 paquet->data = (char*)malloc(sizeof(char)*longueur_data);
duperoux_j 5:be0c602be047 103 memset(paquet->data, 0, longueur_data);
duperoux_j 0:f39d89bfe442 104 strcpy(paquet->data, data);
duperoux_j 5:be0c602be047 105
duperoux_j 5:be0c602be047 106 paquet->longueur = longueur_data;
duperoux_j 2:4fc77e5b08e9 107
duperoux_j 0:f39d89bfe442 108 return paquet;
duperoux_j 0:f39d89bfe442 109 }
duperoux_j 2:4fc77e5b08e9 110 void detruire_paquetdomotique(PaquetDomotique *paquet)
duperoux_j 2:4fc77e5b08e9 111 {
duperoux_j 0:f39d89bfe442 112 free(paquet->data);
duperoux_j 0:f39d89bfe442 113 free(paquet);
duperoux_j 0:f39d89bfe442 114 }
duperoux_j 0:f39d89bfe442 115
duperoux_j 15:7d159715eb14 116 short convertir_score(PaquetDomotique *paquet) {
duperoux_j 14:7118ea22e01f 117 char buff[20];
duperoux_j 14:7118ea22e01f 118 memset(buff, '\0', 20);
duperoux_j 14:7118ea22e01f 119 strncpy(buff, paquet->data, paquet->longueur);
duperoux_j 14:7118ea22e01f 120 return atoi(buff);
duperoux_j 0:f39d89bfe442 121 }