-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
260 lines (213 loc) · 4.86 KB
/
Controller.cpp
File metadata and controls
260 lines (213 loc) · 4.86 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
Controller.cpp - Support Library to control a coffee machine.
*/
#include "Arduino.h"
#include "Controller.h"
#include "ControlMode.h"
// Constructor
Controller::Controller(uint8_t relayPin,
WiFiLink wifiLink)
{
_relayPin = relayPin;
_wiFiLink = wifiLink;
}
// Public
void Controller::setup()
{
pinMode(_relayPin, OUTPUT);
stop();
setMode(ControlMode::off);
_wiFiLink.setup();
_rtc.begin();
}
void Controller::tick()
{
manage();
}
char *Controller::showMode()
{
switch (_mode)
{
case ControlMode::error:
return "Error";
break;
case ControlMode::off:
return "Off";
break;
case ControlMode::schedule_check:
return "Schedule check";
break;
case ControlMode::schedule_fulfill:
return "Schedule fulfill";
break;
case ControlMode::on:
return "On";
break;
default:
return "Unknown";
break;
}
}
bool Controller::isReady()
{
return _isReady;
}
bool Controller::isStarted()
{
return _isStarted;
}
ControlMode Controller::currentMode()
{
return _mode;
}
void Controller::toggleIsReady()
{
setIsReady(!_isReady);
}
void Controller::setMode(ControlMode mode)
{
_mode = mode;
}
void Controller::setSchedule(uint8_t hours, uint8_t minutes, uint8_t keepWarmDuration)
{
DateTime now = _rtc.now();
_startDateTime = DateTime(now.year(),
now.month(),
now.day(),
hours,
minutes,
0);
_endDateTime = _startDateTime + TimeSpan(keepWarmDuration * 60);
}
char *Controller::getSchedule()
{
TimeSpan keepWarm = _endDateTime - _startDateTime;
char res[1000];
snprintf(res,
sizeof(res),
"%d:%d for %dh, %dm",
_startDateTime.hour(),
_startDateTime.minute(),
keepWarm.hours(),
keepWarm.minutes());
return res;
}
// Private
void Controller::manage()
{
switch (_mode)
{
case ControlMode::error... ControlMode::off:
if (_isStarted)
{
stop();
}
break;
case ControlMode::on:
if (!_isStarted)
{
start();
}
break;
case ControlMode::schedule_check:
if (!_isReady)
{
if (_isStarted)
{
stop();
}
break;
}
if (!shouldStart())
{
if (_isStarted)
{
stop();
}
break;
}
if (!_isStarted)
{
start();
setMode(ControlMode::schedule_fulfill);
break;
}
break;
case ControlMode::schedule_fulfill:
if (shouldStop() && _isStarted)
{
stop();
setMode(ControlMode::schedule_check);
break;
}
break;
default:
break;
}
}
void Controller::stop()
{
control(false);
}
void Controller::start()
{
control(true);
}
void Controller::control(bool state)
{
digitalWrite(_relayPin, !state);
_isStarted = state;
setIsReady(false);
}
void Controller::setIsReady(bool isReady)
{
_isReady = isReady;
}
// TODO: Link to schedule
bool Controller::shouldStart()
{
Serial.println("Evaluate shouldStart:");
Serial.println("---------------------");
DateTime now = _rtc.now();
// Display info
Serial.print("> Current time: ");
Serial.println(now.timestamp());
Serial.print("> Start target time: ");
Serial.println(_startDateTime.timestamp());
Serial.print("> End target time: ");
Serial.println(_endDateTime.timestamp());
// Evaluate scenario
bool pastStartMoment = now >= _startDateTime;
bool pastFinishWarmingMoment = now >= _endDateTime;
// Display info
Serial.print("-> pastStartMoment: ");
Serial.println(pastStartMoment);
Serial.print("-> pastFinishWarmingMoment: ");
Serial.println(pastFinishWarmingMoment);
// Evaluate result
bool shouldStart = pastStartMoment && !pastFinishWarmingMoment;
// Display info
Serial.print("=> shouldStart: ");
Serial.println(shouldStart);
Serial.println();
return shouldStart;
}
// TODO: Link to Schedule
bool Controller::shouldStop()
{
Serial.println("Evaluate shouldStop:");
Serial.println("--------------------");
DateTime now = _rtc.now();
// Display info
Serial.print("> Current time: ");
Serial.println(now.timestamp());
Serial.print("> End target time: ");
Serial.println(_endDateTime.timestamp());
// Evaluate result
bool shouldStop = now >= _endDateTime;
// Display info
Serial.print("=> Should stop: ");
Serial.println(shouldStop);
Serial.println();
return shouldStop;
}