USB device stack

Dependents:   mbed-mX-USB-TEST1 USBMSD_SD_HID_HelloWorld HidTest MIDI_usb_bridge ... more

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