Control program for FzeroX controller via USBHID interface.

Dependencies:   Radio USBDevice mbed

Fork of FzeroXcontroller by Interactive Device Design

Committer:
alexandertyler
Date:
Sun Sep 28 20:11:23 2014 +0000
Revision:
1:ec00f549a691
Finished minus radio

Who changed what in which revision?

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