USBDevice
Dependents: QEI_X1_LCD_test3 macnica_test
USBSERIAL/USBSerial.h@0:2d8d0b73e1ff, 2013-04-18 (annotated)
- Committer:
- toucyy
- Date:
- Thu Apr 18 07:49:37 2013 +0000
- Revision:
- 0:2d8d0b73e1ff
[mbed] converted /QEI_HelloWorld/USBDevice
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
toucyy | 0:2d8d0b73e1ff | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
toucyy | 0:2d8d0b73e1ff | 2 | * |
toucyy | 0:2d8d0b73e1ff | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
toucyy | 0:2d8d0b73e1ff | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
toucyy | 0:2d8d0b73e1ff | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
toucyy | 0:2d8d0b73e1ff | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
toucyy | 0:2d8d0b73e1ff | 7 | * Software is furnished to do so, subject to the following conditions: |
toucyy | 0:2d8d0b73e1ff | 8 | * |
toucyy | 0:2d8d0b73e1ff | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
toucyy | 0:2d8d0b73e1ff | 10 | * substantial portions of the Software. |
toucyy | 0:2d8d0b73e1ff | 11 | * |
toucyy | 0:2d8d0b73e1ff | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
toucyy | 0:2d8d0b73e1ff | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
toucyy | 0:2d8d0b73e1ff | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
toucyy | 0:2d8d0b73e1ff | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
toucyy | 0:2d8d0b73e1ff | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
toucyy | 0:2d8d0b73e1ff | 17 | */ |
toucyy | 0:2d8d0b73e1ff | 18 | |
toucyy | 0:2d8d0b73e1ff | 19 | #ifndef USBSERIAL_H |
toucyy | 0:2d8d0b73e1ff | 20 | #define USBSERIAL_H |
toucyy | 0:2d8d0b73e1ff | 21 | |
toucyy | 0:2d8d0b73e1ff | 22 | #include "USBCDC.h" |
toucyy | 0:2d8d0b73e1ff | 23 | #include "Stream.h" |
toucyy | 0:2d8d0b73e1ff | 24 | #include "CircBuffer.h" |
toucyy | 0:2d8d0b73e1ff | 25 | |
toucyy | 0:2d8d0b73e1ff | 26 | |
toucyy | 0:2d8d0b73e1ff | 27 | /** |
toucyy | 0:2d8d0b73e1ff | 28 | * USBSerial example |
toucyy | 0:2d8d0b73e1ff | 29 | * |
toucyy | 0:2d8d0b73e1ff | 30 | * @code |
toucyy | 0:2d8d0b73e1ff | 31 | * #include "mbed.h" |
toucyy | 0:2d8d0b73e1ff | 32 | * #include "USBSerial.h" |
toucyy | 0:2d8d0b73e1ff | 33 | * |
toucyy | 0:2d8d0b73e1ff | 34 | * //Virtual serial port over USB |
toucyy | 0:2d8d0b73e1ff | 35 | * USBSerial serial; |
toucyy | 0:2d8d0b73e1ff | 36 | * |
toucyy | 0:2d8d0b73e1ff | 37 | * int main(void) { |
toucyy | 0:2d8d0b73e1ff | 38 | * |
toucyy | 0:2d8d0b73e1ff | 39 | * while(1) |
toucyy | 0:2d8d0b73e1ff | 40 | * { |
toucyy | 0:2d8d0b73e1ff | 41 | * serial.printf("I am a virtual serial port\n"); |
toucyy | 0:2d8d0b73e1ff | 42 | * wait(1); |
toucyy | 0:2d8d0b73e1ff | 43 | * } |
toucyy | 0:2d8d0b73e1ff | 44 | * } |
toucyy | 0:2d8d0b73e1ff | 45 | * @endcode |
toucyy | 0:2d8d0b73e1ff | 46 | */ |
toucyy | 0:2d8d0b73e1ff | 47 | class USBSerial: public USBCDC, public Stream { |
toucyy | 0:2d8d0b73e1ff | 48 | public: |
toucyy | 0:2d8d0b73e1ff | 49 | |
toucyy | 0:2d8d0b73e1ff | 50 | /** |
toucyy | 0:2d8d0b73e1ff | 51 | * Constructor |
toucyy | 0:2d8d0b73e1ff | 52 | * |
toucyy | 0:2d8d0b73e1ff | 53 | * @param vendor_id Your vendor_id (default: 0x1f00) |
toucyy | 0:2d8d0b73e1ff | 54 | * @param product_id Your product_id (default: 0x2012) |
toucyy | 0:2d8d0b73e1ff | 55 | * @param product_release Your preoduct_release (default: 0x0001) |
toucyy | 0:2d8d0b73e1ff | 56 | * |
toucyy | 0:2d8d0b73e1ff | 57 | */ |
toucyy | 0:2d8d0b73e1ff | 58 | USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001): USBCDC(vendor_id, product_id, product_release), buf(128){ }; |
toucyy | 0:2d8d0b73e1ff | 59 | |
toucyy | 0:2d8d0b73e1ff | 60 | |
toucyy | 0:2d8d0b73e1ff | 61 | /** |
toucyy | 0:2d8d0b73e1ff | 62 | * Send a character. You can use puts, printf. |
toucyy | 0:2d8d0b73e1ff | 63 | * |
toucyy | 0:2d8d0b73e1ff | 64 | * @param c character to be sent |
toucyy | 0:2d8d0b73e1ff | 65 | * @returns true if there is no error, false otherwise |
toucyy | 0:2d8d0b73e1ff | 66 | */ |
toucyy | 0:2d8d0b73e1ff | 67 | virtual int _putc(int c); |
toucyy | 0:2d8d0b73e1ff | 68 | |
toucyy | 0:2d8d0b73e1ff | 69 | /** |
toucyy | 0:2d8d0b73e1ff | 70 | * Read a character: blocking |
toucyy | 0:2d8d0b73e1ff | 71 | * |
toucyy | 0:2d8d0b73e1ff | 72 | * @returns character read |
toucyy | 0:2d8d0b73e1ff | 73 | */ |
toucyy | 0:2d8d0b73e1ff | 74 | virtual int _getc(); |
toucyy | 0:2d8d0b73e1ff | 75 | |
toucyy | 0:2d8d0b73e1ff | 76 | /** |
toucyy | 0:2d8d0b73e1ff | 77 | * Check the number of bytes available. |
toucyy | 0:2d8d0b73e1ff | 78 | * |
toucyy | 0:2d8d0b73e1ff | 79 | * @returns the number of bytes available |
toucyy | 0:2d8d0b73e1ff | 80 | */ |
toucyy | 0:2d8d0b73e1ff | 81 | uint8_t available(); |
toucyy | 0:2d8d0b73e1ff | 82 | |
toucyy | 0:2d8d0b73e1ff | 83 | /** |
toucyy | 0:2d8d0b73e1ff | 84 | * Attach a member function to call when a packet is received. |
toucyy | 0:2d8d0b73e1ff | 85 | * |
toucyy | 0:2d8d0b73e1ff | 86 | * @param tptr pointer to the object to call the member function on |
toucyy | 0:2d8d0b73e1ff | 87 | * @param mptr pointer to the member function to be called |
toucyy | 0:2d8d0b73e1ff | 88 | */ |
toucyy | 0:2d8d0b73e1ff | 89 | template<typename T> |
toucyy | 0:2d8d0b73e1ff | 90 | void attach(T* tptr, void (T::*mptr)(void)) { |
toucyy | 0:2d8d0b73e1ff | 91 | if((mptr != NULL) && (tptr != NULL)) { |
toucyy | 0:2d8d0b73e1ff | 92 | rx.attach(tptr, mptr); |
toucyy | 0:2d8d0b73e1ff | 93 | } |
toucyy | 0:2d8d0b73e1ff | 94 | } |
toucyy | 0:2d8d0b73e1ff | 95 | |
toucyy | 0:2d8d0b73e1ff | 96 | /** |
toucyy | 0:2d8d0b73e1ff | 97 | * Attach a callback called when a packet is received |
toucyy | 0:2d8d0b73e1ff | 98 | * |
toucyy | 0:2d8d0b73e1ff | 99 | * @param fptr function pointer |
toucyy | 0:2d8d0b73e1ff | 100 | */ |
toucyy | 0:2d8d0b73e1ff | 101 | void attach(void (*fn)(void)) { |
toucyy | 0:2d8d0b73e1ff | 102 | if(fn != NULL) { |
toucyy | 0:2d8d0b73e1ff | 103 | rx.attach(fn); |
toucyy | 0:2d8d0b73e1ff | 104 | } |
toucyy | 0:2d8d0b73e1ff | 105 | } |
toucyy | 0:2d8d0b73e1ff | 106 | |
toucyy | 0:2d8d0b73e1ff | 107 | |
toucyy | 0:2d8d0b73e1ff | 108 | protected: |
toucyy | 0:2d8d0b73e1ff | 109 | virtual bool EP2_OUT_callback(); |
toucyy | 0:2d8d0b73e1ff | 110 | |
toucyy | 0:2d8d0b73e1ff | 111 | private: |
toucyy | 0:2d8d0b73e1ff | 112 | FunctionPointer rx; |
toucyy | 0:2d8d0b73e1ff | 113 | CircBuffer<uint8_t> buf; |
toucyy | 0:2d8d0b73e1ff | 114 | }; |
toucyy | 0:2d8d0b73e1ff | 115 | |
toucyy | 0:2d8d0b73e1ff | 116 | #endif |