forked from toniebox-reverse-engineering/hackiebox_cfw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoxEvents.cpp
327 lines (294 loc) · 11.5 KB
/
BoxEvents.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
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
#include "BoxEvents.h"
void BoxEvents::begin() {
}
void BoxEvents::loop() {
}
void BoxEvents::handleEarEvent(BoxButtonEars::EarButton earId, BoxButtonEars::PressedType pressType, BoxButtonEars::PressedTime pressLength) {
char* nameEar;
char* nameType;
char* nameLength;
switch (earId) {
case BoxButtonEars::EarButton::SMALL:
nameEar = "Small ear";
break;
case BoxButtonEars::EarButton::BIG:
nameEar = "Big ear";
break;
case BoxButtonEars::EarButton::BOTH:
nameEar = "Both ears";
break;
default:
nameEar = "Unknown";
break;
}
switch (pressType) {
case BoxButtonEars::PressedType::PRESS:
nameType = "pressed";
break;
case BoxButtonEars::PressedType::RELEASE:
nameType = "released";
break;
default:
nameType = "?";
break;
}
switch (pressLength) {
case BoxButtonEars::PressedTime::SHORT:
nameLength = "short";
break;
case BoxButtonEars::PressedTime::LONG:
nameLength = "long";
break;
case BoxButtonEars::PressedTime::VERY_LONG:
nameLength = "very long";
break;
default:
nameLength = "unknown length";
break;
}
Log.info("%s %s-%s", nameEar, nameLength, nameType);
Box.boxPower.feedSleepTimer();
if (pressType == BoxButtonEars::PressedType::PRESS) {
if (pressLength == BoxButtonEars::PressedTime::SHORT) {
if (earId == BoxButtonEars::EarButton::BIG) {
Box.boxDAC.increaseVolume();
} else if (earId == BoxButtonEars::EarButton::SMALL) {
Box.boxDAC.decreaseVolume();
} else if (earId == BoxButtonEars::EarButton::BOTH) {
}
} else if (pressLength == BoxButtonEars::PressedTime::LONG) {
if (earId == BoxButtonEars::EarButton::BIG) {
if (Box.boxWiFi.getStatus() != WrapperWiFi::ConnectionState::CONNECTED)
Box.boxWiFi.reconnect();
} else if (earId == BoxButtonEars::EarButton::SMALL) {
} else if (earId == BoxButtonEars::EarButton::BOTH) {
}
} else if (pressLength == BoxButtonEars::PressedTime::VERY_LONG) {
if (earId == BoxButtonEars::EarButton::BIG) {
} else if (earId == BoxButtonEars::EarButton::SMALL) {
} else if (earId == BoxButtonEars::EarButton::BOTH) {
if (Box.boxAccel.getOrientation() == BoxAccelerometer::Orientation::EARS_UP) {
Box.boxLEDs.setIdleAnimation(BoxLEDs::ANIMATION_TYPE::PULSE, BoxLEDs::CRGB::Blue);
Box.boxWiFi.apMode();
} else if (Box.boxAccel.getOrientation() == BoxAccelerometer::Orientation::EARS_DOWN) {
Box.boxPower.reset();
} else if (Box.boxAccel.getOrientation() == BoxAccelerometer::Orientation::EARS_FRONT) {
//Prepare Hibernation
Box.boxLEDs.setAllBool(false);
Box.boxEars.waitForRelease();
delay(500);
Box.boxPower.hibernate();
}
}
}
} else if (pressType == BoxButtonEars::PressedType::RELEASE) {
if (pressLength == BoxButtonEars::PressedTime::SHORT) {
if (earId == BoxButtonEars::EarButton::BIG) {
} else if (earId == BoxButtonEars::EarButton::SMALL) {
} else if (earId == BoxButtonEars::EarButton::BOTH) {
}
} else if (pressLength == BoxButtonEars::PressedTime::LONG) {
if (earId == BoxButtonEars::EarButton::BIG) {
if (Box.boxWiFi.getStatus() != WrapperWiFi::ConnectionState::CONNECTED)
Box.boxWiFi.reconnect();
} else if (earId == BoxButtonEars::EarButton::SMALL) {
} else if (earId == BoxButtonEars::EarButton::BOTH) {
}
} else if (pressLength == BoxButtonEars::PressedTime::VERY_LONG) {
if (earId == BoxButtonEars::EarButton::BIG) {
} else if (earId == BoxButtonEars::EarButton::SMALL) {
} else if (earId == BoxButtonEars::EarButton::BOTH) {
}
}
}
}
void BoxEvents::handleBatteryEvent(BoxBattery::BatteryEvent state) {
switch (state) {
case BoxBattery::BatteryEvent::BAT_CRITICAL:
Log.info("Battery is critical, connect the charger, hibernating!");
Box.boxBattery.stopBatteryTest();
Box.boxBattery.logBatteryStatus();
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Orange, 3);
Box.boxLEDs.waitForAnimationToFinish();
Box.boxPower.hibernate();
break;
case BoxBattery::BatteryEvent::BAT_LOW:
Log.info("Battery is low, connect the charger!");
Box.boxBattery.logBatteryStatus();
Box.boxLEDs.setIdleAnimation(BoxLEDs::ANIMATION_TYPE::PULSE, BoxLEDs::CRGB::Orange);
break;
case BoxBattery::BatteryEvent::CHR_CONNECT:
Log.info("Charger connected");
Box.boxBattery.logBatteryStatus();
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::White, 3);
break;
case BoxBattery::BatteryEvent::CHR_DISCONNECT:
Log.info("Charger disconnected");
Box.boxBattery.logBatteryStatus();
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::DarkSlateGray, 3);
break;
}
}
void BoxEvents::handleWiFiEvent(WrapperWiFi::ConnectionState state) {
switch (state) {
case WrapperWiFi::ConnectionState::WAIT_CONNECT:
break;
case WrapperWiFi::ConnectionState::WAIT_IP:
Log.info("WiFi connected, waiting for ip...");
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Cyan, 3);
break;
case WrapperWiFi::ConnectionState::CONNECTED:
Log.info("IP: %s", WiFi.localIP().toString().c_str());
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Blue, 3);
Box.boxWiFi.mDnsAdvertiseSetup();
break;
case WrapperWiFi::ConnectionState::DISCONNECTED:
//Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Cyan, 3);
Log.info("WiFi lost");
break;
default:
break;
}
}
void BoxEvents::handlePowerEvent(BoxPower::PowerEvent event) {
switch (event)
{
case BoxPower::PowerEvent::PRE_HIBERNATE:
Log.info("Go into hibernation...");
break;
case BoxPower::PowerEvent::PRE_RESET:
Log.info("Reset box...");
break;
case BoxPower::PowerEvent::BOX_IDLE:
if (Box.boxBattery.batteryTestActive()) {
Log.info("Box unused, battery test running, keep alive...");
Box.boxPower.feedSleepTimer();
return;
}
Log.info("Box unused, power off.");
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Green, 3);
Box.boxLEDs.waitForAnimationToFinish();
Box.boxPower.hibernate();
break;
default:
break;
}
}
void BoxEvents::handleAccelerometerOrientationEvent(BoxAccelerometer::Orientation orient) {
char* orientText;
switch (orient) {
case BoxAccelerometer::Orientation::EARS_UP:
orientText = "ears up";
Box.boxDAC.play();
break;
case BoxAccelerometer::Orientation::EARS_DOWN:
orientText = "ears down";
break;
case BoxAccelerometer::Orientation::EARS_FRONT:
orientText = "ears front";
Box.boxDAC.pause();
break;
case BoxAccelerometer::Orientation::EARS_BACK:
orientText = "ears back";
break;
case BoxAccelerometer::Orientation::EARS_LEFT:
orientText = "ears left";
break;
case BoxAccelerometer::Orientation::EARS_RIGHT:
orientText = "ears right";
break;
case BoxAccelerometer::Orientation::OTHER:
orientText = "lockout";
break;
default:
orientText = "unknown";
Log.error("Unknown orientation=%i", orient);
break;
}
Log.info("Box' orientation changed to %s", orientText);
Box.boxPower.feedSleepTimer();
}
void BoxEvents::handleTagEvent(BoxRFID::TAG_EVENT event) {
switch (event) {
case BoxRFID::TAG_EVENT::TAG_PLACED:
Log.info("Tag placed", event);
Box.boxLEDs.setIdleAnimation(BoxLEDs::ANIMATION_TYPE::PARTY, BoxLEDs::CRGB::White);
Box.boxRFID.logUID();
if (!Box.boxDAC.hasStopped() && (memcmp(Box.boxRFID.tagUid, Box.boxTonie.currentUid, 8) == 0)) {
Log.info("Continue playing last file");
Box.boxDAC.play();
} else {
DirFs dir;
if(Config.get()->misc.autodump) {
Log.info("Autodump...");
char* rdump = "/rDUMP";
if (!dir.openDir(rdump)) {
Log.info("Create dir %s...", rdump);
if (!FatFs.mkdir(rdump)) {
Log.info("...fail!");
}
}
if (Box.boxRFID.dumpTagMemory(false)) {
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Yellow, 2);
Box.boxDAC.beepMidi(84, 100, false);
Box.boxDAC.beepMidi(76, 100, false);
} else {
}
} else {
Log.info("No Autodump");
}
char* rcontent = "/rCONTENT";
if (!dir.openDir(rcontent)) {
Log.info("Create dir %s...", rcontent);
if (!FatFs.mkdir(rcontent)) {
Log.info("...fail!");
}
}
Box.boxTonie.loadTonieByUid(Box.boxRFID.tagUid);
uint8_t path[strlen(Box.boxTonie.RCONTENT_BASE)+16+1];
uint8_t* uid = Box.boxRFID.tagUid;
sprintf(
(char*)path,
"%s%02X%02X%02X%02X%02X%02X%02X%02X",
Box.boxTonie.RCONTENT_BASE,
uid[0], uid[1], uid[2], uid[3], uid[4], uid[5], uid[6], uid[7]
);
if (!dir.openDir((char*)path)) {
Log.info("Create dir %s...", path);
if (!FatFs.mkdir((char*)path)) {
Log.info("...fail!");
}
} else {
bool foundFile = false;
while (dir.nextFile()) {
if (!dir.isDir()) {
foundFile = true;
break;
}
}
if (!foundFile) {
Log.info("No file play.");
} else {
uint8_t filepath[256];
sprintf(
(char*)filepath,
"%s/%s",
path,
dir.fileName()
);
Box.boxDAC.playFile((const char*)filepath);
}
}
}
break;
case BoxRFID::TAG_EVENT::TAG_REMOVED:
Log.info("Tag removed", event);
Box.boxLEDs.defaultIdleAnimation();
Box.boxDAC.pause();
break;
default:
Log.error("Unknown TAG_EVENT=%X", event);
break;
}
Box.boxPower.feedSleepTimer();
}