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