mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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