Read 3 floats from rfduino and sets rgb led on freedom board
RFduino Project
- Part 1: http://community.arm.com/groups/embedded/blog/2013/12/02/fun-with-rfduino
- Part 2: http://community.arm.com/groups/embedded/blog/2014/01/07/fun-with-rfduino-pt2-it-s-alive
- Project:
Import programRFDuino_example
Read 3 floats from rfduino and sets rgb led on freedom board
Revision 2:48e784d21870, committed 2014-01-08
- Comitter:
- dbarbi1
- Date:
- Wed Jan 08 20:58:33 2014 +0000
- Parent:
- 1:d5258745f048
- Commit message:
- Updates and recommendations from Sam.
Changed in this revision
RFDuino.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/RFDuino.lib Mon Jan 06 15:58:43 2014 +0000 +++ b/RFDuino.lib Wed Jan 08 20:58:33 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/dbarbi1/code/RFDuino/#310c07d23100 +http://mbed.org/users/dbarbi1/code/RFDuino/#7cbe6036c44e
--- a/main.cpp Mon Jan 06 15:58:43 2014 +0000 +++ b/main.cpp Wed Jan 08 20:58:33 2014 +0000 @@ -1,70 +1,78 @@ #include "mbed.h" #include "RFDuino.h" -typedef union { - float fp; - unsigned char byte[4]; -} byteyFloat; - -typedef struct { - byteyFloat red; - byteyFloat green; - byteyFloat blue; -} RGB; - -#define BBLINK blue=0;wait(0.5);blue=1;wait(0.5); - PwmOut red(LED_RED); PwmOut green(LED_GREEN); PwmOut blue(LED_BLUE); RFDuino rfd(PTC4, PTC3); //defaults to 9600 baud -void byteSwap(RGB* rgb) { - byteyFloat temp; - - temp.fp = rgb->red.fp; - rgb->red.byte[0] = temp.byte[3]; - rgb->red.byte[1] = temp.byte[2]; - rgb->red.byte[2] = temp.byte[1]; - rgb->red.byte[3] = temp.byte[0]; - - temp.fp = rgb->blue.fp; - rgb->blue.byte[0] = temp.byte[3]; - rgb->blue.byte[1] = temp.byte[2]; - rgb->blue.byte[2] = temp.byte[1]; - rgb->blue.byte[3] = temp.byte[0]; - - temp.fp = rgb->green.fp; - rgb->green.byte[0] = temp.byte[3]; - rgb->green.byte[1] = temp.byte[2]; - rgb->green.byte[2] = temp.byte[1]; - rgb->green.byte[3] = temp.byte[0]; +typedef union { + float fp; + unsigned int uint_32; +} intFloat; + +typedef struct { + intFloat red; + intFloat green; + intFloat blue; +} RGB; + +void bblink(void) +{ + blue=0; + wait(0.5); + blue=1; + wait(0.5); +} + +void byteSwap(RGB* rgb) +{ + intFloat temp; + + temp.uint_32 = __REV(rgb->red.uint_32); + rgb->red.fp = temp.fp; + + temp.uint_32 = __REV(rgb->blue.uint_32); + rgb->blue.fp = temp.fp; + + temp.uint_32 = __REV(rgb->green.uint_32); + rgb->green.fp = temp.fp; + } -int main() { - RGB rgb; +int main() +{ + RGB rgb = {1.0f, 1.0f, 1.0f}; - //turn off lights - red=green=blue=1; - //init rgb data struct - rgb.red.fp=rgb.blue.fp=rgb.green.fp=1.0; - wait(2); //make sure to wait at least 2 seconds before handshake - + red = blue = green = 1.0f; + wait(2.0f); //make sure to wait at least 2 seconds before handshake + //flash red and halt here if rfduino is not on the line - if(!rfd.handshake()) { red=0; while(1);} + if(!rfd.handshake()) { + while(1) { + red = !red; + wait(0.2f); + } + } + //wait for a connection - while(!rfd.isConnected()) {BBLINK;} + while(!rfd.isConnected()) { + bblink(); + } - while(1) { - if(rfd.dataReady()) { + while(1) { + if(rfd.readable()) { //copy data into data struct - rfd.copyData(&rgb.red.byte[0], sizeof(rgb)); - byteSwap(&rgb); //re-arrange bytes + rfd.read((unsigned char*)&rgb.red, sizeof(rgb)); + + //re-arrange bytes + byteSwap(&rgb); + //set pwms - red = rgb.red.fp; - blue = rgb.blue.fp; + red = rgb.red.fp; + blue = rgb.blue.fp; green = rgb.green.fp; - } + } } }