-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
98 lines (87 loc) · 2.31 KB
/
Copy pathinput.cpp
File metadata and controls
98 lines (87 loc) · 2.31 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
#include "input.h"
#include <M5Unified.h>
#include <math.h>
#define BASELINE_SAMPLES 20
void inputInit(InputState& is) {
memset(&is, 0, sizeof(InputState));
is.baselineSet = false;
is.baselineMag = 1.0f;
float sum = 0;
int validSamples = 0;
for (int i = 0; i < BASELINE_SAMPLES; i++) {
M5.update();
M5.Imu.update();
m5::imu_data_t d;
M5.Imu.getImuData(&d);
float m = sqrtf(d.accel.x * d.accel.x +
d.accel.y * d.accel.y +
d.accel.z * d.accel.z);
if (m > 0.1f) {
sum += m;
validSamples++;
}
delay(15);
}
if (validSamples > 0) {
is.baselineMag = sum / validSamples;
is.baselineSet = true;
}
}
InputEvent inputUpdate(InputState& is) {
unsigned long now = millis();
if (M5.BtnPWR.wasClicked()) {
return INPUT_PWR;
}
M5.Imu.update();
m5::imu_data_t imuData;
M5.Imu.getImuData(&imuData);
float mag = sqrtf(imuData.accel.x * imuData.accel.x +
imuData.accel.y * imuData.accel.y +
imuData.accel.z * imuData.accel.z);
float deviation = fabsf(mag - is.baselineMag);
if (deviation > SHAKE_THRESHOLD && (now - is.lastShakeMs) > SHAKE_COOLDOWN_MS) {
is.lastShakeMs = now;
return INPUT_SHAKE;
}
if (M5.BtnA.wasPressed()) {
is.aHeld = true;
is.aHeldSince = now;
is.aLongFired = false;
is.lastRepeatA = now;
return INPUT_A_PRESS;
}
if (M5.BtnA.wasReleased()) {
is.aHeld = false;
is.aLongFired = false;
}
if (is.aHeld && !is.aLongFired && (now - is.aHeldSince) > LONG_PRESS_MS) {
is.aLongFired = true;
is.lastRepeatA = now;
return INPUT_A_LONG;
}
if (is.aHeld && is.aLongFired && (now - is.lastRepeatA) > REPEAT_DELAY_MS) {
is.lastRepeatA = now;
return INPUT_A_LONG;
}
if (M5.BtnB.wasPressed()) {
is.bHeld = true;
is.bHeldSince = now;
is.bLongFired = false;
is.lastRepeatB = now;
return INPUT_B_PRESS;
}
if (M5.BtnB.wasReleased()) {
is.bHeld = false;
is.bLongFired = false;
}
if (is.bHeld && !is.bLongFired && (now - is.bHeldSince) > LONG_PRESS_MS) {
is.bLongFired = true;
is.lastRepeatB = now;
return INPUT_B_LONG;
}
if (is.bHeld && is.bLongFired && (now - is.lastRepeatB) > REPEAT_DELAY_MS) {
is.lastRepeatB = now;
return INPUT_B_LONG;
}
return INPUT_NONE;
}