Sat, 20 Oct 2018 23:26:33 +0200
Two small fixes
0 | 1 | /** |
2 | * @file recipes.c | |
3 | * @brief Recipes management. | |
4 | */ | |
5 | ||
6 | #include "config.h" | |
7 | ||
8 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
9 | extern sButton Buttons[MAXBUTTONS]; ///< Buttons definitions |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
10 | extern int Main_Screen; ///< Current screen |
0 | 11 | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
12 | bool r_UpdateRec = false; ///< Update record flag |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
13 | int r_CurrentRec = 1; ///< Current record |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
14 | int r_Records = 1; ///< Total records |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
15 | int r_Imported = 0; ///< Total imported |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
16 | char _xml_element[10][32]; ///< XML element array |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
17 | int _xml_depth; ///< Current depths |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
18 | int _xml_mashsteps; ///< Mash steps |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
19 | char _xml_add_name[64]; ///< Mash name |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
20 | int _xml_add_type; ///< Mash type |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
21 | int _xml_add_time; ///< Mash rest time |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
22 | int _xml_add_ramp; ///< Mash ramp time |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
23 | float _xml_add_temp; ///< Mash temperature |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
24 | float _xml_tun_temp; ///< TUN temperature |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
25 | bool _xml_add_valid; ///< Add is valid |
0 | 26 | |
27 | static const char *TAG = "recipes"; | |
28 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
29 | char char_data_buffer[1024]; ///< Data buffer |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
30 | size_t offs; ///< Offset in buffer |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
31 | bool overflow; ///< Overflow in buffer |
0 | 32 | |
33 | ||
34 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
35 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
36 | * @brief Add addition to the recipe. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
37 | * @param Name The addition name. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
38 | * @param Type The addition type. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
39 | * @param Time The addition time to add. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
40 | */ |
0 | 41 | void Addition_Add(char *Name, uint8_t Type, uint16_t Time) |
42 | { | |
43 | printf("Addition_Add(%s, %d, %d)\n", Name, Type, Time); | |
44 | if (! recipe.Additions) { | |
45 | // No entries yet, add the first one. | |
46 | sprintf(recipe.Addition[recipe.Additions].Name, "%s", Name); | |
47 | recipe.Addition[recipe.Additions].Type = Type; | |
48 | recipe.Addition[recipe.Additions].Time = Time; | |
49 | recipe.Additions++; | |
50 | return; | |
51 | } | |
52 | ||
53 | // See if we already got one with the same time. | |
54 | for (int i = 0; i < recipe.Additions; i++) { | |
55 | if (recipe.Addition[i].Time == Time) { | |
56 | // Yes, update the name. | |
57 | snprintf(recipe.Addition[i].Name, 63, "%s, %s", recipe.Addition[i].Name, Name); | |
58 | return; | |
59 | } | |
60 | } | |
61 | ||
62 | // A new entry and we already have some. Add it and keep the list sorted. | |
63 | for (int i = 0; i < recipe.Additions; i++) { | |
64 | if (Time > recipe.Addition[i].Time) { | |
65 | printf("Insert at %d\n", i); | |
66 | // Nake room | |
67 | for (int j = i; j < recipe.Additions; j++) { | |
68 | sprintf(recipe.Addition[j+1].Name, "%s", recipe.Addition[j].Name); | |
69 | recipe.Addition[j+1].Type = recipe.Addition[j].Type; | |
70 | recipe.Addition[j+1].Time = recipe.Addition[j].Time; | |
71 | } | |
72 | sprintf(recipe.Addition[i].Name, "%s", Name); | |
73 | recipe.Addition[i].Type = Type; | |
74 | recipe.Addition[i].Time = Time; | |
75 | recipe.Additions++; | |
76 | return; | |
77 | } | |
78 | } | |
79 | ||
80 | // Just append. | |
81 | sprintf(recipe.Addition[recipe.Additions].Name, "%s", Name); | |
82 | recipe.Addition[recipe.Additions].Type = Type; | |
83 | recipe.Addition[recipe.Additions].Time = Time; | |
84 | recipe.Additions++; | |
85 | } | |
86 | ||
87 | ||
88 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
89 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
90 | * @brief Reset the parser buffer. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
91 | */ |
0 | 92 | void reset_char_data_buffer (void) |
93 | { | |
94 | offs = 0; | |
95 | overflow = false; | |
96 | } | |
97 | ||
98 | ||
99 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
100 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
101 | * @brief Datahandler get bytes into the data buffer. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
102 | */ |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
103 | void char_data(void *userData, const XML_Char *s, int len) |
0 | 104 | { |
105 | if (!overflow) { | |
106 | if (len + offs >= sizeof(char_data_buffer) ) { | |
107 | overflow = true; | |
108 | } else { | |
109 | memcpy(char_data_buffer + offs, s, len); | |
110 | offs += len; | |
111 | } | |
112 | } | |
113 | } | |
114 | ||
115 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
116 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
117 | * @brief Process the XML parser data buffer. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
118 | */ |
0 | 119 | void process_char_data_buffer (void) |
120 | { | |
121 | bool allspace = true; | |
122 | ||
123 | if (offs > 0) { | |
124 | char_data_buffer[ offs ] = '\0'; | |
125 | ||
126 | /* | |
127 | * If all spaces and control characters, the data is invalid. | |
128 | */ | |
129 | for (int i = 0; i < strlen(char_data_buffer); i++) { | |
130 | if (char_data_buffer[i] > ' ') | |
131 | allspace = false; | |
132 | } | |
133 | if (allspace) | |
134 | return; | |
135 | ||
136 | if ((_xml_depth > 2) && (strcmp(_xml_element[0], "RECIPES") == 0) && (strcmp(_xml_element[1], "RECIPE") == 0)) { | |
137 | /* | |
138 | * We are in a recipe | |
139 | */ | |
140 | if ((_xml_depth == 3) && (strcmp("NAME", _xml_element[2]) == 0)) { | |
141 | snprintf(recipe.Name, 127, "%s", char_data_buffer); | |
142 | } else if ((_xml_depth == 3) && (strcmp("BOIL_TIME", _xml_element[2]) == 0)) { | |
143 | recipe.BoilTime = atoi(char_data_buffer); | |
144 | } else if ((_xml_depth == 5) && (strcmp("HOPS", _xml_element[2]) == 0) && (strcmp("HOP", _xml_element[3]) == 0)) { | |
145 | /* | |
146 | * Hops that are added during the boil. | |
147 | * But check for whirlpool hops too. | |
148 | */ | |
149 | if (strcmp("NAME", _xml_element[4]) == 0) { | |
150 | snprintf(_xml_add_name, 63, "%s", char_data_buffer); | |
151 | } else if (strcmp("USE", _xml_element[4]) == 0) { | |
152 | _xml_add_valid = (strcmp("Boil", char_data_buffer) == 0); // Only "Boil" is a valid hop | |
153 | if (strcmp("Aroma", char_data_buffer) == 0) { | |
154 | recipe.Whirlpool7 = 30; | |
155 | } | |
156 | } else if (strcmp("TIME", _xml_element[4]) == 0) { | |
157 | _xml_add_time = atoi(char_data_buffer); | |
158 | } | |
159 | } else if ((_xml_depth >= 3) && (strcmp("STYLE", _xml_element[2]) == 0)) { | |
160 | // Ignore | |
161 | } else if ((_xml_depth >= 3) && (strcmp("EQUIPMENT", _xml_element[2]) == 0)) { | |
162 | // Ignora | |
163 | } else if ((_xml_depth >= 3) && (strcmp("YEASTS", _xml_element[2]) == 0)) { | |
164 | // Ignore | |
165 | } else if ((_xml_depth >= 3) && (strcmp("WATERS", _xml_element[2]) == 0)) { | |
166 | // Ignore | |
167 | } else if ((_xml_depth == 5) && (strcmp("FERMENTABLES", _xml_element[2]) == 0) && (strcmp("FERMENTABLE", _xml_element[3]) == 0)) { | |
168 | /* | |
169 | * Fermentabes that must be added during the boil. | |
170 | */ | |
171 | if (strcmp("NAME", _xml_element[4]) == 0) { | |
172 | snprintf(_xml_add_name, 63, "%s", char_data_buffer); | |
173 | } else if (strcmp("TYPE", _xml_element[4]) == 0) { | |
174 | if ((strcmp("Sugar", char_data_buffer) == 0)/* || (strcmp("Adjunct", char_data_buffer) == 0)*/) { | |
175 | _xml_add_type = ADDITION_FERMENTABLE; | |
176 | _xml_add_time = 10; | |
177 | } else { | |
178 | _xml_add_type = -1; | |
179 | } | |
180 | } else if (strcmp("ADD_AFTER_BOIL", _xml_element[4]) == 0) { | |
181 | _xml_add_valid = (strcmp("FALSE", char_data_buffer) == 0); | |
182 | } | |
183 | } else if ((_xml_depth == 5) && (strcmp("MISCS", _xml_element[2]) == 0) && (strcmp("MISC", _xml_element[3]) == 0)) { | |
184 | /* | |
185 | * Check for Misc additions to add during the boil. | |
186 | */ | |
187 | if (strcmp("NAME", _xml_element[4]) == 0) { | |
188 | snprintf(_xml_add_name, 63, "%s", char_data_buffer); | |
189 | } else if (strcmp("USE", _xml_element[4]) == 0) { | |
190 | _xml_add_valid = (strcmp("Boil", char_data_buffer) == 0); // Only "Boil" is a valid hop. | |
191 | } else if (strcmp("TYPE", _xml_element[4]) == 0) { | |
192 | if (strcmp("Spice", char_data_buffer) == 0) | |
193 | _xml_add_type = ADDITION_SPICE; | |
194 | if (strcmp("Fining", char_data_buffer) == 0) | |
195 | _xml_add_type = ADDITION_FINING; | |
196 | if (strcmp("Herb", char_data_buffer) == 0) | |
197 | _xml_add_type = ADDITION_HERB; | |
198 | if (strcmp("Flavor", char_data_buffer) == 0) | |
199 | _xml_add_type = ADDITION_FLAVOR; | |
200 | if (strcmp("Other", char_data_buffer) == 0) | |
201 | _xml_add_type = ADDITION_OTHER; | |
202 | } else if (strcmp("TIME", _xml_element[4]) == 0) { | |
203 | _xml_add_time = atoi(char_data_buffer); | |
204 | } | |
205 | } else if ((_xml_depth >= 4) && (strcmp("MASH", _xml_element[2]) == 0)) { | |
206 | if ((_xml_depth >= 6) && (strcmp("MASH_STEP", _xml_element[4]) == 0)) { | |
207 | if (strcmp("NAME", _xml_element[5]) == 0) { | |
208 | snprintf(_xml_add_name, 31, "%s", char_data_buffer); | |
209 | } else if (strcmp("TYPE", _xml_element[5]) == 0) { | |
210 | // Temperature Infusion Decoction | |
211 | _xml_add_valid = (strcmp("Temperature", char_data_buffer) == 0); | |
212 | } else if (strcmp("STEP_TEMP", _xml_element[5]) == 0) { | |
213 | _xml_add_temp = atof(char_data_buffer); | |
214 | } else if (strcmp("STEP_TIME", _xml_element[5]) == 0) { | |
215 | _xml_add_time = atoi(char_data_buffer); | |
216 | } else if (strcmp("RAMP_TIME", _xml_element[5]) == 0) { | |
217 | _xml_add_ramp = atoi(char_data_buffer); | |
218 | } | |
219 | } else if ((_xml_depth >= 4) && (strcmp("TUN_TEMP", _xml_element[3]) == 0)) { | |
220 | // Save this and check later if this is the strike temperature. | |
221 | _xml_tun_temp = atof(char_data_buffer); | |
222 | } else if ((_xml_depth >= 4) && (strcmp("SPARGE_TEMP", _xml_element[3]) == 0)) { | |
223 | recipe.SpargeTemp = atof(char_data_buffer); | |
224 | } | |
225 | } | |
226 | } | |
227 | } | |
228 | } | |
229 | ||
230 | ||
231 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
232 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
233 | * @brief Start new XML element. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
234 | * @param userData |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
235 | * @param name XML element name |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
236 | * @param attr XML element attribute |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
237 | */ |
0 | 238 | void startElement(void *userData, const char *name, const char **attr) |
239 | { | |
240 | sprintf(_xml_element[_xml_depth], "%s", name); | |
241 | _xml_depth++; | |
242 | process_char_data_buffer(); | |
243 | reset_char_data_buffer(); | |
244 | } | |
245 | ||
246 | ||
247 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
248 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
249 | * @brief End XML element |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
250 | * @param userData |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
251 | * @param name XML element name |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
252 | */ |
0 | 253 | void endElement(void *userData, const char *name) |
254 | { | |
255 | process_char_data_buffer(); | |
256 | reset_char_data_buffer(); | |
257 | if ((_xml_depth == 4) && (strcmp("HOP", _xml_element[3]) == 0)) { | |
258 | if (_xml_add_valid) { | |
259 | Addition_Add(_xml_add_name, ADDITION_HOP, _xml_add_time); | |
260 | } | |
261 | } | |
262 | if ((_xml_depth == 4) && (strcmp("FERMENTABLE", _xml_element[3]) == 0)) { | |
263 | if (_xml_add_valid && (_xml_add_type == ADDITION_FERMENTABLE)) { | |
264 | Addition_Add(_xml_add_name, _xml_add_type, _xml_add_time); | |
265 | } | |
266 | } | |
267 | if ((_xml_depth == 4) && (strcmp("MISC", _xml_element[3]) == 0)) { | |
268 | if (_xml_add_valid) { | |
269 | Addition_Add(_xml_add_name, _xml_add_type, _xml_add_time); | |
270 | } | |
271 | } | |
272 | if ((_xml_depth == 5) && (strcmp("MASH_STEP", _xml_element[4]) == 0)) { | |
273 | // printf("Flush End MASH_STEP %d %s\n", _xml_depth, _xml_add_name); | |
274 | _xml_mashsteps++; | |
275 | sprintf(recipe.MashStep[_xml_mashsteps].Name, "%s", _xml_add_name); | |
276 | recipe.MashStep[_xml_mashsteps].Temperature = _xml_add_temp; | |
277 | recipe.MashStep[_xml_mashsteps].Resttime = _xml_add_time; | |
278 | recipe.MashStep[_xml_mashsteps].Steptime = _xml_add_ramp; | |
279 | } | |
280 | _xml_depth--; | |
281 | } | |
282 | ||
283 | ||
284 | ||
285 | int ParseRecipe(char *fn, char *code) | |
286 | { | |
287 | char buf[512]; | |
288 | ||
289 | memset(&recipe, 0, sizeof(recipe)); | |
290 | XML_Parser parser = XML_ParserCreate(NULL); | |
291 | ||
292 | int done; | |
293 | _xml_depth = 0; | |
294 | _xml_mashsteps = 0; | |
295 | _xml_tun_temp = 0.0; | |
296 | ||
297 | XML_SetElementHandler(parser, startElement, endElement); | |
298 | XML_SetCharacterDataHandler(parser, char_data); | |
299 | ||
300 | FILE *fp = fopen(fn, "r"); | |
301 | ||
302 | do { | |
303 | int len = (int)fread(buf, 1, sizeof(buf), fp); | |
304 | done = len < sizeof(buf); | |
305 | if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) { | |
306 | printf( "%s at line %5lu\n", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser)); | |
307 | return 1; | |
308 | } | |
309 | vTaskDelay(2 / portTICK_PERIOD_MS); | |
310 | } while (!done); | |
311 | ||
312 | XML_ParserFree(parser); | |
313 | fclose(fp); | |
314 | ||
315 | /* | |
316 | * Fix missing things. | |
317 | */ | |
318 | snprintf(recipe.Code, 31, "%s", code); | |
319 | // The code is created from the recipe filename. | |
320 | for (int i = 0; i < strlen(recipe.Code); i++) { | |
321 | if ((recipe.Code[i] == ' ') || (recipe.Code[i] == '.')) { | |
322 | recipe.Code[i] = '\0'; | |
323 | break; | |
324 | } | |
325 | } | |
326 | recipe.CoolTemp = 20.0; | |
327 | sprintf(recipe.MashStep[0].Name, "Mash-in"); | |
328 | if (_xml_tun_temp > recipe.MashStep[1].Temperature) { | |
329 | recipe.MashStep[0].Temperature = _xml_tun_temp; | |
330 | } else { | |
331 | recipe.MashStep[0].Temperature = recipe.MashStep[1].Temperature + 1.25; | |
332 | } | |
333 | recipe.MashStep[0].Resttime = 1; | |
334 | ||
335 | if (! recipe.MashStep[7].Resttime) { | |
336 | // Move last mash step to position 7. | |
337 | for (int i = 6; i > 1; i--) { | |
338 | if (recipe.MashStep[i].Resttime) { | |
339 | // Got it, move. | |
340 | sprintf(recipe.MashStep[7].Name, "%s", recipe.MashStep[i].Name); | |
341 | recipe.MashStep[i].Name[0] = '\0'; | |
342 | recipe.MashStep[7].Temperature = recipe.MashStep[i].Temperature; | |
343 | recipe.MashStep[i].Temperature = 0.0; | |
344 | recipe.MashStep[7].Resttime = recipe.MashStep[i].Resttime; | |
345 | recipe.MashStep[i].Resttime = 0; | |
346 | recipe.MashStep[7].Steptime = recipe.MashStep[i].Steptime; | |
347 | recipe.MashStep[i].Steptime = 0; | |
348 | break; | |
349 | } | |
350 | } | |
351 | } | |
352 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
353 | #if 0 |
0 | 354 | printf("Recipe: %s\n", recipe.Name); |
355 | printf("Code : %s\n", recipe.Code); | |
356 | printf("Boil time %d minutes\n", recipe.BoilTime); | |
357 | printf("n Stepname temp time ramp\n"); | |
358 | printf("- ------------------------------ ----- ---- ----\n"); | |
359 | for (int i = 0; i < 8; i++) { | |
360 | if (recipe.MashStep[i].Resttime) { | |
361 | printf("%d %-31s %5.2f %4d %4d\n", i, recipe.MashStep[i].Name, recipe.MashStep[i].Temperature, | |
362 | recipe.MashStep[i].Resttime, recipe.MashStep[i].Steptime); | |
363 | } | |
364 | } | |
365 | printf("%d additions\n", recipe.Additions); | |
366 | printf("n Addition name t Tim\n"); | |
367 | printf("- --------------------------------------------------------------- - ---\n"); | |
368 | for (int i = 0; i < recipe.Additions; i++) { | |
369 | printf("%d %-63s %d %3d\n", i, recipe.Addition[i].Name, recipe.Addition[i].Type, recipe.Addition[i].Time); | |
370 | } | |
371 | printf("Cooltemp %5.2f\n", recipe.CoolTemp); | |
372 | if (recipe.Whirlpool9) | |
373 | printf("Whirlpool9 %d\n", recipe.Whirlpool9); | |
374 | if (recipe.Whirlpool7) | |
375 | printf("Whirlpool7 %d\n", recipe.Whirlpool7); | |
376 | if (recipe.Whirlpool6) | |
377 | printf("Whirlpool6 %d\n", recipe.Whirlpool6); | |
378 | if (recipe.Whirlpool2) | |
379 | printf("Whirlpool2 %d\n", recipe.Whirlpool2); | |
380 | printf("SpargeTemp %5.2f\n", recipe.SpargeTemp); | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
381 | #endif |
0 | 382 | |
383 | return 0; | |
384 | } | |
385 | ||
386 | ||
387 | ||
388 | /* | |
389 | * Recipes init function, only runs once a new screen is entered. | |
390 | */ | |
391 | void Recipes_Init(void) | |
392 | { | |
393 | FILE *f; | |
394 | DIR *dir; | |
395 | struct dirent *de; | |
396 | size_t bytes; | |
397 | uint8_t *dst; | |
398 | uint16_t y; | |
399 | char filename[256], newname[256]; | |
400 | int rc; | |
401 | ||
402 | switch (Main_Screen) { | |
403 | case MAIN_TOOLS_RECIPE: | |
404 | TopMessage("Recepten importeren"); | |
405 | r_Imported = 0; | |
406 | TFT_setFont(DEFAULT_FONT, NULL); | |
407 | y = 28; | |
408 | if ((dir = opendir("/sdcard/recipe"))) { | |
409 | de = readdir(dir); | |
410 | while (de) { | |
411 | if (strstr(de->d_name, ".xml") || strstr(de->d_name, ".XML")) { | |
412 | _fg = TFT_YELLOW; | |
413 | TFT_print(de->d_name, 2, y); | |
414 | snprintf(filename, 255, "/sdcard/recipe/%s", de->d_name); | |
415 | snprintf(newname, 255, "/sdcard/recipe/%s", de->d_name); | |
416 | newname[strlen(newname) -2] = 'o'; | |
417 | newname[strlen(newname) -1] = 'k'; | |
418 | rc = ParseRecipe(filename, de->d_name); | |
419 | ESP_LOGI(TAG, "Recipe %s parsed, rc=%d", filename, rc); | |
420 | if (rc == 0) { | |
421 | append_recipe(); | |
422 | rename(filename, newname); | |
423 | _fg = TFT_GREEN; | |
424 | TFT_print(" Ok", LASTX, y); | |
425 | } else { | |
426 | _fg = TFT_RED; | |
427 | TFT_print(" Fout", LASTX, y); | |
428 | } | |
429 | r_Imported++; // Count them all | |
430 | y += 16; | |
431 | } | |
432 | de = readdir(dir); | |
433 | } | |
434 | closedir(dir); | |
435 | } | |
436 | ||
437 | f = fopen("/spiffs/etc/recipe.conf", "r"); | |
438 | dst = (uint8_t*)&recipe; | |
439 | r_Records = 0; | |
440 | while ((bytes = fread(dst, 1, sizeof(recipe), f))) { | |
441 | r_Records++; | |
442 | } | |
443 | fclose(f); | |
444 | // Load the default record. | |
445 | r_CurrentRec = config.RecipeRec; | |
446 | r_UpdateRec = true; | |
447 | break; | |
448 | ||
449 | case MAIN_TOOLS_RECIPE_EDIT: | |
450 | break; | |
451 | ||
452 | default: break; | |
453 | } | |
454 | } | |
455 | ||
456 | ||
457 | ||
458 | /* | |
459 | * Recipes management loop, non-blocking. | |
460 | */ | |
461 | void Recipes_Loop(void) | |
462 | { | |
463 | uint32_t crc1, crc2; | |
464 | uint8_t *dst; | |
465 | int mashsteps; | |
466 | uint16_t y; | |
467 | char tmp[32]; | |
468 | float mintemp; | |
469 | ||
470 | switch (Main_Screen) { | |
471 | case MAIN_TOOLS_RECIPE: | |
472 | if (r_Imported) { | |
473 | Buttons_Clear(); | |
474 | Buttons_Add(135, 210, 50, 30, "Ok" , 0); | |
475 | Buttons_Show(); | |
476 | ||
477 | while (true) { | |
478 | if (Buttons_Scan() == 0) | |
479 | break; | |
480 | vTaskDelay(20 / portTICK_PERIOD_MS); | |
481 | } | |
482 | Buttons_Clear(); | |
483 | r_Imported = 0; | |
484 | } | |
485 | if (r_UpdateRec) { | |
486 | _bg = TFT_BLACK; | |
487 | TFT_fillScreen(_bg); | |
488 | TFT_resetclipwin(); | |
489 | TopMessage("Recepten"); | |
490 | r_UpdateRec = false; | |
491 | read_recipe(r_CurrentRec); | |
492 | TFT_setFont(DEFAULT_FONT, NULL); | |
493 | ShowText(2, 28, "Naam", recipe.Name); | |
494 | ShowText(2, 44, "Code", recipe.Code); | |
495 | ShowInteger(162, 44, "Record", NULL, recipe.Record); | |
496 | ShowInteger(2, 60, "Kooktijd", " min", recipe.BoilTime); | |
497 | ShowFloat(162, 60, "Koel tot", " C", recipe.CoolTemp, 2); | |
498 | ShowFloat(2, 76, "Maisch in", " C", recipe.MashStep[0].Temperature, 2); | |
499 | ShowFloat(162, 76, "Spoelwater", " C", recipe.SpargeTemp, 2); | |
500 | y = 92; | |
501 | _fg = TFT_WHITE; | |
502 | TFT_print("Maisch stap", 2, y); | |
503 | TFT_print("Temp.", 200, y); | |
504 | TFT_print("Rust", 260, y); | |
505 | _fg = TFT_YELLOW; | |
506 | y += 16; | |
507 | for (int i = 1; i < 8; i++) { | |
508 | if (recipe.MashStep[i].Resttime) { | |
509 | TFT_print(recipe.MashStep[i].Name, 2, y); | |
510 | sprintf(tmp, "%.2f", recipe.MashStep[i].Temperature); | |
511 | TFT_print(tmp, 200, y); | |
512 | sprintf(tmp, "%2d min", recipe.MashStep[i].Resttime); | |
513 | TFT_print(tmp, 260, y); | |
514 | y += 16; | |
515 | } | |
516 | } | |
517 | if (recipe.Additions) { | |
518 | _fg = TFT_YELLOW; | |
519 | sprintf(tmp, "%d ", recipe.Additions); | |
520 | TFT_print(tmp, 2, y); | |
521 | _fg = TFT_WHITE; | |
522 | TFT_print("toevoegingen om", LASTX, y); | |
523 | _fg = TFT_YELLOW; | |
524 | for (int i = 1; i <= recipe.Additions; i++) { | |
525 | sprintf(tmp, " %d", recipe.Addition[i-1].Time); | |
526 | TFT_print(tmp, LASTX, y); | |
527 | } | |
528 | _fg = TFT_WHITE; | |
529 | TFT_print(" minuten", LASTX, y); | |
530 | } else { | |
531 | _fg = TFT_WHITE; | |
532 | TFT_print("Geen hop toevoegingen.", 2, y); | |
533 | } | |
534 | y += 16; | |
535 | if (recipe.Whirlpool9 || recipe.Whirlpool7 || recipe.Whirlpool6 || recipe.Whirlpool2) { | |
536 | _fg = TFT_WHITE; | |
537 | TFT_print("Whirlpool ", 2, y); | |
538 | _fg = TFT_YELLOW; | |
539 | if (recipe.Whirlpool9) | |
540 | TFT_print("88+ ", LASTX, y); | |
541 | if (recipe.Whirlpool7) | |
542 | TFT_print("71+ ", LASTX, y); | |
543 | if (recipe.Whirlpool6) | |
544 | TFT_print("60+ ", LASTX, y); | |
545 | if (recipe.Whirlpool2) | |
546 | TFT_print("koud", LASTX, y); | |
547 | } | |
548 | ||
549 | Buttons_Clear(); | |
550 | Buttons_Add( 0, 210, 45, 30, "Ok" , 0); | |
551 | Buttons_Add( 46, 210, 45, 30, "+" , 1); | |
552 | if (r_CurrentRec != config.RecipeRec) | |
553 | Buttons_Add( 92, 210, 45, 30, "-", 2); | |
554 | else | |
555 | Buttons_Add( 92, 210, 45, 30, "" , 2); | |
556 | if (r_CurrentRec > 1) | |
557 | Buttons_Add(138, 210, 45, 30, "<", 3); | |
558 | else | |
559 | Buttons_Add(138, 210, 45, 30, "", 3); | |
560 | if (r_CurrentRec < r_Records) | |
561 | Buttons_Add(184, 210, 45, 30, ">", 4); | |
562 | else | |
563 | Buttons_Add(184, 210, 45, 30, "", 4); | |
564 | if (r_CurrentRec != config.RecipeRec) | |
565 | Buttons_Add(230, 210, 45, 30, "Std", 5); | |
566 | else | |
567 | Buttons_Add(230, 210, 45, 30, "", 5); | |
568 | Buttons_Add(276, 210, 45, 30, "Ed" , 6); | |
569 | Buttons[0].dark = true; | |
570 | Buttons_Show(); | |
571 | } /* if (r_UpdateRec) */ | |
572 | switch (Buttons_Scan()) { | |
573 | case 0: Main_Screen = MAIN_TOOLS; | |
574 | break; | |
575 | ||
576 | case 1: memset(&recipe, 0, sizeof(recipe)); | |
577 | recipe.Version = 1; | |
578 | recipe.Record = r_Records + 1; | |
579 | sprintf(recipe.Name, "Recipe %d", r_Records + 1); | |
580 | sprintf(recipe.Code, "00%d", r_Records + 1); | |
581 | sprintf(recipe.MashStep[0].Name, "Mash-in"); | |
582 | recipe.MashStep[0].Temperature = 67.5; | |
583 | recipe.MashStep[0].Resttime = 1; | |
584 | sprintf(recipe.MashStep[1].Name, "Mash"); | |
585 | recipe.MashStep[1].Temperature = 67.0; | |
586 | recipe.MashStep[1].Resttime = 75; | |
587 | sprintf(recipe.MashStep[7].Name, "Mash-out"); | |
588 | recipe.MashStep[7].Temperature = 78.0; | |
589 | recipe.MashStep[7].Resttime = 5; | |
590 | recipe.BoilTime = 60; | |
591 | recipe.Additions = 2; | |
592 | sprintf(recipe.Addition[0].Name, "Hop"); | |
593 | recipe.Addition[0].Time = 60; | |
594 | recipe.Addition[0].Type = ADDITION_HOP; | |
595 | sprintf(recipe.Addition[1].Name, "Hop"); | |
596 | recipe.Addition[1].Time = 10; | |
597 | recipe.Addition[1].Type = ADDITION_HOP; | |
598 | recipe.CoolTemp = 20.0; | |
599 | recipe.Whirlpool9 = 0; | |
600 | recipe.Whirlpool7 = 0; | |
601 | recipe.Whirlpool6 = 0; | |
602 | recipe.Whirlpool2 = 0; | |
603 | recipe.SpargeTemp = 85.0; | |
604 | append_recipe(); | |
605 | r_Records++; | |
606 | r_CurrentRec = r_Records; | |
607 | r_UpdateRec = true; | |
608 | ESP_LOGI(TAG, "New recipe record %d", recipe.Record); | |
609 | break; | |
610 | ||
611 | case 2: if ((r_CurrentRec != config.RecipeRec) && (r_Records > 1)) { | |
612 | delete_recipe(r_CurrentRec); | |
613 | r_Records--; | |
614 | if (r_CurrentRec > r_Records) | |
615 | r_CurrentRec = r_Records; | |
616 | r_UpdateRec = true; | |
617 | } | |
618 | break; | |
619 | ||
620 | case 3: if (r_CurrentRec > 1) { | |
621 | r_CurrentRec--; | |
622 | r_UpdateRec = true; | |
623 | } | |
624 | break; | |
625 | ||
626 | case 4: if (r_CurrentRec < r_Records) { | |
627 | r_CurrentRec++; | |
628 | r_UpdateRec = true; | |
629 | } | |
630 | break; | |
631 | ||
632 | case 5: if (r_CurrentRec != config.RecipeRec) { | |
633 | config.RecipeRec = r_CurrentRec; | |
634 | write_config(); | |
635 | r_UpdateRec = true; | |
636 | ESP_LOGI(TAG, "Recipe %d `%s' set as default", r_CurrentRec, recipe.Name); | |
637 | } | |
638 | break; | |
639 | ||
640 | case 6: Main_Screen = MAIN_TOOLS_RECIPE_EDIT; | |
641 | break; | |
642 | ||
643 | default: break; | |
644 | } | |
645 | break; | |
646 | ||
647 | case MAIN_TOOLS_RECIPE_EDIT: | |
648 | dst = (uint8_t*)&recipe; | |
649 | crc1 = crc32_le(0, dst, sizeof(recipe)); | |
650 | ||
651 | EditText("Naam", recipe.Name, 31); | |
652 | EditText("Code", recipe.Code, 31); | |
653 | mashsteps = 0; | |
654 | for (int i = 1; i < 7; i++) { | |
655 | if (recipe.MashStep[i].Resttime) | |
656 | mashsteps++; | |
657 | } | |
658 | EditInt("Maisch stappen", &mashsteps, 1, 6); | |
659 | EditFloat("Inmaisch temperatuur", &recipe.MashStep[0].Temperature, 38, 74, 2); | |
660 | // Round to 0.25 values | |
661 | recipe.MashStep[0].Temperature = ((int)(recipe.MashStep[0].Temperature * 4)) / 4.0; | |
662 | for (int i = 1; i <= mashsteps; i++) { | |
663 | sprintf(tmp, "Maisch stap %d naam", i); | |
664 | EditText(tmp, recipe.MashStep[i].Name, 31); | |
665 | sprintf(tmp, "Maisch stap %d temperatuur", i); | |
666 | if (i == 1) | |
667 | mintemp = recipe.MashStep[0].Temperature - 5; | |
668 | else | |
669 | mintemp = recipe.MashStep[i - 1].Temperature; | |
3 | 670 | EditFloat(tmp, &recipe.MashStep[i].Temperature, mintemp, 74, 2); |
0 | 671 | recipe.MashStep[i].Temperature = ((int)(recipe.MashStep[i].Temperature * 4)) / 4.0; |
672 | sprintf(tmp, "Maisch stap %d rusttijd in minuten", i); | |
673 | EditUint16(tmp, &recipe.MashStep[i].Resttime, 1, 480); | |
674 | if (i == 1) { | |
675 | recipe.MashStep[i].Steptime = 1; | |
676 | } else { | |
677 | recipe.MashStep[i].Steptime = (int)(recipe.MashStep[i].Temperature - recipe.MashStep[i - 1].Temperature); | |
678 | } | |
679 | } | |
680 | mintemp = recipe.MashStep[mashsteps].Temperature; | |
681 | EditFloat("Uitmaischen temperatuur", &recipe.MashStep[7].Temperature, mintemp, 80, 2); | |
682 | recipe.MashStep[7].Temperature = ((int)(recipe.MashStep[7].Temperature * 4)) / 4.0; | |
683 | EditUint16("Uitmaischen tijd in minuten", &recipe.MashStep[7].Resttime, 1, 15); | |
684 | recipe.MashStep[7].Steptime = (int)(recipe.MashStep[7].Temperature - recipe.MashStep[mashsteps].Temperature); | |
685 | // Zero any higher steps to diable them. | |
686 | for (int i = (mashsteps + 1); i < 7; i++) | |
687 | recipe.MashStep[i].Resttime = 0; | |
688 | ||
689 | EditUint16("Kook tijd in minuten", &recipe.BoilTime, 3, 480); | |
690 | EditUint8("Hop/kruiden toevoegingen", &recipe.Additions, 1, 10); | |
691 | for (uint8_t i = 0; i < recipe.Additions; i++) { | |
692 | sprintf(tmp, "Toevoeging %d maam", i+1); | |
693 | if (strlen(recipe.Addition[i].Name) == 00) { | |
694 | sprintf(recipe.Addition[i].Name, "Hop %d", (int)i+1); | |
695 | } | |
696 | EditText(tmp, recipe.Addition[i].Name, 40); | |
697 | sprintf(tmp, "Toevoeging %d tijd", i+1); | |
698 | if (i == 0) { | |
699 | EditUint16(tmp, &recipe.Addition[i].Time, 0, recipe.BoilTime); | |
700 | } else { | |
701 | EditUint16(tmp, &recipe.Addition[i].Time, 0, recipe.Addition[i-1].Time - 1); | |
702 | } | |
703 | recipe.Addition[i].Type = ADDITION_HOP; | |
704 | } | |
705 | ||
706 | EditFloat("Koel temperatuur", &recipe.CoolTemp, 10, 30, 2); | |
707 | recipe.CoolTemp = ((int)(recipe.CoolTemp * 4)) / 4.0; | |
708 | EditFloat("Spoelwater temperatuur", &recipe.SpargeTemp, 75, 98, 2); | |
709 | recipe.SpargeTemp = ((int)(recipe.SpargeTemp * 4)) / 4.0; | |
710 | EditUint16("Whirlpool 88..100 graden, 0 = niet", &recipe.Whirlpool9, 0, 120); | |
711 | EditUint16("Whirlpool 71..77 graden, 0 = niet", &recipe.Whirlpool7, 0, 120); | |
712 | EditUint16("Whirlpool 60..66 graden, 0 = niet", &recipe.Whirlpool6, 0, 120); | |
713 | EditUint16("Whirlpool koud, 0 = niet", &recipe.Whirlpool2, 0, 120); | |
714 | ||
715 | crc2 = crc32_le(0, dst, sizeof(recipe)); | |
716 | if ((crc1 != crc2) && Confirm("Gewijzigd, opslaan?", "Ja", "Nee")) { | |
717 | write_recipe(recipe.Record); | |
718 | } | |
719 | Main_Screen = MAIN_TOOLS_RECIPE; | |
720 | break; | |
721 | ||
722 | default: break; | |
723 | } | |
724 | } | |
725 | ||
726 |