mbed library to connect to rfduino
RFDuino.cpp@5:cd05cc4dd824, 2014-01-08 (annotated)
- Committer:
- dbarbi1
- Date:
- Wed Jan 08 21:06:52 2014 +0000
- Revision:
- 5:cd05cc4dd824
- Parent:
- 4:7cbe6036c44e
minor documentation update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dbarbi1 | 4:7cbe6036c44e | 1 | /* RFDuino Interface Library |
dbarbi1 | 4:7cbe6036c44e | 2 | * Copyright (c) 2006-2013 Your Name Here |
dbarbi1 | 4:7cbe6036c44e | 3 | * |
dbarbi1 | 4:7cbe6036c44e | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dbarbi1 | 4:7cbe6036c44e | 5 | * you may not use this file except in compliance with the License. |
dbarbi1 | 4:7cbe6036c44e | 6 | * You may obtain a copy of the License at |
dbarbi1 | 4:7cbe6036c44e | 7 | * |
dbarbi1 | 4:7cbe6036c44e | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dbarbi1 | 4:7cbe6036c44e | 9 | * |
dbarbi1 | 4:7cbe6036c44e | 10 | * Unless required by applicable law or agreed to in writing, software |
dbarbi1 | 4:7cbe6036c44e | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dbarbi1 | 4:7cbe6036c44e | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dbarbi1 | 4:7cbe6036c44e | 13 | * See the License for the specific language governing permissions and |
dbarbi1 | 4:7cbe6036c44e | 14 | * limitations under the License. |
dbarbi1 | 2:effa15a46f51 | 15 | */ |
dbarbi1 | 4:7cbe6036c44e | 16 | |
dbarbi1 | 4:7cbe6036c44e | 17 | #include "RFDuino.h" |
dbarbi1 | 4:7cbe6036c44e | 18 | |
dbarbi1 | 0:af5f495861b2 | 19 | //Commands |
dbarbi1 | 4:7cbe6036c44e | 20 | int const HANDSHAKE = 0x11; |
dbarbi1 | 4:7cbe6036c44e | 21 | int const CONNECTED = 0x22; |
dbarbi1 | 4:7cbe6036c44e | 22 | int const TRANSMIT = 0x33; |
dbarbi1 | 4:7cbe6036c44e | 23 | int const RECEIVE = 0x44; |
dbarbi1 | 0:af5f495861b2 | 24 | |
dbarbi1 | 4:7cbe6036c44e | 25 | RFDuino::RFDuino(PinName tx, PinName rx): rfd(tx,rx) |
dbarbi1 | 4:7cbe6036c44e | 26 | { |
dbarbi1 | 0:af5f495861b2 | 27 | //init |
dbarbi1 | 4:7cbe6036c44e | 28 | dataFlag=false; |
dbarbi1 | 0:af5f495861b2 | 29 | } |
dbarbi1 | 0:af5f495861b2 | 30 | |
dbarbi1 | 0:af5f495861b2 | 31 | |
dbarbi1 | 0:af5f495861b2 | 32 | |
dbarbi1 | 4:7cbe6036c44e | 33 | //rfduino seems to take a few seconds to be ready |
dbarbi1 | 4:7cbe6036c44e | 34 | //for serial comm |
dbarbi1 | 4:7cbe6036c44e | 35 | bool RFDuino::handshake(void) |
dbarbi1 | 4:7cbe6036c44e | 36 | { |
dbarbi1 | 0:af5f495861b2 | 37 | unsigned char temp = 0; |
dbarbi1 | 4:7cbe6036c44e | 38 | // make sure RX irq is detached |
dbarbi1 | 4:7cbe6036c44e | 39 | rfd.attach(NULL); |
dbarbi1 | 4:7cbe6036c44e | 40 | // send a handshake byte |
dbarbi1 | 4:7cbe6036c44e | 41 | rfd.putc(HANDSHAKE); |
dbarbi1 | 4:7cbe6036c44e | 42 | // give some time for data to be sent |
dbarbi1 | 4:7cbe6036c44e | 43 | wait(0.1); |
dbarbi1 | 0:af5f495861b2 | 44 | |
dbarbi1 | 4:7cbe6036c44e | 45 | if(rfd.readable()) { |
dbarbi1 | 4:7cbe6036c44e | 46 | temp = rfd.getc(); |
dbarbi1 | 4:7cbe6036c44e | 47 | //attach Serial isr |
dbarbi1 | 4:7cbe6036c44e | 48 | rfd.attach(this, &RFDuino::receive_isr); |
dbarbi1 | 0:af5f495861b2 | 49 | } |
dbarbi1 | 0:af5f495861b2 | 50 | |
dbarbi1 | 4:7cbe6036c44e | 51 | return (temp) ? 1 : 0; |
dbarbi1 | 0:af5f495861b2 | 52 | } |
dbarbi1 | 0:af5f495861b2 | 53 | |
dbarbi1 | 4:7cbe6036c44e | 54 | bool RFDuino::readable(void) const |
dbarbi1 | 4:7cbe6036c44e | 55 | { |
dbarbi1 | 0:af5f495861b2 | 56 | return dataFlag; |
dbarbi1 | 0:af5f495861b2 | 57 | } |
dbarbi1 | 0:af5f495861b2 | 58 | |
dbarbi1 | 4:7cbe6036c44e | 59 | bool RFDuino::isConnected(void) |
dbarbi1 | 4:7cbe6036c44e | 60 | { |
dbarbi1 | 4:7cbe6036c44e | 61 | unsigned char temp = 0; |
dbarbi1 | 4:7cbe6036c44e | 62 | // make sure RX irq is detached |
dbarbi1 | 4:7cbe6036c44e | 63 | rfd.attach(NULL); |
dbarbi1 | 4:7cbe6036c44e | 64 | // send a byte and read the response |
dbarbi1 | 0:af5f495861b2 | 65 | rfd.putc(CONNECTED); |
dbarbi1 | 0:af5f495861b2 | 66 | temp = rfd.getc(); |
dbarbi1 | 4:7cbe6036c44e | 67 | |
dbarbi1 | 4:7cbe6036c44e | 68 | //attach Serial isr |
dbarbi1 | 4:7cbe6036c44e | 69 | rfd.attach(this, &RFDuino::receive_isr); |
dbarbi1 | 4:7cbe6036c44e | 70 | |
dbarbi1 | 4:7cbe6036c44e | 71 | return (temp) ? 1 : 0; |
dbarbi1 | 4:7cbe6036c44e | 72 | } |
dbarbi1 | 4:7cbe6036c44e | 73 | |
dbarbi1 | 4:7cbe6036c44e | 74 | bool RFDuino::transmit(const unsigned char *buff, const int len) |
dbarbi1 | 4:7cbe6036c44e | 75 | { |
dbarbi1 | 4:7cbe6036c44e | 76 | //needs to be less than 255 bytes |
dbarbi1 | 4:7cbe6036c44e | 77 | if (len > buff_size) { |
dbarbi1 | 4:7cbe6036c44e | 78 | return 0; |
dbarbi1 | 4:7cbe6036c44e | 79 | } |
dbarbi1 | 4:7cbe6036c44e | 80 | |
dbarbi1 | 4:7cbe6036c44e | 81 | // make sure RX irq is detached |
dbarbi1 | 4:7cbe6036c44e | 82 | rfd.attach(NULL); |
dbarbi1 | 4:7cbe6036c44e | 83 | |
dbarbi1 | 4:7cbe6036c44e | 84 | //send command |
dbarbi1 | 4:7cbe6036c44e | 85 | rfd.putc(TRANSMIT); |
dbarbi1 | 4:7cbe6036c44e | 86 | rfd.putc(static_cast<unsigned char>(len)); |
dbarbi1 | 4:7cbe6036c44e | 87 | for(int i=0; i<len; i++) { |
dbarbi1 | 4:7cbe6036c44e | 88 | rfd.putc(buff[i]); |
dbarbi1 | 4:7cbe6036c44e | 89 | } |
dbarbi1 | 4:7cbe6036c44e | 90 | |
dbarbi1 | 4:7cbe6036c44e | 91 | //attach Serial isr |
dbarbi1 | 4:7cbe6036c44e | 92 | rfd.attach(this, &RFDuino::receive_isr); |
dbarbi1 | 4:7cbe6036c44e | 93 | |
dbarbi1 | 4:7cbe6036c44e | 94 | return 1; |
dbarbi1 | 0:af5f495861b2 | 95 | } |
dbarbi1 | 0:af5f495861b2 | 96 | |
dbarbi1 | 0:af5f495861b2 | 97 | |
dbarbi1 | 4:7cbe6036c44e | 98 | int RFDuino::read(unsigned char* buff, const int size) |
dbarbi1 | 4:7cbe6036c44e | 99 | { |
dbarbi1 | 4:7cbe6036c44e | 100 | memcpy(buff, data.buff, size/*data.len*/); |
dbarbi1 | 4:7cbe6036c44e | 101 | dataFlag = false; |
dbarbi1 | 0:af5f495861b2 | 102 | |
dbarbi1 | 0:af5f495861b2 | 103 | return data.len; |
dbarbi1 | 0:af5f495861b2 | 104 | } |
dbarbi1 | 0:af5f495861b2 | 105 | |
dbarbi1 | 4:7cbe6036c44e | 106 | void RFDuino::receive_isr() |
dbarbi1 | 4:7cbe6036c44e | 107 | { |
dbarbi1 | 4:7cbe6036c44e | 108 | if(rfd.getc() == RECEIVE) { |
dbarbi1 | 4:7cbe6036c44e | 109 | data.len = static_cast<int>(rfd.getc()); |
dbarbi1 | 4:7cbe6036c44e | 110 | if(data.len > buff_size) { |
dbarbi1 | 4:7cbe6036c44e | 111 | // signal an error or reset?? |
dbarbi1 | 4:7cbe6036c44e | 112 | data.len = buff_size; |
dbarbi1 | 4:7cbe6036c44e | 113 | } |
dbarbi1 | 4:7cbe6036c44e | 114 | |
dbarbi1 | 4:7cbe6036c44e | 115 | for(int i=0; i<data.len; i++) { |
dbarbi1 | 0:af5f495861b2 | 116 | data.buff[i] = rfd.getc(); |
dbarbi1 | 0:af5f495861b2 | 117 | } |
dbarbi1 | 4:7cbe6036c44e | 118 | //handshake |
dbarbi1 | 4:7cbe6036c44e | 119 | //rfd.putc(HANDSHAKE); |
dbarbi1 | 4:7cbe6036c44e | 120 | |
dbarbi1 | 0:af5f495861b2 | 121 | dataFlag=true; |
dbarbi1 | 4:7cbe6036c44e | 122 | } else { |
dbarbi1 | 4:7cbe6036c44e | 123 | //we dont know this command, read and disregard |
dbarbi1 | 4:7cbe6036c44e | 124 | while(rfd.readable()) { |
dbarbi1 | 4:7cbe6036c44e | 125 | char c = rfd.getc(); |
dbarbi1 | 4:7cbe6036c44e | 126 | c = c; |
dbarbi1 | 0:af5f495861b2 | 127 | } |
dbarbi1 | 0:af5f495861b2 | 128 | } |
dbarbi1 | 0:af5f495861b2 | 129 | } |
dbarbi1 | 0:af5f495861b2 | 130 | |
dbarbi1 | 0:af5f495861b2 | 131 | |
dbarbi1 | 0:af5f495861b2 | 132 | |
dbarbi1 | 0:af5f495861b2 | 133 | |
dbarbi1 | 0:af5f495861b2 | 134 |