max4146x_comp

Dependencies:   MAX14690

Committer:
sdivarci
Date:
Sun Oct 25 20:10:02 2020 +0000
Revision:
0:0061165683ee
sdivarci

Who changed what in which revision?

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