mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
elessair 0:f269e3021894 2 *
elessair 0:f269e3021894 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
elessair 0:f269e3021894 4 * and associated documentation files (the "Software"), to deal in the Software without
elessair 0:f269e3021894 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
elessair 0:f269e3021894 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
elessair 0:f269e3021894 7 * Software is furnished to do so, subject to the following conditions:
elessair 0:f269e3021894 8 *
elessair 0:f269e3021894 9 * The above copyright notice and this permission notice shall be included in all copies or
elessair 0:f269e3021894 10 * substantial portions of the Software.
elessair 0:f269e3021894 11 *
elessair 0:f269e3021894 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
elessair 0:f269e3021894 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
elessair 0:f269e3021894 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
elessair 0:f269e3021894 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
elessair 0:f269e3021894 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
elessair 0:f269e3021894 17 */
elessair 0:f269e3021894 18
elessair 0:f269e3021894 19 #ifndef USBMOUSE_H
elessair 0:f269e3021894 20 #define USBMOUSE_H
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 #include "USBHID.h"
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 #define REPORT_ID_MOUSE 2
elessair 0:f269e3021894 25
elessair 0:f269e3021894 26 /* Common usage */
elessair 0:f269e3021894 27
elessair 0:f269e3021894 28 enum MOUSE_BUTTON
elessair 0:f269e3021894 29 {
elessair 0:f269e3021894 30 MOUSE_LEFT = 1,
elessair 0:f269e3021894 31 MOUSE_RIGHT = 2,
elessair 0:f269e3021894 32 MOUSE_MIDDLE = 4,
elessair 0:f269e3021894 33 };
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 /* X and Y limits */
elessair 0:f269e3021894 36 /* These values do not directly map to screen pixels */
elessair 0:f269e3021894 37 /* Zero may be interpreted as meaning 'no movement' */
elessair 0:f269e3021894 38 #define X_MIN_ABS (1) /*!< Minimum value on x-axis */
elessair 0:f269e3021894 39 #define Y_MIN_ABS (1) /*!< Minimum value on y-axis */
elessair 0:f269e3021894 40 #define X_MAX_ABS (0x7fff) /*!< Maximum value on x-axis */
elessair 0:f269e3021894 41 #define Y_MAX_ABS (0x7fff) /*!< Maximum value on y-axis */
elessair 0:f269e3021894 42
elessair 0:f269e3021894 43 #define X_MIN_REL (-127) /*!< The maximum value that we can move to the left on the x-axis */
elessair 0:f269e3021894 44 #define Y_MIN_REL (-127) /*!< The maximum value that we can move up on the y-axis */
elessair 0:f269e3021894 45 #define X_MAX_REL (127) /*!< The maximum value that we can move to the right on the x-axis */
elessair 0:f269e3021894 46 #define Y_MAX_REL (127) /*!< The maximum value that we can move down on the y-axis */
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 enum MOUSE_TYPE
elessair 0:f269e3021894 49 {
elessair 0:f269e3021894 50 ABS_MOUSE,
elessair 0:f269e3021894 51 REL_MOUSE,
elessair 0:f269e3021894 52 };
elessair 0:f269e3021894 53
elessair 0:f269e3021894 54 /**
elessair 0:f269e3021894 55 *
elessair 0:f269e3021894 56 * USBMouse example
elessair 0:f269e3021894 57 * @code
elessair 0:f269e3021894 58 * #include "mbed.h"
elessair 0:f269e3021894 59 * #include "USBMouse.h"
elessair 0:f269e3021894 60 *
elessair 0:f269e3021894 61 * USBMouse mouse;
elessair 0:f269e3021894 62 *
elessair 0:f269e3021894 63 * int main(void)
elessair 0:f269e3021894 64 * {
elessair 0:f269e3021894 65 * while (1)
elessair 0:f269e3021894 66 * {
elessair 0:f269e3021894 67 * mouse.move(20, 0);
elessair 0:f269e3021894 68 * wait(0.5);
elessair 0:f269e3021894 69 * }
elessair 0:f269e3021894 70 * }
elessair 0:f269e3021894 71 *
elessair 0:f269e3021894 72 * @endcode
elessair 0:f269e3021894 73 *
elessair 0:f269e3021894 74 *
elessair 0:f269e3021894 75 * @code
elessair 0:f269e3021894 76 * #include "mbed.h"
elessair 0:f269e3021894 77 * #include "USBMouse.h"
elessair 0:f269e3021894 78 * #include <math.h>
elessair 0:f269e3021894 79 *
elessair 0:f269e3021894 80 * USBMouse mouse(ABS_MOUSE);
elessair 0:f269e3021894 81 *
elessair 0:f269e3021894 82 * int main(void)
elessair 0:f269e3021894 83 * {
elessair 0:f269e3021894 84 * uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2;
elessair 0:f269e3021894 85 * uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
elessair 0:f269e3021894 86 * uint16_t x_screen = 0;
elessair 0:f269e3021894 87 * uint16_t y_screen = 0;
elessair 0:f269e3021894 88 *
elessair 0:f269e3021894 89 * uint32_t x_origin = x_center;
elessair 0:f269e3021894 90 * uint32_t y_origin = y_center;
elessair 0:f269e3021894 91 * uint32_t radius = 5000;
elessair 0:f269e3021894 92 * uint32_t angle = 0;
elessair 0:f269e3021894 93 *
elessair 0:f269e3021894 94 * while (1)
elessair 0:f269e3021894 95 * {
elessair 0:f269e3021894 96 * x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;
elessair 0:f269e3021894 97 * y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;
elessair 0:f269e3021894 98 *
elessair 0:f269e3021894 99 * mouse.move(x_screen, y_screen);
elessair 0:f269e3021894 100 * angle += 3;
elessair 0:f269e3021894 101 * wait(0.01);
elessair 0:f269e3021894 102 * }
elessair 0:f269e3021894 103 * }
elessair 0:f269e3021894 104 *
elessair 0:f269e3021894 105 * @endcode
elessair 0:f269e3021894 106 */
elessair 0:f269e3021894 107 class USBMouse: public USBHID
elessair 0:f269e3021894 108 {
elessair 0:f269e3021894 109 public:
elessair 0:f269e3021894 110
elessair 0:f269e3021894 111 /**
elessair 0:f269e3021894 112 * Constructor
elessair 0:f269e3021894 113 *
elessair 0:f269e3021894 114 * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
elessair 0:f269e3021894 115 * @param vendor_id Your vendor_id (default: 0x1234)
elessair 0:f269e3021894 116 * @param product_id Your product_id (default: 0x0001)
elessair 0:f269e3021894 117 * @param product_release Your preoduct_release (default: 0x0001)
elessair 0:f269e3021894 118 *
elessair 0:f269e3021894 119 */
elessair 0:f269e3021894 120 USBMouse(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001):
elessair 0:f269e3021894 121 USBHID(0, 0, vendor_id, product_id, product_release, false)
elessair 0:f269e3021894 122 {
elessair 0:f269e3021894 123 button = 0;
elessair 0:f269e3021894 124 this->mouse_type = mouse_type;
elessair 0:f269e3021894 125 connect();
elessair 0:f269e3021894 126 };
elessair 0:f269e3021894 127
elessair 0:f269e3021894 128 /**
elessair 0:f269e3021894 129 * Write a state of the mouse
elessair 0:f269e3021894 130 *
elessair 0:f269e3021894 131 * @param x x-axis position
elessair 0:f269e3021894 132 * @param y y-axis position
elessair 0:f269e3021894 133 * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
elessair 0:f269e3021894 134 * @param z wheel state (>0 to scroll down, <0 to scroll up)
elessair 0:f269e3021894 135 * @returns true if there is no error, false otherwise
elessair 0:f269e3021894 136 */
elessair 0:f269e3021894 137 bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
elessair 0:f269e3021894 138
elessair 0:f269e3021894 139
elessair 0:f269e3021894 140 /**
elessair 0:f269e3021894 141 * Move the cursor to (x, y)
elessair 0:f269e3021894 142 *
elessair 0:f269e3021894 143 * @param x-axis position
elessair 0:f269e3021894 144 * @param y-axis position
elessair 0:f269e3021894 145 * @returns true if there is no error, false otherwise
elessair 0:f269e3021894 146 */
elessair 0:f269e3021894 147 bool move(int16_t x, int16_t y);
elessair 0:f269e3021894 148
elessair 0:f269e3021894 149 /**
elessair 0:f269e3021894 150 * Press one or several buttons
elessair 0:f269e3021894 151 *
elessair 0:f269e3021894 152 * @param button button state (ex: press(MOUSE_LEFT))
elessair 0:f269e3021894 153 * @returns true if there is no error, false otherwise
elessair 0:f269e3021894 154 */
elessair 0:f269e3021894 155 bool press(uint8_t button);
elessair 0:f269e3021894 156
elessair 0:f269e3021894 157 /**
elessair 0:f269e3021894 158 * Release one or several buttons
elessair 0:f269e3021894 159 *
elessair 0:f269e3021894 160 * @param button button state (ex: release(MOUSE_LEFT))
elessair 0:f269e3021894 161 * @returns true if there is no error, false otherwise
elessair 0:f269e3021894 162 */
elessair 0:f269e3021894 163 bool release(uint8_t button);
elessair 0:f269e3021894 164
elessair 0:f269e3021894 165 /**
elessair 0:f269e3021894 166 * Double click (MOUSE_LEFT)
elessair 0:f269e3021894 167 *
elessair 0:f269e3021894 168 * @returns true if there is no error, false otherwise
elessair 0:f269e3021894 169 */
elessair 0:f269e3021894 170 bool doubleClick();
elessair 0:f269e3021894 171
elessair 0:f269e3021894 172 /**
elessair 0:f269e3021894 173 * Click
elessair 0:f269e3021894 174 *
elessair 0:f269e3021894 175 * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
elessair 0:f269e3021894 176 * @returns true if there is no error, false otherwise
elessair 0:f269e3021894 177 */
elessair 0:f269e3021894 178 bool click(uint8_t button);
elessair 0:f269e3021894 179
elessair 0:f269e3021894 180 /**
elessair 0:f269e3021894 181 * Scrolling
elessair 0:f269e3021894 182 *
elessair 0:f269e3021894 183 * @param z value of the wheel (>0 to go down, <0 to go up)
elessair 0:f269e3021894 184 * @returns true if there is no error, false otherwise
elessair 0:f269e3021894 185 */
elessair 0:f269e3021894 186 bool scroll(int8_t z);
elessair 0:f269e3021894 187
elessair 0:f269e3021894 188 /*
elessair 0:f269e3021894 189 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
elessair 0:f269e3021894 190 *
elessair 0:f269e3021894 191 * @returns pointer to the report descriptor
elessair 0:f269e3021894 192 */
elessair 0:f269e3021894 193 virtual uint8_t * reportDesc();
elessair 0:f269e3021894 194
elessair 0:f269e3021894 195 protected:
elessair 0:f269e3021894 196 /*
elessair 0:f269e3021894 197 * Get configuration descriptor
elessair 0:f269e3021894 198 *
elessair 0:f269e3021894 199 * @returns pointer to the configuration descriptor
elessair 0:f269e3021894 200 */
elessair 0:f269e3021894 201 virtual uint8_t * configurationDesc();
elessair 0:f269e3021894 202
elessair 0:f269e3021894 203 private:
elessair 0:f269e3021894 204 MOUSE_TYPE mouse_type;
elessair 0:f269e3021894 205 uint8_t button;
elessair 0:f269e3021894 206 bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
elessair 0:f269e3021894 207 };
elessair 0:f269e3021894 208
elessair 0:f269e3021894 209 #endif