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 KEY_CTRL = 1, 00028 KEY_SHIFT = 2, 00029 KEY_ALT = 4, 00030 }; 00031 00032 00033 enum MEDIA_KEY { 00034 KEY_NEXT_TRACK, /*!< next Track Button */ 00035 KEY_PREVIOUS_TRACK, /*!< Previous track Button */ 00036 KEY_STOP, /*!< Stop Button */ 00037 KEY_PLAY_PAUSE, /*!< Play/Pause Button */ 00038 KEY_MUTE, /*!< Mute Button */ 00039 KEY_VOLUME_UP, /*!< Volume Up Button */ 00040 KEY_VOLUME_DOWN, /*!< Volume Down Button */ 00041 }; 00042 00043 enum FUNCTION_KEY { 00044 KEY_F1 = 128, /* F1 key */ 00045 KEY_F2, /* F2 key */ 00046 KEY_F3, /* F3 key */ 00047 KEY_F4, /* F4 key */ 00048 KEY_F5, /* F5 key */ 00049 KEY_F6, /* F6 key */ 00050 KEY_F7, /* F7 key */ 00051 KEY_F8, /* F8 key */ 00052 KEY_F9, /* F9 key */ 00053 KEY_F10, /* F10 key */ 00054 KEY_F11, /* F11 key */ 00055 KEY_F12, /* F12 key */ 00056 00057 KEY_PRINT_SCREEN, /* Print Screen key */ 00058 KEY_SCROLL_LOCK, /* Scroll lock */ 00059 KEY_CAPS_LOCK, /* caps lock */ 00060 KEY_NUM_LOCK, /* num lock */ 00061 KEY_INSERT, /* Insert key */ 00062 KEY_HOME, /* Home key */ 00063 KEY_PAGE_UP, /* Page Up key */ 00064 KEY_PAGE_DOWN, /* Page Down key */ 00065 00066 RIGHT_ARROW, /* Right arrow */ 00067 LEFT_ARROW, /* Left arrow */ 00068 DOWN_ARROW, /* Down arrow */ 00069 UP_ARROW, /* Up arrow */ 00070 }; 00071 00072 /** 00073 * USBKeyboard example 00074 * @code 00075 * 00076 * #include "mbed.h" 00077 * #include "USBKeyboard.h" 00078 * 00079 * USBKeyboard key; 00080 * 00081 * int main(void) 00082 * { 00083 * while (1) 00084 * { 00085 * key.printf("Hello World\r\n"); 00086 * wait(1); 00087 * } 00088 * } 00089 * 00090 * @endcode 00091 */ 00092 class USBKeyboard: public USBHID, public Stream { 00093 public: 00094 00095 /** 00096 * Constructor 00097 * 00098 * 00099 * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK 00100 * @param vendor_id Your vendor_id (default: 0x1235) 00101 * @param product_id Your product_id (default: 0x0050) 00102 * @param product_release Your preoduct_release (default: 0x0001) 00103 * 00104 */ 00105 USBKeyboard(uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001): 00106 USBHID(0, 0, vendor_id, product_id, product_release, false) { 00107 lock_status = 0; 00108 connect(); 00109 }; 00110 00111 /** 00112 * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key 00113 * 00114 * @code 00115 * //To send CTRL + s (save) 00116 * keyboard.keyCode('s', KEY_CTRL); 00117 * @endcode 00118 * 00119 * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0) 00120 * @param key character to send 00121 * @returns true if there is no error, false otherwise 00122 */ 00123 bool keyCode(uint8_t key, uint8_t modifier = 0); 00124 00125 /** 00126 * Send a character 00127 * 00128 * @param c character to be sent 00129 * @returns true if there is no error, false otherwise 00130 */ 00131 virtual int _putc(int c); 00132 00133 /** 00134 * Control media keys 00135 * 00136 * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN) 00137 * @returns true if there is no error, false otherwise 00138 */ 00139 bool mediaControl(MEDIA_KEY key); 00140 00141 /* 00142 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. 00143 * 00144 * @returns pointer to the report descriptor 00145 */ 00146 virtual uint8_t * reportDesc(); 00147 00148 /* 00149 * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys 00150 * 00151 * @returns if handle by subclass, return true 00152 */ 00153 virtual bool EPINT_OUT_callback(); 00154 00155 /** 00156 * 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: 00157 * - First bit: NUM_LOCK 00158 * - Second bit: CAPS_LOCK 00159 * - Third bit: SCROLL_LOCK 00160 * 00161 * @returns status of lock keys 00162 */ 00163 uint8_t lockStatus(); 00164 00165 protected: 00166 /* 00167 * Get configuration descriptor 00168 * 00169 * @returns pointer to the configuration descriptor 00170 */ 00171 virtual uint8_t * configurationDesc(); 00172 00173 private: 00174 //dummy otherwise it doesn,t compile (we must define all methods of an abstract class) 00175 virtual int _getc() { 00176 return -1; 00177 }; 00178 00179 uint8_t lock_status; 00180 00181 }; 00182 00183 #endif
Generated on Tue Jul 12 2022 17:34:59 by
