blablabla

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
Osator
Date:
Wed Apr 16 12:20:00 2014 +0000
Revision:
0:339b7abfa147
blablabla

Who changed what in which revision?

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