desloges-libioulle / Mbed 2 deprecated app3_router

Dependencies:   mbed-rtos mbed

Committer:
libv2001
Date:
Sun Feb 12 16:06:13 2017 +0000
Revision:
0:0545bc9f81e0
Hello

Who changed what in which revision?

UserRevisionLine numberNew contents of line
libv2001 0:0545bc9f81e0 1 #include "mbed.h"
libv2001 0:0545bc9f81e0 2 #include "rtos.h"
libv2001 0:0545bc9f81e0 3 #include "xbee.h"
libv2001 0:0545bc9f81e0 4
libv2001 0:0545bc9f81e0 5 #define nullptr 0
libv2001 0:0545bc9f81e0 6
libv2001 0:0545bc9f81e0 7 DigitalOut led1(LED1);
libv2001 0:0545bc9f81e0 8
libv2001 0:0545bc9f81e0 9 Serial pc(USBTX, USBRX);
libv2001 0:0545bc9f81e0 10
libv2001 0:0545bc9f81e0 11 DigitalIn * btn;
libv2001 0:0545bc9f81e0 12 I2C * acc;
libv2001 0:0545bc9f81e0 13 Serial *xbee;
libv2001 0:0545bc9f81e0 14 DigitalOut *xbeeRst;
libv2001 0:0545bc9f81e0 15
libv2001 0:0545bc9f81e0 16 Mutex xbeeMutex;
libv2001 0:0545bc9f81e0 17
libv2001 0:0545bc9f81e0 18 int ReadButton(char*);
libv2001 0:0545bc9f81e0 19 int ReadAccelerometer(char*);
libv2001 0:0545bc9f81e0 20
libv2001 0:0545bc9f81e0 21 const uint8_t DEVICE_COUNT = 2;
libv2001 0:0545bc9f81e0 22
libv2001 0:0545bc9f81e0 23 int (* readFunctions[DEVICE_COUNT]) (char *) = {&ReadButton, &ReadAccelerometer};
libv2001 0:0545bc9f81e0 24
libv2001 0:0545bc9f81e0 25 const int HARDWARE_RESET_SIGNAL = 0x10;
libv2001 0:0545bc9f81e0 26 const int JOINED_NETWORK_SIGNAL = 0x20;
libv2001 0:0545bc9f81e0 27 const int TICKER_SIGNAL = 0x40;
libv2001 0:0545bc9f81e0 28 const int RESPONSE_SIGNAL = 0x80;
libv2001 0:0545bc9f81e0 29
libv2001 0:0545bc9f81e0 30 Thread * XBeeConsumer;
libv2001 0:0545bc9f81e0 31 Thread * XBeeProducer;
libv2001 0:0545bc9f81e0 32 Ticker timer;
libv2001 0:0545bc9f81e0 33
libv2001 0:0545bc9f81e0 34 int responseStatus;
libv2001 0:0545bc9f81e0 35
libv2001 0:0545bc9f81e0 36 /*******************************************************/
libv2001 0:0545bc9f81e0 37 /**********************UTILITIES************************/
libv2001 0:0545bc9f81e0 38 /*******************************************************/
libv2001 0:0545bc9f81e0 39
libv2001 0:0545bc9f81e0 40 PinName GetPinName(const int p){
libv2001 0:0545bc9f81e0 41 switch(p){
libv2001 0:0545bc9f81e0 42 case 5: return p5;
libv2001 0:0545bc9f81e0 43 case 6: return p6;
libv2001 0:0545bc9f81e0 44 case 7: return p7;
libv2001 0:0545bc9f81e0 45 case 8: return p8;
libv2001 0:0545bc9f81e0 46 case 9: return p9;
libv2001 0:0545bc9f81e0 47 case 10: return p10;
libv2001 0:0545bc9f81e0 48 case 11: return p11;
libv2001 0:0545bc9f81e0 49 case 12: return p12;
libv2001 0:0545bc9f81e0 50 case 13: return p13;
libv2001 0:0545bc9f81e0 51 case 14: return p14;
libv2001 0:0545bc9f81e0 52 case 15: return p15;
libv2001 0:0545bc9f81e0 53 case 16: return p16;
libv2001 0:0545bc9f81e0 54 case 17: return p17;
libv2001 0:0545bc9f81e0 55 case 18: return p18;
libv2001 0:0545bc9f81e0 56 case 19: return p19;
libv2001 0:0545bc9f81e0 57 case 20: return p20;
libv2001 0:0545bc9f81e0 58 case 21: return p21;
libv2001 0:0545bc9f81e0 59 case 22: return p22;
libv2001 0:0545bc9f81e0 60 case 23: return p23;
libv2001 0:0545bc9f81e0 61 case 24: return p24;
libv2001 0:0545bc9f81e0 62 case 25: return p25;
libv2001 0:0545bc9f81e0 63 case 26: return p26;
libv2001 0:0545bc9f81e0 64 case 27: return p27;
libv2001 0:0545bc9f81e0 65 case 28: return p28;
libv2001 0:0545bc9f81e0 66 case 29: return p29;
libv2001 0:0545bc9f81e0 67 case 30: return p30;
libv2001 0:0545bc9f81e0 68 }
libv2001 0:0545bc9f81e0 69 pc.printf("Numero de pin invalid");
libv2001 0:0545bc9f81e0 70 return NC;
libv2001 0:0545bc9f81e0 71 }
libv2001 0:0545bc9f81e0 72
libv2001 0:0545bc9f81e0 73 /*******************************************************/
libv2001 0:0545bc9f81e0 74 /***********************CONFIG**************************/
libv2001 0:0545bc9f81e0 75 /*******************************************************/
libv2001 0:0545bc9f81e0 76 int panID;
libv2001 0:0545bc9f81e0 77 int pauseTime;
libv2001 0:0545bc9f81e0 78 int btnPin;
libv2001 0:0545bc9f81e0 79 int accSdaPin;
libv2001 0:0545bc9f81e0 80 int accSclPin;
libv2001 0:0545bc9f81e0 81 int xbeeTxPin;
libv2001 0:0545bc9f81e0 82 int xbeeRxPin;
libv2001 0:0545bc9f81e0 83 int xbeeRstPin;
libv2001 0:0545bc9f81e0 84
libv2001 0:0545bc9f81e0 85 char key[10];
libv2001 0:0545bc9f81e0 86
libv2001 0:0545bc9f81e0 87 LocalFileSystem local("local");
libv2001 0:0545bc9f81e0 88
libv2001 0:0545bc9f81e0 89 void ReadConfig(){
libv2001 0:0545bc9f81e0 90 FILE * f = fopen("/local/rooter.cfg", "r");
libv2001 0:0545bc9f81e0 91 fscanf(f,"%s %x", key, &panID);
libv2001 0:0545bc9f81e0 92 pc.printf("Lecture de la config %s : %04x\r\n", key, panID);
libv2001 0:0545bc9f81e0 93 fscanf(f,"%s %d", key, &pauseTime);
libv2001 0:0545bc9f81e0 94 pc.printf("Lecture de la config %s : %d\r\n", key, pauseTime);
libv2001 0:0545bc9f81e0 95 fscanf(f,"%s %d", key, &btnPin);
libv2001 0:0545bc9f81e0 96 pc.printf("Lecture de la config %s : %d\r\n", key, btnPin);
libv2001 0:0545bc9f81e0 97 fscanf(f,"%s %d %d", key, &accSdaPin, &accSclPin);
libv2001 0:0545bc9f81e0 98 pc.printf("Lecture de la config %s : %d %d\r\n", key, accSdaPin, accSclPin);
libv2001 0:0545bc9f81e0 99 fscanf(f,"%s %d %d %d", key, &xbeeTxPin, &xbeeRxPin, &xbeeRstPin);
libv2001 0:0545bc9f81e0 100 pc.printf("Lecture de la config %s : %d %d %d\r\n", key, xbeeTxPin, xbeeRxPin, xbeeRstPin);
libv2001 0:0545bc9f81e0 101 fclose(f);
libv2001 0:0545bc9f81e0 102 }
libv2001 0:0545bc9f81e0 103
libv2001 0:0545bc9f81e0 104 /*******************************************************/
libv2001 0:0545bc9f81e0 105 /**********************XBEE SEND************************/
libv2001 0:0545bc9f81e0 106 /*******************************************************/
libv2001 0:0545bc9f81e0 107
libv2001 0:0545bc9f81e0 108 char frameID = 0;
libv2001 0:0545bc9f81e0 109
libv2001 0:0545bc9f81e0 110 inline char GetFrameID(){
libv2001 0:0545bc9f81e0 111 ++frameID;
libv2001 0:0545bc9f81e0 112 if (frameID == 0){
libv2001 0:0545bc9f81e0 113 frameID = 1;
libv2001 0:0545bc9f81e0 114 }
libv2001 0:0545bc9f81e0 115 return frameID;
libv2001 0:0545bc9f81e0 116 }
libv2001 0:0545bc9f81e0 117
libv2001 0:0545bc9f81e0 118 const char coordinator64bit[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
libv2001 0:0545bc9f81e0 119 const char coordinator16bit[2] = {0xff, 0xfe};
libv2001 0:0545bc9f81e0 120
libv2001 0:0545bc9f81e0 121 void SetCheckSum(char * buffer){
libv2001 0:0545bc9f81e0 122 uint16_t length = GetFrameLength(buffer);
libv2001 0:0545bc9f81e0 123
libv2001 0:0545bc9f81e0 124 char sum = 0;
libv2001 0:0545bc9f81e0 125
libv2001 0:0545bc9f81e0 126 int max = length + 3;
libv2001 0:0545bc9f81e0 127
libv2001 0:0545bc9f81e0 128 for(int i = 3; i < max; ++i){
libv2001 0:0545bc9f81e0 129 sum += buffer[i];
libv2001 0:0545bc9f81e0 130 }
libv2001 0:0545bc9f81e0 131
libv2001 0:0545bc9f81e0 132 buffer[max] = 0xff - sum;
libv2001 0:0545bc9f81e0 133 }
libv2001 0:0545bc9f81e0 134
libv2001 0:0545bc9f81e0 135 void XBeeSend(char * buffer, int count){
libv2001 0:0545bc9f81e0 136 for ( int i = 0; i < count; ++i ){
libv2001 0:0545bc9f81e0 137 xbee->putc(buffer[i]);
libv2001 0:0545bc9f81e0 138 wait_us(25);
libv2001 0:0545bc9f81e0 139 }
libv2001 0:0545bc9f81e0 140 }
libv2001 0:0545bc9f81e0 141
libv2001 0:0545bc9f81e0 142 void XBeeSentTransmitCommand(char * data, int dataLength){
libv2001 0:0545bc9f81e0 143 char buffer[128];
libv2001 0:0545bc9f81e0 144 buffer[START_IDX] = START;
libv2001 0:0545bc9f81e0 145 buffer[LENGTH_MSB_IDX] = (dataLength + TRANSMIT_MIN_SIZE) >> 8;
libv2001 0:0545bc9f81e0 146 buffer[LENGTH_LSB_IDX] = (dataLength + TRANSMIT_MIN_SIZE) & 0xff;
libv2001 0:0545bc9f81e0 147 buffer[API_ID_IDX] = API_ID_TRANSMIT;
libv2001 0:0545bc9f81e0 148 buffer[FRAME_ID_IDX] = GetFrameID();
libv2001 0:0545bc9f81e0 149 memcpy(&buffer[TRANSMIT_64BIT_MSB_IDX], coordinator64bit, ADDR_64BIT_SIZE);
libv2001 0:0545bc9f81e0 150 memcpy(&buffer[TRANSMIT_16BIT_MSB_IDX], coordinator16bit, ADDR_16BIT_SIZE);
libv2001 0:0545bc9f81e0 151 buffer[TRANSMIT_BROADCAST_IDX] = TRANSMIT_DEFAULT_BROADCAST;
libv2001 0:0545bc9f81e0 152 buffer[TRANSMIT_OPT_IDX] = TRANSMIT_DEFAULT_OPT;
libv2001 0:0545bc9f81e0 153 memcpy(&buffer[TRANSMIT_DATA_IDX], data, dataLength);
libv2001 0:0545bc9f81e0 154
libv2001 0:0545bc9f81e0 155 SetCheckSum(buffer);
libv2001 0:0545bc9f81e0 156
libv2001 0:0545bc9f81e0 157 while(true) {
libv2001 0:0545bc9f81e0 158 XBeeSend(buffer, dataLength + TRANSMIT_MIN_SIZE + FRAME_MIN_SIZE);
libv2001 0:0545bc9f81e0 159
libv2001 0:0545bc9f81e0 160 Thread::signal_wait(RESPONSE_SIGNAL);
libv2001 0:0545bc9f81e0 161
libv2001 0:0545bc9f81e0 162 switch (responseStatus){
libv2001 0:0545bc9f81e0 163 case TRANSMIT_STATUS_OK:
libv2001 0:0545bc9f81e0 164 return;
libv2001 0:0545bc9f81e0 165 default:
libv2001 0:0545bc9f81e0 166 pc.printf("This Transit error occured : %02x\r\n", responseStatus);
libv2001 0:0545bc9f81e0 167 break;
libv2001 0:0545bc9f81e0 168 }
libv2001 0:0545bc9f81e0 169 }
libv2001 0:0545bc9f81e0 170 }
libv2001 0:0545bc9f81e0 171
libv2001 0:0545bc9f81e0 172 void XBeeSendATCommand(bool queue, char * type, char * data, int dataLength){
libv2001 0:0545bc9f81e0 173 char buffer[128];
libv2001 0:0545bc9f81e0 174 buffer[START_IDX] = START;
libv2001 0:0545bc9f81e0 175 buffer[LENGTH_MSB_IDX] = (dataLength + AT_MIN_SIZE) >> 8;
libv2001 0:0545bc9f81e0 176 buffer[LENGTH_LSB_IDX] = (dataLength + AT_MIN_SIZE) & 0xff;
libv2001 0:0545bc9f81e0 177 buffer[API_ID_IDX] = queue ? API_ID_AT_CMD_QUEUE : API_ID_AT_CMD;
libv2001 0:0545bc9f81e0 178 buffer[FRAME_ID_IDX] = GetFrameID();
libv2001 0:0545bc9f81e0 179 memcpy(&buffer[AT_CMD_ID_IDX], type, AT_CMD_ID_SIZE);
libv2001 0:0545bc9f81e0 180 memcpy(&buffer[AT_PARAM_IDX], data, dataLength);
libv2001 0:0545bc9f81e0 181
libv2001 0:0545bc9f81e0 182 SetCheckSum(buffer);
libv2001 0:0545bc9f81e0 183
libv2001 0:0545bc9f81e0 184 if (!ValidateCheckSum(buffer)){
libv2001 0:0545bc9f81e0 185 pc.printf("CheckSum problem\r\n");
libv2001 0:0545bc9f81e0 186 }
libv2001 0:0545bc9f81e0 187
libv2001 0:0545bc9f81e0 188 while(true){
libv2001 0:0545bc9f81e0 189 XBeeSend(buffer, dataLength + AT_MIN_SIZE + FRAME_MIN_SIZE);
libv2001 0:0545bc9f81e0 190
libv2001 0:0545bc9f81e0 191 Thread::signal_wait(RESPONSE_SIGNAL);
libv2001 0:0545bc9f81e0 192
libv2001 0:0545bc9f81e0 193 switch (responseStatus){
libv2001 0:0545bc9f81e0 194 case AT_CMD_RSP_STATUS_OK:
libv2001 0:0545bc9f81e0 195 return;
libv2001 0:0545bc9f81e0 196 case AT_CMD_RSP_STATUS_ERROR:
libv2001 0:0545bc9f81e0 197 case AT_CMD_RSP_STATUS_INVALID_CMD:
libv2001 0:0545bc9f81e0 198 case AT_CMD_RSP_STATUS_INVALID_PARAM:
libv2001 0:0545bc9f81e0 199 case AT_CMD_RSP_STATUS_TX_FAILURE:
libv2001 0:0545bc9f81e0 200 default:
libv2001 0:0545bc9f81e0 201 pc.printf("This AT error occured : %02x\r\n", responseStatus);
libv2001 0:0545bc9f81e0 202 break;
libv2001 0:0545bc9f81e0 203 }
libv2001 0:0545bc9f81e0 204 }
libv2001 0:0545bc9f81e0 205 }
libv2001 0:0545bc9f81e0 206
libv2001 0:0545bc9f81e0 207 inline void XBeeSendATID(){
libv2001 0:0545bc9f81e0 208 char idBuf[8];
libv2001 0:0545bc9f81e0 209 for (int i = 0; i < 8; ++i){
libv2001 0:0545bc9f81e0 210 idBuf[i] = (panID >> (56 - 8 * i)) & 0xff;
libv2001 0:0545bc9f81e0 211 }
libv2001 0:0545bc9f81e0 212 XBeeSendATCommand(true, "ID", idBuf, 8);
libv2001 0:0545bc9f81e0 213 }
libv2001 0:0545bc9f81e0 214
libv2001 0:0545bc9f81e0 215 inline void XBeeSendATWR(){
libv2001 0:0545bc9f81e0 216 XBeeSendATCommand(true, "WR", nullptr, 0);
libv2001 0:0545bc9f81e0 217 }
libv2001 0:0545bc9f81e0 218
libv2001 0:0545bc9f81e0 219 inline void XBeeSendATAC(){
libv2001 0:0545bc9f81e0 220 XBeeSendATCommand(true, "AC", nullptr, 0);
libv2001 0:0545bc9f81e0 221 }
libv2001 0:0545bc9f81e0 222
libv2001 0:0545bc9f81e0 223 /*******************************************************/
libv2001 0:0545bc9f81e0 224 /**********************XBEE READ************************/
libv2001 0:0545bc9f81e0 225 /*******************************************************/
libv2001 0:0545bc9f81e0 226
libv2001 0:0545bc9f81e0 227 void HandleXbeeTransmitStatus(char * cmd){
libv2001 0:0545bc9f81e0 228 switch(cmd[TRANSMIT_STATUS_DELIVERY_STATUS_IDX]){
libv2001 0:0545bc9f81e0 229 case TRANSMIT_STATUS_OK:
libv2001 0:0545bc9f81e0 230 responseStatus = cmd[TRANSMIT_STATUS_DELIVERY_STATUS_IDX];
libv2001 0:0545bc9f81e0 231 XBeeProducer->signal_set(RESPONSE_SIGNAL);
libv2001 0:0545bc9f81e0 232 break;
libv2001 0:0545bc9f81e0 233 default:
libv2001 0:0545bc9f81e0 234 pc.printf("Unhandled transmit status received : %02x\r\n", cmd[TRANSMIT_STATUS_DELIVERY_STATUS_IDX]);
libv2001 0:0545bc9f81e0 235 }
libv2001 0:0545bc9f81e0 236 }
libv2001 0:0545bc9f81e0 237
libv2001 0:0545bc9f81e0 238 void HandleXbeeModemStatus(char * cmd){
libv2001 0:0545bc9f81e0 239 switch(cmd[MODEM_STATUS_STATUS_IDX]){
libv2001 0:0545bc9f81e0 240 case MODEM_STATUS_HARDWARE_RST:
libv2001 0:0545bc9f81e0 241 XBeeProducer->signal_set(HARDWARE_RESET_SIGNAL);
libv2001 0:0545bc9f81e0 242 break;
libv2001 0:0545bc9f81e0 243 case MODEM_STATUS_JOINED_NETWORK:
libv2001 0:0545bc9f81e0 244 XBeeProducer->signal_set(JOINED_NETWORK_SIGNAL);
libv2001 0:0545bc9f81e0 245 break;
libv2001 0:0545bc9f81e0 246 case MODEM_STATUS_DISASSOCIATED:
libv2001 0:0545bc9f81e0 247 XBeeProducer->signal_clr(JOINED_NETWORK_SIGNAL);
libv2001 0:0545bc9f81e0 248 break;
libv2001 0:0545bc9f81e0 249 default:
libv2001 0:0545bc9f81e0 250 pc.printf("Unhandled modem status received : %02x\r\n", cmd[MODEM_STATUS_STATUS_IDX]);
libv2001 0:0545bc9f81e0 251 break;
libv2001 0:0545bc9f81e0 252 }
libv2001 0:0545bc9f81e0 253 }
libv2001 0:0545bc9f81e0 254
libv2001 0:0545bc9f81e0 255 void HandleXBeeATCommandResponse(char * cmd){
libv2001 0:0545bc9f81e0 256 responseStatus = cmd[AT_CMD_RSP_STATUS_IDX];
libv2001 0:0545bc9f81e0 257 XBeeProducer->signal_set(RESPONSE_SIGNAL);
libv2001 0:0545bc9f81e0 258 }
libv2001 0:0545bc9f81e0 259
libv2001 0:0545bc9f81e0 260 void HandleXbeeReceivedCommand(char * cmd){
libv2001 0:0545bc9f81e0 261 switch(cmd[API_ID_IDX]){
libv2001 0:0545bc9f81e0 262 case API_ID_AT_CMD_RSP:
libv2001 0:0545bc9f81e0 263 HandleXBeeATCommandResponse(cmd);
libv2001 0:0545bc9f81e0 264 break;
libv2001 0:0545bc9f81e0 265 case API_ID_MODEM_STATUS:
libv2001 0:0545bc9f81e0 266 HandleXbeeModemStatus(cmd);
libv2001 0:0545bc9f81e0 267 break;
libv2001 0:0545bc9f81e0 268 case API_ID_TRANSMIT_STATUS:
libv2001 0:0545bc9f81e0 269 HandleXbeeTransmitStatus(cmd);
libv2001 0:0545bc9f81e0 270 break;
libv2001 0:0545bc9f81e0 271 default:
libv2001 0:0545bc9f81e0 272 pc.printf("Unhandle XBee Command received : %02x\r\n", cmd[API_ID_IDX]);
libv2001 0:0545bc9f81e0 273 }
libv2001 0:0545bc9f81e0 274 }
libv2001 0:0545bc9f81e0 275
libv2001 0:0545bc9f81e0 276 /*******************************************************/
libv2001 0:0545bc9f81e0 277 /************************INIT***************************/
libv2001 0:0545bc9f81e0 278 /*******************************************************/
libv2001 0:0545bc9f81e0 279
libv2001 0:0545bc9f81e0 280 bool InitAcc(){
libv2001 0:0545bc9f81e0 281 char cmd[2];
libv2001 0:0545bc9f81e0 282 char data[1];
libv2001 0:0545bc9f81e0 283 // Lecture du registre WHO_AM_I (0x2a)
libv2001 0:0545bc9f81e0 284 cmd[0] = 0x0d;
libv2001 0:0545bc9f81e0 285 acc->write(0x1d<<1, cmd, 1, true);
libv2001 0:0545bc9f81e0 286 acc->read(0x1d<<1, data, 1);
libv2001 0:0545bc9f81e0 287 if (data[0] != 0x2a){
libv2001 0:0545bc9f81e0 288 return false;
libv2001 0:0545bc9f81e0 289 }
libv2001 0:0545bc9f81e0 290
libv2001 0:0545bc9f81e0 291 //Activation de l'accéléromètre
libv2001 0:0545bc9f81e0 292 cmd[0] = 0x2a; // CTRL_REG1
libv2001 0:0545bc9f81e0 293 cmd[1] = 0x01; // Active = 1
libv2001 0:0545bc9f81e0 294 acc->write(0x1d<<1,cmd,2,false);
libv2001 0:0545bc9f81e0 295
libv2001 0:0545bc9f81e0 296 return true;
libv2001 0:0545bc9f81e0 297 }
libv2001 0:0545bc9f81e0 298
libv2001 0:0545bc9f81e0 299 bool InitXBee(){
libv2001 0:0545bc9f81e0 300 xbeeRst->write(0);
libv2001 0:0545bc9f81e0 301 wait(0.4);
libv2001 0:0545bc9f81e0 302 xbeeRst->write(1);
libv2001 0:0545bc9f81e0 303
libv2001 0:0545bc9f81e0 304 Thread::signal_wait(HARDWARE_RESET_SIGNAL);
libv2001 0:0545bc9f81e0 305
libv2001 0:0545bc9f81e0 306 XBeeSendATID();
libv2001 0:0545bc9f81e0 307 XBeeSendATWR();
libv2001 0:0545bc9f81e0 308 XBeeSendATAC();
libv2001 0:0545bc9f81e0 309
libv2001 0:0545bc9f81e0 310 Thread::signal_wait(JOINED_NETWORK_SIGNAL);
libv2001 0:0545bc9f81e0 311
libv2001 0:0545bc9f81e0 312 pc.printf("XBee configured\r\n");
libv2001 0:0545bc9f81e0 313
libv2001 0:0545bc9f81e0 314 return true;
libv2001 0:0545bc9f81e0 315 }
libv2001 0:0545bc9f81e0 316
libv2001 0:0545bc9f81e0 317 /*******************************************************/
libv2001 0:0545bc9f81e0 318 /************************READ***************************/
libv2001 0:0545bc9f81e0 319 /*******************************************************/
libv2001 0:0545bc9f81e0 320
libv2001 0:0545bc9f81e0 321 int ReadButton(char* buffer){
libv2001 0:0545bc9f81e0 322 buffer[0] = 'B';
libv2001 0:0545bc9f81e0 323 buffer[1] = 'T';
libv2001 0:0545bc9f81e0 324 buffer[2] = 'N';
libv2001 0:0545bc9f81e0 325 buffer[3] = (*btn); // 0x01 | 0x00
libv2001 0:0545bc9f81e0 326
libv2001 0:0545bc9f81e0 327 return 4;
libv2001 0:0545bc9f81e0 328 }
libv2001 0:0545bc9f81e0 329
libv2001 0:0545bc9f81e0 330 int ReadAccelerometer(char* buffer){
libv2001 0:0545bc9f81e0 331 const char deviceAddr = 0x1d<<1;
libv2001 0:0545bc9f81e0 332 char temp[1];
libv2001 0:0545bc9f81e0 333 temp[0] = 0x01; // Adresse de OUT_X_MSB
libv2001 0:0545bc9f81e0 334
libv2001 0:0545bc9f81e0 335 buffer[0] = 'A';
libv2001 0:0545bc9f81e0 336 buffer[1] = 'C';
libv2001 0:0545bc9f81e0 337 buffer[2] = 'C';
libv2001 0:0545bc9f81e0 338 // Lecture des données X, Y et Z
libv2001 0:0545bc9f81e0 339 acc->write(deviceAddr, temp, 1, true);
libv2001 0:0545bc9f81e0 340 acc->read(deviceAddr, &buffer[3], 6);
libv2001 0:0545bc9f81e0 341
libv2001 0:0545bc9f81e0 342 SetCheckSum(buffer);
libv2001 0:0545bc9f81e0 343
libv2001 0:0545bc9f81e0 344 //Number of bytes to send
libv2001 0:0545bc9f81e0 345 return 9;
libv2001 0:0545bc9f81e0 346 }
libv2001 0:0545bc9f81e0 347
libv2001 0:0545bc9f81e0 348 void ReadDevices(){
libv2001 0:0545bc9f81e0 349 int count;
libv2001 0:0545bc9f81e0 350 char buffer[64];
libv2001 0:0545bc9f81e0 351
libv2001 0:0545bc9f81e0 352 for ( int i = 0; i < DEVICE_COUNT; ++i){
libv2001 0:0545bc9f81e0 353 count = readFunctions[i](buffer);
libv2001 0:0545bc9f81e0 354 XBeeSentTransmitCommand(buffer, count);
libv2001 0:0545bc9f81e0 355 }
libv2001 0:0545bc9f81e0 356 }
libv2001 0:0545bc9f81e0 357
libv2001 0:0545bc9f81e0 358 void ConsumerMain(){
libv2001 0:0545bc9f81e0 359 char buffer[128];
libv2001 0:0545bc9f81e0 360 while(true){
libv2001 0:0545bc9f81e0 361 while(!xbee->readable()){
libv2001 0:0545bc9f81e0 362 continue;
libv2001 0:0545bc9f81e0 363 }
libv2001 0:0545bc9f81e0 364 buffer[START_IDX] = xbee->getc();
libv2001 0:0545bc9f81e0 365 if (buffer[START_IDX] != START){
libv2001 0:0545bc9f81e0 366 pc.printf("Wrong start byte received : %02x\r\n", buffer[START_IDX]);
libv2001 0:0545bc9f81e0 367 continue;
libv2001 0:0545bc9f81e0 368 }
libv2001 0:0545bc9f81e0 369 buffer[LENGTH_MSB_IDX] = xbee->getc();
libv2001 0:0545bc9f81e0 370 buffer[LENGTH_LSB_IDX] = xbee->getc();
libv2001 0:0545bc9f81e0 371 int length = GetFrameLength(buffer);
libv2001 0:0545bc9f81e0 372 for (int i = 0; i <= length; ++i){
libv2001 0:0545bc9f81e0 373 buffer[i + API_ID_IDX] = xbee->getc();
libv2001 0:0545bc9f81e0 374 }
libv2001 0:0545bc9f81e0 375
libv2001 0:0545bc9f81e0 376 if (!ValidateCheckSum(buffer)){
libv2001 0:0545bc9f81e0 377 pc.printf("Bad CheckSum : %02x\r\n", buffer[length + FRAME_MIN_SIZE - 1]);
libv2001 0:0545bc9f81e0 378 continue;
libv2001 0:0545bc9f81e0 379 }
libv2001 0:0545bc9f81e0 380
libv2001 0:0545bc9f81e0 381 HandleXbeeReceivedCommand(buffer);
libv2001 0:0545bc9f81e0 382 }
libv2001 0:0545bc9f81e0 383 }
libv2001 0:0545bc9f81e0 384
libv2001 0:0545bc9f81e0 385 bool ProducerInit(){
libv2001 0:0545bc9f81e0 386 //Initilisation
libv2001 0:0545bc9f81e0 387 if (!InitAcc()){
libv2001 0:0545bc9f81e0 388 pc.printf("Connection problem with the ACC\r\n");
libv2001 0:0545bc9f81e0 389 return false;
libv2001 0:0545bc9f81e0 390 }
libv2001 0:0545bc9f81e0 391
libv2001 0:0545bc9f81e0 392 if (!InitXBee()){
libv2001 0:0545bc9f81e0 393 pc.printf("Connection problem with the XBee\r\n");
libv2001 0:0545bc9f81e0 394 return false;
libv2001 0:0545bc9f81e0 395 }
libv2001 0:0545bc9f81e0 396
libv2001 0:0545bc9f81e0 397 return true;
libv2001 0:0545bc9f81e0 398 }
libv2001 0:0545bc9f81e0 399
libv2001 0:0545bc9f81e0 400 void Tick(){
libv2001 0:0545bc9f81e0 401 XBeeProducer->signal_set(TICKER_SIGNAL);
libv2001 0:0545bc9f81e0 402 }
libv2001 0:0545bc9f81e0 403
libv2001 0:0545bc9f81e0 404 void ProducerMain(const void*){
libv2001 0:0545bc9f81e0 405 if (!ProducerInit()){
libv2001 0:0545bc9f81e0 406 pc.printf("Initialization problem\r\n");
libv2001 0:0545bc9f81e0 407 return;
libv2001 0:0545bc9f81e0 408 }
libv2001 0:0545bc9f81e0 409
libv2001 0:0545bc9f81e0 410 timer.attach(&Tick, pauseTime);
libv2001 0:0545bc9f81e0 411
libv2001 0:0545bc9f81e0 412 while(true){
libv2001 0:0545bc9f81e0 413 Thread::signal_wait(TICKER_SIGNAL);
libv2001 0:0545bc9f81e0 414 ReadDevices();
libv2001 0:0545bc9f81e0 415 }
libv2001 0:0545bc9f81e0 416 }
libv2001 0:0545bc9f81e0 417
libv2001 0:0545bc9f81e0 418 int main() {
libv2001 0:0545bc9f81e0 419 // Lecture de la configuration.
libv2001 0:0545bc9f81e0 420 ReadConfig();
libv2001 0:0545bc9f81e0 421
libv2001 0:0545bc9f81e0 422 //Créer les interfaces de communication des capteurs avec les données de la config.
libv2001 0:0545bc9f81e0 423 DigitalIn mainBtn(GetPinName(btnPin));
libv2001 0:0545bc9f81e0 424 I2C mainAcc(GetPinName(accSdaPin), GetPinName(accSclPin));
libv2001 0:0545bc9f81e0 425 Serial mainXbee(GetPinName(xbeeTxPin), GetPinName(xbeeRxPin));
libv2001 0:0545bc9f81e0 426 DigitalOut mainXbeeRst(GetPinName(xbeeRstPin));
libv2001 0:0545bc9f81e0 427
libv2001 0:0545bc9f81e0 428 //Rendre les interfaces de communication globaux.
libv2001 0:0545bc9f81e0 429 btn = &mainBtn;
libv2001 0:0545bc9f81e0 430 acc = &mainAcc;
libv2001 0:0545bc9f81e0 431 xbee = &mainXbee;
libv2001 0:0545bc9f81e0 432 xbeeRst = &mainXbeeRst;
libv2001 0:0545bc9f81e0 433
libv2001 0:0545bc9f81e0 434 Thread consumer(ConsumerMain);
libv2001 0:0545bc9f81e0 435 XBeeConsumer = &consumer;
libv2001 0:0545bc9f81e0 436
libv2001 0:0545bc9f81e0 437 Thread producer(ProducerMain);
libv2001 0:0545bc9f81e0 438 XBeeProducer = &producer;
libv2001 0:0545bc9f81e0 439
libv2001 0:0545bc9f81e0 440 // Mettre la thread principale dans un état de waiting à l'infinie
libv2001 0:0545bc9f81e0 441 Thread::signal_wait(0x1);
libv2001 0:0545bc9f81e0 442 }