liaison bluetooth entre gros robot -> panneau ou gros robot -> petit robot
Fork of liaison_Bluetooth by
LiaisonBluetooth.cpp@5:be0c602be047, 2018-04-13 (annotated)
- Committer:
- duperoux_j
- Date:
- Fri Apr 13 07:39:01 2018 +0000
- Revision:
- 5:be0c602be047
- Parent:
- 4:642635655630
- Child:
- 6:91cf94ff46ed
fix
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:be0c602be047 | 11 | LiaisonBluetooth::LiaisonBluetooth(Serial *bluetooth) : m_bluetooth(bluetooth), 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 | 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 | 5:be0c602be047 | 37 | } else { 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 | 5:be0c602be047 | 43 | } else { 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 | 5:be0c602be047 | 47 | m_bluetooth->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 | 5:be0c602be047 | 57 | m_bluetooth->printf("%x\n", c); |
duperoux_j | 5:be0c602be047 | 58 | |
duperoux_j | 5:be0c602be047 | 59 | if (c == MARQUEUR_FIN_TRAME) { |
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 | 5:be0c602be047 | 64 | } else { m_bluetooth->printf("buffer_data\n"); return NULL; } |
duperoux_j | 0:f39d89bfe442 | 65 | } |
duperoux_j | 2:4fc77e5b08e9 | 66 | |
duperoux_j | 5:be0c602be047 | 67 | return creer_paquetdomotique(identifiant, longueur_paquet, buffer_data); |
duperoux_j | 0:f39d89bfe442 | 68 | } |
duperoux_j | 0:f39d89bfe442 | 69 | |
duperoux_j | 2:4fc77e5b08e9 | 70 | void LiaisonBluetooth::envoyer(char idenfitiant, int longueur_data, char *data) |
duperoux_j | 2:4fc77e5b08e9 | 71 | { |
duperoux_j | 3:46a9b9c1e1c0 | 72 | if (m_state && m_bluetooth->writeable()) { |
duperoux_j | 5:be0c602be047 | 73 | m_bluetooth->putc(MARQUEUR_DEBUT_TRAME); |
duperoux_j | 5:be0c602be047 | 74 | } |
duperoux_j | 5:be0c602be047 | 75 | |
duperoux_j | 5:be0c602be047 | 76 | if (m_state && m_bluetooth->writeable()) { |
duperoux_j | 3:46a9b9c1e1c0 | 77 | m_bluetooth->putc(idenfitiant); |
duperoux_j | 0:f39d89bfe442 | 78 | } |
duperoux_j | 2:4fc77e5b08e9 | 79 | |
duperoux_j | 0:f39d89bfe442 | 80 | char *longueur_buffer = convertir_int_en_4char(longueur_data); |
duperoux_j | 0:f39d89bfe442 | 81 | for (int i = 0 ; i < 4 ; i++) { |
duperoux_j | 3:46a9b9c1e1c0 | 82 | if (m_state && m_bluetooth->writeable()) { |
duperoux_j | 3:46a9b9c1e1c0 | 83 | m_bluetooth->putc(longueur_buffer[i]); |
duperoux_j | 0:f39d89bfe442 | 84 | } |
duperoux_j | 0:f39d89bfe442 | 85 | } |
duperoux_j | 2:4fc77e5b08e9 | 86 | |
duperoux_j | 0:f39d89bfe442 | 87 | for (int i = 0 ; i < longueur_data ; i++) { |
duperoux_j | 3:46a9b9c1e1c0 | 88 | if (m_state && m_bluetooth->writeable()) { |
duperoux_j | 3:46a9b9c1e1c0 | 89 | m_bluetooth->putc(data[i]); |
duperoux_j | 0:f39d89bfe442 | 90 | } |
duperoux_j | 0:f39d89bfe442 | 91 | } |
duperoux_j | 5:be0c602be047 | 92 | |
duperoux_j | 5:be0c602be047 | 93 | if (m_state && m_bluetooth->writeable()) { |
duperoux_j | 5:be0c602be047 | 94 | m_bluetooth->putc(MARQUEUR_FIN_TRAME); |
duperoux_j | 5:be0c602be047 | 95 | } |
duperoux_j | 0:f39d89bfe442 | 96 | } |
duperoux_j | 0:f39d89bfe442 | 97 | |
duperoux_j | 0:f39d89bfe442 | 98 | |
duperoux_j | 5:be0c602be047 | 99 | PaquetDomotique *creer_paquetdomotique(int identifiant, unsigned int longueur_data, char *data) |
duperoux_j | 2:4fc77e5b08e9 | 100 | { |
duperoux_j | 0:f39d89bfe442 | 101 | PaquetDomotique *paquet = (PaquetDomotique*)malloc(sizeof(PaquetDomotique)); |
duperoux_j | 0:f39d89bfe442 | 102 | paquet->identifiant = identifiant; |
duperoux_j | 2:4fc77e5b08e9 | 103 | |
duperoux_j | 5:be0c602be047 | 104 | paquet->data = (char*)malloc(sizeof(char)*longueur_data); |
duperoux_j | 5:be0c602be047 | 105 | memset(paquet->data, 0, longueur_data); |
duperoux_j | 0:f39d89bfe442 | 106 | strcpy(paquet->data, data); |
duperoux_j | 5:be0c602be047 | 107 | |
duperoux_j | 5:be0c602be047 | 108 | paquet->longueur = longueur_data; |
duperoux_j | 2:4fc77e5b08e9 | 109 | |
duperoux_j | 0:f39d89bfe442 | 110 | return paquet; |
duperoux_j | 0:f39d89bfe442 | 111 | } |
duperoux_j | 2:4fc77e5b08e9 | 112 | void detruire_paquetdomotique(PaquetDomotique *paquet) |
duperoux_j | 2:4fc77e5b08e9 | 113 | { |
duperoux_j | 0:f39d89bfe442 | 114 | free(paquet->data); |
duperoux_j | 0:f39d89bfe442 | 115 | free(paquet); |
duperoux_j | 0:f39d89bfe442 | 116 | } |
duperoux_j | 0:f39d89bfe442 | 117 | |
duperoux_j | 5:be0c602be047 | 118 | char *convertir_int_en_4char(unsigned int integer) |
duperoux_j | 2:4fc77e5b08e9 | 119 | { |
duperoux_j | 3:46a9b9c1e1c0 | 120 | char *data = new char[4]; |
duperoux_j | 2:4fc77e5b08e9 | 121 | |
duperoux_j | 0:f39d89bfe442 | 122 | data[3] = (integer>>24) & 0xFF; |
duperoux_j | 0:f39d89bfe442 | 123 | data[2] = (integer>>16) & 0xFF; |
duperoux_j | 0:f39d89bfe442 | 124 | data[1] = (integer>>8) & 0xFF; |
duperoux_j | 0:f39d89bfe442 | 125 | data[0] = integer & 0xFF; |
duperoux_j | 2:4fc77e5b08e9 | 126 | |
duperoux_j | 0:f39d89bfe442 | 127 | return data; |
duperoux_j | 0:f39d89bfe442 | 128 | } |
duperoux_j | 5:be0c602be047 | 129 | unsigned int convertir_4char_en_int(char data[4]) |
duperoux_j | 2:4fc77e5b08e9 | 130 | { |
duperoux_j | 5:be0c602be047 | 131 | return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0]; |
duperoux_j | 5:be0c602be047 | 132 | } |