forked from Skuzzle-UK/ECUMASTER_CAN_DISPLAY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttons.ino
168 lines (149 loc) · 3.49 KB
/
buttons.ino
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
158
159
160
161
162
163
164
165
166
167
168
#define UP_BUTTON 3
#define DOWN_BUTTON 5
#define SET_BUTTON 4
#define LONG_PRESS 2000
byte DEBOUNCE_TIME = 250;
#define SET_TIMEOUT 10000
//////////////////////////////////////////////////////////////
int set_button_state = HIGH;
unsigned long time_set_button_SETd;
bool setMode = false;
unsigned long setModeEntered = 0;
double lastpressed = 0;
//////////////////////////////////////////////////////////////
void SetupButtons() {
time_set_button_SETd = millis();
pinMode(UP_BUTTON, INPUT_PULLUP);
pinMode(DOWN_BUTTON, INPUT_PULLUP);
pinMode(SET_BUTTON, INPUT_PULLUP);
}
//////////////////////////////////////////////////////////////
void CheckButtonPressed() {
//Check up
if (digitalRead(UP_BUTTON) == LOW)
{
Up();
}
//Check down
else if (digitalRead(DOWN_BUTTON) == LOW)
{
Down();
}
//Check set
else if (digitalRead(SET_BUTTON) == LOW) {
switch(screen){
case SCREENS::CLOCK: {
Set();
break;
}
case SCREENS::MAP: {
Set();
break;
}
case SCREENS::FUELTYPE: {
Set();
break;
}
}
}
else {
set_button_state = HIGH;
time_set_button_SETd = millis();
}
}
//////////////////////////////////////////////////////////////
/*Button pressed methods*/
void Set() {
if (set_button_state != LOW) {
DeactivateSetMode();
}
else if (millis() - time_set_button_SETd > LONG_PRESS && !setMode) {
ActivateSetMode();
}
}
//////////////////////////////////////////////////////////////
void ActivateSetMode(){
DEBOUNCE_TIME = 80;
display.clear();
setModeEntered = millis();
setMode = true;
ecm.LOCK();
if (screen == SCREENS::CLOCK){
is_rtc_enabled = false;
PrintHours();
PrintMinutes();
PrintSeconds();
}
}
//////////////////////////////////////////////////////////////
void DeactivateSetMode(){
DEBOUNCE_TIME = 200;
setMode = false;
ecm.UNLOCK();
set_button_state = LOW;
time_set_button_SETd = millis();
if (screen == SCREENS::CLOCK){
is_rtc_enabled = RTC_ENABLED;
}
}
//////////////////////////////////////////////////////////////
void CheckSetTimeout() {
if (millis() - setModeEntered > SET_TIMEOUT && setMode) {
DeactivateSetMode();
}
}
//////////////////////////////////////////////////////////////
void Up() {
if (millis() - lastpressed > DEBOUNCE_TIME) {
display.clear();
if (setMode){
switch(screen){
case SCREENS::CLOCK: {
CLOCK_SET_TIME(DIRECTION::UP);
break;
}
case SCREENS::MAP: {
ecm.SET_BOOST_PRESSURE(DIRECTION::UP);
break;
}
case SCREENS::FUELTYPE: {
ecm.SET_SPARK_MAP();
break;
}
}
setModeEntered = millis(); //Resets timeout
}
else{
ScreenUp();
}
lastpressed = millis();
}
}
//////////////////////////////////////////////////////////////
void Down() {
if (millis() - lastpressed > DEBOUNCE_TIME) {
display.clear();
if (setMode){
switch(screen){
case SCREENS::CLOCK: {
CLOCK_SET_TIME(DIRECTION::DOWN);
break;
}
case SCREENS::MAP: {
ecm.SET_BOOST_PRESSURE(DIRECTION::DOWN);
break;
}
case SCREENS::FUELTYPE: {
ecm.SET_SPARK_MAP();
break;
}
}
setModeEntered = millis(); //Resets timeout
}
else{
ScreenDown();
}
lastpressed = millis();
}
}
//////////////////////////////////////////////////////////////