-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandProcessor.cpp
More file actions
330 lines (307 loc) · 18 KB
/
Copy pathCommandProcessor.cpp
File metadata and controls
330 lines (307 loc) · 18 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "CommandProcessor.h"
using LogCommand = Tiny::TILogTarget<TinyCon::CommandLogLevel>;
void TinyCon::CommandProcessor::Init()
{
// This ensures, that each register byte we don't write will read back the address of the register, providing a
// good way to determine which registers are being read properly and where a read is potentially accessing the
// wrong register.
for (auto i = 0; i < MaxRegisters; ++i) Registers[i] = i & 0xFF;
// Set the read-only registers with static data and initialize the others with the defaults
SetRegister(Tiny::Drivers::Input::TITinyConCommands::ID, Controller.Id);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::Version, 0, Tiny::Drivers::Input::TITinyConVersion >> 8);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::Version, 1, Tiny::Drivers::Input::TITinyConVersion & 0xFF);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::Magic, 0, Tiny::Drivers::Input::TITinyConMagic >> 8);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::Magic, 1, Tiny::Drivers::Input::TITinyConMagic & 0xFF);
// Initialize the configurable registers with the default settings
SetRegister(Tiny::Drivers::Input::TITinyConCommands::MPUDataEnable,
Controller.GetAccelerationEnabled() << 3 | Controller.GetAngularVelocityEnabled() << 2 |
Controller.GetOrientationEnabled() << 1 | Controller.GetTemperatureEnabled());
SetRegister(Tiny::Drivers::Input::TITinyConCommands::FeatureEnable, GetI2CEnabled() << 2 | GetBLEEnabled() << 1 | GetUSBEnabled());
for (int8_t i = 0; i < GamepadController::MaxMpuControllers; ++i)
SetRegister(Tiny::Drivers::Input::TITinyConCommands::MpuConfig1, i,
static_cast<uint8_t>(Controller.GetAccelerometerRange(i)) << 4 | static_cast<uint8_t>(Controller.GetGyroscopeRange(i)));
// Initialize the dynamic data
Update();
}
void TinyCon::CommandProcessor::Update()
{
uint16_t vinVoltage = Tiny::Math::HalfFromFloat(Power.USBPowerVoltage);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::VinVoltage, 0, vinVoltage >> 8);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::VinVoltage, 1, vinVoltage & 0xFF);
uint16_t batteryPercentage = Tiny::Math::HalfFromFloat(Power.Battery.Percentage);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::BatteryPercentage, 0, batteryPercentage >> 8);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::BatteryPercentage, 1, batteryPercentage & 0xFF);
uint16_t batteryVoltage = Tiny::Math::HalfFromFloat(Power.Battery.Voltage);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::BatteryVoltage, 0, batteryVoltage >> 8);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::BatteryVoltage, 1, batteryVoltage & 0xFF);
uint16_t batteryTemperature = Tiny::Math::HalfFromFloat(Power.Battery.Temperature);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::BatteryTemperature, 0, batteryTemperature >> 8);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::BatteryTemperature, 1, batteryTemperature & 0xFF);
// Because devices may be detected any time, we need to update the types registers every time with the data
for (int8_t i = 0; i < GamepadController::MaxHapticControllers; ++i)
SetRegister(Tiny::Drivers::Input::TITinyConCommands::HapticTypes, i, static_cast<uint8_t>(Controller.GetHapticType(i)));
for (int8_t i = 0; i < GamepadController::MaxInputControllers; ++i)
SetRegister(Tiny::Drivers::Input::TITinyConCommands::ControllerTypes, i, static_cast<uint8_t>(Controller.GetControllerType(i)));
for (int8_t i = 0; i < GamepadController::MaxMpuControllers; ++i)
SetRegister(Tiny::Drivers::Input::TITinyConCommands::MpuTypes, i, static_cast<uint8_t>(Controller.GetMpuType(i)));
SetRegister(Tiny::Drivers::Input::TITinyConCommands::AxisCount, Controller.GetAxisCount());
SetRegister(Tiny::Drivers::Input::TITinyConCommands::ButtonCount, Controller.GetButtonCount());
constexpr auto dataStart = static_cast<uint8_t>(Tiny::Drivers::Input::TITinyConCommands::Data);
auto dataOffset = Controller.MakeMpuBuffer({Registers.data() + dataStart, Registers.size() - dataStart});
uint8_t value = 0;
for (int8_t i = 0; i < Controller.GetButtonCount(); ++i)
{
value |= Controller.GetButton(i) << (i & 7);
if ((i & 7) == 7)
{
SetRegister(Tiny::Drivers::Input::TITinyConCommands::Data, dataOffset++, value);
value = 0;
}
}
if (Controller.GetButtonCount() & 7) SetRegister(Tiny::Drivers::Input::TITinyConCommands::Data, dataOffset++, value);
for (auto i = 0; i < Controller.GetAxisCount(); ++i)
{
auto axis = Tiny::Math::HalfFromFloat(Controller.GetAxis(i));
if ((axis >= 0x7c00 && axis < 0x8000) || (axis >= 0xfc00)) axis = 0x0000;
SetRegister(Tiny::Drivers::Input::TITinyConCommands::Data, dataOffset++, axis >> 8);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::Data, dataOffset++, axis & 0xFF);
}
for (dataOffset += dataStart; dataOffset < Registers.size(); ++dataOffset) Registers[dataOffset] = dataOffset & 0xFF;
}
bool TinyCon::CommandProcessor::ProcessCommand(Tiny::Collections::TIFixedSpan<uint8_t> command)
{
LogCommand::Verbose("CMD(", command.size(), "): ");
for (std::size_t i = 0; i < command.size(); ++i)
{
if (command[i] < 0x10) LogCommand::Verbose("0");
LogCommand::Verbose(command[i], Tiny::TIFormat::Hex, " ");
}
LogCommand::Verbose(Tiny::TIEndl);
LastParameter = {};
const uint8_t reg = command[0];
switch (Tiny::Drivers::Input::TITinyConCommands(reg))
{
case Tiny::Drivers::Input::TITinyConCommands::ID:
if (command.size() > 1)
{
Controller.Id = command[1];
Registers[reg] = command[1];
LastParameter = {command[1]};
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::Ok;
LogCommand::Debug("ID:", command[1], Tiny::TIEndl);
}
else LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorIncompleteCommand;
break;
case Tiny::Drivers::Input::TITinyConCommands::Reset:
if (command.size() > 1)
{
if (command[1] == Tiny::Drivers::Input::TITinyConResetConfirm)
{
I2CEnabled = true;
USBEnabled = TinyConUSBEnabledByDefault;
BLEEnabled = TinyConBLEEnabledByDefault;
Controller.Reset();
Init();
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::Ok;
LogCommand::Debug("RST", Tiny::TIEndl);
}
else
{
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorInvalidResetValue;
LogCommand::Error("EIRV:", command[1], Tiny::TIEndl);
}
}
else LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorIncompleteCommand;
break;
case Tiny::Drivers::Input::TITinyConCommands::MpuConfig1:
case Tiny::Drivers::Input::TITinyConCommands::MpuConfig2:
case Tiny::Drivers::Input::TITinyConCommands::MpuConfig3:
case Tiny::Drivers::Input::TITinyConCommands::MpuConfig4:
case Tiny::Drivers::Input::TITinyConCommands::MpuConfig5:
case Tiny::Drivers::Input::TITinyConCommands::MpuConfig6:
if (command.size() > 1)
{
int8_t offset = reg - static_cast<uint8_t>(Tiny::Drivers::Input::TITinyConCommands::MpuConfig1);
if (offset >= 0 && offset < GamepadController::MaxMpuControllers)
{
Controller.SetAccelerometerRange(offset, Tiny::Drivers::Input::TITinyConAccelerometerRanges(command[1] >> 4));
Controller.SetGyroscopeRange(offset, Tiny::Drivers::Input::TITinyConGyroscopeRanges(command[1] & 0xF));
Registers[reg] = command[1];
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::Ok;
LogCommand::Debug("MPUCFG", offset, ":", command[1], Tiny::TIFormat::Hex, Tiny::TIEndl);
}
else
{
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorInvalidMpuIndex;
LogCommand::Error("IMI:", offset, Tiny::TIEndl);
}
LastParameter = {command[1]};
}
else LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorIncompleteCommand;
break;
case Tiny::Drivers::Input::TITinyConCommands::MPUDataEnable:
if (command.size() > 1)
{
Controller.SetAccelerationEnabled((command[1] & 0x08) != 0);
Controller.SetAngularVelocityEnabled((command[1] & 0x04) != 0);
Controller.SetOrientationEnabled((command[1] & 0x02) != 0);
Controller.SetTemperatureEnabled((command[1] & 0x01) != 0);
Registers[reg] = command[1];
LastParameter = {command[1]};
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::Ok;
LogCommand::Debug("MPUDE", ":", command[1], Tiny::TIFormat::Hex, Tiny::TIEndl);
}
else LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorIncompleteCommand;
break;
case Tiny::Drivers::Input::TITinyConCommands::FeatureEnable:
if (command.size() > 1)
{
SetI2CEnabled((command[1] & 0x04) != 0);
SetBLEEnabled((command[1] & 0x02) != 0);
SetUSBEnabled((command[1] & 0x01) != 0);
Registers[reg] = command[1];
LastParameter = {command[1]};
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::Ok;
LogCommand::Debug("FE:", command[1], Tiny::TIFormat::Hex, Tiny::TIEndl);
}
else LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorIncompleteCommand;
break;
case Tiny::Drivers::Input::TITinyConCommands::Haptic:
if (command.size() > 13)
{
switch (Tiny::Drivers::Input::TITinyConHapticCommands(command[2]))
{
case Tiny::Drivers::Input::TITinyConHapticCommands::PlayWaveform:
case Tiny::Drivers::Input::TITinyConHapticCommands::PlayRealtime:
if (command[3] > 8)
{
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorInvalidHapticDataSize;
LogCommand::Error("EIHDS:", command[2], Tiny::TIEndl);
}
else
{
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::Ok;
for (auto bit = 0; bit < 8; ++bit)
if ((!Controller.GetHapticPresent(bit) || (bit >= GamepadController::MaxHapticControllers)) && (command[1] & (1 << bit)) != 0)
{
// We'll still execute the command, but we'll return an error
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::WarningUnknownHapticController;
LogCommand::Warning("WUHC:", bit, Tiny::TIEndl);
}
Controller.AddHapticCommand({command.data() + 1, command.size() - 1});
LastParameter = {command[1], command[2]};
LogCommand::Debug("HC", Tiny::TIEndl);
}
break;
default:
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorInvalidHapticCommand;
LogCommand::Error("EIH:", command[2], Tiny::TIEndl);
break;
}
}
else if (command.size() > 2)
{
int8_t controller = command[1];
int8_t queueIndex = command[2];
if (controller >= GamepadController::MaxHapticControllers || !Controller.GetHapticPresent(controller))
{
memset(Registers.data() + reg, 0xFF, 12);
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorInvalidHapticController;
LogCommand::Error("EIHC:", command[1], Tiny::TIEndl);
}
else if (queueIndex >= 8)
{
memset(Registers.data() + reg, 0xFF, 12);
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorInvalidHapticDataIndex;
LogCommand::Error("EIHDI:", queueIndex, Tiny::TIEndl);
}
else
{
Registers[reg] = static_cast<uint8_t>(Controller.GetHapticCommand(controller, queueIndex));
Registers[reg + 1] = Controller.GetHapticCommandCount(controller, queueIndex);
for (int8_t i = 0; i < 8; ++i) Registers[reg + 2 + i] = Controller.GetHapticCommandData(controller, queueIndex, i);
Registers[reg + 10] = Controller.GetHapticCommandDuration(controller, queueIndex) >> 8;
Registers[reg + 11] = Controller.GetHapticCommandDuration(controller, queueIndex) & 0xFF;
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::Ok;
LogCommand::Debug("HQ:", controller, ":", queueIndex, Tiny::TIEndl);
}
LastParameter = {command[1], command[2]};
}
else LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorIncompleteCommand;
break;
case Tiny::Drivers::Input::TITinyConCommands::HapticRemove:
if (command.size() > 2)
{
int8_t controller = command[1];
int8_t queueIndex = command[2];
if (controller >= GamepadController::MaxHapticControllers || !Controller.GetHapticPresent(controller))
{
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorInvalidHapticController;
LogCommand::Error("EIHC:", controller, Tiny::TIEndl);
}
else if (queueIndex >= 8)
{
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorInvalidHapticDataIndex;
LogCommand::Error("EIHDI:", queueIndex, Tiny::TIEndl);
}
else
{
Controller.RemoveHapticCommand(controller, queueIndex);
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::Ok;
LogCommand::Debug("HR:", controller, ":", queueIndex, Tiny::TIEndl);
}
LastParameter = {command[1], command[2]};
}
else LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorIncompleteCommand;
break;
case Tiny::Drivers::Input::TITinyConCommands::HapticQueueSize:
// This and the haptic command are the only registers that require additional data
// to be read. The haptic command does require 14 byte anyway, so this is fine,
// but the queue size command needs this special case.
if (command.size() > 1)
{
int8_t controller = command[1];
if (controller >= GamepadController::MaxHapticControllers || !Controller.GetHapticPresent(controller))
{
Registers[reg] = 0xFF;
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorInvalidHapticController;
LogCommand::Error("EIHC:", controller, Tiny::TIEndl);
}
else
{
Registers[reg] = Controller.GetHapticQueueSize(controller);
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::Ok;
LogCommand::Debug("HQS:", controller, Tiny::TIEndl);
}
LastParameter = {command[1]};
}
else LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorIncompleteCommand;
break;
case Tiny::Drivers::Input::TITinyConCommands::HapticReset:
if (command.size() > 1)
{
if (command[1] == Tiny::Drivers::Input::TITinyConHapticClearConfirm)
{
Controller.ClearHapticCommands();
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::Ok;
LogCommand::Debug("HC", Tiny::TIEndl);
}
else
{
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorInvalidHapticClearConfirm;
LogCommand::Error("EIHCC:", command[1], Tiny::TIEndl);
}
}
else LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorIncompleteCommand;
break;
default:
LastCommandStatus = Tiny::Drivers::Input::TITinyConCommandStatus::ErrorInvalidCommand;
break;
}
// This needs to update every time we receive a byte, users could be reading back the status at any time
SetRegister(Tiny::Drivers::Input::TITinyConCommands::LastCommand, 0, LastCommand);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::LastCommand, 1, LastParameter[0]);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::LastCommand, 2, LastParameter[1]);
SetRegister(Tiny::Drivers::Input::TITinyConCommands::LastCommand, 3, static_cast<uint8_t>(LastCommandStatus));
return LastCommandStatus == Tiny::Drivers::Input::TITinyConCommandStatus::Ok;
}