Markus Pilz / Mbed 2 deprecated PS2toUSBHIDAdapter

Dependencies:   mbed

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