Added general purpus HID report

Fork of USBDevice by mbed official

Committer:
betaEncoder
Date:
Fri Aug 15 14:14:38 2014 +0000
Revision:
28:6cc8a2d36729
Parent:
25:7c72828865f3
First commit

Who changed what in which revision?

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