mbed library to connect to rfduino
RFDuino.cpp@0:af5f495861b2, 2014-01-06 (annotated)
- Committer:
- dbarbi1
- Date:
- Mon Jan 06 15:50:16 2014 +0000
- Revision:
- 0:af5f495861b2
- Child:
- 2:effa15a46f51
RFDuino library. ; ; **Requires associated RFDuino sketch;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dbarbi1 | 0:af5f495861b2 | 1 | #include "RFDuino.h" |
dbarbi1 | 0:af5f495861b2 | 2 | |
dbarbi1 | 0:af5f495861b2 | 3 | //Commands |
dbarbi1 | 0:af5f495861b2 | 4 | #define HANDSHAKE 0x11 |
dbarbi1 | 0:af5f495861b2 | 5 | #define CONNECTED 0x22 |
dbarbi1 | 0:af5f495861b2 | 6 | #define TRANSMIT 0x33 |
dbarbi1 | 0:af5f495861b2 | 7 | #define RECEIVE 0x44 |
dbarbi1 | 0:af5f495861b2 | 8 | |
dbarbi1 | 0:af5f495861b2 | 9 | |
dbarbi1 | 0:af5f495861b2 | 10 | |
dbarbi1 | 0:af5f495861b2 | 11 | RFDuino::RFDuino(PinName tx, PinName rx): rfd(tx,rx) { |
dbarbi1 | 0:af5f495861b2 | 12 | //init |
dbarbi1 | 0:af5f495861b2 | 13 | dataFlag=false; |
dbarbi1 | 0:af5f495861b2 | 14 | //attach Serial isr |
dbarbi1 | 0:af5f495861b2 | 15 | rfd.attach(this, &RFDuino::receive_isr); |
dbarbi1 | 0:af5f495861b2 | 16 | } |
dbarbi1 | 0:af5f495861b2 | 17 | |
dbarbi1 | 0:af5f495861b2 | 18 | |
dbarbi1 | 0:af5f495861b2 | 19 | |
dbarbi1 | 0:af5f495861b2 | 20 | //rfduino seems to take a few seconds to be ready |
dbarbi1 | 0:af5f495861b2 | 21 | //for serial comm |
dbarbi1 | 0:af5f495861b2 | 22 | bool RFDuino::handshake() { |
dbarbi1 | 0:af5f495861b2 | 23 | unsigned char temp = 0; |
dbarbi1 | 0:af5f495861b2 | 24 | __disable_irq(); |
dbarbi1 | 0:af5f495861b2 | 25 | |
dbarbi1 | 0:af5f495861b2 | 26 | rfd.putc(HANDSHAKE); |
dbarbi1 | 0:af5f495861b2 | 27 | wait(0.1); |
dbarbi1 | 0:af5f495861b2 | 28 | if(rfd.readable()) { |
dbarbi1 | 0:af5f495861b2 | 29 | temp = rfd.getc(); |
dbarbi1 | 0:af5f495861b2 | 30 | } |
dbarbi1 | 0:af5f495861b2 | 31 | __enable_irq(); |
dbarbi1 | 0:af5f495861b2 | 32 | if(temp) { return 1;} |
dbarbi1 | 0:af5f495861b2 | 33 | else { return 0;}; |
dbarbi1 | 0:af5f495861b2 | 34 | |
dbarbi1 | 0:af5f495861b2 | 35 | |
dbarbi1 | 0:af5f495861b2 | 36 | } |
dbarbi1 | 0:af5f495861b2 | 37 | |
dbarbi1 | 0:af5f495861b2 | 38 | bool RFDuino::dataReady() { |
dbarbi1 | 0:af5f495861b2 | 39 | return dataFlag; |
dbarbi1 | 0:af5f495861b2 | 40 | } |
dbarbi1 | 0:af5f495861b2 | 41 | |
dbarbi1 | 0:af5f495861b2 | 42 | bool RFDuino::isConnected() { |
dbarbi1 | 0:af5f495861b2 | 43 | unsigned char temp; |
dbarbi1 | 0:af5f495861b2 | 44 | __disable_irq(); |
dbarbi1 | 0:af5f495861b2 | 45 | |
dbarbi1 | 0:af5f495861b2 | 46 | rfd.putc(CONNECTED); |
dbarbi1 | 0:af5f495861b2 | 47 | temp = rfd.getc(); |
dbarbi1 | 0:af5f495861b2 | 48 | |
dbarbi1 | 0:af5f495861b2 | 49 | |
dbarbi1 | 0:af5f495861b2 | 50 | __enable_irq(); |
dbarbi1 | 0:af5f495861b2 | 51 | return temp; |
dbarbi1 | 0:af5f495861b2 | 52 | } |
dbarbi1 | 0:af5f495861b2 | 53 | |
dbarbi1 | 0:af5f495861b2 | 54 | |
dbarbi1 | 0:af5f495861b2 | 55 | |
dbarbi1 | 0:af5f495861b2 | 56 | |
dbarbi1 | 0:af5f495861b2 | 57 | //needs to be less than 255 bytes |
dbarbi1 | 0:af5f495861b2 | 58 | void RFDuino::transmit(unsigned char* buff, int len) { |
dbarbi1 | 0:af5f495861b2 | 59 | int i; |
dbarbi1 | 0:af5f495861b2 | 60 | __disable_irq(); |
dbarbi1 | 0:af5f495861b2 | 61 | |
dbarbi1 | 0:af5f495861b2 | 62 | //send command |
dbarbi1 | 0:af5f495861b2 | 63 | rfd.putc(TRANSMIT); |
dbarbi1 | 0:af5f495861b2 | 64 | rfd.putc((unsigned char)len); |
dbarbi1 | 0:af5f495861b2 | 65 | for(i=0;i<len;i++) { |
dbarbi1 | 0:af5f495861b2 | 66 | rfd.putc(buff[i]); |
dbarbi1 | 0:af5f495861b2 | 67 | } |
dbarbi1 | 0:af5f495861b2 | 68 | |
dbarbi1 | 0:af5f495861b2 | 69 | __enable_irq(); |
dbarbi1 | 0:af5f495861b2 | 70 | } |
dbarbi1 | 0:af5f495861b2 | 71 | |
dbarbi1 | 0:af5f495861b2 | 72 | |
dbarbi1 | 0:af5f495861b2 | 73 | int RFDuino::copyData(unsigned char* buff, int size) { |
dbarbi1 | 0:af5f495861b2 | 74 | |
dbarbi1 | 0:af5f495861b2 | 75 | memcpy(buff, data.buff, size/*data.len*/); |
dbarbi1 | 0:af5f495861b2 | 76 | dataFlag = false; |
dbarbi1 | 0:af5f495861b2 | 77 | |
dbarbi1 | 0:af5f495861b2 | 78 | return data.len; |
dbarbi1 | 0:af5f495861b2 | 79 | } |
dbarbi1 | 0:af5f495861b2 | 80 | |
dbarbi1 | 0:af5f495861b2 | 81 | void RFDuino::receive_isr() { |
dbarbi1 | 0:af5f495861b2 | 82 | |
dbarbi1 | 0:af5f495861b2 | 83 | if(rfd.getc() == RECEIVE) { |
dbarbi1 | 0:af5f495861b2 | 84 | data.len = (int)rfd.getc(); |
dbarbi1 | 0:af5f495861b2 | 85 | for(int i=0;i<data.len;i++) { |
dbarbi1 | 0:af5f495861b2 | 86 | data.buff[i] = rfd.getc(); |
dbarbi1 | 0:af5f495861b2 | 87 | } |
dbarbi1 | 0:af5f495861b2 | 88 | //handshake |
dbarbi1 | 0:af5f495861b2 | 89 | //rfd.putc(HANDSHAKE); |
dbarbi1 | 0:af5f495861b2 | 90 | |
dbarbi1 | 0:af5f495861b2 | 91 | dataFlag=true; |
dbarbi1 | 0:af5f495861b2 | 92 | } else { |
dbarbi1 | 0:af5f495861b2 | 93 | //we dont know this command, read and disregard |
dbarbi1 | 0:af5f495861b2 | 94 | while(rfd.readable()) { |
dbarbi1 | 0:af5f495861b2 | 95 | rfd.getc(); |
dbarbi1 | 0:af5f495861b2 | 96 | } |
dbarbi1 | 0:af5f495861b2 | 97 | } |
dbarbi1 | 0:af5f495861b2 | 98 | |
dbarbi1 | 0:af5f495861b2 | 99 | } |
dbarbi1 | 0:af5f495861b2 | 100 | |
dbarbi1 | 0:af5f495861b2 | 101 | |
dbarbi1 | 0:af5f495861b2 | 102 | |
dbarbi1 | 0:af5f495861b2 | 103 | |
dbarbi1 | 0:af5f495861b2 | 104 | |
dbarbi1 | 0:af5f495861b2 | 105 |