-
-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathbutton.cpp
More file actions
157 lines (119 loc) · 3.24 KB
/
Copy pathbutton.cpp
File metadata and controls
157 lines (119 loc) · 3.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
#include "button.h"
#include <climits>
#if ESP32
#include <driver/rtc_io.h>
#include <esp_sleep.h>
#endif
#include "GlobalVars.h"
#ifdef ON_OFF_BUTTON_PIN
void IRAM_ATTR buttonInterruptHandler() {
if (OnOffButton::getInstance().buttonPressed) {
return;
}
OnOffButton::getInstance().signalPressStart();
}
void OnOffButton::setup() {
#if ESP8266
digitalWrite(D0, LOW);
pinMode(D0, OUTPUT);
pinMode(ON_OFF_BUTTON_PIN, INPUT);
#endif
#if ESP32
pinMode(
ON_OFF_BUTTON_PIN,
BUTTON_ACTIVE_LEVEL == 0 ? INPUT_PULLUP : INPUT_PULLDOWN
);
esp_deep_sleep_enable_gpio_wakeup(
1 << ON_OFF_BUTTON_PIN,
BUTTON_ACTIVE_LEVEL == 0 ? ESP_GPIO_WAKEUP_GPIO_LOW : ESP_GPIO_WAKEUP_GPIO_HIGH
);
#ifdef BUTTON_IMU_ENABLE_PIN
pinMode(BUTTON_IMU_ENABLE_PIN, OUTPUT);
gpio_hold_dis(static_cast<gpio_num_t>(BUTTON_IMU_ENABLE_PIN));
digitalWrite(BUTTON_IMU_ENABLE_PIN, BUTTON_IMU_ENABLE_ACTIVE_LEVEL);
#endif
gpio_deep_sleep_hold_en();
#endif
attachInterrupt(
ON_OFF_BUTTON_PIN,
buttonInterruptHandler,
BUTTON_ACTIVE_LEVEL == 0 ? FALLING : RISING
);
}
void OnOffButton::tick() {
#ifdef BUTTON_AUTO_SLEEP_TIME_SECONDS
uint64_t autoSleepElapsed = millis() - lastActivityMillis;
if (autoSleepElapsed >= BUTTON_AUTO_SLEEP_TIME_SECONDS * 1e3) {
goToSleep();
}
#endif
#if defined(BUTTON_BATTERY_VOLTAGE_THRESHOLD) && BATTERY_MONITOR == BAT_EXTERNAL
if (battery.getVoltage() >= BUTTON_BATTERY_VOLTAGE_THRESHOLD) {
batteryBad = false;
} else if (!batteryBad) {
batteryBad = true;
batteryBadSinceMillis = millis();
}
if (batteryBad) {
uint64_t batteryBadElapsed = millis() - batteryBadSinceMillis;
if (batteryBadElapsed >= batteryBadTimeoutSeconds * 1e3) {
goToSleep();
}
}
#endif
if (!wasReleasedInitially && !getButton()) {
return;
}
wasReleasedInitially = true;
if (!buttonPressed) {
return;
}
uint64_t timeTaken = millis() - buttonPressStartMillis;
if (getButton() && timeTaken < longPressSeconds * 1e3) {
return;
}
if (timeTaken >= longPressSeconds * 1e3) {
goToSleep();
}
buttonPressed = false;
}
void OnOffButton::onBeforeSleep(std::function<void()> callback) {
callbacks.push_back(callback);
}
void OnOffButton::signalTrackerMoved() { lastActivityMillis = millis(); }
OnOffButton& OnOffButton::getInstance() { return instance; }
bool OnOffButton::getButton() {
static constexpr uint8_t circularBufferBitCount
= sizeof(buttonCircularBuffer) * CHAR_BIT;
bool isPressed = digitalRead(ON_OFF_BUTTON_PIN) == BUTTON_ACTIVE_LEVEL;
buttonCircularBuffer = buttonCircularBuffer << 1 | isPressed;
auto popCount = __builtin_popcount(buttonCircularBuffer);
return popCount >= circularBufferBitCount / 2;
}
void OnOffButton::signalPressStart() {
buttonPressed = true;
buttonPressStartMillis = millis();
buttonCircularBuffer = UINT64_MAX;
}
void OnOffButton::emitOnBeforeSleep() {
for (auto& callback : callbacks) {
callback();
}
}
void OnOffButton::goToSleep() {
emitOnBeforeSleep();
#if defined(BUTTON_IMU_ENABLE_PIN) && ESP32
digitalWrite(BUTTON_IMU_ENABLE_PIN, LOW);
gpio_hold_en(static_cast<gpio_num_t>(BUTTON_IMU_ENABLE_PIN));
#endif
ledManager.pattern(100, 100, 3);
while (buttonPressed && getButton())
;
#if ESP8266
ESP.deepSleep(0);
#elif ESP32
esp_deep_sleep_start();
#endif
}
OnOffButton OnOffButton::instance;
#endif