![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Receiver code
Receiver.cpp@0:f8873e0badb2, 2016-02-18 (annotated)
- Committer:
- vinbel93
- Date:
- Thu Feb 18 13:22:31 2016 +0000
- Revision:
- 0:f8873e0badb2
Receiver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vinbel93 | 0:f8873e0badb2 | 1 | #include "mbed.h" |
vinbel93 | 0:f8873e0badb2 | 2 | #include "Sensors.h" |
vinbel93 | 0:f8873e0badb2 | 3 | #include "Receiver.h" |
vinbel93 | 0:f8873e0badb2 | 4 | #include "Xbee.h" |
vinbel93 | 0:f8873e0badb2 | 5 | |
vinbel93 | 0:f8873e0badb2 | 6 | // FileSystem (pour lire fichier de config) |
vinbel93 | 0:f8873e0badb2 | 7 | LocalFileSystem local("local"); |
vinbel93 | 0:f8873e0badb2 | 8 | |
vinbel93 | 0:f8873e0badb2 | 9 | // Entrées-sorties |
vinbel93 | 0:f8873e0badb2 | 10 | DigitalOut reset(p8); |
vinbel93 | 0:f8873e0badb2 | 11 | Serial xbee(p13, p14); |
vinbel93 | 0:f8873e0badb2 | 12 | Serial pc(USBTX, USBRX); |
vinbel93 | 0:f8873e0badb2 | 13 | |
vinbel93 | 0:f8873e0badb2 | 14 | // Paramètres du fichier de config |
vinbel93 | 0:f8873e0badb2 | 15 | const int paramCount = 2; |
vinbel93 | 0:f8873e0badb2 | 16 | Parameter params[paramCount]; |
vinbel93 | 0:f8873e0badb2 | 17 | |
vinbel93 | 0:f8873e0badb2 | 18 | // Tableau de fonctions pour la lecture des capteurs |
vinbel93 | 0:f8873e0badb2 | 19 | const int sensorsCount = 2; |
vinbel93 | 0:f8873e0badb2 | 20 | void (*readSensorsTable[sensorsCount]) (char*) = {&readButton, &readAccelero}; |
vinbel93 | 0:f8873e0badb2 | 21 | |
vinbel93 | 0:f8873e0badb2 | 22 | // Fonction de lecture du fichier de config |
vinbel93 | 0:f8873e0badb2 | 23 | void readConfig() |
vinbel93 | 0:f8873e0badb2 | 24 | { |
vinbel93 | 0:f8873e0badb2 | 25 | FILE *file = fopen("/local/receiver.cfg", "r"); |
vinbel93 | 0:f8873e0badb2 | 26 | char* token; |
vinbel93 | 0:f8873e0badb2 | 27 | char* delimiter = "="; |
vinbel93 | 0:f8873e0badb2 | 28 | |
vinbel93 | 0:f8873e0badb2 | 29 | params[PAN_ID] = (Parameter) {.name = "PAN_ID", .base = 16}; |
vinbel93 | 0:f8873e0badb2 | 30 | params[PERIOD] = (Parameter) {.name = "Period", .base = 10}; |
vinbel93 | 0:f8873e0badb2 | 31 | |
vinbel93 | 0:f8873e0badb2 | 32 | if (file != NULL) |
vinbel93 | 0:f8873e0badb2 | 33 | { |
vinbel93 | 0:f8873e0badb2 | 34 | char line[128]; |
vinbel93 | 0:f8873e0badb2 | 35 | |
vinbel93 | 0:f8873e0badb2 | 36 | // Lire ligne par ligne |
vinbel93 | 0:f8873e0badb2 | 37 | while (fgets(line, sizeof(line), file) != NULL) |
vinbel93 | 0:f8873e0badb2 | 38 | { |
vinbel93 | 0:f8873e0badb2 | 39 | // token = nom du paramètre |
vinbel93 | 0:f8873e0badb2 | 40 | token = strtok(line, delimiter); |
vinbel93 | 0:f8873e0badb2 | 41 | for (int i = 0; i < paramCount; i++) |
vinbel93 | 0:f8873e0badb2 | 42 | { |
vinbel93 | 0:f8873e0badb2 | 43 | if (strcmp(token, params[i].name) == 0) |
vinbel93 | 0:f8873e0badb2 | 44 | { |
vinbel93 | 0:f8873e0badb2 | 45 | // configValue = valeur du paramètre |
vinbel93 | 0:f8873e0badb2 | 46 | char* configValue = strtok(NULL, delimiter); |
vinbel93 | 0:f8873e0badb2 | 47 | // conversion en entier 64 bit |
vinbel93 | 0:f8873e0badb2 | 48 | params[i].value = strtoll(configValue, NULL, params[i].base); |
vinbel93 | 0:f8873e0badb2 | 49 | } |
vinbel93 | 0:f8873e0badb2 | 50 | } |
vinbel93 | 0:f8873e0badb2 | 51 | } |
vinbel93 | 0:f8873e0badb2 | 52 | |
vinbel93 | 0:f8873e0badb2 | 53 | fclose(file); |
vinbel93 | 0:f8873e0badb2 | 54 | } |
vinbel93 | 0:f8873e0badb2 | 55 | } |
vinbel93 | 0:f8873e0badb2 | 56 | |
vinbel93 | 0:f8873e0badb2 | 57 | // Fonction de lecture des capteurs |
vinbel93 | 0:f8873e0badb2 | 58 | void readSensors(char sensorInfo[]) |
vinbel93 | 0:f8873e0badb2 | 59 | { |
vinbel93 | 0:f8873e0badb2 | 60 | char sensorId = sensorInfo[0]; |
vinbel93 | 0:f8873e0badb2 | 61 | readSensorsTable[sensorId] (sensorInfo); |
vinbel93 | 0:f8873e0badb2 | 62 | } |
vinbel93 | 0:f8873e0badb2 | 63 | |
vinbel93 | 0:f8873e0badb2 | 64 | // Fonction pour combiner deux buffers en un |
vinbel93 | 0:f8873e0badb2 | 65 | int mergeBuffers(char* buffer1, int size1, char* buffer2, int size2, char* result) |
vinbel93 | 0:f8873e0badb2 | 66 | { |
vinbel93 | 0:f8873e0badb2 | 67 | memcpy(result, buffer1, size1); |
vinbel93 | 0:f8873e0badb2 | 68 | memcpy(result + size1, buffer2, size2); |
vinbel93 | 0:f8873e0badb2 | 69 | |
vinbel93 | 0:f8873e0badb2 | 70 | return size1 + size2; |
vinbel93 | 0:f8873e0badb2 | 71 | } |
vinbel93 | 0:f8873e0badb2 | 72 | |
vinbel93 | 0:f8873e0badb2 | 73 | // Fonction principale |
vinbel93 | 0:f8873e0badb2 | 74 | int main() |
vinbel93 | 0:f8873e0badb2 | 75 | { |
vinbel93 | 0:f8873e0badb2 | 76 | // Réinitialisation du Xbee |
vinbel93 | 0:f8873e0badb2 | 77 | reset = 0; |
vinbel93 | 0:f8873e0badb2 | 78 | wait(0.4); |
vinbel93 | 0:f8873e0badb2 | 79 | reset = 1; |
vinbel93 | 0:f8873e0badb2 | 80 | wait(1); |
vinbel93 | 0:f8873e0badb2 | 81 | |
vinbel93 | 0:f8873e0badb2 | 82 | // Vidage du buffer de lecture du Xbee |
vinbel93 | 0:f8873e0badb2 | 83 | while (xbee.readable()) |
vinbel93 | 0:f8873e0badb2 | 84 | { |
vinbel93 | 0:f8873e0badb2 | 85 | char dump = xbee.getc(); |
vinbel93 | 0:f8873e0badb2 | 86 | } |
vinbel93 | 0:f8873e0badb2 | 87 | |
vinbel93 | 0:f8873e0badb2 | 88 | // Lire la config et assigner le PAN ID |
vinbel93 | 0:f8873e0badb2 | 89 | readConfig(); |
vinbel93 | 0:f8873e0badb2 | 90 | setPanId(&xbee, params[PAN_ID].value); |
vinbel93 | 0:f8873e0badb2 | 91 | |
vinbel93 | 0:f8873e0badb2 | 92 | // Buffers pour les données des capteurs |
vinbel93 | 0:f8873e0badb2 | 93 | char buttonBuffer[2]; |
vinbel93 | 0:f8873e0badb2 | 94 | char acceleroBuffer[3]; |
vinbel93 | 0:f8873e0badb2 | 95 | buttonBuffer[0] = 0; // Numéro du capteur |
vinbel93 | 0:f8873e0badb2 | 96 | acceleroBuffer[0] = 1; // Numéro du capteur |
vinbel93 | 0:f8873e0badb2 | 97 | |
vinbel93 | 0:f8873e0badb2 | 98 | while (true) |
vinbel93 | 0:f8873e0badb2 | 99 | { |
vinbel93 | 0:f8873e0badb2 | 100 | // Lire les capteurs |
vinbel93 | 0:f8873e0badb2 | 101 | readSensors(buttonBuffer); |
vinbel93 | 0:f8873e0badb2 | 102 | readSensors(acceleroBuffer); |
vinbel93 | 0:f8873e0badb2 | 103 | |
vinbel93 | 0:f8873e0badb2 | 104 | // Envoi des données au coordinateur |
vinbel93 | 0:f8873e0badb2 | 105 | char transmitBuffer[3]; // Taille du buffer est 2 + 1 |
vinbel93 | 0:f8873e0badb2 | 106 | int size = mergeBuffers(buttonBuffer + 1, 1, acceleroBuffer + 1, 2, transmitBuffer); |
vinbel93 | 0:f8873e0badb2 | 107 | transmitRequest(&xbee, transmitBuffer, size); |
vinbel93 | 0:f8873e0badb2 | 108 | |
vinbel93 | 0:f8873e0badb2 | 109 | // Attente configurable |
vinbel93 | 0:f8873e0badb2 | 110 | wait_ms(params[PERIOD].value); |
vinbel93 | 0:f8873e0badb2 | 111 | } |
vinbel93 | 0:f8873e0badb2 | 112 | } |