Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
USBKeyboard.h
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #ifndef USBKEYBOARD_H 00020 #define USBKEYBOARD_H 00021 00022 #include "USBHID.h" 00023 #include "Stream.h" 00024 00025 /* Modifiers */ 00026 enum MODIFIER_KEY 00027 { 00028 KEY_CTRL = 1, 00029 KEY_SHIFT = 2, 00030 KEY_ALT = 4, 00031 }; 00032 00033 00034 enum MEDIA_KEY 00035 { 00036 KEY_NEXT_TRACK, /*!< next Track Button */ 00037 KEY_PREVIOUS_TRACK, /*!< Previous track Button */ 00038 KEY_STOP, /*!< Stop Button */ 00039 KEY_PLAY_PAUSE, /*!< Play/Pause Button */ 00040 KEY_MUTE, /*!< Mute Button */ 00041 KEY_VOLUME_UP, /*!< Volume Up Button */ 00042 KEY_VOLUME_DOWN, /*!< Volume Down Button */ 00043 }; 00044 00045 enum FUNCTION_KEY 00046 { 00047 KEY_F1 = 128, /* F1 key */ 00048 KEY_F2, /* F2 key */ 00049 KEY_F3, /* F3 key */ 00050 KEY_F4, /* F4 key */ 00051 KEY_F5, /* F5 key */ 00052 KEY_F6, /* F6 key */ 00053 KEY_F7, /* F7 key */ 00054 KEY_F8, /* F8 key */ 00055 KEY_F9, /* F9 key */ 00056 KEY_F10, /* F10 key */ 00057 KEY_F11, /* F11 key */ 00058 KEY_F12, /* F12 key */ 00059 KEY_PRINT_SCREEN, /* Print Screen key */ 00060 KEY_SCROLL_LOCK, /* Scroll lock */ 00061 KEY_CAPS_LOCK, /* caps lock */ 00062 KEY_NUM_LOCK, /* num lock */ 00063 KEY_INSERT, /* Insert key */ 00064 KEY_HOME, /* Home key */ 00065 KEY_PAGE_UP, /* Page Up key */ 00066 KEY_PAGE_DOWN, /* Page Down key */ 00067 }; 00068 00069 /** 00070 * USBKeyboard example 00071 * @code 00072 * 00073 * #include "mbed.h" 00074 * #include "USBKeyboard.h" 00075 * 00076 * USBKeyboard key; 00077 * 00078 * int main(void) 00079 * { 00080 * while (1) 00081 * { 00082 * key.printf("Hello World\r\n"); 00083 * wait(1); 00084 * } 00085 * } 00086 * 00087 * @endcode 00088 */ 00089 class USBKeyboard: public USBHID, public Stream 00090 { 00091 public: 00092 00093 /** 00094 * Constructor 00095 * 00096 * 00097 * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK 00098 * @param vendor_id Your vendor_id (default: 0x1235) 00099 * @param product_id Your product_id (default: 0x0050) 00100 * @param product_release Your preoduct_release (default: 0x0001) 00101 * 00102 */ 00103 USBKeyboard(uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001): 00104 USBHID(0, 0, vendor_id, product_id, product_release, false){ 00105 lock_status = 0; 00106 connect(); 00107 }; 00108 00109 /** 00110 * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key 00111 * 00112 * @code 00113 * //To send CTRL + s (save) 00114 * keyboard.keyCode('s', KEY_CTRL); 00115 * @endcode 00116 * 00117 * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0) 00118 * @param key character to send 00119 * @returns true if there is no error, false otherwise 00120 */ 00121 bool keyCode(uint8_t key, uint8_t modifier = 0); 00122 00123 /** 00124 * Send a character 00125 * 00126 * @param c character to be sent 00127 * @returns true if there is no error, false otherwise 00128 */ 00129 virtual int _putc(int c); 00130 00131 /** 00132 * Control media keys 00133 * 00134 * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN) 00135 * @returns true if there is no error, false otherwise 00136 */ 00137 bool mediaControl(MEDIA_KEY key); 00138 00139 /* 00140 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. 00141 * 00142 * @returns pointer to the report descriptor 00143 */ 00144 virtual uint8_t * reportDesc(); 00145 00146 /* 00147 * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys 00148 * 00149 * @returns if handle by subclass, return true 00150 */ 00151 virtual bool EP1_OUT_callback(); 00152 00153 /** 00154 * 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: 00155 * - First bit: NUM_LOCK 00156 * - Second bit: CAPS_LOCK 00157 * - Third bit: SCROLL_LOCK 00158 * 00159 * @returns status of lock keys 00160 */ 00161 uint8_t lockStatus(); 00162 00163 private: 00164 //dummy otherwise it doesn,t compile (we must define all methods of an abstract class) 00165 virtual int _getc() { return -1;}; 00166 00167 uint8_t lock_status; 00168 }; 00169 00170 #endif
Generated on Wed Jul 13 2022 13:59:01 by
1.7.2