mbed library to connect to rfduino
Diff: RFDuino.cpp
- Revision:
- 4:7cbe6036c44e
- Parent:
- 3:aac9193b7fd3
--- a/RFDuino.cpp Tue Jan 07 23:36:17 2014 +0000 +++ b/RFDuino.cpp Wed Jan 08 20:57:52 2014 +0000 @@ -1,138 +1,134 @@ -/** RFDuino is used for connecting an mbed to rfduino - * +/* RFDuino Interface Library + * Copyright (c) 2006-2013 Your Name Here + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ - - #include "RFDuino.h" - + +#include "RFDuino.h" + //Commands -#define HANDSHAKE 0x11 -#define CONNECTED 0x22 -#define TRANSMIT 0x33 -#define RECEIVE 0x44 +int const HANDSHAKE = 0x11; +int const CONNECTED = 0x22; +int const TRANSMIT = 0x33; +int const RECEIVE = 0x44; -/** Initializes RFDuino. Configures Pins tx and rx for serial communication - * and creats associated ISR - */ -RFDuino::RFDuino(PinName tx, PinName rx): rfd(tx,rx) { +RFDuino::RFDuino(PinName tx, PinName rx): rfd(tx,rx) +{ //init - dataFlag=false; - //attach Serial isr - rfd.attach(this, &RFDuino::receive_isr); + dataFlag=false; } - /** handshake() - * - * @returns - * 1 on succesfull RFDuino serial reply to Handshake command - * 0 on unsuccesfull RFDuino serial reply to Handshake command - */ -bool RFDuino::handshake() { +//rfduino seems to take a few seconds to be ready +//for serial comm +bool RFDuino::handshake(void) +{ unsigned char temp = 0; - __disable_irq(); + // make sure RX irq is detached + rfd.attach(NULL); + // send a handshake byte + rfd.putc(HANDSHAKE); + // give some time for data to be sent + wait(0.1); - rfd.putc(HANDSHAKE); - wait(0.1); - if(rfd.readable()) { - temp = rfd.getc(); + if(rfd.readable()) { + temp = rfd.getc(); + //attach Serial isr + rfd.attach(this, &RFDuino::receive_isr); } - __enable_irq(); - if(temp) { return 1;} - else { return 0;}; - + return (temp) ? 1 : 0; } - /** dataReady() - * - * @returns - * 1 if RFDuino has unread data - * 0 if RFDuino does not have unread data - */ -bool RFDuino::dataReady() { +bool RFDuino::readable(void) const +{ return dataFlag; } - /** isConnected() - * - * @returns - * 1 if the RFDuino has made a successful Bluetooth Connection - * 0 if the RFDuino has not made a successful Bluetooth Connection - */ -bool RFDuino::isConnected() { - unsigned char temp; - __disable_irq(); - +bool RFDuino::isConnected(void) +{ + unsigned char temp = 0; + // make sure RX irq is detached + rfd.attach(NULL); + // send a byte and read the response rfd.putc(CONNECTED); temp = rfd.getc(); - - __enable_irq(); - return temp; + + //attach Serial isr + rfd.attach(this, &RFDuino::receive_isr); + + return (temp) ? 1 : 0; +} + +bool RFDuino::transmit(const unsigned char *buff, const int len) +{ + //needs to be less than 255 bytes + if (len > buff_size) { + return 0; + } + + // make sure RX irq is detached + rfd.attach(NULL); + + //send command + rfd.putc(TRANSMIT); + rfd.putc(static_cast<unsigned char>(len)); + for(int i=0; i<len; i++) { + rfd.putc(buff[i]); + } + + //attach Serial isr + rfd.attach(this, &RFDuino::receive_isr); + + return 1; } - +int RFDuino::read(unsigned char* buff, const int size) +{ + memcpy(buff, data.buff, size/*data.len*/); + dataFlag = false; - /** transmit(buff, len) - * - * @param buff pointer to a byte buffer - * @param len length of byte buffer to transmit - */ -void RFDuino::transmit(unsigned char* buff, int len) { - int i; - __disable_irq(); - - //send command - rfd.putc(TRANSMIT); - rfd.putc((unsigned char)len); - for(i=0;i<len;i++) { - rfd.putc(buff[i]); - } - - __enable_irq(); -} - - /** copyData(buff, size) - * - * @param buff pointer to a byte buffer - * @param size size of buffer to copy data into - * - * @return size of data in RFDuino buffer - */ -int RFDuino::copyData(unsigned char* buff, int size) { - - __disable_irq(); - memcpy(buff, data.buff, size/*data.len*/); - __enable_irq(); - dataFlag = false; - return data.len; } - /** receiv_isr - *Serial ISR. Checks for Receive command, and reads data into buffer - */ -void RFDuino::receive_isr() { - - if(rfd.getc() == RECEIVE) { - data.len = (int)rfd.getc(); - for(int i=0;i<data.len;i++) { +void RFDuino::receive_isr() +{ + if(rfd.getc() == RECEIVE) { + data.len = static_cast<int>(rfd.getc()); + if(data.len > buff_size) { + // signal an error or reset?? + data.len = buff_size; + } + + for(int i=0; i<data.len; i++) { data.buff[i] = rfd.getc(); } - + //handshake + //rfd.putc(HANDSHAKE); + dataFlag=true; - } else { - //we dont know this command, read and disregard - while(rfd.readable()) { - rfd.getc(); + } else { + //we dont know this command, read and disregard + while(rfd.readable()) { + char c = rfd.getc(); + c = c; } } - } -