Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wue 0:7b58cdacf811 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
wue 0:7b58cdacf811 2 *
wue 0:7b58cdacf811 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
wue 0:7b58cdacf811 4 * and associated documentation files (the "Software"), to deal in the Software without
wue 0:7b58cdacf811 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
wue 0:7b58cdacf811 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
wue 0:7b58cdacf811 7 * Software is furnished to do so, subject to the following conditions:
wue 0:7b58cdacf811 8 *
wue 0:7b58cdacf811 9 * The above copyright notice and this permission notice shall be included in all copies or
wue 0:7b58cdacf811 10 * substantial portions of the Software.
wue 0:7b58cdacf811 11 *
wue 0:7b58cdacf811 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
wue 0:7b58cdacf811 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
wue 0:7b58cdacf811 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
wue 0:7b58cdacf811 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wue 0:7b58cdacf811 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wue 0:7b58cdacf811 17 */
wue 0:7b58cdacf811 18
wue 0:7b58cdacf811 19 #ifndef USBSERIAL_H
wue 0:7b58cdacf811 20 #define USBSERIAL_H
wue 0:7b58cdacf811 21
wue 0:7b58cdacf811 22 #include "USBCDC.h"
wue 0:7b58cdacf811 23 #include "Stream.h"
wue 0:7b58cdacf811 24 #include "CircBuffer.h"
wue 0:7b58cdacf811 25
wue 0:7b58cdacf811 26
wue 0:7b58cdacf811 27 /**
wue 0:7b58cdacf811 28 * USBSerial example
wue 0:7b58cdacf811 29 *
wue 0:7b58cdacf811 30 * @code
wue 0:7b58cdacf811 31 * #include "mbed.h"
wue 0:7b58cdacf811 32 * #include "USBSerial.h"
wue 0:7b58cdacf811 33 *
wue 0:7b58cdacf811 34 * //Virtual serial port over USB
wue 0:7b58cdacf811 35 * USBSerial serial;
wue 0:7b58cdacf811 36 *
wue 0:7b58cdacf811 37 * int main(void) {
wue 0:7b58cdacf811 38 *
wue 0:7b58cdacf811 39 * while(1)
wue 0:7b58cdacf811 40 * {
wue 0:7b58cdacf811 41 * serial.printf("I am a virtual serial port\n");
wue 0:7b58cdacf811 42 * wait(1);
wue 0:7b58cdacf811 43 * }
wue 0:7b58cdacf811 44 * }
wue 0:7b58cdacf811 45 * @endcode
wue 0:7b58cdacf811 46 */
wue 0:7b58cdacf811 47 class USBSerial: public USBCDC, public Stream {
wue 0:7b58cdacf811 48 public:
wue 0:7b58cdacf811 49
wue 0:7b58cdacf811 50 /**
wue 0:7b58cdacf811 51 * Constructor
wue 0:7b58cdacf811 52 *
wue 0:7b58cdacf811 53 * @param vendor_id Your vendor_id (default: 0x1f00)
wue 0:7b58cdacf811 54 * @param product_id Your product_id (default: 0x2012)
wue 0:7b58cdacf811 55 * @param product_release Your preoduct_release (default: 0x0001)
wue 0:7b58cdacf811 56 * @param connect_blocking define if the connection must be blocked if USB not plugged in
wue 0:7b58cdacf811 57 *
wue 0:7b58cdacf811 58 */
wue 0:7b58cdacf811 59 USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001, bool connect_blocking = true): USBCDC(vendor_id, product_id, product_release, connect_blocking), buf(128){
wue 0:7b58cdacf811 60 settingsChangedCallback = 0;
wue 0:7b58cdacf811 61 };
wue 0:7b58cdacf811 62
wue 0:7b58cdacf811 63
wue 0:7b58cdacf811 64 /**
wue 0:7b58cdacf811 65 * Send a character. You can use puts, printf.
wue 0:7b58cdacf811 66 *
wue 0:7b58cdacf811 67 * @param c character to be sent
wue 0:7b58cdacf811 68 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 69 */
wue 0:7b58cdacf811 70 virtual int _putc(int c);
wue 0:7b58cdacf811 71
wue 0:7b58cdacf811 72 /**
wue 0:7b58cdacf811 73 * Read a character: blocking
wue 0:7b58cdacf811 74 *
wue 0:7b58cdacf811 75 * @returns character read
wue 0:7b58cdacf811 76 */
wue 0:7b58cdacf811 77 virtual int _getc();
wue 0:7b58cdacf811 78
wue 0:7b58cdacf811 79 /**
wue 0:7b58cdacf811 80 * Check the number of bytes available.
wue 0:7b58cdacf811 81 *
wue 0:7b58cdacf811 82 * @returns the number of bytes available
wue 0:7b58cdacf811 83 */
wue 0:7b58cdacf811 84 uint8_t available();
wue 0:7b58cdacf811 85
wue 0:7b58cdacf811 86 /** Determine if there is a character available to read
wue 0:7b58cdacf811 87 *
wue 0:7b58cdacf811 88 * @returns
wue 0:7b58cdacf811 89 * 1 if there is a character available to read,
wue 0:7b58cdacf811 90 * 0 otherwise
wue 0:7b58cdacf811 91 */
wue 0:7b58cdacf811 92 int readable() { return available() ? 1 : 0; }
wue 0:7b58cdacf811 93
wue 0:7b58cdacf811 94 /** Determine if there is space available to write a character
wue 0:7b58cdacf811 95 *
wue 0:7b58cdacf811 96 * @returns
wue 0:7b58cdacf811 97 * 1 if there is space to write a character,
wue 0:7b58cdacf811 98 * 0 otherwise
wue 0:7b58cdacf811 99 */
wue 0:7b58cdacf811 100 int writeable() { return 1; } // always return 1, for write operation is blocking
wue 0:7b58cdacf811 101
wue 0:7b58cdacf811 102 /**
wue 0:7b58cdacf811 103 * Write a block of data.
wue 0:7b58cdacf811 104 *
wue 0:7b58cdacf811 105 * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written.
wue 0:7b58cdacf811 106 *
wue 0:7b58cdacf811 107 * @param buf pointer on data which will be written
wue 0:7b58cdacf811 108 * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes)
wue 0:7b58cdacf811 109 *
wue 0:7b58cdacf811 110 * @returns true if successfull
wue 0:7b58cdacf811 111 */
wue 0:7b58cdacf811 112 bool writeBlock(uint8_t * buf, uint16_t size);
wue 0:7b58cdacf811 113
wue 0:7b58cdacf811 114 /**
wue 0:7b58cdacf811 115 * Attach a member function to call when a packet is received.
wue 0:7b58cdacf811 116 *
wue 0:7b58cdacf811 117 * @param tptr pointer to the object to call the member function on
wue 0:7b58cdacf811 118 * @param mptr pointer to the member function to be called
wue 0:7b58cdacf811 119 */
wue 0:7b58cdacf811 120 template<typename T>
wue 0:7b58cdacf811 121 void attach(T* tptr, void (T::*mptr)(void)) {
wue 0:7b58cdacf811 122 if((mptr != NULL) && (tptr != NULL)) {
wue 0:7b58cdacf811 123 rx.attach(tptr, mptr);
wue 0:7b58cdacf811 124 }
wue 0:7b58cdacf811 125 }
wue 0:7b58cdacf811 126
wue 0:7b58cdacf811 127 /**
wue 0:7b58cdacf811 128 * Attach a callback called when a packet is received
wue 0:7b58cdacf811 129 *
wue 0:7b58cdacf811 130 * @param fptr function pointer
wue 0:7b58cdacf811 131 */
wue 0:7b58cdacf811 132 void attach(void (*fptr)(void)) {
wue 0:7b58cdacf811 133 if(fptr != NULL) {
wue 0:7b58cdacf811 134 rx.attach(fptr);
wue 0:7b58cdacf811 135 }
wue 0:7b58cdacf811 136 }
wue 0:7b58cdacf811 137
wue 0:7b58cdacf811 138 /**
wue 0:7b58cdacf811 139 * Attach a callback to call when serial's settings are changed.
wue 0:7b58cdacf811 140 *
wue 0:7b58cdacf811 141 * @param fptr function pointer
wue 0:7b58cdacf811 142 */
wue 0:7b58cdacf811 143 void attach(void (*fptr)(int baud, int bits, int parity, int stop)) {
wue 0:7b58cdacf811 144 settingsChangedCallback = fptr;
wue 0:7b58cdacf811 145 }
wue 0:7b58cdacf811 146
wue 0:7b58cdacf811 147 protected:
wue 0:7b58cdacf811 148 virtual bool EP2_OUT_callback();
wue 0:7b58cdacf811 149 virtual void lineCodingChanged(int baud, int bits, int parity, int stop){
wue 0:7b58cdacf811 150 if (settingsChangedCallback) {
wue 0:7b58cdacf811 151 settingsChangedCallback(baud, bits, parity, stop);
wue 0:7b58cdacf811 152 }
wue 0:7b58cdacf811 153 }
wue 0:7b58cdacf811 154
wue 0:7b58cdacf811 155 private:
wue 0:7b58cdacf811 156 FunctionPointer rx;
wue 0:7b58cdacf811 157 CircBuffer<uint8_t> buf;
wue 0:7b58cdacf811 158 void (*settingsChangedCallback)(int baud, int bits, int parity, int stop);
wue 0:7b58cdacf811 159 };
wue 0:7b58cdacf811 160
wue 0:7b58cdacf811 161 #endif