-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLCScript.cpp
219 lines (186 loc) · 5.9 KB
/
MLCScript.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
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
#include "MLCScript.h"
#include "Util.h"
#define ASSUMED_HLT_VOLUME_ML 32000.0f
#define ASSUMED_DEGREES_HEAT_LOSS 1.5f
#define MAX_TEMPERATURE 79.0f
static void readNextNullTerminatedString(char* outBuffer, uint8_t length)
{
while (Serial.available() < length - 1) {}
uint8_t i;
for (i = 0; i < length - 1; i++)
outBuffer[i] = Serial.read();
outBuffer[i] = '\0';
}
static void readNextFourBytes(char* outBuffer) {
while (Serial.available() < 4) {}
for (uint8_t i = 0; i < 4; i++)
outBuffer[i] = Serial.read();
}
static void writeSuccess(void) {
Serial.println("OK");
}
MLCScript::MLCScript(void (*pauseCallback)())
: m_pauseCallback (pauseCallback)
{
reset();
}
void MLCScript::writeUnknownCommand(char* command) {
char msg[21] = "Unknown command: XXX";
msg[17] = command[0];
msg[18] = command[1];
msg[19] = command[2];
return writeFailure(msg);
}
void MLCScript::writeFailure(char* msg) {
Serial.print("ERROR: ");
Serial.println(msg);
Serial.flush();
m_numStatements = 0;
m_completed = true;
}
void MLCScript::reset() {
m_counter = m_numStatements =
m_activeStatementIndex = m_mashWaterVolume = 0;
m_completed = false;
m_hltTemperature = m_grainTemperature = NAN;
}
void MLCScript::step(uint32_t elapsedMillis, float hltTemperature, float grainTemperature)
{
if (m_activeStatementIndex >= m_numStatements) {
m_completed = true;
return;
}
ScriptStatement* currentStatement = &m_statements[m_activeStatementIndex];
uint32_t command = currentStatement->command.uint;
m_hltTemperature = hltTemperature;
m_grainTemperature = grainTemperature;
if (command == STRUINT("INI") ||
command == STRUINT("MSH") ||
command == STRUINT("SPG")) {
if (command == STRUINT("INI"))
m_mode = INITIALIZE;
else if (command == STRUINT("MSH"))
m_mode = MASHING;
else if (command == STRUINT("SPG"))
m_mode = SPARGING;
m_activeStatementIndex++;
step(elapsedMillis, hltTemperature, grainTemperature);
}
else if (command == STRUINT("PAU")) {
m_pauseCallback();
m_activeStatementIndex++;
step(0, hltTemperature, grainTemperature);
}
else if (command == STRUINT("MWV")) {
m_mashWaterVolume = currentStatement->f1.volume;
m_activeStatementIndex++;
step(elapsedMillis, hltTemperature, grainTemperature);
}
else if (command == STRUINT("HEA")) {
float currentTemperature = (m_mode == MASHING) ?
grainTemperature
: hltTemperature;
if (currentTemperature >= currentTemperatureTarget()) {
m_activeStatementIndex++;
step(0, hltTemperature, grainTemperature);
}
}
else if (command == STRUINT("HLD")) {
m_counter += elapsedMillis;
if (m_counter >= currentStatement->f2.time) {
m_counter = 0;
m_activeStatementIndex++;
step(0, hltTemperature, grainTemperature);
}
}
else
return writeUnknownCommand(currentStatement->command.str);
}
bool MLCScript::completed()
{
return m_completed;
}
Mode MLCScript::mode()
{
return m_mode;
}
void MLCScript::readFromSerial()
{
reset();
ScriptStatement currentStatement;
for (uint8_t i = 0; i < NUM_STATEMENTS; i++) {
readNextNullTerminatedString(currentStatement.command.str, 4);
uint32_t command = currentStatement.command.uint;
if (command == STRUINT("INI") ||
command == STRUINT("MSH") ||
command == STRUINT("SPG") ||
command == STRUINT("PAU")) {
currentStatement.f1.temperature = NAN;
writeSuccess();
}
else if (command == STRUINT("MWV") ||
command == STRUINT("HEA")) {
writeSuccess();
readNextFourBytes((char*)¤tStatement.f1);
writeSuccess();
}
else if (command == STRUINT("HLD")) {
writeSuccess();
readNextFourBytes((char*)¤tStatement.f1);
writeSuccess();
readNextFourBytes((char*)¤tStatement.f2);
writeSuccess();
}
else if (command == STRUINT("END"))
return writeSuccess();
else
return writeUnknownCommand(currentStatement.command.str);
m_statements[m_numStatements] = currentStatement;
m_numStatements++;
}
readNextNullTerminatedString(currentStatement.command.str, 4);
if (currentStatement.command.uint != STRUINT("END"))
return writeFailure((char*)"Too many statements in script.");
else
return writeSuccess();
}
float MLCScript::currentTemperatureSetpoint(void)
{
return m_statements[m_activeStatementIndex].f1.temperature;
}
float MLCScript::currentTemperatureTarget(void)
{
float setpoint = currentTemperatureSetpoint(),
offset = 0.0f;
if (m_mode == MASHING) {
offset = m_mashWaterVolume / ASSUMED_HLT_VOLUME_ML
* (setpoint - m_grainTemperature)
+ ASSUMED_DEGREES_HEAT_LOSS;
}
if (setpoint + offset > MAX_TEMPERATURE)
return MAX_TEMPERATURE;
else
return setpoint + offset;
}
uint32_t MLCScript::timeInCurrentInterval(void)
{
return m_counter;
}
uint32_t MLCScript::currentIntervalDuration(void)
{
ScriptStatement* currentStatement = &m_statements[m_activeStatementIndex];
if (currentStatement->command.uint == STRUINT("HLD"))
return currentStatement->f2.time;
else
return 0;
}
void MLCScript::currentCommand(char* outBuffer) {
char defaultCommand[] = "NON";
char* command;
if (m_numStatements > 0)
command = m_statements[m_activeStatementIndex].command.str;
else
command = defaultCommand;
for (int i = 0; i < 4; i++)
outBuffer[i] = command[i];
}