mbed library to connect to rfduino

Dependents:   RFDuino_example

Files at this revision

API Documentation at this revision

Comitter:
dbarbi1
Date:
Wed Jan 08 20:57:52 2014 +0000
Parent:
3:aac9193b7fd3
Child:
5:cd05cc4dd824
Commit message:
Updates and recommendations from Sam.

Changed in this revision

RFDuino.cpp Show annotated file Show diff for this revision Revisions of this file
RFDuino.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
         }
     }
-    
 }
 
 
 
 
 
-    
--- a/RFDuino.h	Tue Jan 07 23:36:17 2014 +0000
+++ b/RFDuino.h	Wed Jan 08 20:57:52 2014 +0000
@@ -1,73 +1,167 @@
+/* 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.
+ */
 
-#ifndef RFDuino_h
-#define RFDuino_h
-
+#ifndef RFDUINO_H
+#define RFDUINO_H
 
 #include "mbed.h"
 
+int const buff_size = 255;
+
 typedef struct  {
-    unsigned char buff[255];
+    unsigned char buff[buff_size];
     int len;
 } RFD_data;
 
-/** RFDuino is used for connecting an mbed to rfduino
- * 
+/** How to use the RFDuino library
+ *  Here is an example:
+ *  @code
+* #include "mbed.h"
+* #include "RFDuino.h"
+* 
+* PwmOut red(LED_RED);
+* PwmOut green(LED_GREEN);
+* PwmOut blue(LED_BLUE);
+* 
+* RFDuino rfd(PTC4, PTC3); // defaults to 9600 baud
+* 
+* typedef union {
+*     float fp;
+*     unsigned int uint_32;
+* } intFloat;
+* 
+* typedef struct  {
+*     intFloat red;
+*     intFloat green;
+*     intFloat blue;
+* } RGB;
+* 
+* void bblink(void)
+* {
+*     blue=0;
+*     wait(0.5);
+*     blue=1;
+*     wait(0.5);
+* }
+* 
+* void byteSwap(RGB* rgb)
+* {
+*     intFloat temp;
+* 
+*     temp.uint_32 = __REV(rgb->red.uint_32);
+*     rgb->red.fp = temp.fp;
+* 
+*     temp.uint_32 = __REV(rgb->blue.uint_32);
+*     rgb->blue.fp = temp.fp;
+* 
+*     temp.uint_32 = __REV(rgb->green.uint_32);
+*     rgb->green.fp = temp.fp;
+* 
+* }
+* 
+* int main()
+* {
+*     RGB rgb = {1.0f, 1.0f, 1.0f};
+*     
+*     red = blue = green = 1.0f;
+*     wait(2.0f); // make sure to wait at least 2 seconds before handshake
+* 
+*     // flash red and halt here if rfduino is not on the line
+*     if(!rfd.handshake()) {
+*         while(1) {
+*             red = !red;
+*             wait(0.2f);
+*         }
+*     }
+* 
+*     // wait for a connection
+*     while(!rfd.isConnected()) {
+*         bblink();
+*     }
+* 
+*     while(1) {
+*         if(rfd.readable()) {
+*             // copy data into data struct
+*             rfd.read((unsigned char*)&rgb.red, sizeof(rgb));
+*             
+*             // re-arrange bytes
+*             byteSwap(&rgb);
+* 
+*             //set pwms
+*             red   = rgb.red.fp;
+*             blue  = rgb.blue.fp;
+*             green = rgb.green.fp;
+*         }
+*     }
+* }
+ *
+ *  @endcode
  */
-class RFDuino    {
-    
-private: 
-    Serial rfd;
-    RFD_data data;
-    bool dataFlag;
-    void receive_isr();
+
+class RFDuino
+{
+
+private:
+    Serial      rfd;
+    RFD_data    data;
+    bool        dataFlag;
     
-public:     
+    void receive_isr();
+
+public:
 
-    /** Initializes RFDuino. Configures Pins tx and rx for serial communication
-     * and creats associated ISR
+    /**
+     *  Constructor for RFDuino
+     *  @param tx - Serial TX pin from target MCU
+     *  @param rx - Serial RX pin from target MCU
      */
-    RFDuino(PinName tx, PinName rx); 
+    RFDuino(PinName tx, PinName rx);
     
-   /** handshake()
-     *
-     * @returns
-     *   1 on succesfull RFDuino serial reply to Handshake command
-     *   0 on unsuccesfull RFDuino serial reply to Handshake command
+    /**
+     *  Do a handshake with the device
+     *  @return 1 if the device is connected, 0 otherwise
      */
     bool handshake(void);
     
-   /** dataReady()
-     *
-     * @returns
-     *   1 if RFDuino has unread data
-     *   0 if RFDuino does not have unread data
+    /**
+     *  Check to see if data is ready to read
+     *  @return 1 if data is available to read, 0 otherwise
      */
-    bool dataReady(void);
+    bool readable(void) const;
     
-    /** isConnected()
-     *
-     * @returns
-     *   1 if the RFDuino has made a successful Bluetooth Connection
-     *   0 if the RFDuino has not made a successful Bluetooth Connection
+    /**
+     *  Check to see if the device is connected
+     *  @return 1 if the device is connected, 0 otherwise
      */
     bool isConnected(void);
     
-    /** transmit(buff, len)
-     *
-     * @param buff pointer to a byte buffer
-     * @param len length of byte buffer to transmit
+    /**
+     *  Send data to the device
+     *  @param buff - A buffer of data to send
+     *  @param len - the amount of data in the buff
      */
-    void transmit(unsigned char* buff, int len);
+    bool transmit(const unsigned char *buff, const int len);
     
-   /** 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
+    /**
+     *  Read data from the device
+     *  @param buff - A buffer to read data into
+     *  @param size - The max amount of data to read
+     *  @return - The amount of data read from the device
      */
-    int  copyData(unsigned char* buff, int size);        
-
+    int read(unsigned char *buff, const int size);
 };
 
 #endif