-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Gamepad.h
136 lines (116 loc) · 4.76 KB
/
Gamepad.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/************************************************************************************
* Gamepad
*
* Base class for implementing a gamepad/controller.
*
* Depends on the Arduino EEPROM library.
************************************************************************************/
#ifndef _GAMEPAD_
#define _GAMEPAD_
#include "USB_HID.h"
#include "GamepadState.h"
#include "GamepadDebouncer.h"
#include "DS3Report.h"
#include "SwitchReport.h"
#include "XInputReport.h"
#include "GamepadStorage.h"
#ifndef DEBOUNCE_MILLIS
#define DEBOUNCE_MILLIS 0
#endif
class Gamepad {
public:
Gamepad() {
#if DEBOUNCE_MILLIS > 0
for (int i = 0; i < GAMEPAD_DEBOUNCE_BUTTON_COUNT; i++) {
debouncers[i].setGamepadState(currentState);
}
#endif
}
GamepadState previousState;
GamepadState currentState;
DpadMode dpadMode = DpadMode::DIGITAL;
InputMode inputMode = InputMode::XINPUT;
SOCDMode socdMode = SOCDMode::UP_PRIORITY;
bool hasAnalogTriggers = false;
bool hasLeftAnalogStick = false;
bool hasRightAnalogStick = false;
/**
* Load the saved configuration from persitent storage
*/
virtual void load();
/**
* Perform pin setup and any other initialization the board requires
*/
virtual void setup();
/**
* Retrieve the raw inputs and save to the current GamepadState
*/
virtual void read();
/**
* Checks and executes any hotkey being pressed
*/
virtual HotkeyAction hotkey();
/**
* Run debouncing algorithm against raw inputs in the current GamepadState
*/
void debounce();
/**
* Process the raw inputs to fill the current GamepadState
*/
void process();
/**
* Checks persistent storage and hotkeys to determine the input mode to use
*/
void configureInputMode();
/**
* Send the latest GamepadState to the USB Host.
*/
void update();
/**
* Returns if the function button/hotkey is pressed, override in derived board class
*/
virtual bool isDpadHotkeyPressed();
/**
* Returns if the function button/hotkey is pressed, override in derived board class
*/
virtual bool isSOCDHotkeyPressed();
DS3Report getDS3Report();
SwitchInputReport getSwitchReport();
XInputReport getXInputReport();
__attribute__((always_inline)) inline bool isDpadUpPressed() { return (currentState.dpadInputs & GAMEPAD_DPAD_UP) > 0; }
__attribute__((always_inline)) inline bool isDpadDownPressed() { return (currentState.dpadInputs & GAMEPAD_DPAD_DOWN) > 0; }
__attribute__((always_inline)) inline bool isDpadLeftPressed() { return (currentState.dpadInputs & GAMEPAD_DPAD_LEFT) > 0; }
__attribute__((always_inline)) inline bool isDpadRightPressed() { return (currentState.dpadInputs & GAMEPAD_DPAD_RIGHT) > 0; }
__attribute__((always_inline)) inline bool isLeftStickButtonPressed() { return (currentState.buttonInputs & GAMEPAD_BUTTON_11) > 0; }
__attribute__((always_inline)) inline bool isRightStickButtonPressed() { return (currentState.buttonInputs & GAMEPAD_BUTTON_12) > 0; }
__attribute__((always_inline)) inline bool isSelectPressed() { return (currentState.buttonInputs & GAMEPAD_BUTTON_09) > 0; }
__attribute__((always_inline)) inline bool isStartPressed() { return (currentState.buttonInputs & GAMEPAD_BUTTON_10) > 0; }
protected:
#if DEBOUNCE_MILLIS > 0
GamepadDebouncer debouncers[GAMEPAD_ACTIVE_BUTTON_COUNT + 4] = {
GamepadDebouncer(GAMEPAD_DPAD_UP, DEBOUNCE_MILLIS, true),
GamepadDebouncer(GAMEPAD_DPAD_DOWN, DEBOUNCE_MILLIS, true),
GamepadDebouncer(GAMEPAD_DPAD_LEFT, DEBOUNCE_MILLIS, true),
GamepadDebouncer(GAMEPAD_DPAD_RIGHT, DEBOUNCE_MILLIS, true),
GamepadDebouncer(GAMEPAD_BUTTON_01, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_02, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_03, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_04, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_05, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_06, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_07, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_08, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_09, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_10, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_11, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_12, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_13, DEBOUNCE_MILLIS, false),
GamepadDebouncer(GAMEPAD_BUTTON_14, DEBOUNCE_MILLIS, false),
// GamepadDebouncer(GAMEPAD_BUTTON_15, DEBOUNCE_MILLIS, false), // unused
// GamepadDebouncer(GAMEPAD_BUTTON_16, DEBOUNCE_MILLIS, false), // unused
}; // Buttons + dpad
#else
GamepadDebouncer debouncers[0];
#endif
};
#endif