Analog comms with xbee. Program for the Coordinator.
Dependencies: C12832_lcd mbed xbeeLibDannelly
Fork of Xbee_Hello_World_B by
main.cpp@1:4ce1378831fe, 2015-02-15 (annotated)
- Committer:
- dannellyz
- Date:
- Sun Feb 15 16:40:31 2015 +0000
- Revision:
- 1:4ce1378831fe
- Parent:
- 0:122702c9168a
- Child:
- 2:9b7d98c295d9
round 1;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tristanjph | 0:122702c9168a | 1 | #include "mbed.h" |
dannellyz | 1:4ce1378831fe | 2 | #include "xbee.h" // Include for xbee code |
dannellyz | 1:4ce1378831fe | 3 | #include "C12832_lcd.h" // Include for LCD code |
dannellyz | 1:4ce1378831fe | 4 | #include <string.h> //Iclude for string handeling |
dannellyz | 1:4ce1378831fe | 5 | xbee xbee1(p9,p10,p30); //Initalise xbee_lib varName(rx,tx,reset) |
dannellyz | 1:4ce1378831fe | 6 | DigitalOut rst1(p30); |
dannellyz | 1:4ce1378831fe | 7 | Serial pc(USBTX, USBRX); //Initalise PC serial comms |
dannellyz | 1:4ce1378831fe | 8 | C12832_LCD lcd; //Initialize LCD Screen |
tristanjph | 0:122702c9168a | 9 | |
tristanjph | 0:122702c9168a | 10 | int main() |
tristanjph | 0:122702c9168a | 11 | { |
dannellyz | 1:4ce1378831fe | 12 | //In AT mode we are constantly transmitting so it is |
dannellyz | 1:4ce1378831fe | 13 | //necessary to implement an external protocol |
dannellyz | 1:4ce1378831fe | 14 | //see https://developer.mbed.org/users/dannellyz/notebook/at-vs-api-when-why-how/# |
dannellyz | 1:4ce1378831fe | 15 | |
dannellyz | 1:4ce1378831fe | 16 | //For this we will use two buffers one for each |
dannellyz | 1:4ce1378831fe | 17 | //expected reading from the appBoard pot1/pot2 |
dannellyz | 1:4ce1378831fe | 18 | char readData1[202]; //Xbee buffer size is 202 bytes |
dannellyz | 1:4ce1378831fe | 19 | char readData2[202]; //Xbee buffer size is 202 bytes |
tristanjph | 0:122702c9168a | 20 | |
dannellyz | 1:4ce1378831fe | 21 | // reset the xbees (at least 200ns) |
dannellyz | 1:4ce1378831fe | 22 | rst1 = 0; |
dannellyz | 1:4ce1378831fe | 23 | wait_ms(1); |
dannellyz | 1:4ce1378831fe | 24 | rst1 = 1; |
dannellyz | 1:4ce1378831fe | 25 | wait_ms(1); |
dannellyz | 1:4ce1378831fe | 26 | |
dannellyz | 1:4ce1378831fe | 27 | //Setup LCD screen |
dannellyz | 1:4ce1378831fe | 28 | lcd.cls(); |
dannellyz | 1:4ce1378831fe | 29 | lcd.locate(0,1); |
dannellyz | 1:4ce1378831fe | 30 | |
dannellyz | 1:4ce1378831fe | 31 | |
tristanjph | 0:122702c9168a | 32 | while(1) { |
dannellyz | 1:4ce1378831fe | 33 | |
dannellyz | 1:4ce1378831fe | 34 | //RecieveDataChar allows you to read data in until the |
dannellyz | 1:4ce1378831fe | 35 | //seperator which is the second argument |
dannellyz | 1:4ce1378831fe | 36 | |
dannellyz | 1:4ce1378831fe | 37 | //Recieve data format POT2a |
dannellyz | 1:4ce1378831fe | 38 | xbee1.RecieveDataChar(readData1,'a'); //Read data from the XBee |
dannellyz | 1:4ce1378831fe | 39 | //Recieve data format POT1b |
dannellyz | 1:4ce1378831fe | 40 | xbee1.RecieveDataChar(readData2,'b'); //Read data from the XBee |
dannellyz | 1:4ce1378831fe | 41 | //Needed to keep in sync with other xbee |
dannellyz | 1:4ce1378831fe | 42 | wait(0.1); |
dannellyz | 1:4ce1378831fe | 43 | |
dannellyz | 1:4ce1378831fe | 44 | //Clean data by removing delimeter |
dannellyz | 1:4ce1378831fe | 45 | readData1[strlen(readData1)-1] = 0; |
dannellyz | 1:4ce1378831fe | 46 | readData2[strlen(readData2)-1] = 0; |
dannellyz | 1:4ce1378831fe | 47 | |
dannellyz | 1:4ce1378831fe | 48 | //Change values back to floats for possible use later |
dannellyz | 1:4ce1378831fe | 49 | float pot1 = atof(readData2); |
dannellyz | 1:4ce1378831fe | 50 | float pot2 = atof(readData1); |
dannellyz | 1:4ce1378831fe | 51 | |
dannellyz | 1:4ce1378831fe | 52 | //Echo data |
dannellyz | 1:4ce1378831fe | 53 | pc.printf("Pot1= %.2f Pot2= %.2f \n\r",pot1,pot2); |
dannellyz | 1:4ce1378831fe | 54 | lcd.printf("Pot1= %.2f Pot2= %.2f \n\r",pot1,pot2); |
tristanjph | 0:122702c9168a | 55 | } |
dannellyz | 1:4ce1378831fe | 56 | } |
dannellyz | 1:4ce1378831fe | 57 |