![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
USB HID Device that emulates a Gamecontroller
Revision 1:018979eb4641, committed 2022-08-25
- Comitter:
- rengro01
- Date:
- Thu Aug 25 07:22:28 2022 +0000
- Parent:
- 0:5037d4be5b6d
- Commit message:
- Final Arcade Gamepad
Changed in this revision
diff -r 5037d4be5b6d -r 018979eb4641 USBDevice.lib --- a/USBDevice.lib Sat Jan 07 21:03:54 2012 +0000 +++ b/USBDevice.lib Thu Aug 25 07:22:28 2022 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/samux/code/USBDevice/#5e24508aa46e +http://mbed.org/users/samux/code/USBDevice/#140cdf8e2d60
diff -r 5037d4be5b6d -r 018979eb4641 USBJoystick.cpp --- a/USBJoystick.cpp Sat Jan 07 21:03:54 2012 +0000 +++ b/USBJoystick.cpp Thu Aug 25 07:22:28 2022 +0000 @@ -1,168 +1,79 @@ -/* Copyright (c) 2010-2011 mbed.org, MIT License -* Modified Mouse code for Joystick - WH 2012 -* -* Permission is hereby granted, free of charge, to any person obtaining a copy of this software -* and associated documentation files (the "Software"), to deal in the Software without -* restriction, including without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all copies or -* substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING -* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ - #include "stdint.h" #include "USBJoystick.h" -bool USBJoystick::update(int16_t t, int16_t r, int16_t x, int16_t y, uint8_t button, uint8_t hat) { - HID_REPORT report; - _t = t; - _r = r; - _x = x; - _y = y; - _button = button; - _hat = hat; - +bool USBJoystick::update(int16_t x_l, int16_t y_l, uint8_t buttons_l, int16_t x_r, int16_t y_r, uint8_t buttons_r, uint8_t hat) +{ + HID_REPORT report; // Fill the report according to the Joystick Descriptor - report.data[0] = _t & 0xff; - report.data[1] = _r & 0xff; - report.data[2] = _x & 0xff; - report.data[3] = _y & 0xff; - report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ; - report.length = 5; - - return send(&report); -} - -bool USBJoystick::update() { - HID_REPORT report; - - // Fill the report according to the Joystick Descriptor - report.data[0] = _t & 0xff; - report.data[1] = _r & 0xff; - report.data[2] = _x & 0xff; - report.data[3] = _y & 0xff; - report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ; - report.length = 5; + report.data[0] = x_l & 0xff; + report.data[1] = y_l & 0xff; + report.data[2] = x_r & 0xff; + report.data[3] = y_r & 0xff; + report.data[4] = hat & 0xff; + report.data[5] = buttons_l; + report.data[6] = buttons_r; + report.length = 7; 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) { - _x = x; - _y = y; - return update(); -} +uint8_t * USBJoystick::reportDesc() +{ + static uint8_t reportDescriptor[] = + { + USAGE_PAGE(1), 0x01, // Generic Desktop + LOGICAL_MINIMUM(1), 0x00, // Logical_Minimum (0) + USAGE(1), 0x04, // Usage (Joystick) -bool USBJoystick::button(uint8_t button) { - _button = button; - return update(); -} - -bool USBJoystick::hat(uint8_t hat) { - _hat = hat; - return update(); -} + COLLECTION(1), 0x01, // Application + USAGE_PAGE(1), 0x01, // Generic Desktop + USAGE(1), 0x01, // Usage (Pointer) + COLLECTION(1), 0x00, // Physical + USAGE(1), 0x30, // X + USAGE(1), 0x31, // Y + USAGE(1), 0x32, // Z + USAGE(1), 0x35, // Rz + LOGICAL_MINIMUM(1), 0x81, // -127 + LOGICAL_MAXIMUM(1), 0x7f, // 127 + REPORT_SIZE(1), 0x08, + REPORT_COUNT(1), 0x04, + INPUT(1), 0x02, // Data, Variable, Absolute + END_COLLECTION(0), - -void USBJoystick::_init() { + USAGE(1), 0x39, // Hat + LOGICAL_MINIMUM(1), 0x00, // 0 + LOGICAL_MAXIMUM(1), 0x07, // 7 + REPORT_SIZE(1), 0x04, // The hat has only 4 bits, which cause an unalignment + REPORT_COUNT(1), 0x01, + PHYSICAL_MINIMUM(1), 0x00, + PHYSICAL_MAXIMUM(1), 0x7f, + UNIT(1), 0x14, + INPUT(1), 0x02, // Data, Variable, Absolute - _t = -127; - _r = -127; - _x = 0; - _y = 0; - _button = 0x00; - _hat = 0x00; -} + USAGE(1), 0x40, // Fake HAT + LOGICAL_MINIMUM(1), 0x00, // 0 + LOGICAL_MAXIMUM(1), 0x07, // 7 + REPORT_SIZE(1), 0x04, // Just have an unused fake hat to realign the buttons below + REPORT_COUNT(1), 0x01, + PHYSICAL_MINIMUM(1), 0x00, + PHYSICAL_MAXIMUM(1), 0x7f, + UNIT(1), 0x14, + INPUT(1), 0x02, // Data, Variable, Absolute -uint8_t * USBJoystick::reportDesc() { - static uint8_t reportDescriptor[] = { - - 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 - 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, -// INPUT(1), 0x02, // Data, Variable, Absolute + USAGE_PAGE(1), 0x09, // Buttons + USAGE_MINIMUM(1), 0x01, // 1 + USAGE_MAXIMUM(1), 0x10, // 16 buttons + LOGICAL_MINIMUM(1), 0x00, // 0 + LOGICAL_MAXIMUM(1), 0x01, // 1 + REPORT_SIZE(1), 0x01, + REPORT_COUNT(1), 0x10, // 16 buttons + UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0) + UNIT(1), 0x00, // Unit (None) + INPUT(1), 0x02, // Data, Variable, Absolute + END_COLLECTION(0) + }; - END_COLLECTION(0), -// 4 Position Hat Switch -// USAGE(1), 0x39, // Usage (Hat switch) -// LOGICAL_MINIMUM(1), 0x00, // 0 -// LOGICAL_MAXIMUM(1), 0x03, // 3 -// PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0) -// PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270) -// UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos) -// REPORT_SIZE(1), 0x04, -// REPORT_COUNT(1), 0x01, -// INPUT(1), 0x02, // Data, Variable, Absolute -// 8 Position Hat Switch - USAGE(1), 0x39, // Usage (Hat switch) - LOGICAL_MINIMUM(1), 0x00, // 0 - LOGICAL_MAXIMUM(1), 0x07, // 7 - PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0) - PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315) - UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos) - REPORT_SIZE(1), 0x04, - REPORT_COUNT(1), 0x01, - INPUT(1), 0x02, // Data, Variable, Absolute -// - USAGE_PAGE(1), 0x09, // Buttons - USAGE_MINIMUM(1), 0x01, // 1 - USAGE_MAXIMUM(1), 0x04, // 4 - LOGICAL_MINIMUM(1), 0x00, // 0 - LOGICAL_MAXIMUM(1), 0x01, // 1 - REPORT_SIZE(1), 0x01, - REPORT_COUNT(1), 0x04, - UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0) - UNIT(1), 0x00, // Unit (None) - INPUT(1), 0x02, // Data, Variable, Absolute - END_COLLECTION(0) - - }; - - reportLength = sizeof(reportDescriptor); - return reportDescriptor; + reportLength = sizeof(reportDescriptor); + return reportDescriptor; } - -
diff -r 5037d4be5b6d -r 018979eb4641 USBJoystick.h --- a/USBJoystick.h Sat Jan 07 21:03:54 2012 +0000 +++ b/USBJoystick.h Thu Aug 25 07:22:28 2022 +0000 @@ -1,8 +1,3 @@ -/* USBJoystick.h */ -/* USB device example: Joystick*/ -/* Copyright (c) 2011 ARM Limited. All rights reserved. */ -/* Modified Mouse code for Joystick - WH 2012 */ - #ifndef USBJOYSTICK_H #define USBJOYSTICK_H @@ -10,201 +5,17 @@ #define REPORT_ID_JOYSTICK 4 -/* Common usage */ -enum JOY_BUTTON { - JOY_B0 = 1, - JOY_B1 = 2, - JOY_B2 = 4, - JOY_B3 = 8, -}; - -#if(0) -enum JOY_HAT { - JOY_HAT_UP = 0, - JOY_HAT_RIGHT = 1, - JOY_HAT_DOWN = 2, - JOY_HAT_LEFT = 3, - JOY_HAT_NEUTRAL = 4, -}; -#else -enum JOY_HAT { - JOY_HAT_UP = 0, - JOY_HAT_UP_RIGHT = 1, - JOY_HAT_RIGHT = 2, - JOY_HAT_RIGHT_DOWN = 3, - JOY_HAT_DOWN = 4, - JOY_HAT_DOWN_LEFT = 5, - JOY_HAT_LEFT = 6, - JOY_HAT_LEFT_UP = 7, - JOY_HAT_NEUTRAL = 8, -}; -#endif - -/* X, Y and T limits */ -/* These values do not directly map to screen pixels */ -/* Zero may be interpreted as meaning 'no movement' */ -#define JX_MIN_ABS (-127) /*!< The maximum value that we can move to the left on the x-axis */ -#define JY_MIN_ABS (-127) /*!< The maximum value that we can move up on the y-axis */ -#define JT_MIN_ABS (-127) /*!< The minimum value for the throttle */ -#define JX_MAX_ABS (127) /*!< The maximum value that we can move to the right on the x-axis */ -#define JY_MAX_ABS (127) /*!< The maximum value that we can move down on the y-axis */ -#define JT_MAX_ABS (127) /*!< The maximum value for the throttle */ - -/** - * - * USBJoystick example - * @code - * #include "mbed.h" - * #include "USBJoystick.h" - * - * USBJoystick joystick; - * - * int main(void) - * { - * while (1) - * { - * joystick.move(20, 0); - * wait(0.5); - * } - * } - * - * @endcode - * - * - * @code - * #include "mbed.h" - * #include "USBJoystick.h" - * #include <math.h> - * - * USBJoystick joystick; - * - * int main(void) - * { - * int16_t i = 0; - * int16_t throttle = 0; - * int16_t rudder = 0; - * int16_t x = 0; - * int16_t y = 0; - * int32_t radius = 120; - * int32_t angle = 0; - * int8_t button = 0; - * int8_t hat = 0; - * - * while (1) { - * // Basic Joystick - * throttle = (i >> 8) & 0xFF; // value -127 .. 128 - * rudder = (i >> 8) & 0xFF; // value -127 .. 128 - * button = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button - * hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral - * i++; - * - * x = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128 - * y = sin((double)angle*3.14/180.0)*radius; // value -127 .. 128 - * angle += 3; - * - * joystick.update(throttle, rudder, x, y, button, hat); - * - * wait(0.001); - * } - * } - * @endcode - */ - - class USBJoystick: public USBHID { public: - - /** - * Constructor - * - * @param vendor_id Your vendor_id (default: 0x1234) - * @param product_id Your product_id (default: 0x0002) - * @param product_release Your product_release (default: 0x0001) - */ - USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0100, uint16_t product_release = 0x0001): - USBHID(0, 0, vendor_id, product_id, product_release, false) - { - _init(); - connect(); - }; - - /** - * Write a state of the mouse - * - * @param t throttle position - * @param r rudder position - * @param x x-axis position - * @param y y-axis position - * @param buttons buttons state - * @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, uint8_t buttons, uint8_t hat); - - /** - * Write a state of the mouse - * - * @returns true if there is no error, false otherwise - */ - bool update(); + USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0100, uint16_t product_release = 0x0001): + USBHID(0, 0, vendor_id, product_id, product_release, false) + { + connect(); + }; - /** - * 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(int16_t x_l, int16_t y_l, uint8_t buttons_l, int16_t x_r, int16_t y_r, uint8_t buttons_r, uint8_t hat); - /** - * Move the cursor to (x, y) - * - * @param x-axis position - * @param y-axis position - * @returns true if there is no error, false otherwise - */ - bool move(int16_t x, int16_t y); - - /** - * Press one or several buttons - * - * @param button button state - * @returns true if there is no error, false otherwise - */ - bool button(uint8_t button); - - /** - * Press hat - * - * @param hat hat state - * @returns true if there is no error, false otherwise - */ - bool hat(uint8_t hat); - - /* - * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. - * - * @returns pointer to the report descriptor - */ virtual uint8_t * reportDesc(); - - private: - int8_t _t; - int8_t _r; - int8_t _x; - int8_t _y; - uint8_t _button; - uint8_t _hat; - - void _init(); }; #endif \ No newline at end of file
diff -r 5037d4be5b6d -r 018979eb4641 main.cpp --- a/main.cpp Sat Jan 07 21:03:54 2012 +0000 +++ b/main.cpp Thu Aug 25 07:22:28 2022 +0000 @@ -2,64 +2,91 @@ #include "USBMouse.h" #include "USBJoystick.h" -//USBMouse mouse; -USBJoystick joystick; +USBJoystick joystick(0x046d, 0xc216); + +// keys range: 288-303 0x120-0x134 -// Variables for Heartbeat and Status monitoring -DigitalOut myled1(LED1); -DigitalOut myled2(LED2); -DigitalOut myled3(LED3); -DigitalOut heartbeatLED(LED4); - -Ticker heartbeat; -Serial pc(USBTX, USBRX); // tx, rx +DigitalIn l_r(p17); +DigitalIn l_l(p18); +DigitalIn l_u(p20); +DigitalIn l_d(p19); +DigitalIn l_b1(p16); +DigitalIn l_b2(p10); +DigitalIn l_b3(p6); +DigitalIn l_b4(p7); +DigitalIn l_b5(p9); +DigitalIn l_b6(p5); -// Heartbeat monitor -void pulse() { - heartbeatLED = !heartbeatLED; -} +DigitalIn r_r(p22); +DigitalIn r_l(p21); +DigitalIn r_u(p23); +DigitalIn r_d(p24); +DigitalIn r_b1(p11); +DigitalIn r_b2(p29); +DigitalIn r_b3(p13); +DigitalIn r_b4(p12); +DigitalIn r_b5(p15); +DigitalIn r_b6(p14); -void heartbeat_start() { - heartbeat.attach(&pulse, 0.5); -} - -void heartbeat_stop() { - heartbeat.detach(); -} - +DigitalIn c_l(p25); +DigitalIn c_r(p26); +DigitalIn c_u(p27); +DigitalIn c_d(p28); int main() { - int16_t i = 0; - int16_t throttle = 0; - int16_t rudder = 0; - int16_t x = 0; - int16_t y = 0; - int32_t radius = 120; - int32_t angle = 0; - int8_t button = 0; - int8_t hat = 0; - - pc.printf("Hello World!\n\r"); + int16_t x_l = 0; + int16_t y_l = 0; + uint8_t buttons_l = 0; + int16_t x_r = 0; + int16_t y_r = 0; + uint8_t buttons_r = 0; + uint8_t hat = 0; - heartbeat_start(); +/* +120 a 121 x 122 y 123 b 124 l1 125 r1 126 l2 127 r2 +128 slt 129 srt 12a tl 12b tr +00 x 01 y +02 z 03 rz +10 hx 11 hy +*/ while (1) { - // Basic Joystick - throttle = (i >> 8) & 0xFF; // value -127 .. 128 - rudder = (i >> 8) & 0xFF; // value -127 .. 128 - button = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button -// hat = (i >> 8) & 0x03; // value 0, 1, 2, 3 or 4 for neutral - hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral - i++; - - x = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128 - y = sin((double)angle*3.14/180.0)*radius; // value -127 .. 128 - angle += 3; + x_l = 0; + y_l = 0; + if(l_l) x_l = -127; + if(l_r) x_l = 127; + if(l_d) y_l = 127; + if(l_u) y_l = -127; + buttons_l = l_b6 | l_b4 << 1 | l_b3 << 2 | l_b5 << 3 | l_b1 << 4 | l_b2 << 5 | r_b5 << 6 | r_b6 << 7; + + x_r = 0; + y_r = 0; + if(r_l) x_r = -127; + if(r_r) x_r = 127; + if(r_d) y_r = 127; + if(r_u) y_r = -127; + buttons_r = r_b1 | r_b2 << 1 | r_b3 << 2 | r_b4 << 3 | 0 << 4 | 0 << 5 | 0 << 6 | 0 << 7; - joystick.update(throttle, rudder, x, y, button, hat); +#define HATSWITCH_UP 0x00 +#define HATSWITCH_UPRIGHT 0x01 +#define HATSWITCH_RIGHT 0x02 +#define HATSWITCH_DOWNRIGHT 0x03 +#define HATSWITCH_DOWN 0x04 +#define HATSWITCH_DOWNLEFT 0x05 +#define HATSWITCH_LEFT 0x06 +#define HATSWITCH_UPLEFT 0x07 +#define HATSWITCH_NONE 0x0F + hat = HATSWITCH_NONE; + + if(c_u) { + if(c_l) {hat = HATSWITCH_UPLEFT;} else if(c_r) {hat = HATSWITCH_UPRIGHT;} else {hat = HATSWITCH_UP;} + } else if(c_d) { + if(c_l) {hat = HATSWITCH_DOWNLEFT;} else if(c_r) {hat = HATSWITCH_DOWNRIGHT;} else {hat = HATSWITCH_DOWN;} + } + else if(c_l) {hat = HATSWITCH_LEFT;} else if(c_r) {hat = HATSWITCH_RIGHT;} + + joystick.update(x_l, y_l, buttons_l, x_r, y_r, buttons_r, hat); wait(0.001); } - - pc.printf("Bye World!\n\r"); } \ No newline at end of file
diff -r 5037d4be5b6d -r 018979eb4641 mbed.bld --- a/mbed.bld Sat Jan 07 21:03:54 2012 +0000 +++ b/mbed.bld Thu Aug 25 07:22:28 2022 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/7110ebee3484 +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file