/
ClockController.cpp
176 lines (147 loc) · 4.19 KB
/
ClockController.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
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
169
170
171
172
173
174
175
176
#include "ClockController.h"
#include "Sound.h"
#include "Settings.h"
#include "HT1632.h"
void ClockController::setup() {
changeFace(faceManager.currentFace());
beepEnabled = EEPROM.read(SETTINGS_BEEP_ENABLED);
beepTriggerEnabled = false;
tick();
Serial.begin(9600);
}
void ClockController::tick() {
int time[7];
rtc.get(time, true);
seconds = time[0];
minutes = time[1];
hours = time[2];
//int dow = time[3];
day = time[4];
month = time[5];
year = time[6];
temperature = rtc.getTemperature();
if(currentFace != NULL) {
if(seconds != currentFace->seconds ||
minutes != currentFace->minutes ||
hours != currentFace->hours)
currentFace->setTime(hours, minutes, seconds);
if(day != currentFace->day ||
month != currentFace->month ||
year != currentFace->year)
currentFace->setDate(day, month, year);
if(temperature != currentFace->temperature)
currentFace->setTemperature(temperature);
currentFace->lastUpdate = millis();
currentFace->updateDisplay();
}
checkForBeep();
checkSerial();
}
void ClockController::buttonClicked(ButtonType button) {
if(button == BUTTON_MENU) {
changeFace(faceManager.nextFace());
}
}
void ClockController::changeFace(ClockFace* newFace) {
currentFace = newFace;
currentFace->init();
ht1632_clear();
}
void ClockController::checkForBeep() {
if(!beepEnabled) return;
if(seconds > 30) {
beepTriggerEnabled = true;
} else if(beepTriggerEnabled) {
if(minutes == 0)
soundChimeLong();
else if(minutes == 30)
soundChimeShort();
if(minutes == 0 || minutes == 30)
beepTriggerEnabled = false;
}
}
void ClockController::checkSerial() {
byte incoming;
while(Serial.available()) {
incoming = Serial.read();
if(incoming == '\n') {
//Serial.println("Got line: " + buffer);
dispatchSerial(buffer);
buffer = "";
} else
buffer += (char)incoming;
}
}
bool ClockController::handleSerialEvent(const String& line) {
bool handled = false;
if(line.startsWith("time ")) {
String timeString = line.substring(5);
int idx = timeString.indexOf(':');
if(idx != -1) {
int hours = timeString.substring(0,2).toInt();
int mins = timeString.substring(3,5).toInt();
Serial.print("Setting time to: ");
Serial.print(hours);
Serial.print(':');
Serial.println(mins);
rtc.stop();
rtc.set(DS3231_SEC, 0);
rtc.set(DS3231_MIN, mins);
rtc.set(DS3231_HR, hours);
rtc.start();
} else {
Serial.println("Couldn't parse time: " + timeString);
}
handled = true;
} else if(line.startsWith("date ")) {
String dateString = line.substring(5);
int idx = dateString.indexOf('/');
if(idx != -1) {
int day = dateString.substring(0,2).toInt();
int month = dateString.substring(3,5).toInt();
int year = dateString.substring(6,10).toInt();
Serial.print("Setting date to: ");
Serial.print(day);
Serial.print('/');
Serial.print(month);
Serial.print('/');
Serial.println(year);
rtc.stop();
rtc.set(DS3231_DATE, day);
rtc.set(DS3231_MTH, month);
rtc.set(DS3231_YR, year);
rtc.start();
} else if(line.startsWith("hourBeep ")) {
bool enabled = (line.substring(9,10).toInt()==1);
EEPROM.write(SETTINGS_BEEP_ENABLED, enabled);
beepEnabled = enabled;
Serial.print(beepEnabled ? "Enabling" : "Disabling");
Serial.println(" beep");
handled = true;
} else {
Serial.println("Couldn't parse date: " + dateString);
}
handled = true;
}
if(!handled) {
//Dispatch to child(s)
if(currentFace != NULL)
handled = currentFace->handleSerialEvent(line);
}
return handled;
}
void ClockController::dispatchSerial(const String& line) {
bool handled = false;
if(line == "beep") {
beep();
handled = true;
} else if(line.equalsIgnoreCase("Open the pod bay doors, HAL")){
Serial.println("Sorry. I can't let you do that, Dave.");
handled = true;
} else {
//Dispatch
handled = this->handleSerialEvent(line);
}
if(!handled)
Serial.println("I'm not wise enough to understand: " + line);
}