mbed library to connect to rfduino

Dependents:   RFDuino_example

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?

UserRevisionLine numberNew 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 4:7cbe6036c44e 15 */
dbarbi1 3:aac9193b7fd3 16
dbarbi1 4:7cbe6036c44e 17 #ifndef RFDUINO_H
dbarbi1 4:7cbe6036c44e 18 #define RFDUINO_H
dbarbi1 0:af5f495861b2 19
dbarbi1 0:af5f495861b2 20 #include "mbed.h"
dbarbi1 0:af5f495861b2 21
dbarbi1 4:7cbe6036c44e 22 int const buff_size = 255;
dbarbi1 4:7cbe6036c44e 23
dbarbi1 0:af5f495861b2 24 typedef struct {
dbarbi1 4:7cbe6036c44e 25 unsigned char buff[buff_size];
dbarbi1 0:af5f495861b2 26 int len;
dbarbi1 0:af5f495861b2 27 } RFD_data;
dbarbi1 0:af5f495861b2 28
dbarbi1 4:7cbe6036c44e 29 /** How to use the RFDuino library
dbarbi1 4:7cbe6036c44e 30 * Here is an example:
dbarbi1 4:7cbe6036c44e 31 * @code
dbarbi1 4:7cbe6036c44e 32 * #include "mbed.h"
dbarbi1 4:7cbe6036c44e 33 * #include "RFDuino.h"
dbarbi1 4:7cbe6036c44e 34 *
dbarbi1 4:7cbe6036c44e 35 * PwmOut red(LED_RED);
dbarbi1 4:7cbe6036c44e 36 * PwmOut green(LED_GREEN);
dbarbi1 4:7cbe6036c44e 37 * PwmOut blue(LED_BLUE);
dbarbi1 4:7cbe6036c44e 38 *
dbarbi1 4:7cbe6036c44e 39 * RFDuino rfd(PTC4, PTC3); // defaults to 9600 baud
dbarbi1 4:7cbe6036c44e 40 *
dbarbi1 4:7cbe6036c44e 41 * typedef union {
dbarbi1 4:7cbe6036c44e 42 * float fp;
dbarbi1 4:7cbe6036c44e 43 * unsigned int uint_32;
dbarbi1 4:7cbe6036c44e 44 * } intFloat;
dbarbi1 4:7cbe6036c44e 45 *
dbarbi1 4:7cbe6036c44e 46 * typedef struct {
dbarbi1 4:7cbe6036c44e 47 * intFloat red;
dbarbi1 4:7cbe6036c44e 48 * intFloat green;
dbarbi1 4:7cbe6036c44e 49 * intFloat blue;
dbarbi1 4:7cbe6036c44e 50 * } RGB;
dbarbi1 4:7cbe6036c44e 51 *
dbarbi1 4:7cbe6036c44e 52 * void bblink(void)
dbarbi1 4:7cbe6036c44e 53 * {
dbarbi1 4:7cbe6036c44e 54 * blue=0;
dbarbi1 4:7cbe6036c44e 55 * wait(0.5);
dbarbi1 4:7cbe6036c44e 56 * blue=1;
dbarbi1 4:7cbe6036c44e 57 * wait(0.5);
dbarbi1 4:7cbe6036c44e 58 * }
dbarbi1 4:7cbe6036c44e 59 *
dbarbi1 4:7cbe6036c44e 60 * void byteSwap(RGB* rgb)
dbarbi1 4:7cbe6036c44e 61 * {
dbarbi1 4:7cbe6036c44e 62 * intFloat temp;
dbarbi1 4:7cbe6036c44e 63 *
dbarbi1 4:7cbe6036c44e 64 * temp.uint_32 = __REV(rgb->red.uint_32);
dbarbi1 4:7cbe6036c44e 65 * rgb->red.fp = temp.fp;
dbarbi1 4:7cbe6036c44e 66 *
dbarbi1 4:7cbe6036c44e 67 * temp.uint_32 = __REV(rgb->blue.uint_32);
dbarbi1 4:7cbe6036c44e 68 * rgb->blue.fp = temp.fp;
dbarbi1 4:7cbe6036c44e 69 *
dbarbi1 4:7cbe6036c44e 70 * temp.uint_32 = __REV(rgb->green.uint_32);
dbarbi1 4:7cbe6036c44e 71 * rgb->green.fp = temp.fp;
dbarbi1 4:7cbe6036c44e 72 *
dbarbi1 4:7cbe6036c44e 73 * }
dbarbi1 4:7cbe6036c44e 74 *
dbarbi1 4:7cbe6036c44e 75 * int main()
dbarbi1 4:7cbe6036c44e 76 * {
dbarbi1 4:7cbe6036c44e 77 * RGB rgb = {1.0f, 1.0f, 1.0f};
dbarbi1 4:7cbe6036c44e 78 *
dbarbi1 4:7cbe6036c44e 79 * red = blue = green = 1.0f;
dbarbi1 4:7cbe6036c44e 80 * wait(2.0f); // make sure to wait at least 2 seconds before handshake
dbarbi1 4:7cbe6036c44e 81 *
dbarbi1 4:7cbe6036c44e 82 * // flash red and halt here if rfduino is not on the line
dbarbi1 4:7cbe6036c44e 83 * if(!rfd.handshake()) {
dbarbi1 4:7cbe6036c44e 84 * while(1) {
dbarbi1 4:7cbe6036c44e 85 * red = !red;
dbarbi1 4:7cbe6036c44e 86 * wait(0.2f);
dbarbi1 4:7cbe6036c44e 87 * }
dbarbi1 4:7cbe6036c44e 88 * }
dbarbi1 4:7cbe6036c44e 89 *
dbarbi1 4:7cbe6036c44e 90 * // wait for a connection
dbarbi1 4:7cbe6036c44e 91 * while(!rfd.isConnected()) {
dbarbi1 4:7cbe6036c44e 92 * bblink();
dbarbi1 4:7cbe6036c44e 93 * }
dbarbi1 4:7cbe6036c44e 94 *
dbarbi1 4:7cbe6036c44e 95 * while(1) {
dbarbi1 4:7cbe6036c44e 96 * if(rfd.readable()) {
dbarbi1 4:7cbe6036c44e 97 * // copy data into data struct
dbarbi1 4:7cbe6036c44e 98 * rfd.read((unsigned char*)&rgb.red, sizeof(rgb));
dbarbi1 4:7cbe6036c44e 99 *
dbarbi1 4:7cbe6036c44e 100 * // re-arrange bytes
dbarbi1 4:7cbe6036c44e 101 * byteSwap(&rgb);
dbarbi1 4:7cbe6036c44e 102 *
dbarbi1 4:7cbe6036c44e 103 * //set pwms
dbarbi1 4:7cbe6036c44e 104 * red = rgb.red.fp;
dbarbi1 4:7cbe6036c44e 105 * blue = rgb.blue.fp;
dbarbi1 4:7cbe6036c44e 106 * green = rgb.green.fp;
dbarbi1 4:7cbe6036c44e 107 * }
dbarbi1 4:7cbe6036c44e 108 * }
dbarbi1 4:7cbe6036c44e 109 * }
dbarbi1 4:7cbe6036c44e 110 *
dbarbi1 4:7cbe6036c44e 111 * @endcode
dbarbi1 3:aac9193b7fd3 112 */
dbarbi1 4:7cbe6036c44e 113
dbarbi1 4:7cbe6036c44e 114 class RFDuino
dbarbi1 4:7cbe6036c44e 115 {
dbarbi1 4:7cbe6036c44e 116
dbarbi1 4:7cbe6036c44e 117 private:
dbarbi1 4:7cbe6036c44e 118 Serial rfd;
dbarbi1 4:7cbe6036c44e 119 RFD_data data;
dbarbi1 4:7cbe6036c44e 120 bool dataFlag;
dbarbi1 2:effa15a46f51 121
dbarbi1 4:7cbe6036c44e 122 void receive_isr();
dbarbi1 4:7cbe6036c44e 123
dbarbi1 4:7cbe6036c44e 124 public:
dbarbi1 0:af5f495861b2 125
dbarbi1 4:7cbe6036c44e 126 /**
dbarbi1 4:7cbe6036c44e 127 * Constructor for RFDuino
dbarbi1 4:7cbe6036c44e 128 * @param tx - Serial TX pin from target MCU
dbarbi1 4:7cbe6036c44e 129 * @param rx - Serial RX pin from target MCU
dbarbi1 3:aac9193b7fd3 130 */
dbarbi1 4:7cbe6036c44e 131 RFDuino(PinName tx, PinName rx);
dbarbi1 3:aac9193b7fd3 132
dbarbi1 4:7cbe6036c44e 133 /**
dbarbi1 4:7cbe6036c44e 134 * Do a handshake with the device
dbarbi1 5:cd05cc4dd824 135 * @return 1 if the RFduino device is connected, 0 otherwise
dbarbi1 3:aac9193b7fd3 136 */
dbarbi1 0:af5f495861b2 137 bool handshake(void);
dbarbi1 3:aac9193b7fd3 138
dbarbi1 4:7cbe6036c44e 139 /**
dbarbi1 4:7cbe6036c44e 140 * Check to see if data is ready to read
dbarbi1 4:7cbe6036c44e 141 * @return 1 if data is available to read, 0 otherwise
dbarbi1 3:aac9193b7fd3 142 */
dbarbi1 4:7cbe6036c44e 143 bool readable(void) const;
dbarbi1 3:aac9193b7fd3 144
dbarbi1 4:7cbe6036c44e 145 /**
dbarbi1 4:7cbe6036c44e 146 * Check to see if the device is connected
dbarbi1 5:cd05cc4dd824 147 * @return 1 if Bluetooth Connection is established, 0 otherwise
dbarbi1 3:aac9193b7fd3 148 */
dbarbi1 0:af5f495861b2 149 bool isConnected(void);
dbarbi1 3:aac9193b7fd3 150
dbarbi1 4:7cbe6036c44e 151 /**
dbarbi1 4:7cbe6036c44e 152 * Send data to the device
dbarbi1 4:7cbe6036c44e 153 * @param buff - A buffer of data to send
dbarbi1 4:7cbe6036c44e 154 * @param len - the amount of data in the buff
dbarbi1 3:aac9193b7fd3 155 */
dbarbi1 4:7cbe6036c44e 156 bool transmit(const unsigned char *buff, const int len);
dbarbi1 3:aac9193b7fd3 157
dbarbi1 4:7cbe6036c44e 158 /**
dbarbi1 4:7cbe6036c44e 159 * Read data from the device
dbarbi1 4:7cbe6036c44e 160 * @param buff - A buffer to read data into
dbarbi1 4:7cbe6036c44e 161 * @param size - The max amount of data to read
dbarbi1 4:7cbe6036c44e 162 * @return - The amount of data read from the device
dbarbi1 3:aac9193b7fd3 163 */
dbarbi1 4:7cbe6036c44e 164 int read(unsigned char *buff, const int size);
dbarbi1 0:af5f495861b2 165 };
dbarbi1 0:af5f495861b2 166
dbarbi1 0:af5f495861b2 167 #endif