USBDevice stack

Dependents:   erg USBSerial_HelloWorld Slingshot SuperScanner ... more

Committer:
samux
Date:
Wed May 30 18:28:11 2012 +0000
Revision:
0:140cdf8e2d60

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:140cdf8e2d60 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 0:140cdf8e2d60 2 *
samux 0:140cdf8e2d60 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 0:140cdf8e2d60 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 0:140cdf8e2d60 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 0:140cdf8e2d60 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 0:140cdf8e2d60 7 * Software is furnished to do so, subject to the following conditions:
samux 0:140cdf8e2d60 8 *
samux 0:140cdf8e2d60 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 0:140cdf8e2d60 10 * substantial portions of the Software.
samux 0:140cdf8e2d60 11 *
samux 0:140cdf8e2d60 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 0:140cdf8e2d60 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 0:140cdf8e2d60 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 0:140cdf8e2d60 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 0:140cdf8e2d60 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 0:140cdf8e2d60 17 */
samux 0:140cdf8e2d60 18
samux 0:140cdf8e2d60 19 #ifndef USBKEYBOARD_H
samux 0:140cdf8e2d60 20 #define USBKEYBOARD_H
samux 0:140cdf8e2d60 21
samux 0:140cdf8e2d60 22 #include "USBHID.h"
samux 0:140cdf8e2d60 23 #include "Stream.h"
samux 0:140cdf8e2d60 24
samux 0:140cdf8e2d60 25 /* Modifiers */
samux 0:140cdf8e2d60 26 enum MODIFIER_KEY {
samux 0:140cdf8e2d60 27 KEY_CTRL = 1,
samux 0:140cdf8e2d60 28 KEY_SHIFT = 2,
samux 0:140cdf8e2d60 29 KEY_ALT = 4,
samux 0:140cdf8e2d60 30 };
samux 0:140cdf8e2d60 31
samux 0:140cdf8e2d60 32
samux 0:140cdf8e2d60 33 enum MEDIA_KEY {
samux 0:140cdf8e2d60 34 KEY_NEXT_TRACK, /*!< next Track Button */
samux 0:140cdf8e2d60 35 KEY_PREVIOUS_TRACK, /*!< Previous track Button */
samux 0:140cdf8e2d60 36 KEY_STOP, /*!< Stop Button */
samux 0:140cdf8e2d60 37 KEY_PLAY_PAUSE, /*!< Play/Pause Button */
samux 0:140cdf8e2d60 38 KEY_MUTE, /*!< Mute Button */
samux 0:140cdf8e2d60 39 KEY_VOLUME_UP, /*!< Volume Up Button */
samux 0:140cdf8e2d60 40 KEY_VOLUME_DOWN, /*!< Volume Down Button */
samux 0:140cdf8e2d60 41 };
samux 0:140cdf8e2d60 42
samux 0:140cdf8e2d60 43 enum FUNCTION_KEY {
samux 0:140cdf8e2d60 44 KEY_F1 = 128, /* F1 key */
samux 0:140cdf8e2d60 45 KEY_F2, /* F2 key */
samux 0:140cdf8e2d60 46 KEY_F3, /* F3 key */
samux 0:140cdf8e2d60 47 KEY_F4, /* F4 key */
samux 0:140cdf8e2d60 48 KEY_F5, /* F5 key */
samux 0:140cdf8e2d60 49 KEY_F6, /* F6 key */
samux 0:140cdf8e2d60 50 KEY_F7, /* F7 key */
samux 0:140cdf8e2d60 51 KEY_F8, /* F8 key */
samux 0:140cdf8e2d60 52 KEY_F9, /* F9 key */
samux 0:140cdf8e2d60 53 KEY_F10, /* F10 key */
samux 0:140cdf8e2d60 54 KEY_F11, /* F11 key */
samux 0:140cdf8e2d60 55 KEY_F12, /* F12 key */
samux 0:140cdf8e2d60 56
samux 0:140cdf8e2d60 57 KEY_PRINT_SCREEN, /* Print Screen key */
samux 0:140cdf8e2d60 58 KEY_SCROLL_LOCK, /* Scroll lock */
samux 0:140cdf8e2d60 59 KEY_CAPS_LOCK, /* caps lock */
samux 0:140cdf8e2d60 60 KEY_NUM_LOCK, /* num lock */
samux 0:140cdf8e2d60 61 KEY_INSERT, /* Insert key */
samux 0:140cdf8e2d60 62 KEY_HOME, /* Home key */
samux 0:140cdf8e2d60 63 KEY_PAGE_UP, /* Page Up key */
samux 0:140cdf8e2d60 64 KEY_PAGE_DOWN, /* Page Down key */
samux 0:140cdf8e2d60 65
samux 0:140cdf8e2d60 66 RIGHT_ARROW, /* Right arrow */
samux 0:140cdf8e2d60 67 LEFT_ARROW, /* Left arrow */
samux 0:140cdf8e2d60 68 DOWN_ARROW, /* Down arrow */
samux 0:140cdf8e2d60 69 UP_ARROW, /* Up arrow */
samux 0:140cdf8e2d60 70 };
samux 0:140cdf8e2d60 71
samux 0:140cdf8e2d60 72 /**
samux 0:140cdf8e2d60 73 * USBKeyboard example
samux 0:140cdf8e2d60 74 * @code
samux 0:140cdf8e2d60 75 *
samux 0:140cdf8e2d60 76 * #include "mbed.h"
samux 0:140cdf8e2d60 77 * #include "USBKeyboard.h"
samux 0:140cdf8e2d60 78 *
samux 0:140cdf8e2d60 79 * USBKeyboard key;
samux 0:140cdf8e2d60 80 *
samux 0:140cdf8e2d60 81 * int main(void)
samux 0:140cdf8e2d60 82 * {
samux 0:140cdf8e2d60 83 * while (1)
samux 0:140cdf8e2d60 84 * {
samux 0:140cdf8e2d60 85 * key.printf("Hello World\r\n");
samux 0:140cdf8e2d60 86 * wait(1);
samux 0:140cdf8e2d60 87 * }
samux 0:140cdf8e2d60 88 * }
samux 0:140cdf8e2d60 89 *
samux 0:140cdf8e2d60 90 * @endcode
samux 0:140cdf8e2d60 91 */
samux 0:140cdf8e2d60 92 class USBKeyboard: public USBHID, public Stream {
samux 0:140cdf8e2d60 93 public:
samux 0:140cdf8e2d60 94
samux 0:140cdf8e2d60 95 /**
samux 0:140cdf8e2d60 96 * Constructor
samux 0:140cdf8e2d60 97 *
samux 0:140cdf8e2d60 98 *
samux 0:140cdf8e2d60 99 * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
samux 0:140cdf8e2d60 100 * @param vendor_id Your vendor_id (default: 0x1235)
samux 0:140cdf8e2d60 101 * @param product_id Your product_id (default: 0x0050)
samux 0:140cdf8e2d60 102 * @param product_release Your preoduct_release (default: 0x0001)
samux 0:140cdf8e2d60 103 *
samux 0:140cdf8e2d60 104 */
samux 0:140cdf8e2d60 105 USBKeyboard(uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001):
samux 0:140cdf8e2d60 106 USBHID(0, 0, vendor_id, product_id, product_release, false) {
samux 0:140cdf8e2d60 107 lock_status = 0;
samux 0:140cdf8e2d60 108 connect();
samux 0:140cdf8e2d60 109 };
samux 0:140cdf8e2d60 110
samux 0:140cdf8e2d60 111 /**
samux 0:140cdf8e2d60 112 * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
samux 0:140cdf8e2d60 113 *
samux 0:140cdf8e2d60 114 * @code
samux 0:140cdf8e2d60 115 * //To send CTRL + s (save)
samux 0:140cdf8e2d60 116 * keyboard.keyCode('s', KEY_CTRL);
samux 0:140cdf8e2d60 117 * @endcode
samux 0:140cdf8e2d60 118 *
samux 0:140cdf8e2d60 119 * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
samux 0:140cdf8e2d60 120 * @param key character to send
samux 0:140cdf8e2d60 121 * @returns true if there is no error, false otherwise
samux 0:140cdf8e2d60 122 */
samux 0:140cdf8e2d60 123 bool keyCode(uint8_t key, uint8_t modifier = 0);
samux 0:140cdf8e2d60 124
samux 0:140cdf8e2d60 125 /**
samux 0:140cdf8e2d60 126 * Send a character
samux 0:140cdf8e2d60 127 *
samux 0:140cdf8e2d60 128 * @param c character to be sent
samux 0:140cdf8e2d60 129 * @returns true if there is no error, false otherwise
samux 0:140cdf8e2d60 130 */
samux 0:140cdf8e2d60 131 virtual int _putc(int c);
samux 0:140cdf8e2d60 132
samux 0:140cdf8e2d60 133 /**
samux 0:140cdf8e2d60 134 * Control media keys
samux 0:140cdf8e2d60 135 *
samux 0:140cdf8e2d60 136 * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
samux 0:140cdf8e2d60 137 * @returns true if there is no error, false otherwise
samux 0:140cdf8e2d60 138 */
samux 0:140cdf8e2d60 139 bool mediaControl(MEDIA_KEY key);
samux 0:140cdf8e2d60 140
samux 0:140cdf8e2d60 141 /*
samux 0:140cdf8e2d60 142 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
samux 0:140cdf8e2d60 143 *
samux 0:140cdf8e2d60 144 * @returns pointer to the report descriptor
samux 0:140cdf8e2d60 145 */
samux 0:140cdf8e2d60 146 virtual uint8_t * reportDesc();
samux 0:140cdf8e2d60 147
samux 0:140cdf8e2d60 148 /*
samux 0:140cdf8e2d60 149 * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
samux 0:140cdf8e2d60 150 *
samux 0:140cdf8e2d60 151 * @returns if handle by subclass, return true
samux 0:140cdf8e2d60 152 */
samux 0:140cdf8e2d60 153 virtual bool EP1_OUT_callback();
samux 0:140cdf8e2d60 154
samux 0:140cdf8e2d60 155 /**
samux 0:140cdf8e2d60 156 * 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:
samux 0:140cdf8e2d60 157 * - First bit: NUM_LOCK
samux 0:140cdf8e2d60 158 * - Second bit: CAPS_LOCK
samux 0:140cdf8e2d60 159 * - Third bit: SCROLL_LOCK
samux 0:140cdf8e2d60 160 *
samux 0:140cdf8e2d60 161 * @returns status of lock keys
samux 0:140cdf8e2d60 162 */
samux 0:140cdf8e2d60 163 uint8_t lockStatus();
samux 0:140cdf8e2d60 164
samux 0:140cdf8e2d60 165 protected:
samux 0:140cdf8e2d60 166 /*
samux 0:140cdf8e2d60 167 * Get configuration descriptor
samux 0:140cdf8e2d60 168 *
samux 0:140cdf8e2d60 169 * @returns pointer to the configuration descriptor
samux 0:140cdf8e2d60 170 */
samux 0:140cdf8e2d60 171 virtual uint8_t * configurationDesc();
samux 0:140cdf8e2d60 172
samux 0:140cdf8e2d60 173 private:
samux 0:140cdf8e2d60 174 //dummy otherwise it doesn,t compile (we must define all methods of an abstract class)
samux 0:140cdf8e2d60 175 virtual int _getc() {
samux 0:140cdf8e2d60 176 return -1;
samux 0:140cdf8e2d60 177 };
samux 0:140cdf8e2d60 178
samux 0:140cdf8e2d60 179 uint8_t lock_status;
samux 0:140cdf8e2d60 180
samux 0:140cdf8e2d60 181 };
samux 0:140cdf8e2d60 182
samux 0:140cdf8e2d60 183 #endif