-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuttons.cpp
More file actions
129 lines (116 loc) · 4.03 KB
/
buttons.cpp
File metadata and controls
129 lines (116 loc) · 4.03 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
#include <stdint.h>
#include <Arduino.h>
#include "config.cpp"
class Buttons {
private:
Config& config;
long timer;
int previousSwitchState;
public:
Buttons(Config& mConfig): config(mConfig) {}
void setup(){
}
void loop(uint8_t switchState, float pitch){
// Block input when angle is invalid
if(pitch < 70 || pitch > 110){
previousSwitchState = 0;
config.pressCount = 0;
config.longPressCount = 0;
return;
}
// Clear press count & state when idle
if(millis() - timer > 1000){
previousSwitchState = 0;
config.pressCount = 0;
config.longPressCount = 0;
}
// Press detected but we dont know what it is yet
if(previousSwitchState == 0 && switchState == 1){
previousSwitchState = 1;
timer = millis();
}
// Single press confirmed upon release, increment counter
if(previousSwitchState == 1 && switchState == 0){
previousSwitchState = 0;
config.pressCount ++;
timer = millis();
}
// Keep togging brightness on a dual hold after a single press
if(config.pressCount == 1 && switchState == 2 && previousSwitchState != 2){
timer = millis();
previousSwitchState = 2;
config.longPressCount++;
config.toggleBrightness();
}else if(config.pressCount == 1 && switchState == 2 && millis() - timer > 500){
timer = millis();
config.longPressCount++;
config.toggleBrightness();
}
// Forward color
if(config.pressCount == 2 && switchState == 2 && previousSwitchState != 2){
timer = millis();
previousSwitchState = 2;
config.longPressCount++;
config.toggleForwardColor();
}else if(config.pressCount == 2 && switchState == 2 && millis() - timer > 500){
timer = millis();
config.longPressCount++;
config.toggleForwardColor();
}
// Backwards color
if(config.pressCount == 3 && switchState == 2 && previousSwitchState != 2){
timer = millis();
previousSwitchState = 2;
config.longPressCount++;
config.toggleBackwardsColor();
}else if(config.pressCount == 3 && switchState == 2 && millis() - timer > 500){
timer = millis();
config.longPressCount++;
config.toggleBackwardsColor();
}
//Voltage Display Mode
if(config.pressCount == 4 && switchState == 2 && previousSwitchState != 2){
timer = millis();
previousSwitchState = 2;
config.longPressCount++;
config.toggleIdle();
}else if(config.pressCount == 4 && switchState == 2 && millis() - timer > 500){
timer = millis();
config.longPressCount++;
config.toggleIdle();
}
// Cell count
if(config.pressCount == 5 && switchState == 2 && previousSwitchState != 2){
timer = millis();
previousSwitchState = 2;
config.longPressCount++;
config.setBatterySeries(1);
}else if(config.pressCount == 5 && switchState == 2 && millis() - timer > 500){
timer = millis();
config.longPressCount++;
config.setBatterySeries(config.batterySeriesState + 1);
}
// LED count
if(config.pressCount == 6 && switchState == 2 && previousSwitchState != 2){
timer = millis();
previousSwitchState = 2;
config.longPressCount++;
config.setLedCount(1);
}else if(config.pressCount == 6 && switchState == 2 && millis() - timer > 500){
timer = millis();
config.longPressCount++;
config.setLedCount(config.ledCountState + 1);
}
// LED Type
if(config.pressCount == 7 && switchState == 2 && previousSwitchState != 2){
timer = millis();
previousSwitchState = 2;
config.longPressCount++;
config.toggleLedType();
}else if(config.pressCount == 7 && switchState == 2 && millis() - timer > 500){
timer = millis();
config.longPressCount++;
config.toggleLedType();
}
}
};