-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTinyController.cpp
More file actions
169 lines (155 loc) · 6.4 KB
/
Copy pathTinyController.cpp
File metadata and controls
169 lines (155 loc) · 6.4 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
#include "TinyController.h"
using LogState = Tiny::TILogTarget<TinyCon::StateLogLevel>;
void TinyCon::TinyController::Init(int8_t hatOffset, const std::array<int8_t, MaxNativeAdcPinCount>& axisPins, const std::array<int8_t, MaxNativeGpioPinCount>& buttonPins, ActiveState activeState)
{
Controller.Init(hatOffset, axisPins, buttonPins, activeState);
Power.Init();
Processor.Init();
#if !NO_I2C_SLAVE
I2C.Init();
#endif
#if !NO_USB
USBControl.Init();
USBControl.SetActive(Power.PowerSource == PowerSources::USB);
#endif
#if !NO_BLE
Bluetooth.Init();
Bluetooth.SetActive(Power.PowerSource == PowerSources::Battery || !USBControl.IsActive());
#endif
Indicators.Init();
}
void TinyCon::TinyController::Update(int32_t deltaTime)
{
bool i2cNeedsUpdate = Power.PowerSource == PowerSources::I2C || (!Processor.GetUSBEnabled() && !Processor.GetBLEEnabled());
bool bluetoothWasConnected = Bluetooth.IsConnected();
bool bluetoothNeedsUpdate = !i2cNeedsUpdate && Bluetooth.IsActive();
bool usbNeedsUpdate = !i2cNeedsUpdate && !Bluetooth.IsConnected() && USBControl.IsActive();
if (i2cNeedsUpdate || bluetoothNeedsUpdate || usbNeedsUpdate)
{
LogState::Info("State: Updating", Tiny::TIEndl);
Controller.Update(deltaTime);
Processor.Update();
if (bluetoothNeedsUpdate) Bluetooth.Update(deltaTime);
if (usbNeedsUpdate) USBControl.Update();
UpdateSelectButton(deltaTime, Controller.GetButton(BluetoothStartButtonIndex));
Suspended = false;
}
else
{
LogState::Info("State: Suspended", Tiny::TIEndl);
UpdateSelectButton(deltaTime, Controller.GetUpdatedButton(BluetoothStartButtonIndex));
Suspended = true;
}
bool powerWasUsb = Power.PowerSource == PowerSources::USB;
bool powerWasI2c = Power.PowerSource == PowerSources::I2C;
Power.Update();
bool powerIsUsb = Power.PowerSource == PowerSources::USB;
if (Power.PowerSource == PowerSources::I2C)
{
LogState::Debug("I2C connected, disable USB and Bluetooth (again)", Tiny::TIEndl);
USBControl.SetActive(false);
Bluetooth.SetActive(false);
}
else if (powerWasI2c)
{
LogState::Debug("I2C disconnected, ");
if (powerIsUsb)
{
LogState::Debug("USB power, enabling USB, disabling Bluetooth", Tiny::TIEndl);
USBControl.SetActive(true);
Bluetooth.SetActive(false);
}
else
{
LogState::Debug("Battery power, enabling Bluetooth, disabling USB", Tiny::TIEndl);
USBControl.SetActive(false);
Bluetooth.SetActive(true);
}
}
else if (powerIsUsb)
{
if (!powerWasUsb)
{
LogState::Debug("USB connected, disabling Bluetooth", Tiny::TIEndl);
USBControl.SetActive(true);
Bluetooth.SetActive(false);
}
else if (bluetoothWasConnected && !Bluetooth.IsConnected())
{
LogState::Debug("Forced Bluetooth on USB disconnect, enabling USB, stopping auto-advertising", Tiny::TIEndl);
USBControl.SetActive(true);
Bluetooth.SetActive(false);
}
}
else if (powerWasUsb)
{
LogState::Debug("USB disconnected, enabling Bluetooth", Tiny::TIEndl);
USBControl.SetActive(false);
Bluetooth.SetActive(true);
}
UpdateIndicators(deltaTime);
}
void TinyCon::TinyController::UpdateSelectButton(int32_t deltaTime, bool selectButton)
{
if (selectButton)
if (BluetoothStartPressedTimeout > 0) BluetoothStartPressedTimeout -= deltaTime;
else
{
LogState::Info("Bluetooth Button Triggered", Tiny::TIEndl);
Bluetooth.SetActive(!Bluetooth.IsActive());
if (!Bluetooth.IsActive() && Power.PowerSource == PowerSources::USB) USBControl.SetActive(true);
BluetoothStartPressedTimeout = BluetoothStartButtonTime;
}
else BluetoothStartPressedTimeout = BluetoothStartButtonTime;
}
void TinyCon::TinyController::UpdateIndicators(int32_t deltaTime)
{
if (Suspended) Indicators.Disable();
else
{
if (Bluetooth.IsAdvertising()) Indicators.SetBlue(IndicatorController::LedEffects::Fade);
else if (Bluetooth.IsConnected()) Indicators.SetBlue(IndicatorController::LedEffects::Pulse);
else Indicators.SetBlue(IndicatorController::LedEffects::Off);
if (Power.PowerSource == PowerSources::I2C) Indicators.SetRed(IndicatorController::LedEffects::Pulse);
else if (Bluetooth.IsConnected() || Power.PowerSource != PowerSources::USB) Indicators.SetRed(IndicatorController::LedEffects::Off);
else if (USBControl.IsConnected()) Indicators.SetRed(IndicatorController::LedEffects::On);
else Indicators.SetRed(IndicatorController::LedEffects::Fade);
#if USE_NEOPIXEL
if (Power.PowerSource == PowerSources::USB)
{
if (Power.Battery.Voltage < 4.1f)
{
Indicators.SetRgbRed(IndicatorController::LedEffects::Pulse);
Indicators.SetRgbGreen(IndicatorController::LedEffects::Off);
}
else
{
Indicators.SetRgbRed(IndicatorController::LedEffects::Off);
Indicators.SetRgbGreen(IndicatorController::LedEffects::On);
}
Indicators.SetRgbBlue(IndicatorController::LedEffects::Off);
}
else if (Power.PowerSource == PowerSources::Battery)
{
if (Power.Battery.Percentage < 0.2f) Indicators.SetRgbRed(IndicatorController::LedEffects::Pulse);
else Indicators.SetRgbRed(IndicatorController::LedEffects::Off);
Indicators.SetRgbGreen(IndicatorController::LedEffects::Off);
Indicators.SetRgbBlue(IndicatorController::LedEffects::Fixed, 127 * Power.Battery.Percentage);
}
else
{
Indicators.SetRgbRed(IndicatorController::LedEffects::Off);
Indicators.SetRgbGreen(IndicatorController::LedEffects::Off);
Indicators.SetRgbBlue(IndicatorController::LedEffects::Off);
}
#endif
char mode = 'D';
if (Power.PowerSource == PowerSources::I2C) mode = 'I';
else if (Bluetooth.IsConnected()) mode = 'B';
else if (Bluetooth.IsAdvertising())
if (USBControl.IsConnected()) mode = 'F';
else mode = 'A';
else if (USBControl.IsConnected()) mode = 'U';
Indicators.Update(deltaTime, mode);
}
}