mbed library to connect to rfduino
Revision 1:310c07d23100, committed 2014-01-06
- Comitter:
- dbarbi1
- Date:
- Mon Jan 06 15:58:07 2014 +0000
- Parent:
- 0:af5f495861b2
- Child:
- 2:effa15a46f51
- Commit message:
- with rfduino sketch
Changed in this revision
rfduino_sketch.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rfduino_sketch.h Mon Jan 06 15:58:07 2014 +0000 @@ -0,0 +1,125 @@ +#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