Ries Twisk / Mbed 2 deprecated JoyStick

Dependencies:   USBDevice mbed-rtos mbed

Fork of JoyStick by Ries Twisk

Committer:
rvt
Date:
Wed Aug 28 02:33:03 2013 +0000
Revision:
0:33bc88c4ab31
Child:
2:ae7a31a3c618
Initial release with RTOS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rvt 0:33bc88c4ab31 1 /* USBJoystick.h */
rvt 0:33bc88c4ab31 2 /* USB device example: Joystick*/
rvt 0:33bc88c4ab31 3 /* Copyright (c) 2011 ARM Limited. All rights reserved. */
rvt 0:33bc88c4ab31 4 /* Modified Mouse code for Joystick - WH 2012 */
rvt 0:33bc88c4ab31 5
rvt 0:33bc88c4ab31 6 #ifndef USBJOYSTICK_H
rvt 0:33bc88c4ab31 7 #define USBJOYSTICK_H
rvt 0:33bc88c4ab31 8
rvt 0:33bc88c4ab31 9 #include "USBHID.h"
rvt 0:33bc88c4ab31 10
rvt 0:33bc88c4ab31 11 #define REPORT_ID_JOYSTICK 4
rvt 0:33bc88c4ab31 12
rvt 0:33bc88c4ab31 13 /* Common usage */
rvt 0:33bc88c4ab31 14 enum JOY_BUTTON {
rvt 0:33bc88c4ab31 15 JOY_B0 = 1,
rvt 0:33bc88c4ab31 16 JOY_B1 = 2,
rvt 0:33bc88c4ab31 17 JOY_B2 = 4,
rvt 0:33bc88c4ab31 18 JOY_B3 = 8,
rvt 0:33bc88c4ab31 19 };
rvt 0:33bc88c4ab31 20
rvt 0:33bc88c4ab31 21 #if(0)
rvt 0:33bc88c4ab31 22 enum JOY_HAT {
rvt 0:33bc88c4ab31 23 JOY_HAT_UP = 0,
rvt 0:33bc88c4ab31 24 JOY_HAT_RIGHT = 1,
rvt 0:33bc88c4ab31 25 JOY_HAT_DOWN = 2,
rvt 0:33bc88c4ab31 26 JOY_HAT_LEFT = 3,
rvt 0:33bc88c4ab31 27 JOY_HAT_NEUTRAL = 4,
rvt 0:33bc88c4ab31 28 };
rvt 0:33bc88c4ab31 29 #else
rvt 0:33bc88c4ab31 30 enum JOY_HAT {
rvt 0:33bc88c4ab31 31 JOY_HAT_UP = 0,
rvt 0:33bc88c4ab31 32 JOY_HAT_UP_RIGHT = 1,
rvt 0:33bc88c4ab31 33 JOY_HAT_RIGHT = 2,
rvt 0:33bc88c4ab31 34 JOY_HAT_RIGHT_DOWN = 3,
rvt 0:33bc88c4ab31 35 JOY_HAT_DOWN = 4,
rvt 0:33bc88c4ab31 36 JOY_HAT_DOWN_LEFT = 5,
rvt 0:33bc88c4ab31 37 JOY_HAT_LEFT = 6,
rvt 0:33bc88c4ab31 38 JOY_HAT_LEFT_UP = 7,
rvt 0:33bc88c4ab31 39 JOY_HAT_NEUTRAL = 8,
rvt 0:33bc88c4ab31 40 };
rvt 0:33bc88c4ab31 41 #endif
rvt 0:33bc88c4ab31 42
rvt 0:33bc88c4ab31 43 /* X, Y and T limits */
rvt 0:33bc88c4ab31 44 /* These values do not directly map to screen pixels */
rvt 0:33bc88c4ab31 45 /* Zero may be interpreted as meaning 'no movement' */
rvt 0:33bc88c4ab31 46 #define JX_MIN_ABS (-127) /*!< The maximum value that we can move to the left on the x-axis */
rvt 0:33bc88c4ab31 47 #define JY_MIN_ABS (-127) /*!< The maximum value that we can move up on the y-axis */
rvt 0:33bc88c4ab31 48 #define JT_MIN_ABS (-127) /*!< The minimum value for the throttle */
rvt 0:33bc88c4ab31 49 #define JX_MAX_ABS (127) /*!< The maximum value that we can move to the right on the x-axis */
rvt 0:33bc88c4ab31 50 #define JY_MAX_ABS (127) /*!< The maximum value that we can move down on the y-axis */
rvt 0:33bc88c4ab31 51 #define JT_MAX_ABS (127) /*!< The maximum value for the throttle */
rvt 0:33bc88c4ab31 52
rvt 0:33bc88c4ab31 53 /**
rvt 0:33bc88c4ab31 54 *
rvt 0:33bc88c4ab31 55 * USBJoystick example
rvt 0:33bc88c4ab31 56 * @code
rvt 0:33bc88c4ab31 57 * #include "mbed.h"
rvt 0:33bc88c4ab31 58 * #include "USBJoystick.h"
rvt 0:33bc88c4ab31 59 *
rvt 0:33bc88c4ab31 60 * USBJoystick joystick;
rvt 0:33bc88c4ab31 61 *
rvt 0:33bc88c4ab31 62 * int main(void)
rvt 0:33bc88c4ab31 63 * {
rvt 0:33bc88c4ab31 64 * while (1)
rvt 0:33bc88c4ab31 65 * {
rvt 0:33bc88c4ab31 66 * joystick.move(20, 0);
rvt 0:33bc88c4ab31 67 * wait(0.5);
rvt 0:33bc88c4ab31 68 * }
rvt 0:33bc88c4ab31 69 * }
rvt 0:33bc88c4ab31 70 *
rvt 0:33bc88c4ab31 71 * @endcode
rvt 0:33bc88c4ab31 72 *
rvt 0:33bc88c4ab31 73 *
rvt 0:33bc88c4ab31 74 * @code
rvt 0:33bc88c4ab31 75 * #include "mbed.h"
rvt 0:33bc88c4ab31 76 * #include "USBJoystick.h"
rvt 0:33bc88c4ab31 77 * #include <math.h>
rvt 0:33bc88c4ab31 78 *
rvt 0:33bc88c4ab31 79 * USBJoystick joystick;
rvt 0:33bc88c4ab31 80 *
rvt 0:33bc88c4ab31 81 * int main(void)
rvt 0:33bc88c4ab31 82 * {
rvt 0:33bc88c4ab31 83 * int16_t i = 0;
rvt 0:33bc88c4ab31 84 * int16_t throttle = 0;
rvt 0:33bc88c4ab31 85 * int16_t rudder = 0;
rvt 0:33bc88c4ab31 86 * int16_t x = 0;
rvt 0:33bc88c4ab31 87 * int16_t y = 0;
rvt 0:33bc88c4ab31 88 * int32_t radius = 120;
rvt 0:33bc88c4ab31 89 * int32_t angle = 0;
rvt 0:33bc88c4ab31 90 * int8_t button = 0;
rvt 0:33bc88c4ab31 91 * int8_t hat = 0;
rvt 0:33bc88c4ab31 92 *
rvt 0:33bc88c4ab31 93 * while (1) {
rvt 0:33bc88c4ab31 94 * // Basic Joystick
rvt 0:33bc88c4ab31 95 * throttle = (i >> 8) & 0xFF; // value -127 .. 128
rvt 0:33bc88c4ab31 96 * rudder = (i >> 8) & 0xFF; // value -127 .. 128
rvt 0:33bc88c4ab31 97 * button = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button
rvt 0:33bc88c4ab31 98 * hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral
rvt 0:33bc88c4ab31 99 * i++;
rvt 0:33bc88c4ab31 100 *
rvt 0:33bc88c4ab31 101 * x = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128
rvt 0:33bc88c4ab31 102 * y = sin((double)angle*3.14/180.0)*radius; // value -127 .. 128
rvt 0:33bc88c4ab31 103 * angle += 3;
rvt 0:33bc88c4ab31 104 *
rvt 0:33bc88c4ab31 105 * joystick.update(throttle, rudder, x, y, button, hat);
rvt 0:33bc88c4ab31 106 *
rvt 0:33bc88c4ab31 107 * wait(0.001);
rvt 0:33bc88c4ab31 108 * }
rvt 0:33bc88c4ab31 109 * }
rvt 0:33bc88c4ab31 110 * @endcode
rvt 0:33bc88c4ab31 111 */
rvt 0:33bc88c4ab31 112
rvt 0:33bc88c4ab31 113
rvt 0:33bc88c4ab31 114 class USBJoystick: public USBHID {
rvt 0:33bc88c4ab31 115 public:
rvt 0:33bc88c4ab31 116
rvt 0:33bc88c4ab31 117 /**
rvt 0:33bc88c4ab31 118 * Constructor
rvt 0:33bc88c4ab31 119 *
rvt 0:33bc88c4ab31 120 * @param vendor_id Your vendor_id (default: 0x1234)
rvt 0:33bc88c4ab31 121 * @param product_id Your product_id (default: 0x0002)
rvt 0:33bc88c4ab31 122 * @param product_release Your product_release (default: 0x0001)
rvt 0:33bc88c4ab31 123 */
rvt 0:33bc88c4ab31 124 USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0100, uint16_t product_release = 0x0001):
rvt 0:33bc88c4ab31 125 USBHID(0, 0, vendor_id, product_id, product_release, false)
rvt 0:33bc88c4ab31 126 {
rvt 0:33bc88c4ab31 127 _init();
rvt 0:33bc88c4ab31 128 connect();
rvt 0:33bc88c4ab31 129 };
rvt 0:33bc88c4ab31 130
rvt 0:33bc88c4ab31 131 /**
rvt 0:33bc88c4ab31 132 * Write a state of the mouse
rvt 0:33bc88c4ab31 133 *
rvt 0:33bc88c4ab31 134 * @param t throttle position
rvt 0:33bc88c4ab31 135 * @param r rudder position
rvt 0:33bc88c4ab31 136 * @param x x-axis position
rvt 0:33bc88c4ab31 137 * @param y y-axis position
rvt 0:33bc88c4ab31 138 * @param buttons buttons state
rvt 0:33bc88c4ab31 139 * @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral)
rvt 0:33bc88c4ab31 140 * @returns true if there is no error, false otherwise
rvt 0:33bc88c4ab31 141 */
rvt 0:33bc88c4ab31 142 bool update(int16_t t, int16_t r, int16_t x, int16_t y, uint8_t buttons, uint8_t hat);
rvt 0:33bc88c4ab31 143
rvt 0:33bc88c4ab31 144 /**
rvt 0:33bc88c4ab31 145 * Write a state of the mouse
rvt 0:33bc88c4ab31 146 *
rvt 0:33bc88c4ab31 147 * @returns true if there is no error, false otherwise
rvt 0:33bc88c4ab31 148 */
rvt 0:33bc88c4ab31 149 bool update();
rvt 0:33bc88c4ab31 150
rvt 0:33bc88c4ab31 151 /**
rvt 0:33bc88c4ab31 152 * Move the throttle position
rvt 0:33bc88c4ab31 153 *
rvt 0:33bc88c4ab31 154 * @param t throttle position
rvt 0:33bc88c4ab31 155 * @returns true if there is no error, false otherwise
rvt 0:33bc88c4ab31 156 */
rvt 0:33bc88c4ab31 157 bool throttle(int16_t t);
rvt 0:33bc88c4ab31 158
rvt 0:33bc88c4ab31 159 /**
rvt 0:33bc88c4ab31 160 * Move the rudder position
rvt 0:33bc88c4ab31 161 *
rvt 0:33bc88c4ab31 162 * @param r rudder position
rvt 0:33bc88c4ab31 163 * @returns true if there is no error, false otherwise
rvt 0:33bc88c4ab31 164 */
rvt 0:33bc88c4ab31 165 bool rudder(int16_t r);
rvt 0:33bc88c4ab31 166
rvt 0:33bc88c4ab31 167 /**
rvt 0:33bc88c4ab31 168 * Move the cursor to (x, y)
rvt 0:33bc88c4ab31 169 *
rvt 0:33bc88c4ab31 170 * @param x-axis position
rvt 0:33bc88c4ab31 171 * @param y-axis position
rvt 0:33bc88c4ab31 172 * @returns true if there is no error, false otherwise
rvt 0:33bc88c4ab31 173 */
rvt 0:33bc88c4ab31 174 bool move(int16_t x, int16_t y);
rvt 0:33bc88c4ab31 175
rvt 0:33bc88c4ab31 176 /**
rvt 0:33bc88c4ab31 177 * Press one or several buttons
rvt 0:33bc88c4ab31 178 *
rvt 0:33bc88c4ab31 179 * @param button button state
rvt 0:33bc88c4ab31 180 * @returns true if there is no error, false otherwise
rvt 0:33bc88c4ab31 181 */
rvt 0:33bc88c4ab31 182 bool button(uint8_t button);
rvt 0:33bc88c4ab31 183
rvt 0:33bc88c4ab31 184 /**
rvt 0:33bc88c4ab31 185 * Press hat
rvt 0:33bc88c4ab31 186 *
rvt 0:33bc88c4ab31 187 * @param hat hat state
rvt 0:33bc88c4ab31 188 * @returns true if there is no error, false otherwise
rvt 0:33bc88c4ab31 189 */
rvt 0:33bc88c4ab31 190 bool hat(uint8_t hat);
rvt 0:33bc88c4ab31 191
rvt 0:33bc88c4ab31 192 /*
rvt 0:33bc88c4ab31 193 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
rvt 0:33bc88c4ab31 194 *
rvt 0:33bc88c4ab31 195 * @returns pointer to the report descriptor
rvt 0:33bc88c4ab31 196 */
rvt 0:33bc88c4ab31 197 virtual uint8_t * reportDesc();
rvt 0:33bc88c4ab31 198
rvt 0:33bc88c4ab31 199 private:
rvt 0:33bc88c4ab31 200 int16_t _t;
rvt 0:33bc88c4ab31 201 int16_t _r;
rvt 0:33bc88c4ab31 202 int16_t _x;
rvt 0:33bc88c4ab31 203 int16_t _y;
rvt 0:33bc88c4ab31 204 uint8_t _button;
rvt 0:33bc88c4ab31 205 uint8_t _hat;
rvt 0:33bc88c4ab31 206
rvt 0:33bc88c4ab31 207 void _init();
rvt 0:33bc88c4ab31 208 };
rvt 0:33bc88c4ab31 209
rvt 0:33bc88c4ab31 210 #endif