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 USBMOUSEKEYBOARD_H
ppo 0:c1e89c49eae5 20 #define USBMOUSEKEYBOARD_H
ppo 0:c1e89c49eae5 21
ppo 0:c1e89c49eae5 22 #define REPORT_ID_KEYBOARD 1
ppo 0:c1e89c49eae5 23 #define REPORT_ID_MOUSE 2
ppo 0:c1e89c49eae5 24 #define REPORT_ID_VOLUME 3
ppo 0:c1e89c49eae5 25
ppo 0:c1e89c49eae5 26 #include "USBMouse.h"
ppo 0:c1e89c49eae5 27 #include "USBKeyboard.h"
ppo 0:c1e89c49eae5 28 #include "Stream.h"
ppo 0:c1e89c49eae5 29 #include "USBHID.h"
ppo 0:c1e89c49eae5 30
ppo 0:c1e89c49eae5 31 /**
ppo 0:c1e89c49eae5 32 * USBMouseKeyboard example
ppo 0:c1e89c49eae5 33 * @code
ppo 0:c1e89c49eae5 34 *
ppo 0:c1e89c49eae5 35 * #include "mbed.h"
ppo 0:c1e89c49eae5 36 * #include "USBMouseKeyboard.h"
ppo 0:c1e89c49eae5 37 *
ppo 0:c1e89c49eae5 38 * USBMouseKeyboard key_mouse;
ppo 0:c1e89c49eae5 39 *
ppo 0:c1e89c49eae5 40 * int main(void)
ppo 0:c1e89c49eae5 41 * {
ppo 0:c1e89c49eae5 42 * while(1)
ppo 0:c1e89c49eae5 43 * {
ppo 0:c1e89c49eae5 44 * key_mouse.move(20, 0);
ppo 0:c1e89c49eae5 45 * key_mouse.printf("Hello From MBED\r\n");
ppo 0:c1e89c49eae5 46 * wait(1);
ppo 0:c1e89c49eae5 47 * }
ppo 0:c1e89c49eae5 48 * }
ppo 0:c1e89c49eae5 49 * @endcode
ppo 0:c1e89c49eae5 50 *
ppo 0:c1e89c49eae5 51 *
ppo 0:c1e89c49eae5 52 * @code
ppo 0:c1e89c49eae5 53 *
ppo 0:c1e89c49eae5 54 * #include "mbed.h"
ppo 0:c1e89c49eae5 55 * #include "USBMouseKeyboard.h"
ppo 0:c1e89c49eae5 56 *
ppo 0:c1e89c49eae5 57 * USBMouseKeyboard key_mouse(ABS_MOUSE);
ppo 0:c1e89c49eae5 58 *
ppo 0:c1e89c49eae5 59 * int main(void)
ppo 0:c1e89c49eae5 60 * {
ppo 0:c1e89c49eae5 61 * while(1)
ppo 0:c1e89c49eae5 62 * {
ppo 0:c1e89c49eae5 63 * key_mouse.move(X_MAX_ABS/2, Y_MAX_ABS/2);
ppo 0:c1e89c49eae5 64 * key_mouse.printf("Hello from MBED\r\n");
ppo 0:c1e89c49eae5 65 * wait(1);
ppo 0:c1e89c49eae5 66 * }
ppo 0:c1e89c49eae5 67 * }
ppo 0:c1e89c49eae5 68 * @endcode
ppo 0:c1e89c49eae5 69 */
ppo 0:c1e89c49eae5 70 class USBMouseKeyboard: public USBHID, public Stream
ppo 0:c1e89c49eae5 71 {
ppo 0:c1e89c49eae5 72 public:
ppo 0:c1e89c49eae5 73
ppo 0:c1e89c49eae5 74 /**
ppo 0:c1e89c49eae5 75 * Constructor
ppo 0:c1e89c49eae5 76 *
ppo 0:c1e89c49eae5 77 * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
ppo 0:c1e89c49eae5 78 * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
ppo 0:c1e89c49eae5 79 * @param vendor_id Your vendor_id (default: 0x1234)
ppo 0:c1e89c49eae5 80 * @param product_id Your product_id (default: 0x0001)
ppo 0:c1e89c49eae5 81 * @param product_release Your preoduct_release (default: 0x0001)
ppo 0:c1e89c49eae5 82 *
ppo 0:c1e89c49eae5 83 */
ppo 0:c1e89c49eae5 84 USBMouseKeyboard(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x0021, uint16_t product_id = 0x0011, uint16_t product_release = 0x0001):
ppo 0:c1e89c49eae5 85 USBHID(0, 0, vendor_id, product_id, product_release, false)
ppo 0:c1e89c49eae5 86 {
ppo 0:c1e89c49eae5 87 lock_status = 0;
ppo 0:c1e89c49eae5 88 button = 0;
ppo 0:c1e89c49eae5 89 this->mouse_type = mouse_type;
ppo 0:c1e89c49eae5 90 connect();
ppo 0:c1e89c49eae5 91 };
ppo 0:c1e89c49eae5 92
ppo 0:c1e89c49eae5 93 /**
ppo 0:c1e89c49eae5 94 * Write a state of the mouse
ppo 0:c1e89c49eae5 95 *
ppo 0:c1e89c49eae5 96 * @param x x-axis position
ppo 0:c1e89c49eae5 97 * @param y y-axis position
ppo 0:c1e89c49eae5 98 * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
ppo 0:c1e89c49eae5 99 * @param z wheel state (>0 to scroll down, <0 to scroll up)
ppo 0:c1e89c49eae5 100 * @returns true if there is no error, false otherwise
ppo 0:c1e89c49eae5 101 */
ppo 0:c1e89c49eae5 102 bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
ppo 0:c1e89c49eae5 103
ppo 0:c1e89c49eae5 104
ppo 0:c1e89c49eae5 105 /**
ppo 0:c1e89c49eae5 106 * Move the cursor to (x, y)
ppo 0:c1e89c49eae5 107 *
ppo 0:c1e89c49eae5 108 * @param x x-axis position
ppo 0:c1e89c49eae5 109 * @param y y-axis position
ppo 0:c1e89c49eae5 110 * @returns true if there is no error, false otherwise
ppo 0:c1e89c49eae5 111 */
ppo 0:c1e89c49eae5 112 bool move(int16_t x, int16_t y);
ppo 0:c1e89c49eae5 113
ppo 0:c1e89c49eae5 114 /**
ppo 0:c1e89c49eae5 115 * Press one or several buttons
ppo 0:c1e89c49eae5 116 *
ppo 0:c1e89c49eae5 117 * @param button button state (ex: press(MOUSE_LEFT))
ppo 0:c1e89c49eae5 118 * @returns true if there is no error, false otherwise
ppo 0:c1e89c49eae5 119 */
ppo 0:c1e89c49eae5 120 bool press(uint8_t button);
ppo 0:c1e89c49eae5 121
ppo 0:c1e89c49eae5 122 /**
ppo 0:c1e89c49eae5 123 * Release one or several buttons
ppo 0:c1e89c49eae5 124 *
ppo 0:c1e89c49eae5 125 * @param button button state (ex: release(MOUSE_LEFT))
ppo 0:c1e89c49eae5 126 * @returns true if there is no error, false otherwise
ppo 0:c1e89c49eae5 127 */
ppo 0:c1e89c49eae5 128 bool release(uint8_t button);
ppo 0:c1e89c49eae5 129
ppo 0:c1e89c49eae5 130 /**
ppo 0:c1e89c49eae5 131 * Double click (MOUSE_LEFT)
ppo 0:c1e89c49eae5 132 *
ppo 0:c1e89c49eae5 133 * @returns true if there is no error, false otherwise
ppo 0:c1e89c49eae5 134 */
ppo 0:c1e89c49eae5 135 bool doubleClick();
ppo 0:c1e89c49eae5 136
ppo 0:c1e89c49eae5 137 /**
ppo 0:c1e89c49eae5 138 * Click
ppo 0:c1e89c49eae5 139 *
ppo 0:c1e89c49eae5 140 * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
ppo 0:c1e89c49eae5 141 * @returns true if there is no error, false otherwise
ppo 0:c1e89c49eae5 142 */
ppo 0:c1e89c49eae5 143 bool click(uint8_t button);
ppo 0:c1e89c49eae5 144
ppo 0:c1e89c49eae5 145 /**
ppo 0:c1e89c49eae5 146 * Scrolling
ppo 0:c1e89c49eae5 147 *
ppo 0:c1e89c49eae5 148 * @param z value of the wheel (>0 to go down, <0 to go up)
ppo 0:c1e89c49eae5 149 * @returns true if there is no error, false otherwise
ppo 0:c1e89c49eae5 150 */
ppo 0:c1e89c49eae5 151 bool scroll(int8_t z);
ppo 0:c1e89c49eae5 152
ppo 0:c1e89c49eae5 153 /**
ppo 0:c1e89c49eae5 154 * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
ppo 0:c1e89c49eae5 155 *
ppo 0:c1e89c49eae5 156 * @code
ppo 0:c1e89c49eae5 157 * //To send CTRL + s (save)
ppo 0:c1e89c49eae5 158 * keyboard.keyCode('s', KEY_CTRL);
ppo 0:c1e89c49eae5 159 * @endcode
ppo 0:c1e89c49eae5 160 *
ppo 0:c1e89c49eae5 161 * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
ppo 0:c1e89c49eae5 162 * @param key character to send
ppo 0:c1e89c49eae5 163 * @returns true if there is no error, false otherwise
ppo 0:c1e89c49eae5 164 */
ppo 0:c1e89c49eae5 165 bool keyCode(uint8_t key, uint8_t modifier = 0);
ppo 0:c1e89c49eae5 166
ppo 0:c1e89c49eae5 167 /**
ppo 0:c1e89c49eae5 168 * Send a character
ppo 0:c1e89c49eae5 169 *
ppo 0:c1e89c49eae5 170 * @param c character to be sent
ppo 0:c1e89c49eae5 171 * @returns true if there is no error, false otherwise
ppo 0:c1e89c49eae5 172 */
ppo 0:c1e89c49eae5 173 virtual int _putc(int c);
ppo 0:c1e89c49eae5 174
ppo 0:c1e89c49eae5 175 /**
ppo 0:c1e89c49eae5 176 * Control media keys
ppo 0:c1e89c49eae5 177 *
ppo 0:c1e89c49eae5 178 * @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 179 * @returns true if there is no error, false otherwise
ppo 0:c1e89c49eae5 180 */
ppo 0:c1e89c49eae5 181 bool mediaControl(MEDIA_KEY key);
ppo 0:c1e89c49eae5 182
ppo 0:c1e89c49eae5 183 /**
ppo 0:c1e89c49eae5 184 * 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 185 * - First bit: NUM_LOCK
ppo 0:c1e89c49eae5 186 * - Second bit: CAPS_LOCK
ppo 0:c1e89c49eae5 187 * - Third bit: SCROLL_LOCK
ppo 0:c1e89c49eae5 188 *
ppo 0:c1e89c49eae5 189 * @returns status of lock keys
ppo 0:c1e89c49eae5 190 */
ppo 0:c1e89c49eae5 191 uint8_t lockStatus();
ppo 0:c1e89c49eae5 192
ppo 0:c1e89c49eae5 193 /*
ppo 0:c1e89c49eae5 194 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
ppo 0:c1e89c49eae5 195 *
ppo 0:c1e89c49eae5 196 * @returns pointer to the report descriptor
ppo 0:c1e89c49eae5 197 */
ppo 0:c1e89c49eae5 198 virtual uint8_t * reportDesc();
ppo 0:c1e89c49eae5 199
ppo 0:c1e89c49eae5 200 /*
ppo 0:c1e89c49eae5 201 * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
ppo 0:c1e89c49eae5 202 *
ppo 0:c1e89c49eae5 203 * @returns if handle by subclass, return true
ppo 0:c1e89c49eae5 204 */
ppo 0:c1e89c49eae5 205 virtual bool EP1_OUT_callback();
ppo 0:c1e89c49eae5 206
ppo 0:c1e89c49eae5 207
ppo 0:c1e89c49eae5 208 private:
ppo 0:c1e89c49eae5 209 bool mouseWrite(int8_t x, int8_t y, uint8_t buttons, int8_t z);
ppo 0:c1e89c49eae5 210 MOUSE_TYPE mouse_type;
ppo 0:c1e89c49eae5 211 uint8_t button;
ppo 0:c1e89c49eae5 212 bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
ppo 0:c1e89c49eae5 213
ppo 0:c1e89c49eae5 214 uint8_t lock_status;
ppo 0:c1e89c49eae5 215
ppo 0:c1e89c49eae5 216 //dummy otherwise it doesn't compile (we must define all methods of an abstract class)
ppo 0:c1e89c49eae5 217 virtual int _getc() { return -1;}
ppo 0:c1e89c49eae5 218 };
ppo 0:c1e89c49eae5 219
ppo 0:c1e89c49eae5 220 #endif