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