Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

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