app3

Dependencies:   mbed

Fork of Code_APP1 by Louis Beaulieu

Committer:
ericbisson
Date:
Tue Feb 14 04:11:07 2017 +0000
Revision:
9:dc81d8ee4a97
Parent:
8:6f2b7f9b0d0d
app3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ericbisson 9:dc81d8ee4a97 1 #include "xbee.h"
ericbisson 9:dc81d8ee4a97 2 #include "Accelerometer.h"
ericbisson 9:dc81d8ee4a97 3 #include "IO_p15.h"
ericbisson 9:dc81d8ee4a97 4 #include "config.h"
LouBe4 1:3430643e8ed4 5
ericbisson 9:dc81d8ee4a97 6 //#define __DEBUG__
ericbisson 9:dc81d8ee4a97 7 #ifdef __DEBUG__
ericbisson 9:dc81d8ee4a97 8 Serial pc(USBTX, USBRX, 9600);
ericbisson 9:dc81d8ee4a97 9 #endif
ericbisson 9:dc81d8ee4a97 10 PwmOut led1(LED1);
ericbisson 9:dc81d8ee4a97 11 //--------------------------------------------------------------//
ericbisson 9:dc81d8ee4a97 12 // APP stuff : tableau de pointeur de fonctions
ericbisson 9:dc81d8ee4a97 13 //--------------------------------------------------------------//
ericbisson 9:dc81d8ee4a97 14 #define NB_FUNCTIONS 2
ericbisson 9:dc81d8ee4a97 15 void func_accel(CArray*);
ericbisson 9:dc81d8ee4a97 16 void func_button(CArray*);
ericbisson 9:dc81d8ee4a97 17 void (*functions[NB_FUNCTIONS])(CArray*) = {func_accel, func_button};
ericbisson 9:dc81d8ee4a97 18 //--------------------------------------------------------------//
ericbisson 8:6f2b7f9b0d0d 19
LouBe4 0:52fba6f1554b 20 int main() {
ericbisson 9:dc81d8ee4a97 21 DigitalOut RESET(p8);
ericbisson 9:dc81d8ee4a97 22 Serial XBee(p13, p14, 9600);
ericbisson 6:2ec2a74c807f 23
ericbisson 9:dc81d8ee4a97 24 // Selon le lab, reset le Xbee
ericbisson 9:dc81d8ee4a97 25 RESET = 0;
ericbisson 9:dc81d8ee4a97 26 wait_ms(400);
ericbisson 9:dc81d8ee4a97 27 RESET = 1;
ericbisson 9:dc81d8ee4a97 28
ericbisson 9:dc81d8ee4a97 29 #ifdef __DEBUG__
ericbisson 9:dc81d8ee4a97 30 pc.format(8, SerialBase::None, 1);
ericbisson 9:dc81d8ee4a97 31 #endif
ericbisson 9:dc81d8ee4a97 32 XBee.format(8, SerialBase::None, 1);
ericbisson 9:dc81d8ee4a97 33
ericbisson 9:dc81d8ee4a97 34 CArray DATA_TO_SEND;
ericbisson 5:1a60144f7163 35
ericbisson 9:dc81d8ee4a97 36 // Addresse spéciale pour le coordinateur
ericbisson 9:dc81d8ee4a97 37 for (int j = 0; j < 8; j++)
ericbisson 9:dc81d8ee4a97 38 DATA_TO_SEND._64bit.bit[j] = 0;
ericbisson 9:dc81d8ee4a97 39 DATA_TO_SEND._16bit.bit[0] = 0xFF;
ericbisson 9:dc81d8ee4a97 40 DATA_TO_SEND._16bit.bit[1] = 0xFE;
ericbisson 9:dc81d8ee4a97 41
ericbisson 9:dc81d8ee4a97 42 DATA_TO_SEND._FrameType = 0x10; // Transmit Request
ericbisson 9:dc81d8ee4a97 43
ericbisson 9:dc81d8ee4a97 44 // options pour l'envoie au coordinateur
ericbisson 9:dc81d8ee4a97 45 DATA_TO_SEND.options = new char[2];
ericbisson 9:dc81d8ee4a97 46 DATA_TO_SEND.options[0] = 0; // broadcast
ericbisson 9:dc81d8ee4a97 47 DATA_TO_SEND.options[1] = 0; // other options
ericbisson 9:dc81d8ee4a97 48 DATA_TO_SEND.opt_size = 2;
ericbisson 9:dc81d8ee4a97 49
ericbisson 9:dc81d8ee4a97 50 bool IsInitialized = false;
ericbisson 9:dc81d8ee4a97 51 char InitBytes = 0;
ericbisson 5:1a60144f7163 52
LouBe4 3:5dcf9b78f3ad 53 while(1)
ericbisson 9:dc81d8ee4a97 54 {
ericbisson 9:dc81d8ee4a97 55 if (IsInitialized)
ericbisson 9:dc81d8ee4a97 56 {
ericbisson 9:dc81d8ee4a97 57 for (char i = 0; i < NB_FUNCTIONS; i++)
ericbisson 9:dc81d8ee4a97 58 {
ericbisson 9:dc81d8ee4a97 59 DATA_TO_SEND._ptr = NULL;
ericbisson 9:dc81d8ee4a97 60
ericbisson 9:dc81d8ee4a97 61 (*functions[i])(&DATA_TO_SEND);
ericbisson 9:dc81d8ee4a97 62
ericbisson 9:dc81d8ee4a97 63 if (DATA_TO_SEND._ptr != NULL)
ericbisson 9:dc81d8ee4a97 64 {
ericbisson 9:dc81d8ee4a97 65 led1 = !led1;
ericbisson 9:dc81d8ee4a97 66 send(&XBee, &DATA_TO_SEND);
ericbisson 9:dc81d8ee4a97 67 #ifdef __DEBUG__
ericbisson 9:dc81d8ee4a97 68 send(&pc, &DATA_TO_SEND); // debug only
ericbisson 9:dc81d8ee4a97 69 #endif
ericbisson 9:dc81d8ee4a97 70 delete DATA_TO_SEND._ptr;
ericbisson 9:dc81d8ee4a97 71 }
ericbisson 9:dc81d8ee4a97 72 }
ericbisson 9:dc81d8ee4a97 73 wait_ms( 1000 / SENSOR_FREQUENCY_HZ);
ericbisson 9:dc81d8ee4a97 74 }
ericbisson 9:dc81d8ee4a97 75 if (XBee.readable())
ericbisson 9:dc81d8ee4a97 76 {
ericbisson 9:dc81d8ee4a97 77 #ifdef __DEBUG__
ericbisson 9:dc81d8ee4a97 78 pc.putc(XBee.getc());
ericbisson 9:dc81d8ee4a97 79 #endif
ericbisson 9:dc81d8ee4a97 80 InitBytes++;
ericbisson 9:dc81d8ee4a97 81 if (InitBytes == 6)
ericbisson 9:dc81d8ee4a97 82 {
ericbisson 9:dc81d8ee4a97 83 IsInitialized = true;
ericbisson 9:dc81d8ee4a97 84 setPAN(&XBee, PAN_ID);
ericbisson 9:dc81d8ee4a97 85 #ifdef __DEBUG__
ericbisson 9:dc81d8ee4a97 86 setPAN(&pc, PAN_ID);
ericbisson 9:dc81d8ee4a97 87 #endif
ericbisson 9:dc81d8ee4a97 88 }
ericbisson 9:dc81d8ee4a97 89 }
LouBe4 3:5dcf9b78f3ad 90
ericbisson 9:dc81d8ee4a97 91 #ifdef __DEBUG__
ericbisson 9:dc81d8ee4a97 92 if (pc.readable())
ericbisson 9:dc81d8ee4a97 93 {
ericbisson 9:dc81d8ee4a97 94 XBee.putc(pc.getc());
ericbisson 9:dc81d8ee4a97 95 }
ericbisson 9:dc81d8ee4a97 96 #endif
LouBe4 3:5dcf9b78f3ad 97 }
ericbisson 5:1a60144f7163 98 }