_mu ki / USBDevice

Dependents:   Clicker

Fork of USBDevice by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBKeyboard.h Source File

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