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:33:54 2018 +0000
Revision:
1:ea044def4e89
Parent:
Liaison_Bluetooth_panneau_grosRob.cpp@0:f39d89bfe442
Child:
2:4fc77e5b08e9
refactor names;

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