USB Joystick library, modified to work as a 6 DOF joystick.
Fork of USBJoystick by
Revision 2:1549541d3b4b, committed 2017-01-13
- Comitter:
- smartsystemdesign
- Date:
- Fri Jan 13 07:09:28 2017 +0000
- Parent:
- 1:8b5f213b169f
- Child:
- 3:f8be03f31e6c
- Commit message:
- Updated joystick class to accommodate a 16 bit, 6 DOF joystick.
Changed in this revision
| USBJoystick.cpp | Show annotated file Show diff for this revision Revisions of this file |
| USBJoystick.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/USBJoystick.cpp Fri Jan 13 05:37:07 2017 +0000
+++ b/USBJoystick.cpp Fri Jan 13 07:09:28 2017 +0000
@@ -27,12 +27,14 @@
#include "stdint.h"
#include "USBJoystick.h"
-bool USBJoystick::update(int16_t t, int16_t r, int16_t x, int16_t y, uint32_t buttons, uint8_t hat) {
+bool USBJoystick::update(int16_t x, int16_t y, int16_t z, int16_t rx, int16_t ry, int16_t rz, uint32_t buttons, uint8_t hat) {
- _t = t;
- _r = r;
_x = x;
- _y = y;
+ _y = y;
+ _z = z;
+ _rx = rx;
+ _ry = ry;
+ _rz = rz;
_buttons = buttons;
_hat = hat;
@@ -43,11 +45,12 @@
HID_REPORT report;
// Fill the report according to the Joystick Descriptor
- report.data[0] = _t & 0xffff;
- report.data[1] = _r & 0xffff;
- report.data[2] = _x & 0xffff;
- report.data[3] = _y & 0xffff;
-
+ report.data[0] = _x & 0xffff;
+ report.data[1] = _y & 0xffff;
+ report.data[2] = _z & 0xffff;
+ report.data[3] = _rx & 0xffff;
+ report.data[4] = _ry & 0xffff;
+ report.data[5] = _rz & 0xffff;
#if (BUTTONS4 == 1)
//Hat and 4 Buttons
@@ -56,11 +59,11 @@
//Use 4 bit padding for hat4 or hat8
- report.data[4] = (_hat & 0x0f) ;
+ report.data[6] = (_hat & 0x0f) ;
//Use 4 bit padding for buttons
- report.data[5] = (_buttons & 0x0f) ;
- report.length = 6;
+ report.data[7] = (_buttons & 0x0f) ;
+ report.length = 8;
#endif
#if (BUTTONS8 == 1)
@@ -72,41 +75,35 @@
// report.length = 6;
//Use 4 bit padding for hat4 or hat8
- report.data[4] = (_hat & 0x0f) ;
+ report.data[6] = (_hat & 0x0f) ;
//Use 8 bits for buttons
- report.data[5] = (_buttons & 0xff) ;
- report.length = 6;
+ report.data[7] = (_buttons & 0xff) ;
+ report.length = 8;
#endif
#if (BUTTONS32 == 1)
//Use 4 bit padding for hat4 or hat8
- report.data[4] = (_hat & 0x0f) ;
+ report.data[6] = (_hat & 0x0f) ;
//No bit padding for 32 buttons
- report.data[5] = (_buttons >> 0) & 0xff;
- report.data[6] = (_buttons >> 8) & 0xff;
- report.data[7] = (_buttons >> 16) & 0xff;
- report.data[8] = (_buttons >> 24) & 0xff;
- report.length = 9;
+ report.data[7] = (_buttons >> 0) & 0xff;
+ report.data[8] = (_buttons >> 8) & 0xff;
+ report.data[9] = (_buttons >> 16) & 0xff;
+ report.data[10] = (_buttons >> 24) & 0xff;
+ report.length = 11;
#endif
return send(&report);
}
-bool USBJoystick::throttle(int16_t t) {
- _t = t;
- return update();
-}
-
-bool USBJoystick::rudder(int16_t r) {
- _r = r;
- return update();
-}
-
-bool USBJoystick::move(int16_t x, int16_t y) {
+bool USBJoystick::move(int16_t x, int16_t y, int16_t z, int16_t rx, int16_t ry, int16_t rz) {
_x = x;
_y = y;
+ _z = z;
+ _rx = rx;
+ _ry = ry;
+ _rz = rz;
return update();
}
@@ -120,12 +117,13 @@
return update();
}
-
void USBJoystick::_init() {
- _t = -32768;
- _r = -32768;
- _x = 0;
- _y = 0;
+ _x = 0x0000;
+ _y = 0x0000;
+ _z = 0x0000;
+ _rx = 0x0000;
+ _ry = 0x0000;
+ _rz = 0x0000;
_buttons = 0x00000000;
_hat = 0x00;
}
@@ -133,38 +131,28 @@
uint8_t * USBJoystick::reportDesc() {
static uint8_t reportDescriptor[] = {
-
+ // value in () is the number of bytes. These bytes follow the comma, least significant byte first
+ // see USBHID_Types.h for more info
USAGE_PAGE(1), 0x01, // Generic Desktop
LOGICAL_MINIMUM(1), 0x00, // Logical_Minimum (0)
USAGE(1), 0x04, // Usage (Joystick)
COLLECTION(1), 0x01, // Application
- USAGE_PAGE(1), 0x02, // Simulation Controls
- USAGE(1), 0xBB, // Throttle
- USAGE(1), 0xBA, // Rudder
- LOGICAL_MINIMUM(1), 0x81, // -127
- LOGICAL_MAXIMUM(1), 0x7f, // 127
- REPORT_SIZE(1), 0x08,
- REPORT_COUNT(1), 0x02,
- INPUT(1), 0x02, // Data, Variable, Absolute
+
+// 6 Axes of Joystick
USAGE_PAGE(1), 0x01, // Generic Desktop
USAGE(1), 0x01, // Usage (Pointer)
COLLECTION(1), 0x00, // Physical
USAGE(1), 0x30, // X
USAGE(1), 0x31, // Y
-//// 8 bit values
-// LOGICAL_MINIMUM(1), 0x81, // -127
-// LOGICAL_MAXIMUM(1), 0x7f, // 127
-// REPORT_SIZE(1), 0x08,
-// REPORT_COUNT(1), 0x02,
-// INPUT(1), 0x02, // Data, Variable, Absolute
-
-// 16 bit values
- LOGICAL_MINIMUM(1), 0x00, // 0
- LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
- REPORT_SIZE(1), 0x10,
- REPORT_COUNT(1), 0x02,
+ USAGE(1), 0x32, // Z
+ USAGE(1), 0x33, // RX
+ USAGE(1), 0x34, // RY
+ USAGE(1), 0x35, // RZ
+ LOGICAL_MINIMUM(2), 0xff, 0xff, // -32768 (using 2's complement)
+ LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 (0x7fff, least significant byte first)
+ REPORT_SIZE(1), 0x10, // REPORT_SIZE describes the number of bits in this element (16, in this case)
+ REPORT_COUNT(1), 0x06,
INPUT(1), 0x02, // Data, Variable, Absolute
-
END_COLLECTION(0),
#if (HAT4 == 1)
--- a/USBJoystick.h Fri Jan 13 05:37:07 2017 +0000
+++ b/USBJoystick.h Fri Jan 13 07:09:28 2017 +0000
@@ -109,30 +109,14 @@
* @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral)
* @returns true if there is no error, false otherwise
*/
- bool update(int16_t t, int16_t r, int16_t x, int16_t y, uint32_t buttons, uint8_t hat);
+ bool update(int16_t x, int16_t y, int16_t z, int16_t rx, int16_t ry, int16_t rz, uint32_t buttons, uint8_t hat);
/**
* Write state of the joystick
*
* @returns true if there is no error, false otherwise
*/
- bool update();
-
- /**
- * Move the throttle position
- *
- * @param t throttle position
- * @returns true if there is no error, false otherwise
- */
- bool throttle(int16_t t);
-
- /**
- * Move the rudder position
- *
- * @param r rudder position
- * @returns true if there is no error, false otherwise
- */
- bool rudder(int16_t r);
+ bool update();
/**
* Move the cursor to (x, y)
@@ -141,7 +125,7 @@
* @param y-axis position
* @returns true if there is no error, false otherwise
*/
- bool move(int16_t x, int16_t y);
+ bool move(int16_t x, int16_t y, int16_t z, int16_t rx, int16_t ry, int16_t rz);
/**
* Press one or several buttons
@@ -167,13 +151,15 @@
virtual uint8_t * reportDesc();
private:
- int16_t _t;
- int16_t _r;
- int16_t _x;
- int16_t _y;
+ int16_t _x;
+ int16_t _y;
+ int16_t _z;
+ int16_t _rx;
+ int16_t _ry;
+ int16_t _rz;
uint32_t _buttons;
uint8_t _hat;
-
+
void _init();
};
