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