forked from wouterdevinck/nixie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonsTask.cpp
74 lines (67 loc) · 1.68 KB
/
ButtonsTask.cpp
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
#include "ButtonsTask.h"
ButtonsTask::ButtonsTask(Chronodot* rtc, TimeTask* buddy) {
_rtc = rtc;
_buddy = buddy;
_state = Run;
}
void ButtonsTask::begin() {
pinMode(PIN_SW1, INPUT);
pinMode(PIN_SW2, INPUT);
}
void ButtonsTask::task() {
boolean sw1 = debounce(digitalRead(PIN_SW1) == LOW, &sw1db);
boolean sw2 = debounce(digitalRead(PIN_SW2) == LOW, &sw2db);
if(sw1) {
if(_state == Run) {
_state = SetHour;
} else if (_state == SetHour) {
_state = SetMinute;
} else if (_state == SetMinute) {
_state = SetSecond;
} else if (_state == SetSecond) {
_state = Run;
}
_buddy->setState(_state);
}
if(sw2 && _state != Run) {
DateTime now = _rtc->now();
if (_state == SetHour) {
int hour = now.hour();
if(hour == 23) {
hour = 0;
} else {
hour++;
}
_rtc->adjust(DateTime(now.year(), now.month(), now.day(), hour, now.minute(), now.second()));
} else if (_state == SetMinute) {
int minute = now.minute();
if(minute == 59) {
minute = 0;
} else {
minute++;
}
_rtc->adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), minute, now.second()));
} else if (_state == SetSecond) {
int second = now.second();
if(second == 59) {
second = 0;
} else {
second++;
}
_rtc->adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), now.minute(), second));
}
_buddy->task();
}
}
boolean ButtonsTask::debounce(boolean value, boolean* store) {
if(value) {
if(*store) {
value = false;
} else {
*store = true;
}
} else {
*store = false;
}
return value;
}