BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Committer:
gustavatmel
Date:
Tue May 01 15:55:34 2018 +0000
Revision:
2:798925c9e4a8
Parent:
1:9c5af431a1f1
bluetooth

Who changed what in which revision?

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