-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlfredoConnect.cpp
204 lines (183 loc) · 7.76 KB
/
AlfredoConnect.cpp
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
// TODO: for both keyboard keys and gamepad buttons, really want
// keyPressed, buttonPressed - true only on the frame of the press
// keyHeld, buttonHeld - true for the entire duration of the press
// this means we need to save previous state, doubling memory use
// TODO: could we implement gamepads as a dynamically allocated linked list?
// this would help alleviate memory use from storing both current and
// previous button states.
#include "AlfredoConnect.h"
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_ALFREDOCONNECT)
AlfredoConnectParser AlfredoConnect;
#endif
void AlfredoConnectParser::begin() {
begin(Serial);
}
void AlfredoConnectParser::begin(Stream& inputStream) {
this->inputStream = &inputStream;
}
/**
* @param suppressErrors suppresses writing error output to the inputStream
*/
void AlfredoConnectParser::begin(Stream& inputStream, bool suppressErrors) {
this->suppressErrors = suppressErrors;
begin(inputStream);
}
/**
* Parses packets from AlfredoConnect into keyboard and gamepad states.
*
* @return 0 when successful, which includes when no packets were the stream. Return
* values over 0 are failure codes.
*/
uint8_t AlfredoConnectParser::update() {
if (inputStream == NULL) return 1;
uint8_t keysPressed[NUM_KEYS];
int keysPressedSize = 0;
Gamepad gamepadsTemp[MAX_GAMEPADS];
int gamepadsSizeTemp = 0;
bool keyboardStateCleared = false; // We want to clear the keyboard state at most once per update
while (inputStream->available()) {
if (inputStream->peek() == '#') {
readWithTimeout(inputStream); // read the hash
// Keyboard
if ((keysPressedSize = readWithTimeout(inputStream)) < 0) return 2;
if (readAndDiscard(inputStream, keysPressed, NUM_KEYS, keysPressedSize) == 1) return 3;
// Gamepads
if ((gamepadsSizeTemp = readWithTimeout(inputStream)) < 0) return 4;
for (uint8_t i = 0; i < min(gamepadsSizeTemp, MAX_GAMEPADS); i++) {
// Axes
if ((gamepadsTemp[i].axesSize = readWithTimeout(inputStream)) < 0) return 5;
if (readAndDiscard(inputStream, gamepadsTemp[i].axes, MAX_AXES, gamepadsTemp[i].axesSize) < 0) return 6;
// Buttons
if ((gamepadsTemp[i].buttonsSize = readWithTimeout(inputStream)) < 0) return 7;
uint8_t buttonsBytes = (gamepadsTemp[i].buttonsSize / 8) + (gamepadsTemp[i].buttonsSize % 8 == 0 ? 0 : 1);
if (readAndDiscard(inputStream, gamepadsTemp[i].buttons, MAX_BUTTONS, buttonsBytes) < 0) return 9;
}
// If more than 8 gamepads were sent, discard the extras
for (uint8_t i = MAX_GAMEPADS; i < gamepadsSizeTemp; i++) {
int axesSize;
if ((axesSize = readWithTimeout(inputStream)) < 0) return 10;
for (uint8_t j = 0; j < axesSize; j++) if (readWithTimeout(inputStream) == -1) return 11;
int buttonsSize;
if ((buttonsSize = readWithTimeout(inputStream)) < 0) return 12;
uint8_t buttonsBytes = (gamepadsTemp[i].buttonsSize / 8) + (gamepadsTemp[i].buttonsSize % 8 == 0 ? 0 : 1);
for (uint8_t j = 0; j < buttonsBytes; j++) if (readWithTimeout(inputStream) == -1) return 13;
}
if (readWithTimeout(inputStream) != '$') return 14;
if (!keyboardStateCleared) {
keyboardStateCleared = true;
memset(keyboardState, 0, NUM_KEYS);
}
for (int i = 0; i < keysPressedSize; i++) keyboardState[keysPressed[i] / 8] |= 1 << (keysPressed[i] % 8);
memcpy(gamepads, gamepadsTemp, sizeof(Gamepad) * gamepadsSizeTemp);
gamepadsSize = gamepadsSizeTemp;
} else inputStream->read();
}
return 0;
}
/**
* Reads `numToRead` bytes from `inputStream`, placing up to `bufferLen` of them in `buffer`, discarding the rest.
*/
uint8_t AlfredoConnectParser::readAndDiscard(Stream* inputStream, uint8_t *buffer, uint8_t bufferLen, uint8_t numToRead) {
if (inputStream->readBytes(buffer, min(bufferLen, numToRead)) != min(bufferLen, numToRead)) return 1;
for (int i = bufferLen; i < numToRead; i++) if (readWithTimeout(inputStream) == -1) return 1;
return 0;
}
/**
* Similar to calling `read` on the provided stream, but with a timeout like `readBytes`.
*/
int AlfredoConnectParser::readWithTimeout(Stream* inputStream) {
uint8_t c;
if (inputStream->readBytes(&c, 1) != 1) return -1;
return c;
}
bool AlfredoConnectParser::keyHeld(Key key) {
return keyboardState[(uint8_t)key / 8] & (1 << ((uint8_t)key % 8));
}
uint8_t AlfredoConnectParser::getGamepadCount() {
return gamepadsSize;
}
uint8_t AlfredoConnectParser::getAxisCount(uint8_t gamepad) {
if (gamepad >= gamepadsSize) {
if (!suppressErrors) {
inputStream->print("[ERROR - AlfredoConnectParser::getAxisCount] Gamepad ");
inputStream->print(gamepad);
inputStream->println(" is not connected.");
}
return 0;
}
return gamepads[gamepad].axesSize;
}
uint8_t AlfredoConnectParser::getButtonCount(uint8_t gamepad) {
if (gamepad >= gamepadsSize) {
if (!suppressErrors) {
inputStream->print("[ERROR - AlfredoConnectParser::getButtonCount] Gamepad ");
inputStream->print(gamepad);
inputStream->println(" is not connected.");
}
return 0;
}
return gamepads[gamepad].buttonsSize;
}
float AlfredoConnectParser::getAxis(uint8_t gamepad, uint8_t axis) {
if (gamepad >= gamepadsSize) {
if (!suppressErrors) {
inputStream->print("[ERROR - AlfredoConnectParser::getAxis] Gamepad ");
inputStream->print(gamepad);
inputStream->println(" is not connected.");
}
return 0;
}
if (axis >= gamepads[gamepad].axesSize) {
if (!suppressErrors) {
inputStream->print("[ERROR - AlfredoConnectParser::getAxis] Gamepad ");
inputStream->print(gamepad);
inputStream->print(" has no axis ");
inputStream->print(axis);
inputStream->println(".");
}
return 0;
}
return ((float)(gamepads[gamepad].axes[axis]) / 127.5) - 1;
}
uint8_t AlfredoConnectParser::getRawAxis(uint8_t gamepad, uint8_t axis) {
if (gamepad >= gamepadsSize) {
if (!suppressErrors) {
inputStream->print("[ERROR - AlfredoConnectParser::getRawAxis] Gamepad ");
inputStream->print(gamepad);
inputStream->println(" is not connected.");
}
return 0;
}
if (axis >= gamepads[gamepad].axesSize) {
if (!suppressErrors) {
inputStream->print("[ERROR - AlfredoConnectParser::getRawAxis] Gamepad ");
inputStream->print(gamepad);
inputStream->print(" has no axis ");
inputStream->print(axis);
inputStream->println(".");
}
return 0;
}
return gamepads[gamepad].axes[axis];
}
bool AlfredoConnectParser::buttonHeld(uint8_t gamepad, uint8_t button) {
if (gamepad >= gamepadsSize) {
if (!suppressErrors) {
inputStream->print("[ERROR - AlfredoConnectParser::buttonHeld] Gamepad ");
inputStream->print(gamepad);
inputStream->println(" is not connected.");
}
return false;
}
if (button >= gamepads[gamepad].buttonsSize) {
if (!suppressErrors) {
inputStream->print("[ERROR - AlfredoConnectParser::buttonHeld] Gamepad ");
inputStream->print(gamepad);
inputStream->print(" has no button ");
inputStream->print(button);
inputStream->println(".");
}
return false;
}
return gamepads[gamepad].buttons[button / 8] & (1 << (button % 8));
}