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