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

Fork of liaison_Bluetooth by j d

Committer:
duperoux_j
Date:
Wed Apr 11 12:59:45 2018 +0000
Revision:
2:4fc77e5b08e9
Parent:
1:ea044def4e89
Child:
3:46a9b9c1e1c0
added example;

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 LiaisonBluetooth liaison(bluetooth, bluetooth_state);
duperoux_j 2:4fc77e5b08e9 5
duperoux_j 2:4fc77e5b08e9 6 if (action_robot) {
duperoux_j 2:4fc77e5b08e9 7 char *score_char_array = convertir_int_en_4char(score_a_ajouter);
duperoux_j 2:4fc77e5b08e9 8 paquet_domotique_envoyer_ajouterscore(bluetooth, score_char_array);
duperoux_j 2:4fc77e5b08e9 9 }
duperoux_j 2:4fc77e5b08e9 10 if (action_robot1) {
duperoux_j 2:4fc77e5b08e9 11 char *score_char_array = convertir_int_en_4char(score_total);
duperoux_j 2:4fc77e5b08e9 12 paquet_domotique_envoyer_rafraichirscore(bluetooth, score_char_array);
duperoux_j 2:4fc77e5b08e9 13 }
duperoux_j 2:4fc77e5b08e9 14 if (action_robot_2) {
duperoux_j 2:4fc77e5b08e9 15 char *score_char_array = convertir_int_en_4char(score_total);
duperoux_j 2:4fc77e5b08e9 16 paquet_domotique_envoyer_finmatch(bluetooth, score_char_array);
duperoux_j 2:4fc77e5b08e9 17 }
duperoux_j 2:4fc77e5b08e9 18 */
duperoux_j 2:4fc77e5b08e9 19
duperoux_j 2:4fc77e5b08e9 20
duperoux_j 2:4fc77e5b08e9 21 LiaisonBluetooth::LiaisonBluetooth(Serial bluetooth, DigitalIn state) : m_bluetooth(bluetooth), m_state(state)
duperoux_j 2:4fc77e5b08e9 22 {
duperoux_j 0:f39d89bfe442 23 }
duperoux_j 0:f39d89bfe442 24
duperoux_j 2:4fc77e5b08e9 25 bool LiaisonBluetooth::paquet_en_attente()
duperoux_j 2:4fc77e5b08e9 26 {
duperoux_j 0:f39d89bfe442 27 while (m_bluetooth.readable() && m_state) {
duperoux_j 0:f39d89bfe442 28 if (m_bluetooth.getc() == MARQUEUR_DEBUT_TRAME) {
duperoux_j 0:f39d89bfe442 29 return true;
duperoux_j 0:f39d89bfe442 30 }
duperoux_j 0:f39d89bfe442 31 }
duperoux_j 0:f39d89bfe442 32
duperoux_j 0:f39d89bfe442 33 return false;
duperoux_j 0:f39d89bfe442 34 }
duperoux_j 0:f39d89bfe442 35
duperoux_j 2:4fc77e5b08e9 36 PaquetDomotique *LiaisonBluetooth::lire()
duperoux_j 2:4fc77e5b08e9 37 {
duperoux_j 0:f39d89bfe442 38 char identifiant = 0;
duperoux_j 0:f39d89bfe442 39 if (m_bluetooth.readable() && m_state) {
duperoux_j 0:f39d89bfe442 40 identifiant = m_bluetooth.getc();
duperoux_j 0:f39d89bfe442 41 } else return NULL;
duperoux_j 2:4fc77e5b08e9 42
duperoux_j 0:f39d89bfe442 43 char buffer_longueur_paquet[8];
duperoux_j 0:f39d89bfe442 44 for (int i = 0 ; i < 8 ; i++) {
duperoux_j 0:f39d89bfe442 45 if (m_bluetooth.readable() && m_state) {
duperoux_j 0:f39d89bfe442 46 buffer_longueur_paquet[i] = m_bluetooth.getc();
duperoux_j 0:f39d89bfe442 47 } else return NULL;
duperoux_j 0:f39d89bfe442 48 }
duperoux_j 0:f39d89bfe442 49 int longueur_paquet = convertir_4char_en_int(buffer_longueur_paquet);
duperoux_j 2:4fc77e5b08e9 50
duperoux_j 0:f39d89bfe442 51 char buffer_data[longueur_paquet];
duperoux_j 0:f39d89bfe442 52 int index = 0;
duperoux_j 0:f39d89bfe442 53 while (index < longueur_paquet-1) {
duperoux_j 0:f39d89bfe442 54 if (m_bluetooth.readable() && m_state) {
duperoux_j 0:f39d89bfe442 55 char c = m_bluetooth.getc();
duperoux_j 2:4fc77e5b08e9 56
duperoux_j 0:f39d89bfe442 57 if (c == MARQUEUR_FIN_TRAME)
duperoux_j 0:f39d89bfe442 58 return NULL;
duperoux_j 2:4fc77e5b08e9 59
duperoux_j 0:f39d89bfe442 60 buffer_data[index++] = c;
duperoux_j 0:f39d89bfe442 61 } else return NULL;
duperoux_j 0:f39d89bfe442 62 }
duperoux_j 2:4fc77e5b08e9 63
duperoux_j 0:f39d89bfe442 64 return creer_paquetdomotique(identifiant, buffer_data);
duperoux_j 0:f39d89bfe442 65 }
duperoux_j 0:f39d89bfe442 66
duperoux_j 2:4fc77e5b08e9 67 void LiaisonBluetooth::envoyer(char idenfitiant, int longueur_data, char *data)
duperoux_j 2:4fc77e5b08e9 68 {
duperoux_j 0:f39d89bfe442 69 if (m_state && m_bluetooth.writeable()) {
duperoux_j 2:4fc77e5b08e9 70 m_bluetooth.putc(idenfitiant);
duperoux_j 0:f39d89bfe442 71 }
duperoux_j 2:4fc77e5b08e9 72
duperoux_j 0:f39d89bfe442 73 char *longueur_buffer = convertir_int_en_4char(longueur_data);
duperoux_j 0:f39d89bfe442 74 for (int i = 0 ; i < 4 ; i++) {
duperoux_j 0:f39d89bfe442 75 if (m_state && m_bluetooth.writeable()) {
duperoux_j 2:4fc77e5b08e9 76 m_bluetooth.putc(longueur_buffer[i]);
duperoux_j 0:f39d89bfe442 77 }
duperoux_j 0:f39d89bfe442 78 }
duperoux_j 2:4fc77e5b08e9 79
duperoux_j 0:f39d89bfe442 80 for (int i = 0 ; i < longueur_data ; i++) {
duperoux_j 0:f39d89bfe442 81 if (m_state && m_bluetooth.writeable()) {
duperoux_j 2:4fc77e5b08e9 82 m_bluetooth.putc(data[i]);
duperoux_j 0:f39d89bfe442 83 }
duperoux_j 0:f39d89bfe442 84 }
duperoux_j 0:f39d89bfe442 85 }
duperoux_j 0:f39d89bfe442 86
duperoux_j 0:f39d89bfe442 87
duperoux_j 2:4fc77e5b08e9 88 PaquetDomotique *creer_paquetdomotique(int identifiant, char *data)
duperoux_j 2:4fc77e5b08e9 89 {
duperoux_j 0:f39d89bfe442 90 PaquetDomotique *paquet = (PaquetDomotique*)malloc(sizeof(PaquetDomotique));
duperoux_j 0:f39d89bfe442 91 paquet->identifiant = identifiant;
duperoux_j 2:4fc77e5b08e9 92
duperoux_j 0:f39d89bfe442 93 paquet->data = (char*)malloc(sizeof(char)*strlen(data));
duperoux_j 0:f39d89bfe442 94 strcpy(paquet->data, data);
duperoux_j 2:4fc77e5b08e9 95
duperoux_j 0:f39d89bfe442 96 return paquet;
duperoux_j 0:f39d89bfe442 97 }
duperoux_j 2:4fc77e5b08e9 98 void detruire_paquetdomotique(PaquetDomotique *paquet)
duperoux_j 2:4fc77e5b08e9 99 {
duperoux_j 0:f39d89bfe442 100 free(paquet->data);
duperoux_j 0:f39d89bfe442 101 free(paquet);
duperoux_j 0:f39d89bfe442 102 }
duperoux_j 0:f39d89bfe442 103
duperoux_j 2:4fc77e5b08e9 104 char *convertir_int_en_4char(int integer)
duperoux_j 2:4fc77e5b08e9 105 {
duperoux_j 0:f39d89bfe442 106 char data[4];
duperoux_j 2:4fc77e5b08e9 107
duperoux_j 0:f39d89bfe442 108 data[3] = (integer>>24) & 0xFF;
duperoux_j 0:f39d89bfe442 109 data[2] = (integer>>16) & 0xFF;
duperoux_j 0:f39d89bfe442 110 data[1] = (integer>>8) & 0xFF;
duperoux_j 0:f39d89bfe442 111 data[0] = integer & 0xFF;
duperoux_j 2:4fc77e5b08e9 112
duperoux_j 0:f39d89bfe442 113 return data;
duperoux_j 0:f39d89bfe442 114 }
duperoux_j 2:4fc77e5b08e9 115 int convertir_4char_en_int(char data[4])
duperoux_j 2:4fc77e5b08e9 116 {
duperoux_j 0:f39d89bfe442 117 return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
duperoux_j 0:f39d89bfe442 118 }