lalallaa

Dependencies:   MMA8452 mbed

Committer:
EtienneB
Date:
Tue Oct 03 01:28:03 2017 +0000
Revision:
1:6738a65b699f
Parent:
0:4a3d5d86258c
Version semi-finale et commentee de la version a valider

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EtienneB 0:4a3d5d86258c 1 /*
EtienneB 0:4a3d5d86258c 2 BEAE3011
EtienneB 0:4a3d5d86258c 3 PERV2807
EtienneB 0:4a3d5d86258c 4
EtienneB 0:4a3d5d86258c 5 Noeuds Capteur Version 2
EtienneB 0:4a3d5d86258c 6 - Accelerometre
EtienneB 0:4a3d5d86258c 7 - Bouton poussoir
EtienneB 0:4a3d5d86258c 8
EtienneB 0:4a3d5d86258c 9 TO DO
EtienneB 0:4a3d5d86258c 10 - Architecture logicielle qui permet reconfiguration facile
EtienneB 0:4a3d5d86258c 11 - Utiliser pointeur de fonction pour la lecture et appeler les differentes fonctions a travers un tableau de fonctions
EtienneB 0:4a3d5d86258c 12 - Un fichier de configuration qui doit contenir les parametres de l'appli https://os.mbed.com/cookbook/ConfigFile
EtienneB 0:4a3d5d86258c 13 -> Identifiant reseau PAN
EtienneB 0:4a3d5d86258c 14 -> Periode de lecture des capteurs (en secondes)
EtienneB 0:4a3d5d86258c 15 - Simplifier la configuration de noeuds
EtienneB 0:4a3d5d86258c 16 -> Adresse 64 bits doivent etre decouvertes dynamiquement
EtienneB 0:4a3d5d86258c 17
EtienneB 0:4a3d5d86258c 18 - Ajouter une led qui allume 1 seconde lorsquune erreur est detectee
EtienneB 0:4a3d5d86258c 19 - Communication doit etre BIDIRECTIONNELLE
EtienneB 0:4a3d5d86258c 20 -> Faire allumer une led du noeud capteur par une commande du coordo. en utilisant une commande de configuration a distance (Remote AT Cmds)
EtienneB 0:4a3d5d86258c 21 -> Clignote a une frequence de 1Hz et seulement controlee par coordo.
EtienneB 0:4a3d5d86258c 22 */
EtienneB 0:4a3d5d86258c 23
EtienneB 0:4a3d5d86258c 24 #include "mbed.h"
EtienneB 0:4a3d5d86258c 25 #include "xbee_command.h"
EtienneB 0:4a3d5d86258c 26 #include "MMA8452.h"
EtienneB 0:4a3d5d86258c 27
EtienneB 0:4a3d5d86258c 28
EtienneB 0:4a3d5d86258c 29 Serial pc(USBTX, USBRX); // Initialiser le porte serie du PC
EtienneB 0:4a3d5d86258c 30 DigitalOut resetXBEE(p8); // Initialiser P8 comme broche de sortie numerique
EtienneB 0:4a3d5d86258c 31 Serial serialXBEE(p13, p14);// Initialiser le port de comm serie des broches p13 p14
EtienneB 0:4a3d5d86258c 32 DigitalIn button(p7); // Initialiser le bouton (capteur contact seche)
EtienneB 0:4a3d5d86258c 33 MMA8452 acc(p9, p10, 10000); // Initialiser l'accelerometre
EtienneB 0:4a3d5d86258c 34 LocalFileSystem local("local"); // Pour lecture fichier de configuration
EtienneB 0:4a3d5d86258c 35
EtienneB 0:4a3d5d86258c 36 // LED LPC1768
EtienneB 0:4a3d5d86258c 37 DigitalOut led1(LED1);
EtienneB 0:4a3d5d86258c 38 DigitalOut led2(LED2);
EtienneB 0:4a3d5d86258c 39 DigitalOut led3(LED3);
EtienneB 0:4a3d5d86258c 40 DigitalOut led4(LED4);
EtienneB 0:4a3d5d86258c 41
EtienneB 0:4a3d5d86258c 42 // Fonctions de lecture des capteurs
EtienneB 0:4a3d5d86258c 43 uint16_t read_button();
EtienneB 0:4a3d5d86258c 44 uint16_t read_acc_x();
EtienneB 0:4a3d5d86258c 45 uint16_t read_acc_y();
EtienneB 0:4a3d5d86258c 46 uint16_t read_acc_z();
EtienneB 0:4a3d5d86258c 47 void read_capteurs(int frequence, uint16_t* capteurs_value);
EtienneB 0:4a3d5d86258c 48
EtienneB 0:4a3d5d86258c 49 // Fonction de lecture du fichier de configuration
EtienneB 0:4a3d5d86258c 50 void read_config_file(uint16_t *config_variables);
EtienneB 0:4a3d5d86258c 51
EtienneB 0:4a3d5d86258c 52 // Tableau de fonction pour lecture des capteurs
EtienneB 0:4a3d5d86258c 53 uint16_t (*p_fonction[4])();
EtienneB 0:4a3d5d86258c 54
EtienneB 0:4a3d5d86258c 55 //Template frame
EtienneB 0:4a3d5d86258c 56 uint8_t at_cmd_frame[255];
EtienneB 0:4a3d5d86258c 57 uint8_t remote_command_request_frame[255];
EtienneB 0:4a3d5d86258c 58 uint8_t tx_request_frame[255];
EtienneB 0:4a3d5d86258c 59 uint64_t parameter_value;
EtienneB 0:4a3d5d86258c 60 uint8_t at_cmd;
EtienneB 0:4a3d5d86258c 61 uint8_t message_recu[255];
EtienneB 0:4a3d5d86258c 62
EtienneB 0:4a3d5d86258c 63 uint8_t get_MSB(uint16_t info);
EtienneB 0:4a3d5d86258c 64 uint8_t get_LSB(uint16_t info);
EtienneB 0:4a3d5d86258c 65
EtienneB 0:4a3d5d86258c 66 uint8_t mac_adresse[8];
EtienneB 0:4a3d5d86258c 67
EtienneB 0:4a3d5d86258c 68 int main() {
EtienneB 0:4a3d5d86258c 69 uint16_t button_state = 0;
EtienneB 0:4a3d5d86258c 70 uint8_t lsb_button_state = 0;
EtienneB 0:4a3d5d86258c 71 uint16_t acc_x = 0;
EtienneB 0:4a3d5d86258c 72 uint8_t msb1_acc_x = 0;
EtienneB 0:4a3d5d86258c 73 uint8_t msb2_acc_x = 0;
EtienneB 0:4a3d5d86258c 74 uint8_t msb3_acc_x = 0;
EtienneB 0:4a3d5d86258c 75 uint8_t msb4_acc_x = 0;
EtienneB 0:4a3d5d86258c 76 uint8_t lsb1_acc_x = 0;
EtienneB 0:4a3d5d86258c 77 uint8_t lsb2_acc_x = 0;
EtienneB 0:4a3d5d86258c 78 uint8_t lsb3_acc_x = 0;
EtienneB 0:4a3d5d86258c 79 uint8_t lsb4_acc_x = 0;
EtienneB 0:4a3d5d86258c 80 uint16_t acc_y = 0;
EtienneB 0:4a3d5d86258c 81 uint16_t acc_z = 0;
EtienneB 0:4a3d5d86258c 82 uint16_t capteurs_value[] = {button_state, acc_x, acc_y, acc_z};
EtienneB 0:4a3d5d86258c 83 uint8_t frame_length = 0;
EtienneB 0:4a3d5d86258c 84 uint16_t* config_values[] = {&PAN_ID, &FREQ_LECTURE_CAPTEURS}; // Tableau de pointeur pour variables de configurations
EtienneB 0:4a3d5d86258c 85
EtienneB 0:4a3d5d86258c 86 read_config_file(*config_values); // Lecture du fichier de configuration
EtienneB 0:4a3d5d86258c 87 xbee_init(PAN_ID); // Init xbee avec pan ID provenant du fichier de configuration
EtienneB 0:4a3d5d86258c 88
EtienneB 0:4a3d5d86258c 89 frame_length = build_remote_cmd_request_frame(FRAME_ID01, PAN_ID, AT_CMD_ID, remote_command_request_frame);
EtienneB 0:4a3d5d86258c 90 send_frame_2xbee(remote_command_request_frame, frame_length);
EtienneB 0:4a3d5d86258c 91 wait_ms(400);
EtienneB 0:4a3d5d86258c 92
EtienneB 0:4a3d5d86258c 93 receive_Xbee(message_recu);
EtienneB 0:4a3d5d86258c 94 message_received(message_recu);
EtienneB 0:4a3d5d86258c 95
EtienneB 0:4a3d5d86258c 96 pc.printf("MAC = ");
EtienneB 0:4a3d5d86258c 97 for(uint8_t i = 0; i < 8; i++){
EtienneB 0:4a3d5d86258c 98 pc.printf("%02X ", mac_adresse[i]);
EtienneB 0:4a3d5d86258c 99 }
EtienneB 0:4a3d5d86258c 100 pc.printf("%\r\n");
EtienneB 0:4a3d5d86258c 101
EtienneB 0:4a3d5d86258c 102 while(1) {
EtienneB 0:4a3d5d86258c 103 read_capteurs(FREQ_LECTURE_CAPTEURS, capteurs_value);
EtienneB 0:4a3d5d86258c 104
EtienneB 0:4a3d5d86258c 105 if((capteurs_value[0] & 0x0001 == 0x0001)){
EtienneB 0:4a3d5d86258c 106 lsb_button_state = 0x31;
EtienneB 0:4a3d5d86258c 107 }
EtienneB 0:4a3d5d86258c 108 else{
EtienneB 0:4a3d5d86258c 109 lsb_button_state = 0x30;
EtienneB 0:4a3d5d86258c 110 }
EtienneB 0:4a3d5d86258c 111
EtienneB 0:4a3d5d86258c 112 for(uint8_t a; a < 16; a++){
EtienneB 0:4a3d5d86258c 113
EtienneB 0:4a3d5d86258c 114 }
EtienneB 0:4a3d5d86258c 115
EtienneB 0:4a3d5d86258c 116 msb1_acc_x = 0x31;
EtienneB 0:4a3d5d86258c 117 msb2_acc_x = 0x32;
EtienneB 0:4a3d5d86258c 118 msb3_acc_x = 0x33;
EtienneB 0:4a3d5d86258c 119 msb4_acc_x = 0x34;
EtienneB 0:4a3d5d86258c 120 lsb1_acc_x = 0x35;
EtienneB 0:4a3d5d86258c 121 lsb2_acc_x = 0x36;
EtienneB 0:4a3d5d86258c 122 lsb3_acc_x = 0x37;
EtienneB 0:4a3d5d86258c 123 lsb4_acc_x = 0x38;
EtienneB 0:4a3d5d86258c 124
EtienneB 0:4a3d5d86258c 125
EtienneB 0:4a3d5d86258c 126
EtienneB 0:4a3d5d86258c 127 uint8_t data[] = {'B', '=', lsb_button_state,
EtienneB 0:4a3d5d86258c 128 0x09,
EtienneB 0:4a3d5d86258c 129 'X', '=', msb1_acc_x, msb2_acc_x, msb3_acc_x, msb4_acc_x};
EtienneB 0:4a3d5d86258c 130
EtienneB 0:4a3d5d86258c 131 frame_length = build_tx_request_frame(mac_adresse, PAN_ID, tx_request_frame, data, sizeof(data));
EtienneB 0:4a3d5d86258c 132 send_frame_2xbee(tx_request_frame, frame_length);
EtienneB 0:4a3d5d86258c 133 //receive_Xbee(message_recu);
EtienneB 0:4a3d5d86258c 134
EtienneB 0:4a3d5d86258c 135 }
EtienneB 0:4a3d5d86258c 136 }
EtienneB 1:6738a65b699f 137 /**
EtienneB 1:6738a65b699f 138 Analyse des trames recues par le xbee
EtienneB 1:6738a65b699f 139 */
EtienneB 0:4a3d5d86258c 140 bool message_received(uint8_t* message_recu){
EtienneB 0:4a3d5d86258c 141 uint8_t frame_id = message_recu[3];
EtienneB 0:4a3d5d86258c 142 //pc.printf("\n\r frame id = %02X\r\n", frame_id);
EtienneB 0:4a3d5d86258c 143 switch(frame_id){
EtienneB 0:4a3d5d86258c 144 case RMT_CMD_RSPNS:
EtienneB 0:4a3d5d86258c 145 for(uint8_t i = 0; i < 8; i++){
EtienneB 0:4a3d5d86258c 146 mac_adresse[i] = message_recu[5 + i];
EtienneB 0:4a3d5d86258c 147 }
EtienneB 0:4a3d5d86258c 148 }
EtienneB 0:4a3d5d86258c 149 return true;
EtienneB 0:4a3d5d86258c 150 }
EtienneB 1:6738a65b699f 151 /**
EtienneB 1:6738a65b699f 152 Envoie de la valeur du pan id lu dans le fichier vers le registre
EtienneB 1:6738a65b699f 153 correspondant du xbee
EtienneB 1:6738a65b699f 154 */
EtienneB 0:4a3d5d86258c 155 bool xbee_init(uint16_t pan_id){
EtienneB 0:4a3d5d86258c 156 uint8_t MSB_pan_id = get_MSB(PAN_ID);
EtienneB 0:4a3d5d86258c 157 uint8_t LSB_pan_id = get_LSB(PAN_ID);
EtienneB 0:4a3d5d86258c 158
EtienneB 0:4a3d5d86258c 159 uint8_t enter_at_cmd_mode[] = {'+', '+', '+'};
EtienneB 0:4a3d5d86258c 160 uint8_t data_pan_id[] = {MSB_pan_id, LSB_pan_id};
EtienneB 0:4a3d5d86258c 161
EtienneB 0:4a3d5d86258c 162 uint8_t frame_length = 0;
EtienneB 0:4a3d5d86258c 163
EtienneB 0:4a3d5d86258c 164 // Mettre a 0 la broche RESET pour 400ms
EtienneB 0:4a3d5d86258c 165 resetXBEE = 0; wait_ms(400);
EtienneB 0:4a3d5d86258c 166 resetXBEE = 1;
EtienneB 0:4a3d5d86258c 167
EtienneB 0:4a3d5d86258c 168 // +++
EtienneB 0:4a3d5d86258c 169 send_frame_2xbee(enter_at_cmd_mode, sizeof(enter_at_cmd_mode));
EtienneB 0:4a3d5d86258c 170 wait_ms(400);
EtienneB 0:4a3d5d86258c 171
EtienneB 0:4a3d5d86258c 172 receive_Xbee(message_recu);
EtienneB 0:4a3d5d86258c 173 // ID = 9600
EtienneB 0:4a3d5d86258c 174 frame_length = build_AT_cmd_frame(FRAME_ID01, AT_CMD_ID, data_pan_id, sizeof(data_pan_id), at_cmd_frame);
EtienneB 0:4a3d5d86258c 175 send_frame_2xbee(at_cmd_frame, frame_length);
EtienneB 0:4a3d5d86258c 176 wait_ms(30);
EtienneB 0:4a3d5d86258c 177
EtienneB 0:4a3d5d86258c 178 receive_Xbee(message_recu);
EtienneB 0:4a3d5d86258c 179 // WR
EtienneB 0:4a3d5d86258c 180 frame_length = build_AT_cmd_frame(FRAME_ID01, AT_CMD_WR, NULL, 0, at_cmd_frame);
EtienneB 0:4a3d5d86258c 181 send_frame_2xbee(at_cmd_frame, frame_length);
EtienneB 0:4a3d5d86258c 182 wait_ms(30);
EtienneB 0:4a3d5d86258c 183
EtienneB 0:4a3d5d86258c 184 receive_Xbee(message_recu);
EtienneB 0:4a3d5d86258c 185
EtienneB 0:4a3d5d86258c 186 // AC
EtienneB 0:4a3d5d86258c 187 frame_length = build_AT_cmd_frame(FRAME_ID01, AT_CMD_AC, NULL, 0, at_cmd_frame);
EtienneB 0:4a3d5d86258c 188 send_frame_2xbee(at_cmd_frame, frame_length);
EtienneB 0:4a3d5d86258c 189 wait_ms(30);
EtienneB 0:4a3d5d86258c 190
EtienneB 0:4a3d5d86258c 191 receive_Xbee(message_recu);
EtienneB 0:4a3d5d86258c 192
EtienneB 0:4a3d5d86258c 193 return true;
EtienneB 0:4a3d5d86258c 194 }
EtienneB 1:6738a65b699f 195 /**
EtienneB 1:6738a65b699f 196 Envoie de la trame vers le xbee
EtienneB 1:6738a65b699f 197 */
EtienneB 0:4a3d5d86258c 198 bool send_frame_2xbee(uint8_t* frame, uint8_t frame_length){
EtienneB 0:4a3d5d86258c 199 for(uint8_t i = 0; i < frame_length; i++){
EtienneB 0:4a3d5d86258c 200 led2 = !led2;
EtienneB 0:4a3d5d86258c 201 serialXBEE.putc(frame[i]);
EtienneB 0:4a3d5d86258c 202 pc.printf("%02X ", frame[i]);
EtienneB 0:4a3d5d86258c 203 wait_ms(50);
EtienneB 0:4a3d5d86258c 204 }
EtienneB 0:4a3d5d86258c 205 pc.printf("%\r\n");
EtienneB 0:4a3d5d86258c 206 return true;
EtienneB 0:4a3d5d86258c 207
EtienneB 0:4a3d5d86258c 208 }
EtienneB 1:6738a65b699f 209 /**
EtienneB 1:6738a65b699f 210 Mise dans un tableau de la trame recue par le xbee
EtienneB 1:6738a65b699f 211 */
EtienneB 0:4a3d5d86258c 212 uint8_t receive_Xbee(uint8_t* message_recu){
EtienneB 0:4a3d5d86258c 213 uint8_t caratere_recu;
EtienneB 0:4a3d5d86258c 214 uint8_t message_length = 0;
EtienneB 0:4a3d5d86258c 215
EtienneB 0:4a3d5d86258c 216 while(serialXBEE.readable() == 0){
EtienneB 0:4a3d5d86258c 217 //pc.printf(" ");
EtienneB 0:4a3d5d86258c 218 }
EtienneB 0:4a3d5d86258c 219
EtienneB 0:4a3d5d86258c 220 while(serialXBEE.readable() == 1){
EtienneB 0:4a3d5d86258c 221 //pc.printf("%d %d \r\n", serialXBEE.readable(), pc.readable());
EtienneB 0:4a3d5d86258c 222 led1 = !led1;
EtienneB 0:4a3d5d86258c 223 caratere_recu = serialXBEE.getc(); pc.printf("%02X ", caratere_recu);// ST
EtienneB 0:4a3d5d86258c 224 message_recu[message_length] = caratere_recu;
EtienneB 0:4a3d5d86258c 225 message_length++;
EtienneB 0:4a3d5d86258c 226 wait_ms(50);
EtienneB 0:4a3d5d86258c 227 }
EtienneB 0:4a3d5d86258c 228
EtienneB 0:4a3d5d86258c 229 pc.printf("\r\n");
EtienneB 0:4a3d5d86258c 230 return message_length;
EtienneB 0:4a3d5d86258c 231 }
EtienneB 1:6738a65b699f 232 /**
EtienneB 1:6738a65b699f 233 Construit la trame pour envoie de commande AT
EtienneB 1:6738a65b699f 234 */
EtienneB 0:4a3d5d86258c 235 uint8_t build_AT_cmd_frame(uint8_t frame_id, uint16_t p_AT_cmd, uint8_t* p_value, uint8_t p_value_length, uint8_t* p_at_cmd_frame){
EtienneB 0:4a3d5d86258c 236 uint8_t at_cmd_char1 = get_MSB(p_AT_cmd);
EtienneB 0:4a3d5d86258c 237 uint8_t at_cmd_char2 = get_LSB(p_AT_cmd);
EtienneB 0:4a3d5d86258c 238 uint8_t checksum = 0;
EtienneB 0:4a3d5d86258c 239 uint8_t frame_index = 0;
EtienneB 0:4a3d5d86258c 240
EtienneB 0:4a3d5d86258c 241 uint16_t length = p_value_length + 0x04;
EtienneB 0:4a3d5d86258c 242 uint8_t MSB = get_MSB(length);
EtienneB 0:4a3d5d86258c 243 uint8_t LSB = get_LSB(length);
EtienneB 0:4a3d5d86258c 244 // (optional)
EtienneB 0:4a3d5d86258c 245 // {ST, MSB, LSB, 0x08, frameID, AT_cmd_1, AT_cmd_2, p_value, checksum}
EtienneB 0:4a3d5d86258c 246
EtienneB 0:4a3d5d86258c 247 p_at_cmd_frame[0] = START;
EtienneB 0:4a3d5d86258c 248 p_at_cmd_frame[1] = MSB;
EtienneB 0:4a3d5d86258c 249 p_at_cmd_frame[2] = LSB;
EtienneB 0:4a3d5d86258c 250 p_at_cmd_frame[3] = AT_CMD;
EtienneB 0:4a3d5d86258c 251 p_at_cmd_frame[4] = frame_id;
EtienneB 0:4a3d5d86258c 252 p_at_cmd_frame[5] = at_cmd_char1;
EtienneB 0:4a3d5d86258c 253 p_at_cmd_frame[6] = at_cmd_char2;
EtienneB 0:4a3d5d86258c 254
EtienneB 0:4a3d5d86258c 255 if(p_value_length != 0){
EtienneB 0:4a3d5d86258c 256 for(frame_index = 7; frame_index < p_value_length + 7; frame_index++){
EtienneB 0:4a3d5d86258c 257 p_at_cmd_frame[frame_index] = p_value[frame_index - 7];
EtienneB 0:4a3d5d86258c 258 }
EtienneB 0:4a3d5d86258c 259 }
EtienneB 0:4a3d5d86258c 260
EtienneB 0:4a3d5d86258c 261 // Calculate checksum
EtienneB 0:4a3d5d86258c 262 for(frame_index = 3; frame_index < 7 + p_value_length; frame_index++){
EtienneB 0:4a3d5d86258c 263 checksum += p_at_cmd_frame[frame_index];
EtienneB 0:4a3d5d86258c 264 //pc.printf(" checksum = %02X \r\n", checksum);
EtienneB 0:4a3d5d86258c 265 }
EtienneB 0:4a3d5d86258c 266
EtienneB 0:4a3d5d86258c 267 checksum = 0xFF - checksum;
EtienneB 0:4a3d5d86258c 268
EtienneB 0:4a3d5d86258c 269 p_at_cmd_frame[frame_index] = checksum; // checksum
EtienneB 0:4a3d5d86258c 270
EtienneB 0:4a3d5d86258c 271 return frame_index + 1;
EtienneB 0:4a3d5d86258c 272 }
EtienneB 1:6738a65b699f 273 /**
EtienneB 1:6738a65b699f 274 Construit la trame pour envoie de commande AT a un xbee distant
EtienneB 1:6738a65b699f 275 */
EtienneB 0:4a3d5d86258c 276 uint8_t build_remote_cmd_request_frame(uint8_t p_frame_id, uint16_t p_pan_id, uint16_t p_AT_cmd, uint8_t* p_remote_cmd_request_frame){
EtienneB 0:4a3d5d86258c 277 uint64_t mask_mac_id = 0x00000000000000FF;
EtienneB 0:4a3d5d86258c 278
EtienneB 0:4a3d5d86258c 279
EtienneB 0:4a3d5d86258c 280 p_pan_id = 0xFFFE;
EtienneB 0:4a3d5d86258c 281 uint8_t mac_bit_shift = 56;
EtienneB 0:4a3d5d86258c 282 uint8_t frame_index = 0;
EtienneB 0:4a3d5d86258c 283 uint16_t checksum = 0x0000;
EtienneB 0:4a3d5d86258c 284
EtienneB 0:4a3d5d86258c 285 uint16_t length = 15;
EtienneB 0:4a3d5d86258c 286 uint8_t MSB = get_MSB(length);
EtienneB 0:4a3d5d86258c 287 uint8_t LSB = get_LSB(length);
EtienneB 0:4a3d5d86258c 288
EtienneB 0:4a3d5d86258c 289 uint8_t at_cmd_char1 = get_MSB(p_AT_cmd);
EtienneB 0:4a3d5d86258c 290 uint8_t at_cmd_char2 = get_LSB(p_AT_cmd);
EtienneB 0:4a3d5d86258c 291
EtienneB 0:4a3d5d86258c 292 uint64_t mac_address = 0x00000000; // broadcast
EtienneB 0:4a3d5d86258c 293
EtienneB 0:4a3d5d86258c 294 p_remote_cmd_request_frame[0] = START; // ST
EtienneB 0:4a3d5d86258c 295 p_remote_cmd_request_frame[1] = MSB; // MSB
EtienneB 0:4a3d5d86258c 296 p_remote_cmd_request_frame[2] = LSB; // LSB
EtienneB 0:4a3d5d86258c 297 p_remote_cmd_request_frame[3] = RMT_AT_RQST; // frame type 0x10
EtienneB 0:4a3d5d86258c 298 p_remote_cmd_request_frame[4] = p_frame_id; // frame id
EtienneB 0:4a3d5d86258c 299 // 64b destination address 5 a 12
EtienneB 0:4a3d5d86258c 300 for(frame_index = 5; frame_index < 5 + 8; frame_index++){
EtienneB 0:4a3d5d86258c 301 p_remote_cmd_request_frame[frame_index] = (mac_address >> mac_bit_shift) & mask_mac_id;
EtienneB 0:4a3d5d86258c 302 mac_bit_shift = mac_bit_shift - 8;
EtienneB 0:4a3d5d86258c 303 }
EtienneB 0:4a3d5d86258c 304 // 16b destination network address 13 et 14
EtienneB 0:4a3d5d86258c 305 p_remote_cmd_request_frame[13] = (p_pan_id >> 8) & 0x00FF; // MSB pan_id
EtienneB 0:4a3d5d86258c 306 p_remote_cmd_request_frame[14] = p_pan_id & 0x00FF; // LSB_pan_id
EtienneB 0:4a3d5d86258c 307
EtienneB 0:4a3d5d86258c 308 p_remote_cmd_request_frame[15] = 0x02; // Remote Command Option
EtienneB 0:4a3d5d86258c 309
EtienneB 0:4a3d5d86258c 310 p_remote_cmd_request_frame[16] = at_cmd_char1; // AT Command char1
EtienneB 0:4a3d5d86258c 311 p_remote_cmd_request_frame[17] = at_cmd_char2; // At Command char2
EtienneB 0:4a3d5d86258c 312
EtienneB 0:4a3d5d86258c 313 // Calculate checksum
EtienneB 0:4a3d5d86258c 314 for(frame_index = 3; frame_index < 18; frame_index++){
EtienneB 0:4a3d5d86258c 315 checksum += p_remote_cmd_request_frame[frame_index];
EtienneB 0:4a3d5d86258c 316 //pc.printf(" checksum = %02X \r\n", checksum);
EtienneB 0:4a3d5d86258c 317 }
EtienneB 0:4a3d5d86258c 318
EtienneB 0:4a3d5d86258c 319 checksum = 0xFF - checksum;
EtienneB 0:4a3d5d86258c 320
EtienneB 0:4a3d5d86258c 321 p_remote_cmd_request_frame[frame_index] = checksum; // checksum
EtienneB 0:4a3d5d86258c 322
EtienneB 0:4a3d5d86258c 323 return frame_index + 1;
EtienneB 0:4a3d5d86258c 324 }
EtienneB 1:6738a65b699f 325 /**
EtienneB 1:6738a65b699f 326 Construit la trame pour envoie de de trame vers un xbee distant
EtienneB 1:6738a65b699f 327 */
EtienneB 0:4a3d5d86258c 328 uint8_t build_tx_request_frame(uint8_t* p_mac_address, uint16_t p_pan_id, uint8_t* p_tx_request_frame, uint8_t *data, uint8_t data_length){ //{ST, MSB, LSB, 0x08, frameID, AT_cmd_1, AT_cmd_2, parameter_value (optional), checksum}
EtienneB 0:4a3d5d86258c 329 uint64_t mask_mac_id = 0x00000000000000FF;
EtienneB 0:4a3d5d86258c 330
EtienneB 0:4a3d5d86258c 331 uint8_t mac_bit_shift = 56;
EtienneB 0:4a3d5d86258c 332 uint8_t frame_index = 0;
EtienneB 0:4a3d5d86258c 333 uint16_t checksum = 0x0000;
EtienneB 0:4a3d5d86258c 334
EtienneB 0:4a3d5d86258c 335 uint16_t length = data_length + 14;
EtienneB 0:4a3d5d86258c 336 uint8_t MSB = get_MSB(length);
EtienneB 0:4a3d5d86258c 337 uint8_t LSB = get_LSB(length);
EtienneB 0:4a3d5d86258c 338
EtienneB 0:4a3d5d86258c 339 p_tx_request_frame[0] = START; // ST
EtienneB 0:4a3d5d86258c 340 p_tx_request_frame[1] = MSB; // MSB
EtienneB 0:4a3d5d86258c 341 p_tx_request_frame[2] = LSB; // LSB
EtienneB 0:4a3d5d86258c 342 p_tx_request_frame[3] = TX_REQUEST; // frame type 0x10
EtienneB 0:4a3d5d86258c 343 p_tx_request_frame[4] = FRAME_ID01; // frame id
EtienneB 1:6738a65b699f 344
EtienneB 0:4a3d5d86258c 345 for(frame_index = 5; frame_index < 5 + 8; frame_index++){
EtienneB 0:4a3d5d86258c 346 p_tx_request_frame[frame_index] = p_mac_address[frame_index - 5];
EtienneB 0:4a3d5d86258c 347 }
EtienneB 1:6738a65b699f 348
EtienneB 0:4a3d5d86258c 349 p_tx_request_frame[13] = (p_pan_id >> 8) & 0x00FF; // MSB pan_id
EtienneB 0:4a3d5d86258c 350 p_tx_request_frame[14] = p_pan_id & 0x00FF; // LSB_pan_id
EtienneB 0:4a3d5d86258c 351
EtienneB 0:4a3d5d86258c 352 p_tx_request_frame[15] = 0x00; // broadcast radius
EtienneB 0:4a3d5d86258c 353 p_tx_request_frame[16] = 0x00; // option
EtienneB 0:4a3d5d86258c 354
EtienneB 0:4a3d5d86258c 355 // RF data
EtienneB 0:4a3d5d86258c 356 for(frame_index = 17; frame_index < 17 + data_length; frame_index++){
EtienneB 0:4a3d5d86258c 357 p_tx_request_frame[frame_index] = data[frame_index - 17];
EtienneB 0:4a3d5d86258c 358 }
EtienneB 0:4a3d5d86258c 359 // Calculate checksum
EtienneB 0:4a3d5d86258c 360 for(frame_index = 3; frame_index < 17 + data_length; frame_index++){
EtienneB 0:4a3d5d86258c 361 checksum += p_tx_request_frame[frame_index];
EtienneB 0:4a3d5d86258c 362 //pc.printf(" checksum = %02X \r\n", checksum);
EtienneB 0:4a3d5d86258c 363 }
EtienneB 0:4a3d5d86258c 364
EtienneB 0:4a3d5d86258c 365 checksum = 0xFF - checksum;
EtienneB 0:4a3d5d86258c 366
EtienneB 0:4a3d5d86258c 367 p_tx_request_frame[frame_index] = checksum; // checksum
EtienneB 0:4a3d5d86258c 368
EtienneB 0:4a3d5d86258c 369 return frame_index + 1;
EtienneB 0:4a3d5d86258c 370 }
EtienneB 1:6738a65b699f 371 /**
EtienneB 1:6738a65b699f 372 Isole les 4 bits les plus significatif dune valeur short et la renvoie
EtienneB 1:6738a65b699f 373 en char
EtienneB 1:6738a65b699f 374 */
EtienneB 0:4a3d5d86258c 375 uint8_t get_MSB(uint16_t info){
EtienneB 0:4a3d5d86258c 376 uint8_t MSB = (info >> 8) & 0x00FF;
EtienneB 0:4a3d5d86258c 377
EtienneB 0:4a3d5d86258c 378 return MSB;
EtienneB 0:4a3d5d86258c 379 }
EtienneB 1:6738a65b699f 380 /**
EtienneB 1:6738a65b699f 381 Isole les 4 bits les moins significatif dune valeur short et la renvoie
EtienneB 1:6738a65b699f 382 en char
EtienneB 1:6738a65b699f 383 */
EtienneB 0:4a3d5d86258c 384 uint8_t get_LSB(uint16_t info){
EtienneB 0:4a3d5d86258c 385 uint8_t LSB = info & 0x00FF;
EtienneB 0:4a3d5d86258c 386
EtienneB 0:4a3d5d86258c 387 return LSB;
EtienneB 0:4a3d5d86258c 388 }
EtienneB 1:6738a65b699f 389
EtienneB 0:4a3d5d86258c 390 uint16_t read_button(){
EtienneB 0:4a3d5d86258c 391 if(button){
EtienneB 0:4a3d5d86258c 392 return 1;
EtienneB 0:4a3d5d86258c 393 }
EtienneB 0:4a3d5d86258c 394 else{
EtienneB 0:4a3d5d86258c 395 return 0;
EtienneB 0:4a3d5d86258c 396 }
EtienneB 0:4a3d5d86258c 397 }
EtienneB 1:6738a65b699f 398
EtienneB 1:6738a65b699f 399 /**
EtienneB 1:6738a65b699f 400 Lecture acceleration grav. en x
EtienneB 1:6738a65b699f 401 */
EtienneB 0:4a3d5d86258c 402 uint16_t read_acc_x(){
EtienneB 0:4a3d5d86258c 403 int x;
EtienneB 0:4a3d5d86258c 404 acc.readXCount(&x);
EtienneB 0:4a3d5d86258c 405 return (uint16_t)x;
EtienneB 0:4a3d5d86258c 406 }
EtienneB 1:6738a65b699f 407 /**
EtienneB 1:6738a65b699f 408 Lecture acceleration grav. en y
EtienneB 1:6738a65b699f 409 */
EtienneB 0:4a3d5d86258c 410 uint16_t read_acc_y(){
EtienneB 0:4a3d5d86258c 411 int y;
EtienneB 0:4a3d5d86258c 412 acc.readYCount(&y);
EtienneB 0:4a3d5d86258c 413 return (uint16_t)y;
EtienneB 0:4a3d5d86258c 414 }
EtienneB 1:6738a65b699f 415 /**
EtienneB 1:6738a65b699f 416 Lecture acceleration grav. en z
EtienneB 1:6738a65b699f 417 */
EtienneB 0:4a3d5d86258c 418 uint16_t read_acc_z(){
EtienneB 0:4a3d5d86258c 419 int z;
EtienneB 0:4a3d5d86258c 420 acc.readZCount(&z);
EtienneB 0:4a3d5d86258c 421 return (uint16_t)z;
EtienneB 0:4a3d5d86258c 422 }
EtienneB 1:6738a65b699f 423 /**
EtienneB 1:6738a65b699f 424 Lecture periodique de tout les capteurs
EtienneB 1:6738a65b699f 425 */
EtienneB 0:4a3d5d86258c 426 void read_capteurs(int frequence, uint16_t* capteurs_value){
EtienneB 0:4a3d5d86258c 427 p_fonction[0] = &read_button;
EtienneB 0:4a3d5d86258c 428 p_fonction[1] = &read_acc_x;
EtienneB 0:4a3d5d86258c 429 p_fonction[2] = &read_acc_y;
EtienneB 0:4a3d5d86258c 430 p_fonction[3] = &read_acc_z;
EtienneB 0:4a3d5d86258c 431
EtienneB 0:4a3d5d86258c 432 capteurs_value[0] = (*p_fonction[0])(); // bouton
EtienneB 0:4a3d5d86258c 433 capteurs_value[1] = (*p_fonction[1])(); // x
EtienneB 0:4a3d5d86258c 434 capteurs_value[2] = (*p_fonction[2])(); // y
EtienneB 0:4a3d5d86258c 435 capteurs_value[3] = (*p_fonction[3])(); // z
EtienneB 0:4a3d5d86258c 436
EtienneB 0:4a3d5d86258c 437 wait(frequence);
EtienneB 0:4a3d5d86258c 438 }
EtienneB 1:6738a65b699f 439 /**
EtienneB 1:6738a65b699f 440 Lecture du fichier de configuration pour etablir la valeur
EtienneB 1:6738a65b699f 441 du temps de lecture des capteur et du pan_id (en decimal dans le fichier)
EtienneB 1:6738a65b699f 442 */
EtienneB 0:4a3d5d86258c 443 void read_config_file(uint16_t *config_variables){
EtienneB 0:4a3d5d86258c 444 char ch;
EtienneB 0:4a3d5d86258c 445 char valeur[10];
EtienneB 0:4a3d5d86258c 446 uint8_t len = 0;
EtienneB 0:4a3d5d86258c 447 uint8_t cpt_variable = 0;
EtienneB 0:4a3d5d86258c 448
EtienneB 0:4a3d5d86258c 449 FILE *fp = fopen("/local/config.txt", "r");
EtienneB 0:4a3d5d86258c 450
EtienneB 0:4a3d5d86258c 451 while(!feof(fp)){
EtienneB 0:4a3d5d86258c 452 ch=fgetc(fp);
EtienneB 0:4a3d5d86258c 453
EtienneB 0:4a3d5d86258c 454 if((((ch >= '0') & (ch <= '9'))) | (ch == '\n')){
EtienneB 0:4a3d5d86258c 455 //pc.printf("%c", ch);
EtienneB 0:4a3d5d86258c 456 valeur[len] = ch;
EtienneB 0:4a3d5d86258c 457 len++;
EtienneB 0:4a3d5d86258c 458 if(ch == '\n'){
EtienneB 0:4a3d5d86258c 459 config_variables[cpt_variable] = atoi(valeur);
EtienneB 0:4a3d5d86258c 460 cpt_variable++;
EtienneB 0:4a3d5d86258c 461 for(int i = 0; i < 10; i++){
EtienneB 0:4a3d5d86258c 462 valeur[i] = NULL;
EtienneB 0:4a3d5d86258c 463 }
EtienneB 0:4a3d5d86258c 464 len = 0;
EtienneB 0:4a3d5d86258c 465 }
EtienneB 0:4a3d5d86258c 466 }
EtienneB 0:4a3d5d86258c 467 }
EtienneB 0:4a3d5d86258c 468 fclose(fp);
EtienneB 0:4a3d5d86258c 469 }