Control the wondrous spinning-frog game of Zuma's Revenge with a rotating chair and an Airzooka. Maps compass rotation, flex sensor and push button input to USB actions to control Zuma's Revenge (http://www.popcap.com/games/zumas-revenge/online)

Dependencies:   LSM303DLHC mbed

Note that content for USB HID and USB Device is actually from the USBDevice mbed library. However, we made a couple of small changes to this library (allowing USB clicks at a particular location) that required us to break it off from the main project if we wanted to publish without pushing upstream.

Committer:
andrewhead
Date:
Mon Sep 29 01:12:20 2014 +0000
Revision:
0:4df415dde990
Initial Commit.

Who changed what in which revision?

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