-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamepadController.cpp
More file actions
196 lines (179 loc) · 7.24 KB
/
Copy pathGamepadController.cpp
File metadata and controls
196 lines (179 loc) · 7.24 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
#include "GamepadController.h"
using LogGamepad = Tiny::TILogTarget<TinyCon::GamepadLogLevel>;
using LogI2C = Tiny::TILogTarget<TinyCon::I2CLogLevel>;
void TinyCon::GamepadController::Init(int8_t hatOffset, const std::array<int8_t, MaxNativeAdcPinCount>& axisPins, const std::array<int8_t, MaxNativeGpioPinCount>& buttonPins, ActiveState activeState)
{
// Input -1 is always the device itself with raw ADC and GPIO pins
Inputs[Inputs.size() - 1].Init(axisPins, buttonPins, activeState);
for (std::size_t i = 0; i < Inputs.size() - 1; ++i) Inputs[i].Init(I2C0, i);
auto anyMpuInitialized = false;
for (std::size_t i = 0; i < Mpus.size(); ++i)
{
Mpus[i].Init(I2C0, i);
if (!anyMpuInitialized) Mpus[i].Update();
anyMpuInitialized |= Mpus[i].Present;
}
Haptics[1].Init(I2C0);
Haptics[0].Init(I2C1);
HatOffset = hatOffset;
}
void TinyCon::GamepadController::Update(uint32_t deltaTime)
{
if constexpr (Tiny::GlobalLogThreshold >= Tiny::TILogLevel::Verbose)
{
LogI2C::Verbose("I2C0 Devices: ");
bool found = false;
for (auto addr = 0x02; addr < 0x78; ++addr)
{
I2C0.beginTransmission(addr);
if (I2C0.endTransmission() == 0)
{
if (found) LogI2C::Verbose(", ");
found = true;
LogI2C::Verbose("0x", addr, Tiny::TIFormat::Hex);
}
}
LogI2C::Verbose(Tiny::TIEndl);
LogI2C::Verbose("I2C1 Devices: ");
found = false;
for (auto addr = 0x02; addr < 0x78; ++addr)
{
I2C1.beginTransmission(addr);
if (I2C1.endTransmission() == 0)
{
if (found) LogI2C::Verbose(", ");
found = true;
LogI2C::Verbose("0x", addr, Tiny::TIFormat::Hex);
}
}
LogI2C::Verbose(Tiny::TIEndl);
}
I2C0.setClock(400000);
LogGamepad::Info("Controller Update:", Tiny::TIEndl);
auto mpuInitialized = false;
for (auto& mpu : Mpus)
{
auto time = millis();
auto mpuWasPresent = mpu.Present;
if (!mpuInitialized || mpu.Present) mpu.Update();
if (!mpuWasPresent && mpu.Present) mpuInitialized = true;
LogGamepad::Debug(" MPU: (", mpu.Acceleration.X, ", ", mpu.Acceleration.Y, ", ", mpu.Acceleration.Z,
"), (", mpu.AngularVelocity.X, ", ", mpu.AngularVelocity.Y, ", ", mpu.AngularVelocity.Z,
"), (", mpu.Orientation.X, ", ", mpu.Orientation.Y, ", ", mpu.Orientation.Z,
"), ", mpu.Temperature, ", ", millis() - time, "ms", Tiny::TIEndl);
}
I2C0.setClock(800000);
for (auto& input : Inputs)
if (input.Present)
{
auto time = millis();
LogGamepad::Debug(" Input: (");
input.Update();
for (int8_t j = 0; j < input.GetAxisCount(); ++j)
{
if (j > 0) LogGamepad::Debug(", ");
LogGamepad::Debug(input.Axis[j]);
}
LogGamepad::Debug("), (");
for (int8_t j = 0; j < input.GetButtonCount(); ++j)
{
if (j > 0) LogGamepad::Debug(", ");
LogGamepad::Debug(input.Buttons[j] ? "Down" : "Up");
}
LogGamepad::Debug("), ", millis() - time, "ms", Tiny::TIEndl);
}
I2C0.setClock(400000);
for (auto& haptic : Haptics)
if (haptic.Present && haptic.Enabled)
{
auto time = millis();
LogGamepad::Debug(" Haptic: ", haptic.Available(), " ");
haptic.Update(deltaTime);
LogGamepad::Debug(", ", millis() - time, "ms");
LogGamepad::Info(Tiny::TIEndl);
}
}
#if !NO_BLE || !NO_USB
hid_gamepad_report_t TinyCon::GamepadController::MakeHidReport() const
{
hid_gamepad_report_t report = {};
// XXX: Figure out how we can support more than 2+1 axis per controller
// Assume any extra axis is 0-ed if not available or the controller is disabled
report.x = GetAxis(0) * 127;
report.y = GetAxis(1) * 127;
report.z = GetAxis(2) * 127;
report.rz = GetAxis(3) * 127;
report.rx = GetAxis(4) * 127;
report.ry = GetAxis(5) * 127;
if (HatOffset < 0) report.hat = GAMEPAD_HAT_CENTERED;
else
{
// Figure out the hat, clock-wise starting at the top position button. Offsetting this by 5
if (GetButton(HatOffset) && GetButton(HatOffset + 3)) report.hat = GAMEPAD_HAT_UP_LEFT;
else if (GetButton(HatOffset) && GetButton(HatOffset + 1)) report.hat = GAMEPAD_HAT_UP_RIGHT;
else if (GetButton(HatOffset)) report.hat = GAMEPAD_HAT_UP;
else if (GetButton(HatOffset + 1) && GetButton(HatOffset + 2)) report.hat = GAMEPAD_HAT_DOWN_RIGHT;
else if (GetButton(HatOffset + 1)) report.hat = GAMEPAD_HAT_RIGHT;
else if (GetButton(HatOffset + 2) && GetButton(HatOffset + 3)) report.hat = GAMEPAD_HAT_DOWN_LEFT;
else if (GetButton(HatOffset + 2)) report.hat = GAMEPAD_HAT_DOWN;
else if (GetButton(HatOffset + 3)) report.hat = GAMEPAD_HAT_LEFT;
else report.hat = GAMEPAD_HAT_CENTERED;
}
for (int8_t index = 0; index < 32; ++index)
if (HatOffset < 0 || index < HatOffset) report.buttons |= GetButton(index) << (index);
else report.buttons |= GetButton(index + 4) << (index);
return report;
}
#endif
std::size_t TinyCon::GamepadController::MakeMpuBuffer(Tiny::Collections::TIFixedSpan<uint8_t> data) const
{
auto size = 0;
for (auto& mpu : Mpus) if (mpu.Present)
size += mpu.FillBuffer({data.data() + size, data.size()});
return size;
}
void TinyCon::GamepadController::AddHapticCommand(Tiny::Collections::TIFixedSpan<uint8_t> data)
{
if (data.size() > 12)
{
for (int8_t bit = 0; bit < 8; ++bit)
if ((data[0] & (1 << bit)) != 0)
{
uint8_t controller = bit;
uint8_t command = data[1];
uint8_t count = data[2];
const uint8_t* sequence = data.data() + 3;
uint16_t timeout = (data[11] << 8) | data[12];
Haptics[controller].Insert(command, count, sequence, timeout);
LogGamepad::Info("Add Haptic Command: ", controller, ", ", command, ", ", count, ", ", timeout, Tiny::TIEndl);
}
}
}
float TinyCon::GamepadController::GetAxis(int8_t axisIndex) const
{
for (auto& input : Inputs)
if (axisIndex < input.GetAxisCount()) return input.Axis[axisIndex];
else axisIndex -= input.GetAxisCount();
return 0.0f;
}
bool TinyCon::GamepadController::GetButton(int8_t buttonIndex) const
{
for (auto& input : Inputs)
if (buttonIndex < input.GetButtonCount()) return input.Buttons[buttonIndex];
else buttonIndex -= input.GetButtonCount();
return false;
}
bool TinyCon::GamepadController::GetUpdatedButton(int8_t buttonIndex) const
{
for (auto& input : Inputs)
if (buttonIndex < input.GetButtonCount()) return input.GetUpdatedButton(buttonIndex);
else buttonIndex -= input.GetButtonCount();
return false;
}
void TinyCon::GamepadController::Reset()
{
Id = 0;
for (auto& haptic : Haptics) haptic.Reset();
for (auto& mpu : Mpus) mpu.Reset();
for (auto& input : Inputs) input.Reset();
}