mbed library to connect to rfduino
rfduino_sketch.txt@5:cd05cc4dd824, 2014-01-08 (annotated)
- Committer:
- dbarbi1
- Date:
- Wed Jan 08 21:06:52 2014 +0000
- Revision:
- 5:cd05cc4dd824
- Parent:
- 2:effa15a46f51
minor documentation update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dbarbi1 | 1:310c07d23100 | 1 | /* |
dbarbi1 | 2:effa15a46f51 | 2 | * RFDuino sketch. Load this sketch into the rfduino using |
dbarbi1 | 2:effa15a46f51 | 3 | * the Arduino ide: http://arduino.cc/en/Main/Software |
dbarbi1 | 1:310c07d23100 | 4 | */ |
dbarbi1 | 1:310c07d23100 | 5 | #include <RFduinoBLE.h> |
dbarbi1 | 1:310c07d23100 | 6 | |
dbarbi1 | 1:310c07d23100 | 7 | //Commands |
dbarbi1 | 1:310c07d23100 | 8 | #define HANDSHAKE 0x11 |
dbarbi1 | 1:310c07d23100 | 9 | #define CONNECTED 0x22 |
dbarbi1 | 1:310c07d23100 | 10 | #define TRANSMIT 0x33 |
dbarbi1 | 1:310c07d23100 | 11 | #define RECEIVE 0x44 |
dbarbi1 | 1:310c07d23100 | 12 | |
dbarbi1 | 1:310c07d23100 | 13 | //connected flag |
dbarbi1 | 1:310c07d23100 | 14 | unsigned char cFlag; |
dbarbi1 | 1:310c07d23100 | 15 | |
dbarbi1 | 1:310c07d23100 | 16 | //Transmit buffer |
dbarbi1 | 1:310c07d23100 | 17 | unsigned char Tbuf[255]; |
dbarbi1 | 1:310c07d23100 | 18 | |
dbarbi1 | 1:310c07d23100 | 19 | |
dbarbi1 | 1:310c07d23100 | 20 | void setup() { |
dbarbi1 | 1:310c07d23100 | 21 | //clear flags |
dbarbi1 | 1:310c07d23100 | 22 | cFlag = 0; |
dbarbi1 | 1:310c07d23100 | 23 | |
dbarbi1 | 1:310c07d23100 | 24 | // initialize serial: |
dbarbi1 | 1:310c07d23100 | 25 | Serial.begin(9600); |
dbarbi1 | 1:310c07d23100 | 26 | |
dbarbi1 | 1:310c07d23100 | 27 | RFduinoBLE.deviceName = "RFduino"; |
dbarbi1 | 1:310c07d23100 | 28 | RFduinoBLE.advertisementInterval = MILLISECONDS(300); |
dbarbi1 | 1:310c07d23100 | 29 | // this is the data we want to appear in the advertisement |
dbarbi1 | 1:310c07d23100 | 30 | // (the deviceName length plus the advertisement length must be <= 18 bytes |
dbarbi1 | 1:310c07d23100 | 31 | RFduinoBLE.advertisementData = "rgb"; |
dbarbi1 | 1:310c07d23100 | 32 | |
dbarbi1 | 1:310c07d23100 | 33 | // start the BLE stack |
dbarbi1 | 1:310c07d23100 | 34 | RFduinoBLE.begin(); |
dbarbi1 | 1:310c07d23100 | 35 | } |
dbarbi1 | 1:310c07d23100 | 36 | |
dbarbi1 | 1:310c07d23100 | 37 | void loop() { |
dbarbi1 | 1:310c07d23100 | 38 | |
dbarbi1 | 1:310c07d23100 | 39 | // switch to lower power mode |
dbarbi1 | 1:310c07d23100 | 40 | RFduino_ULPDelay(INFINITE); |
dbarbi1 | 1:310c07d23100 | 41 | |
dbarbi1 | 1:310c07d23100 | 42 | } |
dbarbi1 | 1:310c07d23100 | 43 | |
dbarbi1 | 1:310c07d23100 | 44 | void Handshake() { |
dbarbi1 | 1:310c07d23100 | 45 | Serial.write(HANDSHAKE); |
dbarbi1 | 1:310c07d23100 | 46 | } |
dbarbi1 | 1:310c07d23100 | 47 | |
dbarbi1 | 1:310c07d23100 | 48 | void Connected() { |
dbarbi1 | 1:310c07d23100 | 49 | Serial.write(cFlag); |
dbarbi1 | 1:310c07d23100 | 50 | } |
dbarbi1 | 1:310c07d23100 | 51 | |
dbarbi1 | 1:310c07d23100 | 52 | void Transmit() { |
dbarbi1 | 1:310c07d23100 | 53 | unsigned int len; |
dbarbi1 | 1:310c07d23100 | 54 | |
dbarbi1 | 1:310c07d23100 | 55 | //get transmistion length |
dbarbi1 | 1:310c07d23100 | 56 | len = (unsigned int)Serial.read(); |
dbarbi1 | 1:310c07d23100 | 57 | if(len > 255) {len = 255;} |
dbarbi1 | 1:310c07d23100 | 58 | |
dbarbi1 | 1:310c07d23100 | 59 | //read byes to transmit |
dbarbi1 | 1:310c07d23100 | 60 | for(int i=0;i<len;len++) { |
dbarbi1 | 1:310c07d23100 | 61 | Tbuf[i]=Serial.read(); |
dbarbi1 | 1:310c07d23100 | 62 | } |
dbarbi1 | 1:310c07d23100 | 63 | |
dbarbi1 | 1:310c07d23100 | 64 | RFduinoBLE.send((const char*)Tbuf, len); |
dbarbi1 | 1:310c07d23100 | 65 | |
dbarbi1 | 1:310c07d23100 | 66 | } |
dbarbi1 | 1:310c07d23100 | 67 | |
dbarbi1 | 1:310c07d23100 | 68 | |
dbarbi1 | 1:310c07d23100 | 69 | /* |
dbarbi1 | 1:310c07d23100 | 70 | SerialEvent occurs whenever a new data comes in the |
dbarbi1 | 1:310c07d23100 | 71 | hardware serial RX. This routine is run between each |
dbarbi1 | 1:310c07d23100 | 72 | time loop() runs, so using delay inside loop can delay |
dbarbi1 | 1:310c07d23100 | 73 | response. Multiple bytes of data may be available. |
dbarbi1 | 1:310c07d23100 | 74 | */ |
dbarbi1 | 1:310c07d23100 | 75 | void serialEvent() { |
dbarbi1 | 1:310c07d23100 | 76 | if(Serial.available()) { |
dbarbi1 | 1:310c07d23100 | 77 | // get the new byte: |
dbarbi1 | 1:310c07d23100 | 78 | unsigned char comm = (char)Serial.read(); |
dbarbi1 | 1:310c07d23100 | 79 | |
dbarbi1 | 1:310c07d23100 | 80 | switch (comm) { |
dbarbi1 | 1:310c07d23100 | 81 | case HANDSHAKE: |
dbarbi1 | 1:310c07d23100 | 82 | Handshake(); |
dbarbi1 | 1:310c07d23100 | 83 | break; |
dbarbi1 | 1:310c07d23100 | 84 | case CONNECTED: |
dbarbi1 | 1:310c07d23100 | 85 | Connected(); |
dbarbi1 | 1:310c07d23100 | 86 | break; |
dbarbi1 | 1:310c07d23100 | 87 | case TRANSMIT: |
dbarbi1 | 1:310c07d23100 | 88 | Transmit(); |
dbarbi1 | 1:310c07d23100 | 89 | break; |
dbarbi1 | 1:310c07d23100 | 90 | default: |
dbarbi1 | 1:310c07d23100 | 91 | break; |
dbarbi1 | 1:310c07d23100 | 92 | } |
dbarbi1 | 1:310c07d23100 | 93 | |
dbarbi1 | 1:310c07d23100 | 94 | } |
dbarbi1 | 1:310c07d23100 | 95 | } |
dbarbi1 | 1:310c07d23100 | 96 | |
dbarbi1 | 1:310c07d23100 | 97 | void RFduinoBLE_onConnect() { |
dbarbi1 | 1:310c07d23100 | 98 | cFlag=1; |
dbarbi1 | 1:310c07d23100 | 99 | } |
dbarbi1 | 1:310c07d23100 | 100 | |
dbarbi1 | 1:310c07d23100 | 101 | void RFduinoBLE_onDisconnect() { |
dbarbi1 | 1:310c07d23100 | 102 | cFlag=0; |
dbarbi1 | 1:310c07d23100 | 103 | } |
dbarbi1 | 1:310c07d23100 | 104 | |
dbarbi1 | 1:310c07d23100 | 105 | void RFduinoBLE_onReceive(char *data, int len) { |
dbarbi1 | 1:310c07d23100 | 106 | |
dbarbi1 | 1:310c07d23100 | 107 | noInterrupts(); |
dbarbi1 | 1:310c07d23100 | 108 | |
dbarbi1 | 1:310c07d23100 | 109 | if(len>255) { len=255;} //limit to 255 bytes for now |
dbarbi1 | 1:310c07d23100 | 110 | Serial.write(RECEIVE); |
dbarbi1 | 1:310c07d23100 | 111 | Serial.write((unsigned char)len); |
dbarbi1 | 1:310c07d23100 | 112 | |
dbarbi1 | 1:310c07d23100 | 113 | for(int i=0; i<len; i++) { |
dbarbi1 | 1:310c07d23100 | 114 | Serial.write(data[i]); |
dbarbi1 | 1:310c07d23100 | 115 | } |
dbarbi1 | 1:310c07d23100 | 116 | |
dbarbi1 | 1:310c07d23100 | 117 | //wait for handshake |
dbarbi1 | 1:310c07d23100 | 118 | //Serial.read(); |
dbarbi1 | 1:310c07d23100 | 119 | |
dbarbi1 | 1:310c07d23100 | 120 | interrupts(); |
dbarbi1 | 1:310c07d23100 | 121 | } |