
Adjusts the great pinscape controller to work with a cheap linear potentiometer instead of the expensive CCD array
Fork of Pinscape_Controller by
USBJoystick/USBJoystick.h@3:3514575d4f86, 2014-07-23 (annotated)
- Committer:
- mjr
- Date:
- Wed Jul 23 17:53:28 2014 +0000
- Revision:
- 3:3514575d4f86
- Parent:
- USBJoystick.h@0:5acbbe3f4cf4
- Child:
- 4:02c7cd7b2183
Conversion to interrupt-based sampling of the accelerometer working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mjr | 0:5acbbe3f4cf4 | 1 | /* USBJoystick.h */ |
mjr | 0:5acbbe3f4cf4 | 2 | /* USB device example: Joystick*/ |
mjr | 0:5acbbe3f4cf4 | 3 | /* Copyright (c) 2011 ARM Limited. All rights reserved. */ |
mjr | 0:5acbbe3f4cf4 | 4 | /* Modified Mouse code for Joystick - WH 2012 */ |
mjr | 0:5acbbe3f4cf4 | 5 | |
mjr | 0:5acbbe3f4cf4 | 6 | #ifndef USBJOYSTICK_H |
mjr | 0:5acbbe3f4cf4 | 7 | #define USBJOYSTICK_H |
mjr | 0:5acbbe3f4cf4 | 8 | |
mjr | 0:5acbbe3f4cf4 | 9 | #include "USBHID.h" |
mjr | 0:5acbbe3f4cf4 | 10 | |
mjr | 0:5acbbe3f4cf4 | 11 | #define REPORT_ID_JOYSTICK 4 |
mjr | 0:5acbbe3f4cf4 | 12 | |
mjr | 0:5acbbe3f4cf4 | 13 | /* Common usage */ |
mjr | 0:5acbbe3f4cf4 | 14 | enum JOY_BUTTON { |
mjr | 0:5acbbe3f4cf4 | 15 | JOY_B0 = 0x0001, |
mjr | 0:5acbbe3f4cf4 | 16 | JOY_B1 = 0x0002, |
mjr | 0:5acbbe3f4cf4 | 17 | JOY_B2 = 0x0004, |
mjr | 0:5acbbe3f4cf4 | 18 | JOY_B3 = 0x0008, |
mjr | 0:5acbbe3f4cf4 | 19 | JOY_B4 = 0x0010, |
mjr | 0:5acbbe3f4cf4 | 20 | JOY_B5 = 0x0020, |
mjr | 0:5acbbe3f4cf4 | 21 | JOY_B6 = 0x0040, |
mjr | 0:5acbbe3f4cf4 | 22 | JOY_B7 = 0x0080, |
mjr | 0:5acbbe3f4cf4 | 23 | JOY_B8 = 0x0100, |
mjr | 0:5acbbe3f4cf4 | 24 | JOY_B9 = 0x0200, |
mjr | 0:5acbbe3f4cf4 | 25 | JOY_B10 = 0x0400, |
mjr | 0:5acbbe3f4cf4 | 26 | JOY_B11 = 0x0800, |
mjr | 0:5acbbe3f4cf4 | 27 | JOY_B12 = 0x1000, |
mjr | 0:5acbbe3f4cf4 | 28 | JOY_B13 = 0x2000, |
mjr | 0:5acbbe3f4cf4 | 29 | JOY_B14 = 0x4000, |
mjr | 0:5acbbe3f4cf4 | 30 | JOY_B15 = 0x8000 |
mjr | 0:5acbbe3f4cf4 | 31 | }; |
mjr | 0:5acbbe3f4cf4 | 32 | |
mjr | 0:5acbbe3f4cf4 | 33 | /* X, Y and T limits */ |
mjr | 0:5acbbe3f4cf4 | 34 | /* These values do not directly map to screen pixels */ |
mjr | 0:5acbbe3f4cf4 | 35 | /* Zero may be interpreted as meaning 'no movement' */ |
mjr | 0:5acbbe3f4cf4 | 36 | #define JX_MIN_ABS (-127) /*!< The maximum value that we can move to the left on the x-axis */ |
mjr | 0:5acbbe3f4cf4 | 37 | #define JY_MIN_ABS (-127) /*!< The maximum value that we can move up on the y-axis */ |
mjr | 0:5acbbe3f4cf4 | 38 | #define JZ_MIN_ABS (-127) /*!< The minimum value for the Z axis */ |
mjr | 0:5acbbe3f4cf4 | 39 | #define JX_MAX_ABS (127) /*!< The maximum value that we can move to the right on the x-axis */ |
mjr | 0:5acbbe3f4cf4 | 40 | #define JY_MAX_ABS (127) /*!< The maximum value that we can move down on the y-axis */ |
mjr | 0:5acbbe3f4cf4 | 41 | #define JZ_MAX_ABS (127) /*!< The maximum value for the Z axis */ |
mjr | 0:5acbbe3f4cf4 | 42 | |
mjr | 0:5acbbe3f4cf4 | 43 | /** |
mjr | 0:5acbbe3f4cf4 | 44 | * |
mjr | 0:5acbbe3f4cf4 | 45 | * USBJoystick example |
mjr | 0:5acbbe3f4cf4 | 46 | * @code |
mjr | 0:5acbbe3f4cf4 | 47 | * #include "mbed.h" |
mjr | 0:5acbbe3f4cf4 | 48 | * #include "USBJoystick.h" |
mjr | 0:5acbbe3f4cf4 | 49 | * |
mjr | 0:5acbbe3f4cf4 | 50 | * USBJoystick joystick; |
mjr | 0:5acbbe3f4cf4 | 51 | * |
mjr | 0:5acbbe3f4cf4 | 52 | * int main(void) |
mjr | 0:5acbbe3f4cf4 | 53 | * { |
mjr | 0:5acbbe3f4cf4 | 54 | * while (1) |
mjr | 0:5acbbe3f4cf4 | 55 | * { |
mjr | 0:5acbbe3f4cf4 | 56 | * joystick.move(20, 0); |
mjr | 0:5acbbe3f4cf4 | 57 | * wait(0.5); |
mjr | 0:5acbbe3f4cf4 | 58 | * } |
mjr | 0:5acbbe3f4cf4 | 59 | * } |
mjr | 0:5acbbe3f4cf4 | 60 | * |
mjr | 0:5acbbe3f4cf4 | 61 | * @endcode |
mjr | 0:5acbbe3f4cf4 | 62 | * |
mjr | 0:5acbbe3f4cf4 | 63 | * |
mjr | 0:5acbbe3f4cf4 | 64 | * @code |
mjr | 0:5acbbe3f4cf4 | 65 | * #include "mbed.h" |
mjr | 0:5acbbe3f4cf4 | 66 | * #include "USBJoystick.h" |
mjr | 0:5acbbe3f4cf4 | 67 | * #include <math.h> |
mjr | 0:5acbbe3f4cf4 | 68 | * |
mjr | 0:5acbbe3f4cf4 | 69 | * USBJoystick joystick; |
mjr | 0:5acbbe3f4cf4 | 70 | * |
mjr | 0:5acbbe3f4cf4 | 71 | * int main(void) |
mjr | 0:5acbbe3f4cf4 | 72 | * { |
mjr | 0:5acbbe3f4cf4 | 73 | * while (1) { |
mjr | 0:5acbbe3f4cf4 | 74 | * // Basic Joystick |
mjr | 0:5acbbe3f4cf4 | 75 | * joystick.update(tx, y, z, buttonBits); |
mjr | 0:5acbbe3f4cf4 | 76 | * wait(0.001); |
mjr | 0:5acbbe3f4cf4 | 77 | * } |
mjr | 0:5acbbe3f4cf4 | 78 | * } |
mjr | 0:5acbbe3f4cf4 | 79 | * @endcode |
mjr | 0:5acbbe3f4cf4 | 80 | */ |
mjr | 0:5acbbe3f4cf4 | 81 | |
mjr | 0:5acbbe3f4cf4 | 82 | |
mjr | 0:5acbbe3f4cf4 | 83 | class USBJoystick: public USBHID { |
mjr | 0:5acbbe3f4cf4 | 84 | public: |
mjr | 0:5acbbe3f4cf4 | 85 | |
mjr | 0:5acbbe3f4cf4 | 86 | /** |
mjr | 0:5acbbe3f4cf4 | 87 | * Constructor |
mjr | 0:5acbbe3f4cf4 | 88 | * |
mjr | 0:5acbbe3f4cf4 | 89 | * @param vendor_id Your vendor_id (default: 0x1234) |
mjr | 0:5acbbe3f4cf4 | 90 | * @param product_id Your product_id (default: 0x0002) |
mjr | 0:5acbbe3f4cf4 | 91 | * @param product_release Your product_release (default: 0x0001) |
mjr | 0:5acbbe3f4cf4 | 92 | */ |
mjr | 0:5acbbe3f4cf4 | 93 | USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0100, uint16_t product_release = 0x0001): |
mjr | 0:5acbbe3f4cf4 | 94 | USBHID(8, 8, vendor_id, product_id, product_release, false) |
mjr | 0:5acbbe3f4cf4 | 95 | { |
mjr | 0:5acbbe3f4cf4 | 96 | _init(); |
mjr | 0:5acbbe3f4cf4 | 97 | connect(); |
mjr | 0:5acbbe3f4cf4 | 98 | }; |
mjr | 0:5acbbe3f4cf4 | 99 | |
mjr | 0:5acbbe3f4cf4 | 100 | /** |
mjr | 0:5acbbe3f4cf4 | 101 | * Write a state of the mouse |
mjr | 0:5acbbe3f4cf4 | 102 | * |
mjr | 0:5acbbe3f4cf4 | 103 | * @param x x-axis position |
mjr | 0:5acbbe3f4cf4 | 104 | * @param y y-axis position |
mjr | 0:5acbbe3f4cf4 | 105 | * @param z z-axis position |
mjr | 0:5acbbe3f4cf4 | 106 | * @param buttons buttons state, as a bit mask (combination with '|' of JOY_Bn values) |
mjr | 0:5acbbe3f4cf4 | 107 | * @returns true if there is no error, false otherwise |
mjr | 0:5acbbe3f4cf4 | 108 | */ |
mjr | 3:3514575d4f86 | 109 | bool update(int16_t x, int16_t y, int16_t z, int16_t rx, int16_t ry, uint16_t buttons); |
mjr | 0:5acbbe3f4cf4 | 110 | |
mjr | 0:5acbbe3f4cf4 | 111 | /** |
mjr | 0:5acbbe3f4cf4 | 112 | * Write a state of the mouse |
mjr | 0:5acbbe3f4cf4 | 113 | * |
mjr | 0:5acbbe3f4cf4 | 114 | * @returns true if there is no error, false otherwise |
mjr | 0:5acbbe3f4cf4 | 115 | */ |
mjr | 0:5acbbe3f4cf4 | 116 | bool update(); |
mjr | 0:5acbbe3f4cf4 | 117 | |
mjr | 0:5acbbe3f4cf4 | 118 | /** |
mjr | 0:5acbbe3f4cf4 | 119 | * Move the cursor to (x, y) |
mjr | 0:5acbbe3f4cf4 | 120 | * |
mjr | 0:5acbbe3f4cf4 | 121 | * @param x x-axis position |
mjr | 0:5acbbe3f4cf4 | 122 | * @param y y-axis position |
mjr | 0:5acbbe3f4cf4 | 123 | * @returns true if there is no error, false otherwise |
mjr | 0:5acbbe3f4cf4 | 124 | */ |
mjr | 0:5acbbe3f4cf4 | 125 | bool move(int16_t x, int16_t y); |
mjr | 0:5acbbe3f4cf4 | 126 | |
mjr | 0:5acbbe3f4cf4 | 127 | /** |
mjr | 0:5acbbe3f4cf4 | 128 | * Set the z position |
mjr | 0:5acbbe3f4cf4 | 129 | * |
mjr | 0:5acbbe3f4cf4 | 130 | * @param z z-axis osition |
mjr | 0:5acbbe3f4cf4 | 131 | */ |
mjr | 0:5acbbe3f4cf4 | 132 | bool setZ(int16_t z); |
mjr | 0:5acbbe3f4cf4 | 133 | |
mjr | 0:5acbbe3f4cf4 | 134 | /** |
mjr | 0:5acbbe3f4cf4 | 135 | * Press one or several buttons |
mjr | 0:5acbbe3f4cf4 | 136 | * |
mjr | 0:5acbbe3f4cf4 | 137 | * @param buttons button state, as a bitwise combination of JOY_Bn values |
mjr | 0:5acbbe3f4cf4 | 138 | * @returns true if there is no error, false otherwise |
mjr | 0:5acbbe3f4cf4 | 139 | */ |
mjr | 0:5acbbe3f4cf4 | 140 | bool buttons(uint16_t buttons); |
mjr | 0:5acbbe3f4cf4 | 141 | |
mjr | 0:5acbbe3f4cf4 | 142 | /* |
mjr | 0:5acbbe3f4cf4 | 143 | * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. |
mjr | 0:5acbbe3f4cf4 | 144 | * |
mjr | 0:5acbbe3f4cf4 | 145 | * @returns pointer to the report descriptor |
mjr | 0:5acbbe3f4cf4 | 146 | */ |
mjr | 0:5acbbe3f4cf4 | 147 | virtual uint8_t * reportDesc(); |
mjr | 0:5acbbe3f4cf4 | 148 | |
mjr | 0:5acbbe3f4cf4 | 149 | /* USB descriptor string overrides */ |
mjr | 0:5acbbe3f4cf4 | 150 | virtual uint8_t *stringImanufacturerDesc(); |
mjr | 0:5acbbe3f4cf4 | 151 | virtual uint8_t *stringIserialDesc(); |
mjr | 0:5acbbe3f4cf4 | 152 | virtual uint8_t *stringIproductDesc(); |
mjr | 0:5acbbe3f4cf4 | 153 | |
mjr | 0:5acbbe3f4cf4 | 154 | private: |
mjr | 0:5acbbe3f4cf4 | 155 | int8_t _x; |
mjr | 0:5acbbe3f4cf4 | 156 | int8_t _y; |
mjr | 0:5acbbe3f4cf4 | 157 | int8_t _z; |
mjr | 3:3514575d4f86 | 158 | int8_t _rx; |
mjr | 3:3514575d4f86 | 159 | int8_t _ry; |
mjr | 0:5acbbe3f4cf4 | 160 | uint16_t _buttons; |
mjr | 0:5acbbe3f4cf4 | 161 | |
mjr | 0:5acbbe3f4cf4 | 162 | void _init(); |
mjr | 0:5acbbe3f4cf4 | 163 | }; |
mjr | 0:5acbbe3f4cf4 | 164 | |
mjr | 0:5acbbe3f4cf4 | 165 | #endif |