A toy program reading LLAP messages from a XRF radio. Switches LED1 upon receiving aXXLEDON---- and xAALEDOFF--- messages.
main.cpp@0:4ead3e5b879e, 2013-11-10 (annotated)
- Committer:
- 8fromPi
- Date:
- Sun Nov 10 18:29:34 2013 +0000
- Revision:
- 0:4ead3e5b879e
A toy program reading LLAP messages via XRF radio modules
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
8fromPi | 0:4ead3e5b879e | 1 | /* How-to for using Ciseco XRF modules as serial devices, also showing |
8fromPi | 0:4ead3e5b879e | 2 | * toy LLAP example using the "lightweight-local-automation-protocol" |
8fromPi | 0:4ead3e5b879e | 3 | * and how to react to a PINATA message (remote control of a LED) |
8fromPi | 0:4ead3e5b879e | 4 | * The main() performs a pseudo activity and the response to incoming XRF messages is linked to an interrupt |
8fromPi | 0:4ead3e5b879e | 5 | * |
8fromPi | 0:4ead3e5b879e | 6 | * This example can be condensed to a dew dozen lines, the REM marked lines are just for visual confirmation. |
8fromPi | 0:4ead3e5b879e | 7 | */ |
8fromPi | 0:4ead3e5b879e | 8 | |
8fromPi | 0:4ead3e5b879e | 9 | #include "mbed.h" |
8fromPi | 0:4ead3e5b879e | 10 | #include <string> aXXLEDOFF--- |
8fromPi | 0:4ead3e5b879e | 11 | #include "C12832_lcd.h" // REM |
8fromPi | 0:4ead3e5b879e | 12 | |
8fromPi | 0:4ead3e5b879e | 13 | // Code segments for debugging and eye candy. You can remove REM commented lines, they're not essential |
8fromPi | 0:4ead3e5b879e | 14 | DigitalOut ledrec(LED3); // just for some diagnostics REM |
8fromPi | 0:4ead3e5b879e | 15 | DigitalOut ledmsg(LED4); // REM |
8fromPi | 0:4ead3e5b879e | 16 | C12832_LCD screen; // comment all 'screen' and ledrec/ledmsg stuff when not using mbed Applicatio Board REM |
8fromPi | 0:4ead3e5b879e | 17 | // end of eye candy definitions. |
8fromPi | 0:4ead3e5b879e | 18 | |
8fromPi | 0:4ead3e5b879e | 19 | DigitalOut led(LED1); // the one we are going to let blink via PINATA/LLAP (see link below) |
8fromPi | 0:4ead3e5b879e | 20 | |
8fromPi | 0:4ead3e5b879e | 21 | Serial XRF(p9, p10); // mbed Application Board: p9 = tx, p10 = rx, or whever you're using rx and tx |
8fromPi | 0:4ead3e5b879e | 22 | char XRFbuffer[12]; // a ring buffer plus counter variable |
8fromPi | 0:4ead3e5b879e | 23 | unsigned char i = 0; |
8fromPi | 0:4ead3e5b879e | 24 | string XRFmsg; |
8fromPi | 0:4ead3e5b879e | 25 | |
8fromPi | 0:4ead3e5b879e | 26 | string ourDeviceHeader = "aXX"; // the a intro character plus two character device ID as detailed |
8fromPi | 0:4ead3e5b879e | 27 | // http://openmicros.org/index.php/articles/93-llap-lightweight-local-automation-protocol/pinata-the-easy-way-to-rf-control-your-micro/200-pinata |
8fromPi | 0:4ead3e5b879e | 28 | // aXXLEDON---- or aXXLEDOFF--- |
8fromPi | 0:4ead3e5b879e | 29 | |
8fromPi | 0:4ead3e5b879e | 30 | // print a character on the Application Board screen |
8fromPi | 0:4ead3e5b879e | 31 | void print(char c) { // REM |
8fromPi | 0:4ead3e5b879e | 32 | screen.printf("%c", c); // REM |
8fromPi | 0:4ead3e5b879e | 33 | } // REM |
8fromPi | 0:4ead3e5b879e | 34 | // end of printing routine |
8fromPi | 0:4ead3e5b879e | 35 | |
8fromPi | 0:4ead3e5b879e | 36 | |
8fromPi | 0:4ead3e5b879e | 37 | void receive() { |
8fromPi | 0:4ead3e5b879e | 38 | // if a character is received, blink the ledrec once and print it on screen |
8fromPi | 0:4ead3e5b879e | 39 | ledrec = !ledrec; // REM |
8fromPi | 0:4ead3e5b879e | 40 | char recd = XRF.getc(); // get character from serial |
8fromPi | 0:4ead3e5b879e | 41 | print(recd); // REM |
8fromPi | 0:4ead3e5b879e | 42 | XRFbuffer[i] = recd; // add to ring buffer |
8fromPi | 0:4ead3e5b879e | 43 | i++; // increase ring buffer counter |
8fromPi | 0:4ead3e5b879e | 44 | |
8fromPi | 0:4ead3e5b879e | 45 | if (i == 12) { // whenever we have 12 characters together (i.e. every time after the first 12) |
8fromPi | 0:4ead3e5b879e | 46 | ledmsg = !ledmsg; // blink the ledmsg REN |
8fromPi | 0:4ead3e5b879e | 47 | i = 11; // decrease the buffer counter by one so with the next char we arrive here again |
8fromPi | 0:4ead3e5b879e | 48 | |
8fromPi | 0:4ead3e5b879e | 49 | XRFmsg = ""; // define an empty string |
8fromPi | 0:4ead3e5b879e | 50 | // 12 chrs per message, copy the ring buffer into the string |
8fromPi | 0:4ead3e5b879e | 51 | for (unsigned char j=0; j<12; j++) { |
8fromPi | 0:4ead3e5b879e | 52 | XRFmsg += (char)XRFbuffer[j]; |
8fromPi | 0:4ead3e5b879e | 53 | if (j < 11) { |
8fromPi | 0:4ead3e5b879e | 54 | XRFbuffer[j] = XRFbuffer[j+1]; // for all but one character shift the entries in the ring buffer by 1 |
8fromPi | 0:4ead3e5b879e | 55 | } |
8fromPi | 0:4ead3e5b879e | 56 | } |
8fromPi | 0:4ead3e5b879e | 57 | |
8fromPi | 0:4ead3e5b879e | 58 | //XRF.printf("/%s", XRFmsg); // REM debug only -- check the content of the parse string |
8fromPi | 0:4ead3e5b879e | 59 | |
8fromPi | 0:4ead3e5b879e | 60 | // parse the XRF message |
8fromPi | 0:4ead3e5b879e | 61 | if ( XRFmsg.substr(0,3) == ourDeviceHeader ) { |
8fromPi | 0:4ead3e5b879e | 62 | // seems the message is for us: device XX |
8fromPi | 0:4ead3e5b879e | 63 | |
8fromPi | 0:4ead3e5b879e | 64 | // this is the most trivial reaction to a LLAP message |
8fromPi | 0:4ead3e5b879e | 65 | // if you know your pin names, this piece of code should be generalised to dynamically allow |
8fromPi | 0:4ead3e5b879e | 66 | // switching of any pin via LLAP. |
8fromPi | 0:4ead3e5b879e | 67 | // See http://openmicros.org/index.php/articles/93-llap-lightweight-local-automation-protocol/pinata-the-easy-way-to-rf-control-your-micro/203-pinata-for-mbed |
8fromPi | 0:4ead3e5b879e | 68 | // for further inspiration. Their code, however, expects a LPC1768. |
8fromPi | 0:4ead3e5b879e | 69 | if (XRFmsg.substr(3, 5) == "LEDON") { |
8fromPi | 0:4ead3e5b879e | 70 | led = 1; |
8fromPi | 0:4ead3e5b879e | 71 | } |
8fromPi | 0:4ead3e5b879e | 72 | if (XRFmsg.substr(3, 6) == "LEDOFF") { |
8fromPi | 0:4ead3e5b879e | 73 | led = 0; |
8fromPi | 0:4ead3e5b879e | 74 | } |
8fromPi | 0:4ead3e5b879e | 75 | |
8fromPi | 0:4ead3e5b879e | 76 | } |
8fromPi | 0:4ead3e5b879e | 77 | // end of parsing |
8fromPi | 0:4ead3e5b879e | 78 | |
8fromPi | 0:4ead3e5b879e | 79 | ledmsg != ledmsg; // REM |
8fromPi | 0:4ead3e5b879e | 80 | } |
8fromPi | 0:4ead3e5b879e | 81 | ledrec = !ledrec; // REM |
8fromPi | 0:4ead3e5b879e | 82 | } |
8fromPi | 0:4ead3e5b879e | 83 | |
8fromPi | 0:4ead3e5b879e | 84 | Timer pseudo_activity; |
8fromPi | 0:4ead3e5b879e | 85 | |
8fromPi | 0:4ead3e5b879e | 86 | int main() { |
8fromPi | 0:4ead3e5b879e | 87 | XRF.attach(&receive); // attach the receive function to serial interrupt |
8fromPi | 0:4ead3e5b879e | 88 | |
8fromPi | 0:4ead3e5b879e | 89 | while (1) { |
8fromPi | 0:4ead3e5b879e | 90 | // do important stuff |
8fromPi | 0:4ead3e5b879e | 91 | pseud_activity.start(); |
8fromPi | 0:4ead3e5b879e | 92 | XRF.printf("Oi, I cannot only receive LLAP messages, I'm actually capable of doing any length of serial communication\n"); |
8fromPi | 0:4ead3e5b879e | 93 | while (pseudo_activity.read() < 10) { |
8fromPi | 0:4ead3e5b879e | 94 | // do more important stuff |
8fromPi | 0:4ead3e5b879e | 95 | } |
8fromPi | 0:4ead3e5b879e | 96 | pseudo_activity.reset(); |
8fromPi | 0:4ead3e5b879e | 97 | } |
8fromPi | 0:4ead3e5b879e | 98 | } |