-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathButtonHandler.cpp
More file actions
94 lines (82 loc) · 2.61 KB
/
ButtonHandler.cpp
File metadata and controls
94 lines (82 loc) · 2.61 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
#include "ButtonHandler.h"
ButtonHandler::ButtonHandler(int pin, int pinGND, std::function<void(int8_t)> pressCallback)
{
// Initialize pins and callbacks
_pin = pin;
_pinGND = pinGND;
_pressCallback = pressCallback;
// Initialize state variables
_lastButtonState = HIGH;
_buttonState = HIGH;
_lastDebounceTime = 0;
_pressStartTime = 0;
_isLongPressActive = false;
_lastGearChangeTime = 0;
// Set up the button pin
pinMode(_pin, INPUT_PULLUP);
pinMode(_pinGND, OUTPUT);
digitalWrite(_pinGND, LOW);
}
void ButtonHandler::update()
{
int reading = digitalRead(_pin);
// Debounce the button
if (reading != _lastButtonState)
{
_lastDebounceTime = millis();
}
if ((millis() - _lastDebounceTime) > DEBOUNCE_TIME)
{
// Button state changed after debounce period
if (reading != _buttonState)
{
_buttonState = reading;
// Handle button press (LOW with pull-up resistor)
if (_buttonState == LOW)
{
_pressStartTime = millis();
_isLongPressActive = false;
}
// Handle button release (HIGH with pull-up resistor)
else if (_buttonState == HIGH)
{
if (!_isLongPressActive && (millis() - _pressStartTime) >= DEBOUNCE_TIME)
{
// Short press action (single gear change)
if (_pressCallback)
{
_pressCallback(SHORT_PRESS_NUM_SHIFTS);
}
}
_isLongPressActive = false; // Reset long press flag on release
}
}
}
// Check for long press (only if button is still pressed)
if (_buttonState == LOW && !_isLongPressActive)
{
if (millis() - _pressStartTime > LONG_PRESS_DURATION)
{
_isLongPressActive = true;
_lastGearChangeTime = millis(); // Initialize interval timing
// First gear change immediately when long press detected
if (_pressCallback)
{
_pressCallback(LONG_PRESS_NUM_SHIFTS);
}
}
}
// Handle repeated gear changes during long press (only if button still pressed)
if (_buttonState == LOW && _isLongPressActive)
{
if (millis() - _lastGearChangeTime >= LONG_PRESS_INTERVAL)
{
_lastGearChangeTime = millis();
if (_pressCallback)
{
_pressCallback(LONG_PRESS_NUM_SHIFTS);
}
}
}
_lastButtonState = reading;
}