mbed library to connect to rfduino
RFDuino.cpp@3:aac9193b7fd3, 2014-01-07 (annotated)
- Committer:
- dbarbi1
- Date:
- Tue Jan 07 23:36:17 2014 +0000
- Revision:
- 3:aac9193b7fd3
- Parent:
- 2:effa15a46f51
- Child:
- 4:7cbe6036c44e
now with working comments :)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dbarbi1 | 2:effa15a46f51 | 1 | /** RFDuino is used for connecting an mbed to rfduino |
dbarbi1 | 2:effa15a46f51 | 2 | * |
dbarbi1 | 2:effa15a46f51 | 3 | */ |
dbarbi1 | 2:effa15a46f51 | 4 | |
dbarbi1 | 3:aac9193b7fd3 | 5 | #include "RFDuino.h" |
dbarbi1 | 3:aac9193b7fd3 | 6 | |
dbarbi1 | 0:af5f495861b2 | 7 | //Commands |
dbarbi1 | 0:af5f495861b2 | 8 | #define HANDSHAKE 0x11 |
dbarbi1 | 0:af5f495861b2 | 9 | #define CONNECTED 0x22 |
dbarbi1 | 0:af5f495861b2 | 10 | #define TRANSMIT 0x33 |
dbarbi1 | 0:af5f495861b2 | 11 | #define RECEIVE 0x44 |
dbarbi1 | 0:af5f495861b2 | 12 | |
dbarbi1 | 2:effa15a46f51 | 13 | /** Initializes RFDuino. Configures Pins tx and rx for serial communication |
dbarbi1 | 2:effa15a46f51 | 14 | * and creats associated ISR |
dbarbi1 | 2:effa15a46f51 | 15 | */ |
dbarbi1 | 0:af5f495861b2 | 16 | RFDuino::RFDuino(PinName tx, PinName rx): rfd(tx,rx) { |
dbarbi1 | 0:af5f495861b2 | 17 | //init |
dbarbi1 | 0:af5f495861b2 | 18 | dataFlag=false; |
dbarbi1 | 0:af5f495861b2 | 19 | //attach Serial isr |
dbarbi1 | 0:af5f495861b2 | 20 | rfd.attach(this, &RFDuino::receive_isr); |
dbarbi1 | 0:af5f495861b2 | 21 | } |
dbarbi1 | 0:af5f495861b2 | 22 | |
dbarbi1 | 0:af5f495861b2 | 23 | |
dbarbi1 | 0:af5f495861b2 | 24 | |
dbarbi1 | 2:effa15a46f51 | 25 | /** handshake() |
dbarbi1 | 2:effa15a46f51 | 26 | * |
dbarbi1 | 2:effa15a46f51 | 27 | * @returns |
dbarbi1 | 2:effa15a46f51 | 28 | * 1 on succesfull RFDuino serial reply to Handshake command |
dbarbi1 | 2:effa15a46f51 | 29 | * 0 on unsuccesfull RFDuino serial reply to Handshake command |
dbarbi1 | 2:effa15a46f51 | 30 | */ |
dbarbi1 | 0:af5f495861b2 | 31 | bool RFDuino::handshake() { |
dbarbi1 | 0:af5f495861b2 | 32 | unsigned char temp = 0; |
dbarbi1 | 0:af5f495861b2 | 33 | __disable_irq(); |
dbarbi1 | 0:af5f495861b2 | 34 | |
dbarbi1 | 0:af5f495861b2 | 35 | rfd.putc(HANDSHAKE); |
dbarbi1 | 0:af5f495861b2 | 36 | wait(0.1); |
dbarbi1 | 0:af5f495861b2 | 37 | if(rfd.readable()) { |
dbarbi1 | 0:af5f495861b2 | 38 | temp = rfd.getc(); |
dbarbi1 | 0:af5f495861b2 | 39 | } |
dbarbi1 | 0:af5f495861b2 | 40 | __enable_irq(); |
dbarbi1 | 0:af5f495861b2 | 41 | if(temp) { return 1;} |
dbarbi1 | 0:af5f495861b2 | 42 | else { return 0;}; |
dbarbi1 | 0:af5f495861b2 | 43 | |
dbarbi1 | 0:af5f495861b2 | 44 | |
dbarbi1 | 0:af5f495861b2 | 45 | } |
dbarbi1 | 0:af5f495861b2 | 46 | |
dbarbi1 | 2:effa15a46f51 | 47 | /** dataReady() |
dbarbi1 | 2:effa15a46f51 | 48 | * |
dbarbi1 | 2:effa15a46f51 | 49 | * @returns |
dbarbi1 | 2:effa15a46f51 | 50 | * 1 if RFDuino has unread data |
dbarbi1 | 2:effa15a46f51 | 51 | * 0 if RFDuino does not have unread data |
dbarbi1 | 2:effa15a46f51 | 52 | */ |
dbarbi1 | 0:af5f495861b2 | 53 | bool RFDuino::dataReady() { |
dbarbi1 | 0:af5f495861b2 | 54 | return dataFlag; |
dbarbi1 | 0:af5f495861b2 | 55 | } |
dbarbi1 | 0:af5f495861b2 | 56 | |
dbarbi1 | 2:effa15a46f51 | 57 | /** isConnected() |
dbarbi1 | 2:effa15a46f51 | 58 | * |
dbarbi1 | 2:effa15a46f51 | 59 | * @returns |
dbarbi1 | 2:effa15a46f51 | 60 | * 1 if the RFDuino has made a successful Bluetooth Connection |
dbarbi1 | 2:effa15a46f51 | 61 | * 0 if the RFDuino has not made a successful Bluetooth Connection |
dbarbi1 | 2:effa15a46f51 | 62 | */ |
dbarbi1 | 0:af5f495861b2 | 63 | bool RFDuino::isConnected() { |
dbarbi1 | 0:af5f495861b2 | 64 | unsigned char temp; |
dbarbi1 | 0:af5f495861b2 | 65 | __disable_irq(); |
dbarbi1 | 0:af5f495861b2 | 66 | |
dbarbi1 | 0:af5f495861b2 | 67 | rfd.putc(CONNECTED); |
dbarbi1 | 0:af5f495861b2 | 68 | temp = rfd.getc(); |
dbarbi1 | 0:af5f495861b2 | 69 | |
dbarbi1 | 0:af5f495861b2 | 70 | __enable_irq(); |
dbarbi1 | 0:af5f495861b2 | 71 | return temp; |
dbarbi1 | 0:af5f495861b2 | 72 | } |
dbarbi1 | 0:af5f495861b2 | 73 | |
dbarbi1 | 0:af5f495861b2 | 74 | |
dbarbi1 | 0:af5f495861b2 | 75 | |
dbarbi1 | 0:af5f495861b2 | 76 | |
dbarbi1 | 2:effa15a46f51 | 77 | /** transmit(buff, len) |
dbarbi1 | 2:effa15a46f51 | 78 | * |
dbarbi1 | 2:effa15a46f51 | 79 | * @param buff pointer to a byte buffer |
dbarbi1 | 2:effa15a46f51 | 80 | * @param len length of byte buffer to transmit |
dbarbi1 | 2:effa15a46f51 | 81 | */ |
dbarbi1 | 0:af5f495861b2 | 82 | void RFDuino::transmit(unsigned char* buff, int len) { |
dbarbi1 | 0:af5f495861b2 | 83 | int i; |
dbarbi1 | 0:af5f495861b2 | 84 | __disable_irq(); |
dbarbi1 | 0:af5f495861b2 | 85 | |
dbarbi1 | 0:af5f495861b2 | 86 | //send command |
dbarbi1 | 0:af5f495861b2 | 87 | rfd.putc(TRANSMIT); |
dbarbi1 | 0:af5f495861b2 | 88 | rfd.putc((unsigned char)len); |
dbarbi1 | 0:af5f495861b2 | 89 | for(i=0;i<len;i++) { |
dbarbi1 | 0:af5f495861b2 | 90 | rfd.putc(buff[i]); |
dbarbi1 | 0:af5f495861b2 | 91 | } |
dbarbi1 | 0:af5f495861b2 | 92 | |
dbarbi1 | 0:af5f495861b2 | 93 | __enable_irq(); |
dbarbi1 | 0:af5f495861b2 | 94 | } |
dbarbi1 | 0:af5f495861b2 | 95 | |
dbarbi1 | 2:effa15a46f51 | 96 | /** copyData(buff, size) |
dbarbi1 | 2:effa15a46f51 | 97 | * |
dbarbi1 | 2:effa15a46f51 | 98 | * @param buff pointer to a byte buffer |
dbarbi1 | 2:effa15a46f51 | 99 | * @param size size of buffer to copy data into |
dbarbi1 | 2:effa15a46f51 | 100 | * |
dbarbi1 | 2:effa15a46f51 | 101 | * @return size of data in RFDuino buffer |
dbarbi1 | 2:effa15a46f51 | 102 | */ |
dbarbi1 | 0:af5f495861b2 | 103 | int RFDuino::copyData(unsigned char* buff, int size) { |
dbarbi1 | 0:af5f495861b2 | 104 | |
dbarbi1 | 2:effa15a46f51 | 105 | __disable_irq(); |
dbarbi1 | 0:af5f495861b2 | 106 | memcpy(buff, data.buff, size/*data.len*/); |
dbarbi1 | 2:effa15a46f51 | 107 | __enable_irq(); |
dbarbi1 | 0:af5f495861b2 | 108 | dataFlag = false; |
dbarbi1 | 0:af5f495861b2 | 109 | |
dbarbi1 | 0:af5f495861b2 | 110 | return data.len; |
dbarbi1 | 0:af5f495861b2 | 111 | } |
dbarbi1 | 0:af5f495861b2 | 112 | |
dbarbi1 | 2:effa15a46f51 | 113 | /** receiv_isr |
dbarbi1 | 2:effa15a46f51 | 114 | *Serial ISR. Checks for Receive command, and reads data into buffer |
dbarbi1 | 2:effa15a46f51 | 115 | */ |
dbarbi1 | 0:af5f495861b2 | 116 | void RFDuino::receive_isr() { |
dbarbi1 | 0:af5f495861b2 | 117 | |
dbarbi1 | 0:af5f495861b2 | 118 | if(rfd.getc() == RECEIVE) { |
dbarbi1 | 0:af5f495861b2 | 119 | data.len = (int)rfd.getc(); |
dbarbi1 | 0:af5f495861b2 | 120 | for(int i=0;i<data.len;i++) { |
dbarbi1 | 0:af5f495861b2 | 121 | data.buff[i] = rfd.getc(); |
dbarbi1 | 0:af5f495861b2 | 122 | } |
dbarbi1 | 0:af5f495861b2 | 123 | |
dbarbi1 | 0:af5f495861b2 | 124 | dataFlag=true; |
dbarbi1 | 0:af5f495861b2 | 125 | } else { |
dbarbi1 | 0:af5f495861b2 | 126 | //we dont know this command, read and disregard |
dbarbi1 | 0:af5f495861b2 | 127 | while(rfd.readable()) { |
dbarbi1 | 0:af5f495861b2 | 128 | rfd.getc(); |
dbarbi1 | 0:af5f495861b2 | 129 | } |
dbarbi1 | 0:af5f495861b2 | 130 | } |
dbarbi1 | 0:af5f495861b2 | 131 | |
dbarbi1 | 0:af5f495861b2 | 132 | } |
dbarbi1 | 0:af5f495861b2 | 133 | |
dbarbi1 | 0:af5f495861b2 | 134 | |
dbarbi1 | 0:af5f495861b2 | 135 | |
dbarbi1 | 0:af5f495861b2 | 136 | |
dbarbi1 | 0:af5f495861b2 | 137 | |
dbarbi1 | 0:af5f495861b2 | 138 |