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