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 USBMOUSE_H
Zaitsev 10:41552d038a69 20 #define USBMOUSE_H
Zaitsev 10:41552d038a69 21
Zaitsev 10:41552d038a69 22 #include "USBHID.h"
Zaitsev 10:41552d038a69 23
Zaitsev 10:41552d038a69 24 #define REPORT_ID_MOUSE 2
Zaitsev 10:41552d038a69 25
Zaitsev 10:41552d038a69 26 /* Common usage */
Zaitsev 10:41552d038a69 27
Zaitsev 10:41552d038a69 28 enum MOUSE_BUTTON
Zaitsev 10:41552d038a69 29 {
Zaitsev 10:41552d038a69 30 MOUSE_LEFT = 1,
Zaitsev 10:41552d038a69 31 MOUSE_RIGHT = 2,
Zaitsev 10:41552d038a69 32 MOUSE_MIDDLE = 4,
Zaitsev 10:41552d038a69 33 };
Zaitsev 10:41552d038a69 34
Zaitsev 10:41552d038a69 35 /* X and Y limits */
Zaitsev 10:41552d038a69 36 /* These values do not directly map to screen pixels */
Zaitsev 10:41552d038a69 37 /* Zero may be interpreted as meaning 'no movement' */
Zaitsev 10:41552d038a69 38 #define X_MIN_ABS (1) /*!< Minimum value on x-axis */
Zaitsev 10:41552d038a69 39 #define Y_MIN_ABS (1) /*!< Minimum value on y-axis */
Zaitsev 10:41552d038a69 40 #define X_MAX_ABS (0x7fff) /*!< Maximum value on x-axis */
Zaitsev 10:41552d038a69 41 #define Y_MAX_ABS (0x7fff) /*!< Maximum value on y-axis */
Zaitsev 10:41552d038a69 42
Zaitsev 10:41552d038a69 43 #define X_MIN_REL (-127) /*!< The maximum value that we can move to the left on the x-axis */
Zaitsev 10:41552d038a69 44 #define Y_MIN_REL (-127) /*!< The maximum value that we can move up on the y-axis */
Zaitsev 10:41552d038a69 45 #define X_MAX_REL (127) /*!< The maximum value that we can move to the right on the x-axis */
Zaitsev 10:41552d038a69 46 #define Y_MAX_REL (127) /*!< The maximum value that we can move down on the y-axis */
Zaitsev 10:41552d038a69 47
Zaitsev 10:41552d038a69 48 enum MOUSE_TYPE
Zaitsev 10:41552d038a69 49 {
Zaitsev 10:41552d038a69 50 ABS_MOUSE,
Zaitsev 10:41552d038a69 51 REL_MOUSE,
Zaitsev 10:41552d038a69 52 };
Zaitsev 10:41552d038a69 53
Zaitsev 10:41552d038a69 54 /**
Zaitsev 10:41552d038a69 55 *
Zaitsev 10:41552d038a69 56 * USBMouse example
Zaitsev 10:41552d038a69 57 * @code
Zaitsev 10:41552d038a69 58 * #include "mbed.h"
Zaitsev 10:41552d038a69 59 * #include "USBMouse.h"
Zaitsev 10:41552d038a69 60 *
Zaitsev 10:41552d038a69 61 * USBMouse mouse;
Zaitsev 10:41552d038a69 62 *
Zaitsev 10:41552d038a69 63 * int main(void)
Zaitsev 10:41552d038a69 64 * {
Zaitsev 10:41552d038a69 65 * while (1)
Zaitsev 10:41552d038a69 66 * {
Zaitsev 10:41552d038a69 67 * mouse.move(20, 0);
Zaitsev 10:41552d038a69 68 * wait(0.5);
Zaitsev 10:41552d038a69 69 * }
Zaitsev 10:41552d038a69 70 * }
Zaitsev 10:41552d038a69 71 *
Zaitsev 10:41552d038a69 72 * @endcode
Zaitsev 10:41552d038a69 73 *
Zaitsev 10:41552d038a69 74 *
Zaitsev 10:41552d038a69 75 * @code
Zaitsev 10:41552d038a69 76 * #include "mbed.h"
Zaitsev 10:41552d038a69 77 * #include "USBMouse.h"
Zaitsev 10:41552d038a69 78 * #include <math.h>
Zaitsev 10:41552d038a69 79 *
Zaitsev 10:41552d038a69 80 * USBMouse mouse(ABS_MOUSE);
Zaitsev 10:41552d038a69 81 *
Zaitsev 10:41552d038a69 82 * int main(void)
Zaitsev 10:41552d038a69 83 * {
Zaitsev 10:41552d038a69 84 * uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2;
Zaitsev 10:41552d038a69 85 * uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
Zaitsev 10:41552d038a69 86 * uint16_t x_screen = 0;
Zaitsev 10:41552d038a69 87 * uint16_t y_screen = 0;
Zaitsev 10:41552d038a69 88 *
Zaitsev 10:41552d038a69 89 * uint32_t x_origin = x_center;
Zaitsev 10:41552d038a69 90 * uint32_t y_origin = y_center;
Zaitsev 10:41552d038a69 91 * uint32_t radius = 5000;
Zaitsev 10:41552d038a69 92 * uint32_t angle = 0;
Zaitsev 10:41552d038a69 93 *
Zaitsev 10:41552d038a69 94 * while (1)
Zaitsev 10:41552d038a69 95 * {
Zaitsev 10:41552d038a69 96 * x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;
Zaitsev 10:41552d038a69 97 * y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;
Zaitsev 10:41552d038a69 98 *
Zaitsev 10:41552d038a69 99 * mouse.move(x_screen, y_screen);
Zaitsev 10:41552d038a69 100 * angle += 3;
Zaitsev 10:41552d038a69 101 * wait(0.01);
Zaitsev 10:41552d038a69 102 * }
Zaitsev 10:41552d038a69 103 * }
Zaitsev 10:41552d038a69 104 *
Zaitsev 10:41552d038a69 105 * @endcode
Zaitsev 10:41552d038a69 106 */
Zaitsev 10:41552d038a69 107 class USBMouse: public USBHID
Zaitsev 10:41552d038a69 108 {
Zaitsev 10:41552d038a69 109 public:
Zaitsev 10:41552d038a69 110
Zaitsev 10:41552d038a69 111 /**
Zaitsev 10:41552d038a69 112 * Constructor
Zaitsev 10:41552d038a69 113 *
Zaitsev 10:41552d038a69 114 * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
Zaitsev 10:41552d038a69 115 * @param vendor_id Your vendor_id (default: 0x1234)
Zaitsev 10:41552d038a69 116 * @param product_id Your product_id (default: 0x0001)
Zaitsev 10:41552d038a69 117 * @param product_release Your preoduct_release (default: 0x0001)
Zaitsev 10:41552d038a69 118 *
Zaitsev 10:41552d038a69 119 */
Zaitsev 10:41552d038a69 120 USBMouse(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001):
Zaitsev 10:41552d038a69 121 USBHID(0, 0, vendor_id, product_id, product_release, false)
Zaitsev 10:41552d038a69 122 {
Zaitsev 10:41552d038a69 123 button = 0;
Zaitsev 10:41552d038a69 124 this->mouse_type = mouse_type;
Zaitsev 10:41552d038a69 125 connect();
Zaitsev 10:41552d038a69 126 };
Zaitsev 10:41552d038a69 127
Zaitsev 10:41552d038a69 128 /**
Zaitsev 10:41552d038a69 129 * Write a state of the mouse
Zaitsev 10:41552d038a69 130 *
Zaitsev 10:41552d038a69 131 * @param x x-axis position
Zaitsev 10:41552d038a69 132 * @param y y-axis position
Zaitsev 10:41552d038a69 133 * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
Zaitsev 10:41552d038a69 134 * @param z wheel state (>0 to scroll down, <0 to scroll up)
Zaitsev 10:41552d038a69 135 * @returns true if there is no error, false otherwise
Zaitsev 10:41552d038a69 136 */
Zaitsev 10:41552d038a69 137 bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
Zaitsev 10:41552d038a69 138
Zaitsev 10:41552d038a69 139
Zaitsev 10:41552d038a69 140 /**
Zaitsev 10:41552d038a69 141 * Move the cursor to (x, y)
Zaitsev 10:41552d038a69 142 *
Zaitsev 10:41552d038a69 143 * @param x-axis position
Zaitsev 10:41552d038a69 144 * @param y-axis position
Zaitsev 10:41552d038a69 145 * @returns true if there is no error, false otherwise
Zaitsev 10:41552d038a69 146 */
Zaitsev 10:41552d038a69 147 bool move(int16_t x, int16_t y);
Zaitsev 10:41552d038a69 148
Zaitsev 10:41552d038a69 149 /**
Zaitsev 10:41552d038a69 150 * Press one or several buttons
Zaitsev 10:41552d038a69 151 *
Zaitsev 10:41552d038a69 152 * @param button button state (ex: press(MOUSE_LEFT))
Zaitsev 10:41552d038a69 153 * @returns true if there is no error, false otherwise
Zaitsev 10:41552d038a69 154 */
Zaitsev 10:41552d038a69 155 bool press(uint8_t button);
Zaitsev 10:41552d038a69 156
Zaitsev 10:41552d038a69 157 /**
Zaitsev 10:41552d038a69 158 * Release one or several buttons
Zaitsev 10:41552d038a69 159 *
Zaitsev 10:41552d038a69 160 * @param button button state (ex: release(MOUSE_LEFT))
Zaitsev 10:41552d038a69 161 * @returns true if there is no error, false otherwise
Zaitsev 10:41552d038a69 162 */
Zaitsev 10:41552d038a69 163 bool release(uint8_t button);
Zaitsev 10:41552d038a69 164
Zaitsev 10:41552d038a69 165 /**
Zaitsev 10:41552d038a69 166 * Double click (MOUSE_LEFT)
Zaitsev 10:41552d038a69 167 *
Zaitsev 10:41552d038a69 168 * @returns true if there is no error, false otherwise
Zaitsev 10:41552d038a69 169 */
Zaitsev 10:41552d038a69 170 bool doubleClick();
Zaitsev 10:41552d038a69 171
Zaitsev 10:41552d038a69 172 /**
Zaitsev 10:41552d038a69 173 * Click
Zaitsev 10:41552d038a69 174 *
Zaitsev 10:41552d038a69 175 * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
Zaitsev 10:41552d038a69 176 * @returns true if there is no error, false otherwise
Zaitsev 10:41552d038a69 177 */
Zaitsev 10:41552d038a69 178 bool click(uint8_t button);
Zaitsev 10:41552d038a69 179
Zaitsev 10:41552d038a69 180 /**
Zaitsev 10:41552d038a69 181 * Scrolling
Zaitsev 10:41552d038a69 182 *
Zaitsev 10:41552d038a69 183 * @param z value of the wheel (>0 to go down, <0 to go up)
Zaitsev 10:41552d038a69 184 * @returns true if there is no error, false otherwise
Zaitsev 10:41552d038a69 185 */
Zaitsev 10:41552d038a69 186 bool scroll(int8_t z);
Zaitsev 10:41552d038a69 187
Zaitsev 10:41552d038a69 188 /*
Zaitsev 10:41552d038a69 189 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
Zaitsev 10:41552d038a69 190 *
Zaitsev 10:41552d038a69 191 * @returns pointer to the report descriptor
Zaitsev 10:41552d038a69 192 */
Zaitsev 10:41552d038a69 193 virtual uint8_t * reportDesc();
Zaitsev 10:41552d038a69 194
Zaitsev 10:41552d038a69 195 protected:
Zaitsev 10:41552d038a69 196 /*
Zaitsev 10:41552d038a69 197 * Get configuration descriptor
Zaitsev 10:41552d038a69 198 *
Zaitsev 10:41552d038a69 199 * @returns pointer to the configuration descriptor
Zaitsev 10:41552d038a69 200 */
Zaitsev 10:41552d038a69 201 virtual uint8_t * configurationDesc();
Zaitsev 10:41552d038a69 202
Zaitsev 10:41552d038a69 203 private:
Zaitsev 10:41552d038a69 204 MOUSE_TYPE mouse_type;
Zaitsev 10:41552d038a69 205 uint8_t button;
Zaitsev 10:41552d038a69 206 bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
Zaitsev 10:41552d038a69 207 };
Zaitsev 10:41552d038a69 208
Zaitsev 10:41552d038a69 209 #endif