USB Serial application
Fork of USBSerial_HelloWorld by
USBDevice/USBHID/USBKeyboard.h@11:b3f2a8bdac4d, 2017-12-16 (annotated)
- Committer:
- Zaitsev
- Date:
- Sat Dec 16 10:26:48 2017 +0000
- Revision:
- 11:b3f2a8bdac4d
- Parent:
- 10:41552d038a69
A copy for D.S;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Zaitsev | 10:41552d038a69 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
Zaitsev | 10:41552d038a69 | 2 | * |
Zaitsev | 10:41552d038a69 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Zaitsev | 10:41552d038a69 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
Zaitsev | 10:41552d038a69 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
Zaitsev | 10:41552d038a69 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
Zaitsev | 10:41552d038a69 | 7 | * Software is furnished to do so, subject to the following conditions: |
Zaitsev | 10:41552d038a69 | 8 | * |
Zaitsev | 10:41552d038a69 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Zaitsev | 10:41552d038a69 | 10 | * substantial portions of the Software. |
Zaitsev | 10:41552d038a69 | 11 | * |
Zaitsev | 10:41552d038a69 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Zaitsev | 10:41552d038a69 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Zaitsev | 10:41552d038a69 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Zaitsev | 10:41552d038a69 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Zaitsev | 10:41552d038a69 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Zaitsev | 10:41552d038a69 | 17 | */ |
Zaitsev | 10:41552d038a69 | 18 | |
Zaitsev | 10:41552d038a69 | 19 | #ifndef USBKEYBOARD_H |
Zaitsev | 10:41552d038a69 | 20 | #define USBKEYBOARD_H |
Zaitsev | 10:41552d038a69 | 21 | |
Zaitsev | 10:41552d038a69 | 22 | #include "USBHID.h" |
Zaitsev | 10:41552d038a69 | 23 | #include "Stream.h" |
Zaitsev | 10:41552d038a69 | 24 | |
Zaitsev | 10:41552d038a69 | 25 | /* Modifiers */ |
Zaitsev | 10:41552d038a69 | 26 | enum MODIFIER_KEY { |
Zaitsev | 10:41552d038a69 | 27 | KEY_CTRL = 1, |
Zaitsev | 10:41552d038a69 | 28 | KEY_SHIFT = 2, |
Zaitsev | 10:41552d038a69 | 29 | KEY_ALT = 4, |
Zaitsev | 10:41552d038a69 | 30 | }; |
Zaitsev | 10:41552d038a69 | 31 | |
Zaitsev | 10:41552d038a69 | 32 | |
Zaitsev | 10:41552d038a69 | 33 | enum MEDIA_KEY { |
Zaitsev | 10:41552d038a69 | 34 | KEY_NEXT_TRACK, /*!< next Track Button */ |
Zaitsev | 10:41552d038a69 | 35 | KEY_PREVIOUS_TRACK, /*!< Previous track Button */ |
Zaitsev | 10:41552d038a69 | 36 | KEY_STOP, /*!< Stop Button */ |
Zaitsev | 10:41552d038a69 | 37 | KEY_PLAY_PAUSE, /*!< Play/Pause Button */ |
Zaitsev | 10:41552d038a69 | 38 | KEY_MUTE, /*!< Mute Button */ |
Zaitsev | 10:41552d038a69 | 39 | KEY_VOLUME_UP, /*!< Volume Up Button */ |
Zaitsev | 10:41552d038a69 | 40 | KEY_VOLUME_DOWN, /*!< Volume Down Button */ |
Zaitsev | 10:41552d038a69 | 41 | }; |
Zaitsev | 10:41552d038a69 | 42 | |
Zaitsev | 10:41552d038a69 | 43 | enum FUNCTION_KEY { |
Zaitsev | 10:41552d038a69 | 44 | KEY_F1 = 128, /* F1 key */ |
Zaitsev | 10:41552d038a69 | 45 | KEY_F2, /* F2 key */ |
Zaitsev | 10:41552d038a69 | 46 | KEY_F3, /* F3 key */ |
Zaitsev | 10:41552d038a69 | 47 | KEY_F4, /* F4 key */ |
Zaitsev | 10:41552d038a69 | 48 | KEY_F5, /* F5 key */ |
Zaitsev | 10:41552d038a69 | 49 | KEY_F6, /* F6 key */ |
Zaitsev | 10:41552d038a69 | 50 | KEY_F7, /* F7 key */ |
Zaitsev | 10:41552d038a69 | 51 | KEY_F8, /* F8 key */ |
Zaitsev | 10:41552d038a69 | 52 | KEY_F9, /* F9 key */ |
Zaitsev | 10:41552d038a69 | 53 | KEY_F10, /* F10 key */ |
Zaitsev | 10:41552d038a69 | 54 | KEY_F11, /* F11 key */ |
Zaitsev | 10:41552d038a69 | 55 | KEY_F12, /* F12 key */ |
Zaitsev | 10:41552d038a69 | 56 | |
Zaitsev | 10:41552d038a69 | 57 | KEY_PRINT_SCREEN, /* Print Screen key */ |
Zaitsev | 10:41552d038a69 | 58 | KEY_SCROLL_LOCK, /* Scroll lock */ |
Zaitsev | 10:41552d038a69 | 59 | KEY_CAPS_LOCK, /* caps lock */ |
Zaitsev | 10:41552d038a69 | 60 | KEY_NUM_LOCK, /* num lock */ |
Zaitsev | 10:41552d038a69 | 61 | KEY_INSERT, /* Insert key */ |
Zaitsev | 10:41552d038a69 | 62 | KEY_HOME, /* Home key */ |
Zaitsev | 10:41552d038a69 | 63 | KEY_PAGE_UP, /* Page Up key */ |
Zaitsev | 10:41552d038a69 | 64 | KEY_PAGE_DOWN, /* Page Down key */ |
Zaitsev | 10:41552d038a69 | 65 | |
Zaitsev | 10:41552d038a69 | 66 | RIGHT_ARROW, /* Right arrow */ |
Zaitsev | 10:41552d038a69 | 67 | LEFT_ARROW, /* Left arrow */ |
Zaitsev | 10:41552d038a69 | 68 | DOWN_ARROW, /* Down arrow */ |
Zaitsev | 10:41552d038a69 | 69 | UP_ARROW, /* Up arrow */ |
Zaitsev | 10:41552d038a69 | 70 | }; |
Zaitsev | 10:41552d038a69 | 71 | |
Zaitsev | 10:41552d038a69 | 72 | /** |
Zaitsev | 10:41552d038a69 | 73 | * USBKeyboard example |
Zaitsev | 10:41552d038a69 | 74 | * @code |
Zaitsev | 10:41552d038a69 | 75 | * |
Zaitsev | 10:41552d038a69 | 76 | * #include "mbed.h" |
Zaitsev | 10:41552d038a69 | 77 | * #include "USBKeyboard.h" |
Zaitsev | 10:41552d038a69 | 78 | * |
Zaitsev | 10:41552d038a69 | 79 | * USBKeyboard key; |
Zaitsev | 10:41552d038a69 | 80 | * |
Zaitsev | 10:41552d038a69 | 81 | * int main(void) |
Zaitsev | 10:41552d038a69 | 82 | * { |
Zaitsev | 10:41552d038a69 | 83 | * while (1) |
Zaitsev | 10:41552d038a69 | 84 | * { |
Zaitsev | 10:41552d038a69 | 85 | * key.printf("Hello World\r\n"); |
Zaitsev | 10:41552d038a69 | 86 | * wait(1); |
Zaitsev | 10:41552d038a69 | 87 | * } |
Zaitsev | 10:41552d038a69 | 88 | * } |
Zaitsev | 10:41552d038a69 | 89 | * |
Zaitsev | 10:41552d038a69 | 90 | * @endcode |
Zaitsev | 10:41552d038a69 | 91 | */ |
Zaitsev | 10:41552d038a69 | 92 | class USBKeyboard: public USBHID, public Stream { |
Zaitsev | 10:41552d038a69 | 93 | public: |
Zaitsev | 10:41552d038a69 | 94 | |
Zaitsev | 10:41552d038a69 | 95 | /** |
Zaitsev | 10:41552d038a69 | 96 | * Constructor |
Zaitsev | 10:41552d038a69 | 97 | * |
Zaitsev | 10:41552d038a69 | 98 | * |
Zaitsev | 10:41552d038a69 | 99 | * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK |
Zaitsev | 10:41552d038a69 | 100 | * @param vendor_id Your vendor_id (default: 0x1235) |
Zaitsev | 10:41552d038a69 | 101 | * @param product_id Your product_id (default: 0x0050) |
Zaitsev | 10:41552d038a69 | 102 | * @param product_release Your preoduct_release (default: 0x0001) |
Zaitsev | 10:41552d038a69 | 103 | * |
Zaitsev | 10:41552d038a69 | 104 | */ |
Zaitsev | 10:41552d038a69 | 105 | USBKeyboard(uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001): |
Zaitsev | 10:41552d038a69 | 106 | USBHID(0, 0, vendor_id, product_id, product_release, false) { |
Zaitsev | 10:41552d038a69 | 107 | lock_status = 0; |
Zaitsev | 10:41552d038a69 | 108 | connect(); |
Zaitsev | 10:41552d038a69 | 109 | }; |
Zaitsev | 10:41552d038a69 | 110 | |
Zaitsev | 10:41552d038a69 | 111 | /** |
Zaitsev | 10:41552d038a69 | 112 | * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key |
Zaitsev | 10:41552d038a69 | 113 | * |
Zaitsev | 10:41552d038a69 | 114 | * @code |
Zaitsev | 10:41552d038a69 | 115 | * //To send CTRL + s (save) |
Zaitsev | 10:41552d038a69 | 116 | * keyboard.keyCode('s', KEY_CTRL); |
Zaitsev | 10:41552d038a69 | 117 | * @endcode |
Zaitsev | 10:41552d038a69 | 118 | * |
Zaitsev | 10:41552d038a69 | 119 | * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0) |
Zaitsev | 10:41552d038a69 | 120 | * @param key character to send |
Zaitsev | 10:41552d038a69 | 121 | * @returns true if there is no error, false otherwise |
Zaitsev | 10:41552d038a69 | 122 | */ |
Zaitsev | 10:41552d038a69 | 123 | bool keyCode(uint8_t key, uint8_t modifier = 0); |
Zaitsev | 10:41552d038a69 | 124 | |
Zaitsev | 10:41552d038a69 | 125 | /** |
Zaitsev | 10:41552d038a69 | 126 | * Send a character |
Zaitsev | 10:41552d038a69 | 127 | * |
Zaitsev | 10:41552d038a69 | 128 | * @param c character to be sent |
Zaitsev | 10:41552d038a69 | 129 | * @returns true if there is no error, false otherwise |
Zaitsev | 10:41552d038a69 | 130 | */ |
Zaitsev | 10:41552d038a69 | 131 | virtual int _putc(int c); |
Zaitsev | 10:41552d038a69 | 132 | |
Zaitsev | 10:41552d038a69 | 133 | /** |
Zaitsev | 10:41552d038a69 | 134 | * Control media keys |
Zaitsev | 10:41552d038a69 | 135 | * |
Zaitsev | 10:41552d038a69 | 136 | * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN) |
Zaitsev | 10:41552d038a69 | 137 | * @returns true if there is no error, false otherwise |
Zaitsev | 10:41552d038a69 | 138 | */ |
Zaitsev | 10:41552d038a69 | 139 | bool mediaControl(MEDIA_KEY key); |
Zaitsev | 10:41552d038a69 | 140 | |
Zaitsev | 10:41552d038a69 | 141 | /* |
Zaitsev | 10:41552d038a69 | 142 | * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. |
Zaitsev | 10:41552d038a69 | 143 | * |
Zaitsev | 10:41552d038a69 | 144 | * @returns pointer to the report descriptor |
Zaitsev | 10:41552d038a69 | 145 | */ |
Zaitsev | 10:41552d038a69 | 146 | virtual uint8_t * reportDesc(); |
Zaitsev | 10:41552d038a69 | 147 | |
Zaitsev | 10:41552d038a69 | 148 | /* |
Zaitsev | 10:41552d038a69 | 149 | * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys |
Zaitsev | 10:41552d038a69 | 150 | * |
Zaitsev | 10:41552d038a69 | 151 | * @returns if handle by subclass, return true |
Zaitsev | 10:41552d038a69 | 152 | */ |
Zaitsev | 10:41552d038a69 | 153 | virtual bool EPINT_OUT_callback(); |
Zaitsev | 10:41552d038a69 | 154 | |
Zaitsev | 10:41552d038a69 | 155 | /** |
Zaitsev | 10:41552d038a69 | 156 | * Read status of lock keys. Useful to switch-on/off leds according to key pressed. Only the first three bits of the result is important: |
Zaitsev | 10:41552d038a69 | 157 | * - First bit: NUM_LOCK |
Zaitsev | 10:41552d038a69 | 158 | * - Second bit: CAPS_LOCK |
Zaitsev | 10:41552d038a69 | 159 | * - Third bit: SCROLL_LOCK |
Zaitsev | 10:41552d038a69 | 160 | * |
Zaitsev | 10:41552d038a69 | 161 | * @returns status of lock keys |
Zaitsev | 10:41552d038a69 | 162 | */ |
Zaitsev | 10:41552d038a69 | 163 | uint8_t lockStatus(); |
Zaitsev | 10:41552d038a69 | 164 | |
Zaitsev | 10:41552d038a69 | 165 | protected: |
Zaitsev | 10:41552d038a69 | 166 | /* |
Zaitsev | 10:41552d038a69 | 167 | * Get configuration descriptor |
Zaitsev | 10:41552d038a69 | 168 | * |
Zaitsev | 10:41552d038a69 | 169 | * @returns pointer to the configuration descriptor |
Zaitsev | 10:41552d038a69 | 170 | */ |
Zaitsev | 10:41552d038a69 | 171 | virtual uint8_t * configurationDesc(); |
Zaitsev | 10:41552d038a69 | 172 | |
Zaitsev | 10:41552d038a69 | 173 | private: |
Zaitsev | 10:41552d038a69 | 174 | //dummy otherwise it doesn,t compile (we must define all methods of an abstract class) |
Zaitsev | 10:41552d038a69 | 175 | virtual int _getc() { |
Zaitsev | 10:41552d038a69 | 176 | return -1; |
Zaitsev | 10:41552d038a69 | 177 | }; |
Zaitsev | 10:41552d038a69 | 178 | |
Zaitsev | 10:41552d038a69 | 179 | uint8_t lock_status; |
Zaitsev | 10:41552d038a69 | 180 | |
Zaitsev | 10:41552d038a69 | 181 | }; |
Zaitsev | 10:41552d038a69 | 182 | |
Zaitsev | 10:41552d038a69 | 183 | #endif |