335 final proj

Dependencies:   mbed USBDevice

Committer:
jresnik
Date:
Mon Nov 23 19:44:11 2020 +0000
Revision:
0:aee369fac3bd
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jresnik 0:aee369fac3bd 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
jresnik 0:aee369fac3bd 2 *
jresnik 0:aee369fac3bd 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jresnik 0:aee369fac3bd 4 * and associated documentation files (the "Software"), to deal in the Software without
jresnik 0:aee369fac3bd 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
jresnik 0:aee369fac3bd 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
jresnik 0:aee369fac3bd 7 * Software is furnished to do so, subject to the following conditions:
jresnik 0:aee369fac3bd 8 *
jresnik 0:aee369fac3bd 9 * The above copyright notice and this permission notice shall be included in all copies or
jresnik 0:aee369fac3bd 10 * substantial portions of the Software.
jresnik 0:aee369fac3bd 11 *
jresnik 0:aee369fac3bd 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jresnik 0:aee369fac3bd 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jresnik 0:aee369fac3bd 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jresnik 0:aee369fac3bd 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jresnik 0:aee369fac3bd 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jresnik 0:aee369fac3bd 17 */
jresnik 0:aee369fac3bd 18
jresnik 0:aee369fac3bd 19 #ifndef USBMOUSEKEYBOARD_H
jresnik 0:aee369fac3bd 20 #define USBMOUSEKEYBOARD_H
jresnik 0:aee369fac3bd 21
jresnik 0:aee369fac3bd 22 #define REPORT_ID_KEYBOARD 1
jresnik 0:aee369fac3bd 23 #define REPORT_ID_MOUSE 2
jresnik 0:aee369fac3bd 24 #define REPORT_ID_VOLUME 3
jresnik 0:aee369fac3bd 25
jresnik 0:aee369fac3bd 26 #include "USBMouse.h"
jresnik 0:aee369fac3bd 27 #include "USBKeyboard.h"
jresnik 0:aee369fac3bd 28 #include "Stream.h"
jresnik 0:aee369fac3bd 29 #include "USBHID.h"
jresnik 0:aee369fac3bd 30
jresnik 0:aee369fac3bd 31 /**
jresnik 0:aee369fac3bd 32 * USBMouseKeyboard example
jresnik 0:aee369fac3bd 33 * @code
jresnik 0:aee369fac3bd 34 *
jresnik 0:aee369fac3bd 35 * #include "mbed.h"
jresnik 0:aee369fac3bd 36 * #include "USBMouseKeyboard.h"
jresnik 0:aee369fac3bd 37 *
jresnik 0:aee369fac3bd 38 * USBMouseKeyboard key_mouse;
jresnik 0:aee369fac3bd 39 *
jresnik 0:aee369fac3bd 40 * int main(void)
jresnik 0:aee369fac3bd 41 * {
jresnik 0:aee369fac3bd 42 * while(1)
jresnik 0:aee369fac3bd 43 * {
jresnik 0:aee369fac3bd 44 * key_mouse.move(20, 0);
jresnik 0:aee369fac3bd 45 * key_mouse.printf("Hello From MBED\r\n");
jresnik 0:aee369fac3bd 46 * wait(1);
jresnik 0:aee369fac3bd 47 * }
jresnik 0:aee369fac3bd 48 * }
jresnik 0:aee369fac3bd 49 * @endcode
jresnik 0:aee369fac3bd 50 *
jresnik 0:aee369fac3bd 51 *
jresnik 0:aee369fac3bd 52 * @code
jresnik 0:aee369fac3bd 53 *
jresnik 0:aee369fac3bd 54 * #include "mbed.h"
jresnik 0:aee369fac3bd 55 * #include "USBMouseKeyboard.h"
jresnik 0:aee369fac3bd 56 *
jresnik 0:aee369fac3bd 57 * USBMouseKeyboard key_mouse(ABS_MOUSE);
jresnik 0:aee369fac3bd 58 *
jresnik 0:aee369fac3bd 59 * int main(void)
jresnik 0:aee369fac3bd 60 * {
jresnik 0:aee369fac3bd 61 * while(1)
jresnik 0:aee369fac3bd 62 * {
jresnik 0:aee369fac3bd 63 * key_mouse.move(X_MAX_ABS/2, Y_MAX_ABS/2);
jresnik 0:aee369fac3bd 64 * key_mouse.printf("Hello from MBED\r\n");
jresnik 0:aee369fac3bd 65 * wait(1);
jresnik 0:aee369fac3bd 66 * }
jresnik 0:aee369fac3bd 67 * }
jresnik 0:aee369fac3bd 68 * @endcode
jresnik 0:aee369fac3bd 69 */
jresnik 0:aee369fac3bd 70 class USBMouseKeyboard: public USBHID, public Stream
jresnik 0:aee369fac3bd 71 {
jresnik 0:aee369fac3bd 72 public:
jresnik 0:aee369fac3bd 73
jresnik 0:aee369fac3bd 74 /**
jresnik 0:aee369fac3bd 75 * Constructor
jresnik 0:aee369fac3bd 76 *
jresnik 0:aee369fac3bd 77 * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
jresnik 0:aee369fac3bd 78 * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
jresnik 0:aee369fac3bd 79 * @param vendor_id Your vendor_id (default: 0x1234)
jresnik 0:aee369fac3bd 80 * @param product_id Your product_id (default: 0x0001)
jresnik 0:aee369fac3bd 81 * @param product_release Your preoduct_release (default: 0x0001)
jresnik 0:aee369fac3bd 82 *
jresnik 0:aee369fac3bd 83 */
jresnik 0:aee369fac3bd 84 USBMouseKeyboard(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x0021, uint16_t product_id = 0x0011, uint16_t product_release = 0x0001):
jresnik 0:aee369fac3bd 85 USBHID(0, 0, vendor_id, product_id, product_release, false)
jresnik 0:aee369fac3bd 86 {
jresnik 0:aee369fac3bd 87 lock_status = 0;
jresnik 0:aee369fac3bd 88 button = 0;
jresnik 0:aee369fac3bd 89 this->mouse_type = mouse_type;
jresnik 0:aee369fac3bd 90 connect();
jresnik 0:aee369fac3bd 91 };
jresnik 0:aee369fac3bd 92
jresnik 0:aee369fac3bd 93 /**
jresnik 0:aee369fac3bd 94 * Write a state of the mouse
jresnik 0:aee369fac3bd 95 *
jresnik 0:aee369fac3bd 96 * @param x x-axis position
jresnik 0:aee369fac3bd 97 * @param y y-axis position
jresnik 0:aee369fac3bd 98 * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
jresnik 0:aee369fac3bd 99 * @param z wheel state (>0 to scroll down, <0 to scroll up)
jresnik 0:aee369fac3bd 100 * @returns true if there is no error, false otherwise
jresnik 0:aee369fac3bd 101 */
jresnik 0:aee369fac3bd 102 bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
jresnik 0:aee369fac3bd 103
jresnik 0:aee369fac3bd 104
jresnik 0:aee369fac3bd 105 /**
jresnik 0:aee369fac3bd 106 * Move the cursor to (x, y)
jresnik 0:aee369fac3bd 107 *
jresnik 0:aee369fac3bd 108 * @param x x-axis position
jresnik 0:aee369fac3bd 109 * @param y y-axis position
jresnik 0:aee369fac3bd 110 * @returns true if there is no error, false otherwise
jresnik 0:aee369fac3bd 111 */
jresnik 0:aee369fac3bd 112 bool move(int16_t x, int16_t y);
jresnik 0:aee369fac3bd 113
jresnik 0:aee369fac3bd 114 /**
jresnik 0:aee369fac3bd 115 * Press one or several buttons
jresnik 0:aee369fac3bd 116 *
jresnik 0:aee369fac3bd 117 * @param button button state (ex: press(MOUSE_LEFT))
jresnik 0:aee369fac3bd 118 * @returns true if there is no error, false otherwise
jresnik 0:aee369fac3bd 119 */
jresnik 0:aee369fac3bd 120 bool press(uint8_t button);
jresnik 0:aee369fac3bd 121
jresnik 0:aee369fac3bd 122 /**
jresnik 0:aee369fac3bd 123 * Release one or several buttons
jresnik 0:aee369fac3bd 124 *
jresnik 0:aee369fac3bd 125 * @param button button state (ex: release(MOUSE_LEFT))
jresnik 0:aee369fac3bd 126 * @returns true if there is no error, false otherwise
jresnik 0:aee369fac3bd 127 */
jresnik 0:aee369fac3bd 128 bool release(uint8_t button);
jresnik 0:aee369fac3bd 129
jresnik 0:aee369fac3bd 130 /**
jresnik 0:aee369fac3bd 131 * Double click (MOUSE_LEFT)
jresnik 0:aee369fac3bd 132 *
jresnik 0:aee369fac3bd 133 * @returns true if there is no error, false otherwise
jresnik 0:aee369fac3bd 134 */
jresnik 0:aee369fac3bd 135 bool doubleClick();
jresnik 0:aee369fac3bd 136
jresnik 0:aee369fac3bd 137 /**
jresnik 0:aee369fac3bd 138 * Click
jresnik 0:aee369fac3bd 139 *
jresnik 0:aee369fac3bd 140 * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
jresnik 0:aee369fac3bd 141 * @returns true if there is no error, false otherwise
jresnik 0:aee369fac3bd 142 */
jresnik 0:aee369fac3bd 143 bool click(uint8_t button);
jresnik 0:aee369fac3bd 144
jresnik 0:aee369fac3bd 145 /**
jresnik 0:aee369fac3bd 146 * Scrolling
jresnik 0:aee369fac3bd 147 *
jresnik 0:aee369fac3bd 148 * @param z value of the wheel (>0 to go down, <0 to go up)
jresnik 0:aee369fac3bd 149 * @returns true if there is no error, false otherwise
jresnik 0:aee369fac3bd 150 */
jresnik 0:aee369fac3bd 151 bool scroll(int8_t z);
jresnik 0:aee369fac3bd 152
jresnik 0:aee369fac3bd 153 /**
jresnik 0:aee369fac3bd 154 * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
jresnik 0:aee369fac3bd 155 *
jresnik 0:aee369fac3bd 156 * @code
jresnik 0:aee369fac3bd 157 * //To send CTRL + s (save)
jresnik 0:aee369fac3bd 158 * keyboard.keyCode('s', KEY_CTRL);
jresnik 0:aee369fac3bd 159 * @endcode
jresnik 0:aee369fac3bd 160 *
jresnik 0:aee369fac3bd 161 * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
jresnik 0:aee369fac3bd 162 * @param key character to send
jresnik 0:aee369fac3bd 163 * @returns true if there is no error, false otherwise
jresnik 0:aee369fac3bd 164 */
jresnik 0:aee369fac3bd 165 bool keyCode(uint8_t key, uint8_t modifier = 0);
jresnik 0:aee369fac3bd 166
jresnik 0:aee369fac3bd 167 /**
jresnik 0:aee369fac3bd 168 * Send a character
jresnik 0:aee369fac3bd 169 *
jresnik 0:aee369fac3bd 170 * @param c character to be sent
jresnik 0:aee369fac3bd 171 * @returns true if there is no error, false otherwise
jresnik 0:aee369fac3bd 172 */
jresnik 0:aee369fac3bd 173 virtual int _putc(int c);
jresnik 0:aee369fac3bd 174
jresnik 0:aee369fac3bd 175 /**
jresnik 0:aee369fac3bd 176 * Control media keys
jresnik 0:aee369fac3bd 177 *
jresnik 0:aee369fac3bd 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)
jresnik 0:aee369fac3bd 179 * @returns true if there is no error, false otherwise
jresnik 0:aee369fac3bd 180 */
jresnik 0:aee369fac3bd 181 bool mediaControl(MEDIA_KEY key);
jresnik 0:aee369fac3bd 182
jresnik 0:aee369fac3bd 183 /**
jresnik 0:aee369fac3bd 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:
jresnik 0:aee369fac3bd 185 * - First bit: NUM_LOCK
jresnik 0:aee369fac3bd 186 * - Second bit: CAPS_LOCK
jresnik 0:aee369fac3bd 187 * - Third bit: SCROLL_LOCK
jresnik 0:aee369fac3bd 188 *
jresnik 0:aee369fac3bd 189 * @returns status of lock keys
jresnik 0:aee369fac3bd 190 */
jresnik 0:aee369fac3bd 191 uint8_t lockStatus();
jresnik 0:aee369fac3bd 192
jresnik 0:aee369fac3bd 193 /*
jresnik 0:aee369fac3bd 194 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
jresnik 0:aee369fac3bd 195 *
jresnik 0:aee369fac3bd 196 * @returns pointer to the report descriptor
jresnik 0:aee369fac3bd 197 */
jresnik 0:aee369fac3bd 198 virtual uint8_t * reportDesc();
jresnik 0:aee369fac3bd 199
jresnik 0:aee369fac3bd 200 /*
jresnik 0:aee369fac3bd 201 * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
jresnik 0:aee369fac3bd 202 *
jresnik 0:aee369fac3bd 203 * @returns if handle by subclass, return true
jresnik 0:aee369fac3bd 204 */
jresnik 0:aee369fac3bd 205 virtual bool EPINT_OUT_callback();
jresnik 0:aee369fac3bd 206
jresnik 0:aee369fac3bd 207
jresnik 0:aee369fac3bd 208 private:
jresnik 0:aee369fac3bd 209 bool mouseWrite(int8_t x, int8_t y, uint8_t buttons, int8_t z);
jresnik 0:aee369fac3bd 210 MOUSE_TYPE mouse_type;
jresnik 0:aee369fac3bd 211 uint8_t button;
jresnik 0:aee369fac3bd 212 bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
jresnik 0:aee369fac3bd 213
jresnik 0:aee369fac3bd 214 uint8_t lock_status;
jresnik 0:aee369fac3bd 215
jresnik 0:aee369fac3bd 216 //dummy otherwise it doesn't compile (we must define all methods of an abstract class)
jresnik 0:aee369fac3bd 217 virtual int _getc() { return -1;}
jresnik 0:aee369fac3bd 218 };
jresnik 0:aee369fac3bd 219
jresnik 0:aee369fac3bd 220 #endif