USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Sat Dec 16 10:26:48 2017 +0000
Revision:
11:b3f2a8bdac4d
Parent:
10:41552d038a69
A copy for D.S;

Who changed what in which revision?

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