-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ino
215 lines (194 loc) · 7.6 KB
/
code.ino
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Conventions followed based on a mix of
// Xbox Core Wireless Controller/Logitech F310 Gamepad: Face Buttons Naming Convention and HID outputs in the Windows Game Controllers tool
// Nintendo Switch Controller: Bumpers Naming Convention
// Sony DualShock 4 Controller: Triggers and Stick Buttons Naming Convention
#include <Joystick.h>
#define DEBUG false
#define USE_HAT true
#define NANO_ANALOG true // Becuase Leonardo Board doesn't have 8 Analog pins like the Nano 33 BLE, the two right stick pins were changed.
#define MAP_ANALOG (!DEBUG || true)
#define USE_DEADZONE true
#define SEPARATE_TRIGGERS false
#if USE_HAT
// Buttons
const int NUM_BUTTONS = 10;
int pinToButton [NUM_BUTTONS] = { 0, 2, 4, 3, 12, 11, 6, 5, 13, 1};
String buttonName [NUM_BUTTONS] = {"A", "B", "X", "Y", "L", "R", "Select", "Start", "L3", "R3"};
int lastButtonState[NUM_BUTTONS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// D-Pad
const int NUM_HATS = 1;
const int HAT_INDEX = 0;
const int NUM_HAT_BUTTONS = 4;
int pinToHat [NUM_HAT_BUTTONS] = { 7, 8, 9, 10};
String hatName [NUM_HAT_BUTTONS] = {"Up", "Right", "Down", "Left"};
int lastHatState[NUM_HAT_BUTTONS] = {0, 0, 0, 0};
int lastHatAngle = -1;
#else
// Buttons
const int NUM_BUTTONS = 14;
int pinToButton [NUM_BUTTONS] = { 0, 2, 4, 3, 12, 11, 6, 5, 13, 1, 7, 8, 9, 10};
String buttonName [NUM_BUTTONS] = {"A", "B", "X", "Y", "L", "R", "Select", "Start", "L3", "R3", "Up", "Right", "Down", "Left"};
int lastButtonState[NUM_BUTTONS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const int NUM_HATS = 0;
#endif
// Analog Sticks and Triggers
// Using X axis and Y axis for left movement stick
// Using Rx axis and Ry axis for right stick
// Using either Z axis and Rz axis or Z axis alone for trigger switches
const int NUM_ANALOG = 6;
const int JOYSTICK_POS = 4;
#if NANO_ANALOG
int pinToAnalog [NUM_ANALOG] = { A4, A5, A6, A7, A2, A3};
#else
int pinToAnalog [NUM_ANALOG] = { A4, A5, A0, A1, A2, A3};
#endif
String analogName [NUM_ANALOG] = {"Left Stick X", "Left Stick Y", "Right Stick X", "Right Stick Y", "L2", "R2"};
int lastAnalogState[NUM_ANALOG] = {0, 0, 0, 0, 0, 0};
#define STICK_MIN 10
#define STICK_MAX 1020
#define STICK_DEADZONE 20
#define FSR_MIN 10
#define FSR_MAX 750
#define LIB_MIN 0
#define LIB_MAX 1023
int pinRange[NUM_ANALOG][2] = {{STICK_MIN, STICK_MAX}, {STICK_MIN, STICK_MAX}, {STICK_MIN, STICK_MAX}, {STICK_MIN, STICK_MAX}, {FSR_MIN, FSR_MAX}, {FSR_MIN, FSR_MAX}};
// Create the Joystick
#define HAS_X_AXIS true
#define HAS_Y_AXIS true
#define HAS_Z_AXIS true
#define HAS_RX_AXIS true
#define HAS_RY_AXIS true
#if SEPARATE_TRIGGERS
#define HAS_RZ_AXIS true
#else
#define HAS_RZ_AXIS false
#endif
#define HAS_RUDDER false
#define HAS_THROTTLE false
#define HAS_ACCELERATOR false
#define HAS_BRAKE false
#define HAS_STEERING false
Joystick_ joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
NUM_BUTTONS, NUM_HATS, HAS_X_AXIS, HAS_Y_AXIS, HAS_Z_AXIS, HAS_RX_AXIS, HAS_RY_AXIS, HAS_RZ_AXIS,
HAS_RUDDER, HAS_THROTTLE, HAS_ACCELERATOR, HAS_BRAKE, HAS_STEERING);
int constrain_map(long x, long oldRangeMin, long oldRangeMax, long newRangeMin, long newRangeMax){
int newX = constrain(x, oldRangeMin, oldRangeMax);
return map(newX, oldRangeMin, oldRangeMax, newRangeMin, newRangeMax);
}
void setup() {
#if DEBUG
// Initialise Serial Connection
Serial.begin(115200);
#endif
// Initialise Button Pins
for (int i = 0; i < NUM_BUTTONS; i++){
pinMode(pinToButton[i], INPUT_PULLUP);
}
#if USE_HAT
// Initialise Hat Pins
for (int i = 0; i < NUM_HAT_BUTTONS; i++){
pinMode(pinToHat[i], INPUT_PULLUP);
}
#endif
// Initialise Joystick Library
joystick.begin();
}
void loop() {
// Handle Buttons
for (int i = 0; i < NUM_BUTTONS; i++) {
int currentButtonState = !digitalRead(pinToButton[i]);
#if DEBUG
Serial.print(buttonName[i] + " = " + currentButtonState + " ");
#endif
if (currentButtonState != lastButtonState[i]) {
joystick.setButton(i, currentButtonState);
lastButtonState[i] = currentButtonState;
}
}
#if DEBUG
Serial.println();
#endif
#if USE_HAT
// Handle D-Pad
bool hatChanged = false;
for (int i = 0; i < NUM_HAT_BUTTONS; i++) {
int currentButtonState = !digitalRead(pinToHat[i]);
#if DEBUG
Serial.print(hatName[i] + " = "+ currentButtonState+ " ");
#endif
if (currentButtonState != lastHatState[i]) {
hatChanged = true;
lastHatState[i] = currentButtonState;
}
}
if(hatChanged){
int numPressed = lastHatState[0] + lastHatState[1] + lastHatState[2] + lastHatState[3];
// Only allow 1 button or 2 adjacent buttons to be pressed
if (numPressed < 1 || numPressed > 2) lastHatAngle = -1;
// Ordinals or Diagonals
else if(lastHatState[0] == 1 && lastHatState[1] == 1) lastHatAngle = 45; // NE
else if(lastHatState[1] == 1 && lastHatState[2] == 1) lastHatAngle = 135; // SE
else if(lastHatState[2] == 1 && lastHatState[3] == 1) lastHatAngle = 225; // SW
else if(lastHatState[3] == 1 && lastHatState[0] == 1) lastHatAngle = 315; // NW
// Invalid Combos: Up + Down, Left + Right
else if(lastHatState[0] == 1 && lastHatState[2] == 1) lastHatAngle = -1;
else if(lastHatState[1] == 1 && lastHatState[3] == 1) lastHatAngle = -1;
// Cardinals or Non-Diagonals
else if(lastHatState[0] == 1) lastHatAngle = 0; // N
else if(lastHatState[1] == 1) lastHatAngle = 90; // E
else if(lastHatState[2] == 1) lastHatAngle = 180; // S
else if(lastHatState[3] == 1) lastHatAngle = 270; // W
else lastHatAngle = -1;
joystick.setHatSwitch(HAT_INDEX, lastHatAngle);
}
#if DEBUG
Serial.print("Hat Angle = ");
Serial.println(lastHatAngle);
#endif
#endif
// Handle Analog
for (int i = 0; i < NUM_ANALOG; i++) {
int currentState = analogRead(pinToAnalog[i]);
#if MAP_ANALOG
currentState = constrain_map(currentState, pinRange[i][0], pinRange[i][1], LIB_MIN, LIB_MAX);
#endif
#if USE_DEADZONE
if(i < JOYSTICK_POS && ((LIB_MAX / 2 - STICK_DEADZONE) < currentState && currentState < (LIB_MAX / 2 + STICK_DEADZONE))) currentState = 512;
#endif
#if DEBUG
Serial.print(analogName[i] + " = " + currentState + " ");
#endif
lastAnalogState[i] = currentState;
}
#if DEBUG
Serial.println();
#endif
// Left Stick
joystick.setXAxis(lastAnalogState[0]);
joystick.setYAxis(lastAnalogState[1]);
// Right Stick
joystick.setRxAxis(lastAnalogState[2]);
joystick.setRyAxis(lastAnalogState[3]);
// Triggers
#if !MAP_ANALOG
// Since FSR range is not 0 - 1023 we need to always map if we haven't already
lastAnalogState[4] = constrain_map(lastAnalogState[4], FSR_MIN, FSR_MAX, LIB_MIN, LIB_MAX);
lastAnalogState[5] = constrain_map(lastAnalogState[5], FSR_MIN, FSR_MAX, LIB_MIN, LIB_MAX);
#endif
#if SEPARATE_TRIGGERS
joystick.setZAxis(lastAnalogState[4]);
joystick.setRzAxis(lastAnalogState[5]);
#else
lastAnalogState[4] = map(lastAnalogState[4], LIB_MIN, LIB_MAX, LIB_MIN, LIB_MAX / 2);
lastAnalogState[5] = map(lastAnalogState[5], LIB_MIN, LIB_MAX, LIB_MIN, LIB_MAX / 2);
int stickValue = map(512 + lastAnalogState[4] - lastAnalogState[5], LIB_MIN + 1, LIB_MAX, LIB_MIN, LIB_MAX);
joystick.setZAxis(stickValue);
#endif
#if DEBUG
// Delay to read Serial console
delay(1000);
#else
// Delay for responsive controls
delay(16);
#endif
}