mbed library to connect to rfduino
Revision 2:effa15a46f51, committed 2014-01-07
- Comitter:
- dbarbi1
- Date:
- Tue Jan 07 23:21:04 2014 +0000
- Parent:
- 1:310c07d23100
- Child:
- 3:aac9193b7fd3
- Commit message:
- updated documentation
Changed in this revision
--- a/RFDuino.cpp Mon Jan 06 15:58:07 2014 +0000 +++ b/RFDuino.cpp Tue Jan 07 23:21:04 2014 +0000 @@ -1,13 +1,18 @@ #include "RFDuino.h" +/** RFDuino is used for connecting an mbed to rfduino + * + */ + //Commands #define HANDSHAKE 0x11 #define CONNECTED 0x22 #define TRANSMIT 0x33 #define RECEIVE 0x44 - - +/** Initializes RFDuino. Configures Pins tx and rx for serial communication + * and creats associated ISR + */ RFDuino::RFDuino(PinName tx, PinName rx): rfd(tx,rx) { //init dataFlag=false; @@ -17,8 +22,12 @@ -//rfduino seems to take a few seconds to be ready -//for serial comm + /** handshake() + * + * @returns + * 1 on succesfull RFDuino serial reply to Handshake command + * 0 on unsuccesfull RFDuino serial reply to Handshake command + */ bool RFDuino::handshake() { unsigned char temp = 0; __disable_irq(); @@ -35,10 +44,22 @@ } + /** dataReady() + * + * @returns + * 1 if RFDuino has unread data + * 0 if RFDuino does not have unread data + */ bool RFDuino::dataReady() { return dataFlag; } + /** isConnected() + * + * @returns + * 1 if the RFDuino has made a successful Bluetooth Connection + * 0 if the RFDuino has not made a successful Bluetooth Connection + */ bool RFDuino::isConnected() { unsigned char temp; __disable_irq(); @@ -46,7 +67,6 @@ rfd.putc(CONNECTED); temp = rfd.getc(); - __enable_irq(); return temp; } @@ -54,7 +74,11 @@ -//needs to be less than 255 bytes + /** transmit(buff, len) + * + * @param buff pointer to a byte buffer + * @param len length of byte buffer to transmit + */ void RFDuino::transmit(unsigned char* buff, int len) { int i; __disable_irq(); @@ -69,15 +93,26 @@ __enable_irq(); } - + /** copyData(buff, size) + * + * @param buff pointer to a byte buffer + * @param size size of buffer to copy data into + * + * @return size of data in RFDuino buffer + */ int RFDuino::copyData(unsigned char* buff, int size) { + __disable_irq(); memcpy(buff, data.buff, size/*data.len*/); + __enable_irq(); dataFlag = false; return data.len; } + /** receiv_isr + *Serial ISR. Checks for Receive command, and reads data into buffer + */ void RFDuino::receive_isr() { if(rfd.getc() == RECEIVE) { @@ -85,8 +120,6 @@ for(int i=0;i<data.len;i++) { data.buff[i] = rfd.getc(); } - //handshake - //rfd.putc(HANDSHAKE); dataFlag=true; } else {
--- a/RFDuino.h Mon Jan 06 15:58:07 2014 +0000 +++ b/RFDuino.h Tue Jan 07 23:21:04 2014 +0000 @@ -14,18 +14,18 @@ private: Serial rfd; RFD_data data; - bool dataFlag; - + bool dataFlag; + void receive_isr(); + public: - RFDuino(PinName tx, PinName rx); + + RFDuino(PinName tx, PinName rx); bool handshake(void); - bool dataReady(void); + bool dataReady(void); bool isConnected(void); - void transmit(unsigned char* buff, int len); - int copyData(unsigned char* buff, int size); - void receive_isr(); - + void transmit(unsigned char* buff, int len); + int copyData(unsigned char* buff, int size); };
--- a/rfduino_sketch.h Mon Jan 06 15:58:07 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,125 +0,0 @@ -#ifdef DO_NOT_COMPILE - -/* - RFDuino sketch. Load this sketch into the rfduino using - the Arduino ide: http://arduino.cc/en/Main/Software -*/ -#include <RFduinoBLE.h> - -//Commands -#define HANDSHAKE 0x11 -#define CONNECTED 0x22 -#define TRANSMIT 0x33 -#define RECEIVE 0x44 - -//connected flag -unsigned char cFlag; - -//Transmit buffer -unsigned char Tbuf[255]; - - -void setup() { - //clear flags - cFlag = 0; - - // initialize serial: - Serial.begin(9600); - - RFduinoBLE.deviceName = "RFduino"; - RFduinoBLE.advertisementInterval = MILLISECONDS(300); - // this is the data we want to appear in the advertisement - // (the deviceName length plus the advertisement length must be <= 18 bytes - RFduinoBLE.advertisementData = "rgb"; - - // start the BLE stack - RFduinoBLE.begin(); -} - -void loop() { - - // switch to lower power mode - RFduino_ULPDelay(INFINITE); - -} - -void Handshake() { - Serial.write(HANDSHAKE); -} - -void Connected() { - Serial.write(cFlag); -} - -void Transmit() { - unsigned int len; - - //get transmistion length - len = (unsigned int)Serial.read(); - if(len > 255) {len = 255;} - - //read byes to transmit - for(int i=0;i<len;len++) { - Tbuf[i]=Serial.read(); - } - - RFduinoBLE.send((const char*)Tbuf, len); - -} - - -/* - SerialEvent occurs whenever a new data comes in the - hardware serial RX. This routine is run between each - time loop() runs, so using delay inside loop can delay - response. Multiple bytes of data may be available. - */ -void serialEvent() { - if(Serial.available()) { - // get the new byte: - unsigned char comm = (char)Serial.read(); - - switch (comm) { - case HANDSHAKE: - Handshake(); - break; - case CONNECTED: - Connected(); - break; - case TRANSMIT: - Transmit(); - break; - default: - break; - } - - } -} - -void RFduinoBLE_onConnect() { - cFlag=1; -} - -void RFduinoBLE_onDisconnect() { - cFlag=0; -} - -void RFduinoBLE_onReceive(char *data, int len) { - - noInterrupts(); - - if(len>255) { len=255;} //limit to 255 bytes for now - Serial.write(RECEIVE); - Serial.write((unsigned char)len); - - for(int i=0; i<len; i++) { - Serial.write(data[i]); - } - - //wait for handshake - //Serial.read(); - - interrupts(); -} - -#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rfduino_sketch.txt Tue Jan 07 23:21:04 2014 +0000 @@ -0,0 +1,121 @@ +/* + * RFDuino sketch. Load this sketch into the rfduino using + * the Arduino ide: http://arduino.cc/en/Main/Software +*/ +#include <RFduinoBLE.h> + +//Commands +#define HANDSHAKE 0x11 +#define CONNECTED 0x22 +#define TRANSMIT 0x33 +#define RECEIVE 0x44 + +//connected flag +unsigned char cFlag; + +//Transmit buffer +unsigned char Tbuf[255]; + + +void setup() { + //clear flags + cFlag = 0; + + // initialize serial: + Serial.begin(9600); + + RFduinoBLE.deviceName = "RFduino"; + RFduinoBLE.advertisementInterval = MILLISECONDS(300); + // this is the data we want to appear in the advertisement + // (the deviceName length plus the advertisement length must be <= 18 bytes + RFduinoBLE.advertisementData = "rgb"; + + // start the BLE stack + RFduinoBLE.begin(); +} + +void loop() { + + // switch to lower power mode + RFduino_ULPDelay(INFINITE); + +} + +void Handshake() { + Serial.write(HANDSHAKE); +} + +void Connected() { + Serial.write(cFlag); +} + +void Transmit() { + unsigned int len; + + //get transmistion length + len = (unsigned int)Serial.read(); + if(len > 255) {len = 255;} + + //read byes to transmit + for(int i=0;i<len;len++) { + Tbuf[i]=Serial.read(); + } + + RFduinoBLE.send((const char*)Tbuf, len); + +} + + +/* + SerialEvent occurs whenever a new data comes in the + hardware serial RX. This routine is run between each + time loop() runs, so using delay inside loop can delay + response. Multiple bytes of data may be available. + */ +void serialEvent() { + if(Serial.available()) { + // get the new byte: + unsigned char comm = (char)Serial.read(); + + switch (comm) { + case HANDSHAKE: + Handshake(); + break; + case CONNECTED: + Connected(); + break; + case TRANSMIT: + Transmit(); + break; + default: + break; + } + + } +} + +void RFduinoBLE_onConnect() { + cFlag=1; +} + +void RFduinoBLE_onDisconnect() { + cFlag=0; +} + +void RFduinoBLE_onReceive(char *data, int len) { + + noInterrupts(); + + if(len>255) { len=255;} //limit to 255 bytes for now + Serial.write(RECEIVE); + Serial.write((unsigned char)len); + + for(int i=0; i<len; i++) { + Serial.write(data[i]); + } + + //wait for handshake + //Serial.read(); + + interrupts(); +}