Skip to content

Commit f430612

Browse files
committed
Use the Button class, introduce constant.
1 parent 3d03378 commit f430612

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

src/OpenBikeSensorFirmware.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ CircularBuffer<DataSet*, 10> dataBuffer;
103103
FileWriter* writer;
104104

105105
const uint8_t displayAddress = 0x3c;
106-
106+
const uint16_t BUTTON_PRESS_THRESHOLD_FOR_SHUTDOWN_MS = 10000;
107107

108108
// Enable dev-mode. Allows to
109109
// - set wifi config
@@ -233,35 +233,26 @@ static uint8_t shutdownState = 0;
233233
// Power-management keep alive timer
234234
// This function is called every 100 ms
235235
static unsigned long timeOfLastPowerKeepAlive = 0;
236-
static uint8_t buttonPressedCounter = 0;
237236
static void powerKeepAliveTimerISR()
238237
{
239238
// Send "keep alive" trigger to power management module
240239
// This is done by toggling the pin every 300 ms or more
241240
if(shutdownState == 0)
242241
{
243-
if(!digitalRead(IP5306_BUTTON) && millis() - timeOfLastPowerKeepAlive > POWER_KEEP_ALIVE_INTERVAL_MS)
242+
unsigned long timeSinceLastPowerKeepAlive = millis() - timeOfLastPowerKeepAlive;
243+
if(!digitalRead(IP5306_BUTTON) && timeSinceLastPowerKeepAlive > POWER_KEEP_ALIVE_INTERVAL_MS)
244244
{
245245
timeOfLastPowerKeepAlive = millis();
246246
digitalWrite(IP5306_BUTTON, HIGH);
247247
}
248-
else if(digitalRead(IP5306_BUTTON) && millis() - timeOfLastPowerKeepAlive > 300)
248+
else if(digitalRead(IP5306_BUTTON) && timeSinceLastPowerKeepAlive > 300)
249249
{
250250
timeOfLastPowerKeepAlive = millis();
251251
digitalWrite(IP5306_BUTTON, LOW);
252252
}
253253
}
254254

255-
// Soft power-off OBSPro when button is pressed for more than 10 seconds
256-
if(button.read())
257-
{
258-
if(buttonPressedCounter < 255)
259-
buttonPressedCounter++;
260-
}
261-
else
262-
buttonPressedCounter = 0;
263-
264-
if(shutdownState == 0 && buttonPressedCounter >= 100) {
255+
if(shutdownState == 0 && button.read() && button.getCurrentStateMillis() >= BUTTON_PRESS_THRESHOLD_FOR_SHUTDOWN_MS) {
265256
shutdownState = 1;
266257
}
267258
switch(shutdownState)

0 commit comments

Comments
 (0)