usb device

Committer:
ppo
Date:
Sat May 14 17:24:10 2022 +0000
Revision:
0:c1e89c49eae5
commit

Who changed what in which revision?

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