Skip to content

Commit 484611a

Browse files
committed
Allow reducing gamepad buttons to 16 and removing Z-Axis easily
This patch does nothing by default, but adds a few configuration options for the above into HID-Settings.h.
1 parent b16be57 commit 484611a

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

src/HID-APIs/GamepadAPI.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@ THE SOFTWARE.
3838
#define GAMEPAD_DPAD_LEFT 7
3939
#define GAMEPAD_DPAD_UP_LEFT 8
4040

41+
#ifdef HID_ENABLE_32BUTTONS
42+
typedef uint32_t HID_Buttons_t;
43+
#else
44+
typedef uint16_t HID_Buttons_t;
45+
#endif
4146

4247
typedef union ATTRIBUTE_PACKED {
4348
// 32 Buttons, 6 Axis, 2 D-Pads
4449
uint8_t whole8[0];
4550
uint16_t whole16[0];
4651
uint32_t whole32[0];
47-
uint32_t buttons;
52+
HID_Buttons_t buttons;
4853

4954
struct ATTRIBUTE_PACKED {
5055
uint8_t button1 : 1;
@@ -65,6 +70,7 @@ typedef union ATTRIBUTE_PACKED {
6570
uint8_t button15 : 1;
6671
uint8_t button16 : 1;
6772

73+
#ifdef HID_ENABLE_32BUTTONS
6874
uint8_t button17 : 1;
6975
uint8_t button18 : 1;
7076
uint8_t button19 : 1;
@@ -82,16 +88,18 @@ typedef union ATTRIBUTE_PACKED {
8288
uint8_t button30 : 1;
8389
uint8_t button31 : 1;
8490
uint8_t button32 : 1;
91+
#endif
8592

8693
int16_t xAxis;
8794
int16_t yAxis;
8895

8996
int16_t rxAxis;
9097
int16_t ryAxis;
9198

99+
#ifdef HID_ENABLE_ZAXIS
92100
int8_t zAxis;
93101
int8_t rzAxis;
94-
102+
#endif
95103
uint8_t dPad1 : 4;
96104
uint8_t dPad2 : 4;
97105
};
@@ -108,13 +116,17 @@ class GamepadAPI{
108116
inline void release(uint8_t b);
109117
inline void releaseAll(void);
110118

111-
inline void buttons(uint32_t b);
119+
inline void buttons(HID_Buttons_t b);
112120
inline void xAxis(int16_t a);
113121
inline void yAxis(int16_t a);
122+
#ifdef HID_ENABLE_ZAXIS
114123
inline void zAxis(int8_t a);
124+
#endif
115125
inline void rxAxis(int16_t a);
116126
inline void ryAxis(int16_t a);
127+
#ifdef HID_ENABLE_ZAXIS
117128
inline void rzAxis(int8_t a);
129+
#endif
118130
inline void dPad1(int8_t d);
119131
inline void dPad2(int8_t d);
120132

src/HID-APIs/GamepadAPI.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ void GamepadAPI::write(void){
4545

4646

4747
void GamepadAPI::press(uint8_t b){
48-
_report.buttons |= (uint32_t)1 << (b - 1);
48+
_report.buttons |= (HID_Buttons_t)1 << (b - 1);
4949
}
5050

5151

5252
void GamepadAPI::release(uint8_t b){
53-
_report.buttons &= ~((uint32_t)1 << (b - 1));
53+
_report.buttons &= ~((HID_Buttons_t)1 << (b - 1));
5454
}
5555

5656

5757
void GamepadAPI::releaseAll(void){
5858
memset(&_report, 0x00, sizeof(_report));
5959
}
6060

61-
void GamepadAPI::buttons(uint32_t b){
61+
void GamepadAPI::buttons(HID_Buttons_t b){
6262
_report.buttons = b;
6363
}
6464

@@ -72,11 +72,11 @@ void GamepadAPI::yAxis(int16_t a){
7272
_report.yAxis = a;
7373
}
7474

75-
75+
#ifdef HID_ENABLE_ZAXIS
7676
void GamepadAPI::zAxis(int8_t a){
7777
_report.zAxis = a;
7878
}
79-
79+
#endif
8080

8181
void GamepadAPI::rxAxis(int16_t a){
8282
_report.rxAxis = a;
@@ -87,11 +87,11 @@ void GamepadAPI::ryAxis(int16_t a){
8787
_report.ryAxis = a;
8888
}
8989

90-
90+
#ifdef HID_ENABLE_ZAXIS
9191
void GamepadAPI::rzAxis(int8_t a){
9292
_report.rzAxis = a;
9393
}
94-
94+
#endif
9595

9696
void GamepadAPI::dPad1(int8_t d){
9797
_report.dPad1 = d;

src/HID-Settings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ THE SOFTWARE.
2828
// Settings
2929
//================================================================================
3030

31+
// Undefine this if 16 buttons are enough for your Gamepads
32+
#define HID_ENABLE_32BUTTONS
33+
34+
// Undefine this is your Gamepads don't need a Z Axis
35+
#define HID_ENABLE_ZAXIS
3136

3237
#define HID_REPORTID_NONE 0
3338

src/MultiReport/Gamepad.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,27 @@ THE SOFTWARE.
2525

2626

2727
static const uint8_t _hidMultiReportDescriptorGamepad[] PROGMEM = {
28-
/* Gamepad with 32 buttons and 6 axis*/
28+
/* Gamepad with 16/32 buttons and 6 axis*/
2929
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
3030
0x09, 0x04, /* USAGE (Joystick) */
3131
0xa1, 0x01, /* COLLECTION (Application) */
3232
0x85, HID_REPORTID_GAMEPAD, /* REPORT_ID */
33-
/* 32 Buttons */
33+
/* 16/32 Buttons */
3434
0x05, 0x09, /* USAGE_PAGE (Button) */
3535
0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
36+
#ifdef HID_ENABLE_32BUTTONS
3637
0x29, 0x20, /* USAGE_MAXIMUM (Button 32) */
38+
#else
39+
0x29, 0x10, /* USAGE_MAXIMUM (Button 16) */
40+
#endif
3741
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
3842
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
3943
0x75, 0x01, /* REPORT_SIZE (1) */
44+
#ifdef HID_ENABLE_32BUTTONS
4045
0x95, 0x20, /* REPORT_COUNT (32) */
46+
#else
47+
0x95, 0x10, /* REPORT_COUNT (16) */
48+
#endif
4149
0x81, 0x02, /* INPUT (Data,Var,Abs) */
4250
/* 4 16bit Axis */
4351
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
@@ -51,6 +59,7 @@ static const uint8_t _hidMultiReportDescriptorGamepad[] PROGMEM = {
5159
0x75, 0x10, /* REPORT_SIZE (16) */
5260
0x95, 0x04, /* REPORT_COUNT (4) */
5361
0x81, 0x02, /* INPUT (Data,Var,Abs) */
62+
#ifdef HID_ENABLE_ZAXIS
5463
/* 2 8bit Axis */
5564
0x09, 0x32, /* USAGE (Z) */
5665
0x09, 0x35, /* USAGE (Rz) */
@@ -59,6 +68,7 @@ static const uint8_t _hidMultiReportDescriptorGamepad[] PROGMEM = {
5968
0x75, 0x08, /* REPORT_SIZE (8) */
6069
0x95, 0x02, /* REPORT_COUNT (2) */
6170
0x81, 0x02, /* INPUT (Data,Var,Abs) */
71+
#endif
6272
0xc0, /* END_COLLECTION */
6373
/* 2 Hat Switches */
6474
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */

src/SingleReport/SingleGamepad.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,26 @@ THE SOFTWARE.
2424
#include "SingleGamepad.h"
2525

2626
static const uint8_t _hidReportDescriptorGamepad[] PROGMEM = {
27-
/* Gamepad with 32 buttons and 6 axis*/
27+
/* Gamepad with 16/32 buttons and 6 axis*/
2828
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
2929
0x09, 0x04, /* USAGE (Joystick) */
3030
0xa1, 0x01, /* COLLECTION (Application) */
31-
/* 32 Buttons */
31+
/* 16/32 Buttons */
3232
0x05, 0x09, /* USAGE_PAGE (Button) */
3333
0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
34+
#ifdef HID_ENABLE_32BUTTONS
3435
0x29, 0x20, /* USAGE_MAXIMUM (Button 32) */
36+
#else
37+
0x29, 0x10, /* USAGE_MAXIMUM (Button 16) */
38+
#endif
3539
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
3640
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
3741
0x75, 0x01, /* REPORT_SIZE (1) */
42+
#ifdef HID_ENABLE_32BUTTONS
3843
0x95, 0x20, /* REPORT_COUNT (32) */
44+
#else
45+
0x95, 0x10, /* REPORT_COUNT (32) */
46+
#endif
3947
0x81, 0x02, /* INPUT (Data,Var,Abs) */
4048
/* 4 16bit Axis */
4149
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
@@ -49,6 +57,7 @@ static const uint8_t _hidReportDescriptorGamepad[] PROGMEM = {
4957
0x75, 0x10, /* REPORT_SIZE (16) */
5058
0x95, 0x04, /* REPORT_COUNT (4) */
5159
0x81, 0x02, /* INPUT (Data,Var,Abs) */
60+
#ifdef HID_ENABLE_ZAXIS
5261
/* 2 8bit Axis */
5362
0x09, 0x32, /* USAGE (Z) */
5463
0x09, 0x35, /* USAGE (Rz) */
@@ -57,6 +66,7 @@ static const uint8_t _hidReportDescriptorGamepad[] PROGMEM = {
5766
0x75, 0x08, /* REPORT_SIZE (8) */
5867
0x95, 0x02, /* REPORT_COUNT (2) */
5968
0x81, 0x02, /* INPUT (Data,Var,Abs) */
69+
#endif
6070
0xc0, /* END_COLLECTION */
6171
/* 2 Hat Switches */
6272
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */

0 commit comments

Comments
 (0)