Fri, 26 Oct 2018 23:32:32 +0200
Removed some unused code from the vnc websocket connection
0 | 1 | /** |
2 | * @file task_sdcard.c | |
3 | * @brief SD/MMC driver. This driver is for a slot for the user on | |
4 | * the front panel. It will detect an inserted card and then | |
4 | 5 | * mount it. It also detects the removal of the card and then |
6 | * unmounts it. Be carefull, only do this in the main menu. | |
0 | 7 | * Also, brew logging is handled here and finished brewlogs |
8 | * are copied to the SD card if it is mounted. | |
9 | * Recipes to import must go into the /sdcard/recipe folder | |
10 | * and have extension .xml and the contents be a beerxml file. | |
4 | 11 | * Backup and restore is also done to this card. |
0 | 12 | */ |
13 | ||
14 | ||
15 | #include "vfs_fat_internal.h" | |
16 | #include "driver/sdmmc_host.h" | |
17 | #include "driver/sdspi_host.h" | |
18 | #include "sdmmc_cmd.h" | |
19 | #include "diskio.h" | |
20 | ||
21 | #include "config.h" | |
22 | ||
23 | ||
24 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
25 | SDCARD_State *sdcard_state; ///< SD card status |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
26 | JSON_log *json_log; ///< JSON log array |
0 | 27 | EventGroupHandle_t xEventGroupSDcard; ///< SD card events. |
28 | ||
29 | static const char *TAG = "task_sdcard"; | |
30 | static sdmmc_card_t* s_card = NULL; | |
31 | static uint8_t s_pdrv = 0; | |
32 | static char * s_base_path = NULL; | |
33 | ||
34 | BYTE pdrv = 0xFF; | |
35 | ||
36 | ||
37 | #define SDCARD_HOST_SLOT VSPI_HOST ///< HSPI_HOST is used by the TFT | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
38 | #define SDCARD_PIN_NUM_MISO 2 ///< MISO pin |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
39 | #define SDCARD_PIN_NUM_MOSI 15 ///< MOSI pin |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
40 | #define SDCARD_PIN_NUM_CLK 14 ///< CLOCK |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
41 | #define SDCARD_PIN_NUM_CS 13 ///< Chip select pin |
0 | 42 | #define SDCARD_DMA_CHANNEL 2 ///< Channel 1 is used by the TFT |
43 | ||
44 | ||
45 | const int TASK_SDCARD_LOG_CLEAN = BIT0; ///< Clean spiffs logfile directory | |
46 | const int TASK_SDCARD_LOG_CLOSE = BIT1; ///< Close the logfile. | |
47 | ||
48 | extern time_t now; | |
49 | extern struct tm timeinfo; | |
50 | ||
51 | ||
52 | ||
53 | void log_begin(time_t t) | |
54 | { | |
55 | struct tm timeinfo; | |
56 | ||
57 | /* | |
58 | * If there is an open logfile from an crashed brew, open that one. | |
59 | */ | |
60 | if (! strlen(sdcard_state->logfile) && strlen(runtime.Logfile)) { | |
61 | sprintf(sdcard_state->logfile, "%s", runtime.Logfile); | |
62 | ESP_LOGI(TAG, "Resumed %s", sdcard_state->logfile); | |
63 | } else { | |
64 | localtime_r(&t, &timeinfo); | |
65 | if (timeinfo.tm_year > (2016 - 1900)) { | |
66 | // Valid time, construct the filename. | |
67 | strftime(sdcard_state->logfile, sizeof(sdcard_state->logfile), "br%y%m%d%H%M", &timeinfo); | |
68 | } else { | |
69 | sprintf(sdcard_state->logfile, "brewlog"); | |
70 | } | |
71 | ESP_LOGI(TAG, "Create %s", sdcard_state->logfile); | |
72 | sprintf(runtime.Logfile, "%s", sdcard_state->logfile); | |
73 | } | |
74 | } | |
75 | ||
76 | ||
77 | ||
78 | void log_close(void) | |
79 | { | |
80 | xEventGroupSetBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLOSE); | |
81 | } | |
82 | ||
83 | ||
84 | ||
85 | void log_clean(void) | |
86 | { | |
87 | xEventGroupSetBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLEAN); | |
88 | } | |
89 | ||
90 | ||
91 | ||
92 | void log_json(void) | |
93 | { | |
94 | char filename[32], strftime_buf[64]; | |
95 | FILE *f; | |
96 | bool addcomma = true; | |
97 | ||
98 | if (strlen(sdcard_state->logfile)) { | |
99 | sprintf(filename, "/spiffs/log/%s.json", sdcard_state->logfile); | |
100 | f = fopen(filename, "r"); | |
101 | if (f == NULL) { | |
102 | // Create the file and add the header | |
103 | f = fopen(filename, "a"); | |
104 | if (f) { | |
105 | strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); | |
106 | fprintf(f, "{\n"); | |
107 | fprintf(f, " \"brew\": [{\n"); | |
108 | fprintf(f, " \"Recipe\":\"%s\",\n", recipe.Name); | |
109 | fprintf(f, " \"Date\":\"%s\",\n", strftime_buf); | |
110 | fprintf(f, " \"brewdata\":[\n"); | |
111 | addcomma = false; | |
112 | fclose(f); | |
113 | } else { | |
114 | ESP_LOGE(TAG, "Failed to create %s error %d", filename, errno); | |
115 | return; | |
116 | } | |
117 | } else { | |
118 | fclose(f); // Was open for reading. | |
119 | } | |
120 | ||
121 | f = fopen(filename, "a"); | |
122 | if (f) { | |
123 | if (addcomma) { | |
124 | fprintf(f, ",\n"); | |
125 | } | |
126 | addcomma = true; | |
127 | fprintf(f, " {\"MLT_sp\":\"%.3f\",\"MLT_pv\":\"%.3f\",\"MLT_pwm\":\"%d\",\"MLT_tr\":\"%d\",\"Pump\":\"%d\",", | |
128 | json_log->mlt_sp, json_log->mlt_pv, json_log->mlt_power, json_log->mlt_tempreached, json_log->pump_run); | |
129 | if (json_log->hlt_sp > 0.0) { | |
130 | fprintf(f, "\"HLT_sp\":\"%.3f\",\"HLT_pv\":\"%.3f\",\"HLT_pwm\":\"%d\",", json_log->hlt_sp, json_log->hlt_pv, json_log->hlt_power); | |
131 | } | |
132 | fprintf(f, "\"Label\":\"%s\"}", json_log->time); | |
133 | fclose(f); | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | ||
139 | ||
140 | void log_annotation(int annotation_type, char *label) | |
141 | { | |
142 | char filename[32]; | |
143 | char bordercolor[9], color[9], pos[8]; | |
144 | FILE *f; | |
145 | bool addcomma = true; | |
146 | ||
147 | if (strlen(sdcard_state->logfile)) { | |
148 | sprintf(filename, "/spiffs/log/%s.anno", sdcard_state->logfile); | |
149 | ||
150 | switch (annotation_type) { | |
151 | case ANNOTATION_STAGE: snprintf(bordercolor, 8, "#8942f4"); | |
152 | snprintf(color, 8, "#00215b"); | |
153 | snprintf(pos, 7, "bottom"); | |
154 | break; | |
155 | ||
156 | case ANNOTATION_EVENT: snprintf(bordercolor, 8, "#42f445"); | |
157 | snprintf(color, 8, "#00215b"); | |
158 | snprintf(pos, 7, "top"); | |
159 | break; | |
160 | ||
161 | case ANNOTATION_SYSTEM: snprintf(bordercolor, 8, "black"); | |
162 | snprintf(color, 8, "red"); | |
163 | snprintf(pos, 7, "center"); | |
164 | break; | |
165 | } | |
166 | ||
167 | // Check if the file exists to see if we need to insert a comma. | |
168 | f = fopen(filename, "r"); | |
169 | if (f == NULL) { | |
170 | addcomma = false; | |
171 | } else { | |
172 | fclose(f); | |
173 | } | |
174 | ||
175 | f = fopen(filename, "a"); | |
176 | if (f) { | |
177 | if (addcomma) { | |
178 | fprintf(f, ",\n"); | |
179 | } | |
180 | addcomma = true; | |
181 | fprintf(f, "{\"type\":\"line\",\"mode\":\"vertical\",\"scaleID\":\"x-axis-0\",\"value\":\"%s\",\"borderColor\":\"%s\",\"borderWidth\":2,\"label\":{\"backgroundColor\":\"%s\",\"content\":\"%s\",\"position\":\"%s\",\"enabled\":true}}", json_log->time, bordercolor, color, label, pos); | |
182 | fclose(f); | |
183 | } | |
184 | } | |
185 | } | |
186 | ||
187 | ||
188 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
189 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
190 | * @brief This is a local modified version of the esp_vfs_fat_sdmmc_mount() function in |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
191 | * the FreeRTOS components library. It is here so we can better handle errors |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
192 | * for our application. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
193 | * @param base_path The mount path |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
194 | * @param host_config SPI host configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
195 | * @param slot_config Slot configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
196 | * @return Error condition. |
0 | 197 | */ |
198 | esp_err_t my_vfs_fat_sdmmc_init(const char* base_path, const sdmmc_host_t* host_config, const void* slot_config) | |
199 | { | |
200 | if (s_card != NULL) { | |
201 | return ESP_ERR_INVALID_STATE; | |
202 | } | |
203 | ||
204 | // connect SDMMC driver to FATFS | |
205 | pdrv = 0xFF; | |
206 | if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == 0xFF) { | |
207 | ESP_LOGI(TAG, "the maximum count of volumes is already mounted"); | |
208 | return ESP_ERR_NO_MEM; | |
209 | } | |
210 | ||
211 | s_base_path = strdup(base_path); | |
212 | if (!s_base_path) { | |
213 | ESP_LOGI(TAG, "could not copy base_path"); | |
214 | return ESP_ERR_NO_MEM; | |
215 | } | |
216 | esp_err_t err = ESP_OK; | |
217 | s_card = malloc(sizeof(sdmmc_card_t)); | |
218 | if (s_card == NULL) { | |
219 | err = ESP_ERR_NO_MEM; | |
220 | goto fail; | |
221 | } | |
222 | ||
223 | err = (*host_config->init)(); | |
224 | if (err != ESP_OK) { | |
225 | ESP_LOGI(TAG, "host init returned rc=0x%x", err); | |
226 | goto fail; | |
227 | } | |
228 | ||
229 | // configure SD slot | |
230 | if (host_config->flags == SDMMC_HOST_FLAG_SPI) { | |
231 | err = sdspi_host_init_slot(host_config->slot, (const sdspi_slot_config_t*) slot_config); | |
232 | } else { | |
233 | err = sdmmc_host_init_slot(host_config->slot, (const sdmmc_slot_config_t*) slot_config); | |
234 | } | |
235 | if (err != ESP_OK) { | |
236 | ESP_LOGI(TAG, "slot_config returned rc=0x%x", err); | |
237 | goto fail; | |
238 | } | |
239 | return ESP_OK; | |
240 | ||
241 | fail: | |
242 | host_config->deinit(); | |
243 | free(s_card); | |
244 | s_card = NULL; | |
245 | return err; | |
246 | } | |
247 | ||
248 | ||
249 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
250 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
251 | * @brief Mount SD card. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
252 | * @param base_path The mountpoint |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
253 | * @param host_config SPI host configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
254 | * @param slot_config Slot configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
255 | * @param mount_config Mount configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
256 | * @param[out] out_card Card information |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
257 | * @return Error condition |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
258 | */ |
0 | 259 | esp_err_t my_esp_vfs_fat_sdmmc_mount(const char* base_path, |
260 | const sdmmc_host_t* host_config, | |
261 | const void* slot_config, | |
262 | const esp_vfs_fat_mount_config_t* mount_config, | |
263 | sdmmc_card_t** out_card) | |
264 | { | |
265 | FATFS* fs = NULL; | |
266 | esp_err_t err = ESP_OK; | |
267 | ||
268 | if (s_card == NULL) { | |
269 | return ESP_ERR_INVALID_STATE; | |
270 | } | |
271 | ||
272 | // probe and initialize card | |
273 | err = sdmmc_card_init(host_config, s_card); | |
274 | if (err != ESP_OK) { | |
275 | if (err != ESP_ERR_INVALID_RESPONSE) { // No card present, do not log | |
276 | ESP_LOGI(TAG, "sdmmc_card_init failed 0x(%x)", err); | |
277 | } | |
278 | goto fail; | |
279 | } | |
280 | if (out_card != NULL) { | |
281 | *out_card = s_card; | |
282 | } | |
283 | ||
284 | ff_diskio_register_sdmmc(pdrv, s_card); | |
285 | s_pdrv = pdrv; | |
286 | ESP_LOGD(TAG, "using pdrv=%i", pdrv); | |
287 | char drv[3] = {(char)('0' + pdrv), ':', 0}; | |
288 | ||
289 | // connect FATFS to VFS | |
290 | err = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs); | |
291 | if (err == ESP_ERR_INVALID_STATE) { | |
292 | // it's okay, already registered with VFS | |
293 | } else if (err != ESP_OK) { | |
294 | ESP_LOGI(TAG, "esp_vfs_fat_register failed 0x(%x)", err); | |
295 | goto fail; | |
296 | } | |
297 | ||
298 | // Try to mount partition | |
299 | FRESULT res = f_mount(fs, drv, 1); | |
300 | if (res != FR_OK) { | |
301 | err = ESP_FAIL; | |
302 | ESP_LOGD(TAG, "f_mount failed (%d)", res); | |
303 | goto fail; | |
304 | } | |
305 | return ESP_OK; | |
306 | ||
307 | fail: | |
308 | if (fs) { | |
309 | f_mount(NULL, drv, 0); | |
310 | } | |
311 | esp_vfs_fat_unregister_path(base_path); | |
312 | ff_diskio_unregister(pdrv); | |
313 | return err; | |
314 | } | |
315 | ||
316 | ||
317 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
318 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
319 | * @brief Unmount a mounted SD card, |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
320 | * @return Error condition |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
321 | */ |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
322 | esp_err_t my_esp_vfs_fat_sdmmc_unmount(void) |
0 | 323 | { |
324 | if (s_card == NULL) { | |
325 | return ESP_ERR_INVALID_STATE; | |
326 | } | |
327 | // unmount | |
328 | char drv[3] = {(char)('0' + s_pdrv), ':', 0}; | |
329 | f_mount(0, drv, 0); | |
330 | return ESP_OK; | |
331 | } | |
332 | ||
333 | ||
334 | ||
335 | int FileCopy(char *ff, char *tf) | |
336 | { | |
337 | FILE *f, *t; | |
338 | uint8_t buf[512]; | |
339 | size_t bytes; | |
340 | ||
341 | f = fopen(ff, "r"); | |
342 | if (f == NULL) { | |
343 | ESP_LOGE(TAG, "FileCopy cannot open %s for read, error %d", ff, errno); | |
344 | return 1; | |
345 | } | |
346 | ||
347 | t = fopen(tf, "w+"); | |
348 | if (t == NULL) { | |
349 | ESP_LOGE(TAG, "FileCopy cannot open %s for create/write, error %d", tf, errno); | |
350 | fclose(f); | |
351 | return 1; | |
352 | } | |
353 | ||
354 | while ((bytes = fread(&buf, 1, 512, f))) { | |
355 | fwrite(&buf, 1, bytes, t); | |
356 | vTaskDelay(10 / portTICK_PERIOD_MS); | |
357 | } | |
358 | ||
359 | fclose(f); | |
360 | fclose(t); | |
361 | return 0; | |
362 | } | |
363 | ||
364 | ||
365 | ||
366 | void task_sdcard(void *pvParameter) | |
367 | { | |
368 | sdmmc_card_t* card; | |
369 | esp_err_t ret; | |
370 | EventBits_t uxBits; | |
371 | char filename[64]; | |
372 | ||
373 | sdcard_state = malloc(sizeof(SDCARD_State)); | |
374 | sdcard_state->host_ok = false; | |
375 | sdcard_state->card_present = false; | |
376 | sdcard_state->logfile[0] = '\0'; | |
377 | ||
378 | json_log = malloc(sizeof(JSON_log)); | |
379 | ||
380 | ESP_LOGI(TAG, "Starting SD card"); | |
381 | sdmmc_host_t host = SDSPI_HOST_DEFAULT(); | |
382 | host.slot = SDCARD_HOST_SLOT; // HSPI_HOST is in use by the TFT. | |
383 | sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT(); | |
384 | slot_config.gpio_miso = SDCARD_PIN_NUM_MISO; | |
385 | slot_config.gpio_mosi = SDCARD_PIN_NUM_MOSI; | |
386 | slot_config.gpio_sck = SDCARD_PIN_NUM_CLK; | |
387 | slot_config.gpio_cs = SDCARD_PIN_NUM_CS; | |
388 | slot_config.dma_channel = SDCARD_DMA_CHANNEL; | |
389 | // This initializes the slot without card detect (CD) and write protect (WP) signals. | |
390 | // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. | |
391 | ||
392 | /* | |
393 | * No errors from the sdmmc_cmd driver. | |
394 | */ | |
395 | esp_log_level_set("sdmmc_cmd", ESP_LOG_NONE); | |
396 | ||
397 | /* | |
398 | * Options for mounting the filesystem. | |
399 | * If format_if_mount_failed is set to true, SD card will be partitioned and | |
400 | * formatted in case when mounting fails. | |
401 | */ | |
402 | esp_vfs_fat_sdmmc_mount_config_t mount_config = { | |
403 | .format_if_mount_failed = false, | |
404 | .max_files = 5, | |
405 | .allocation_unit_size = 16 * 1024 | |
406 | }; | |
407 | ||
408 | ret = my_vfs_fat_sdmmc_init("/sdcard", &host, &slot_config); | |
409 | if (ret == ESP_OK) { | |
410 | ESP_LOGI(TAG, "SPI card interface ready"); | |
411 | sdcard_state->host_ok = true; | |
412 | } else { | |
413 | ESP_LOGE(TAG, "SPI card interface failed, abort task"); | |
414 | vTaskDelete(NULL); | |
415 | return; | |
416 | } | |
417 | ||
418 | xEventGroupSDcard = xEventGroupCreate(); | |
419 | ||
420 | /* | |
421 | * Task loop, continues check of the inserted cards. | |
422 | */ | |
423 | while (1) { | |
424 | ||
425 | if (sdcard_state->card_present == false) { | |
426 | /* | |
427 | * If the card is not mounted, try it. | |
428 | */ | |
429 | ret = my_esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card); | |
430 | if (ret == ESP_OK) { | |
431 | ESP_LOGI(TAG, "SD card mounted on /sdcard"); | |
432 | sdcard_state->card_present = true; | |
433 | ||
434 | DIR* dir = opendir("/sdcard/w/log"); | |
435 | if (dir == NULL) { | |
436 | ret = mkdir("/sdcard/w/log", 0755); | |
437 | } else { | |
438 | closedir(dir); | |
439 | } | |
440 | } | |
441 | } else { | |
442 | /* | |
443 | * Check if the mounted card is still in the slot. | |
444 | */ | |
445 | DIR* dir = opendir("/sdcard/w/log"); | |
446 | if (dir == NULL) { | |
447 | ESP_LOGI(TAG, "SD card missing, unmount"); | |
448 | my_esp_vfs_fat_sdmmc_unmount(); | |
449 | sdcard_state->card_present = false; | |
450 | } else { | |
451 | closedir(dir); | |
452 | } | |
453 | } | |
454 | ||
455 | uxBits = xEventGroupWaitBits(xEventGroupSDcard, | |
456 | TASK_SDCARD_LOG_CLEAN | TASK_SDCARD_LOG_CLOSE, pdFALSE, pdFALSE, 1000 / portTICK_PERIOD_MS); | |
457 | ||
458 | if (uxBits & TASK_SDCARD_LOG_CLEAN) { | |
459 | DIR *dir = opendir("/spiffs/log"); | |
460 | char lf[32]; | |
461 | ||
462 | if (dir != NULL) { | |
463 | struct dirent *de = readdir(dir); | |
464 | while (de) { | |
465 | sprintf(lf, "/spiffs/log/%s", de->d_name); | |
466 | if (unlink(lf) == ESP_OK) { | |
467 | ESP_LOGI(TAG, "Removed old %s", lf); | |
468 | } | |
469 | de = readdir(dir); | |
470 | vTaskDelay(2 / portTICK_PERIOD_MS); | |
471 | } | |
472 | closedir(dir); | |
473 | } | |
474 | xEventGroupClearBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLEAN); | |
475 | } | |
476 | ||
477 | if (uxBits & TASK_SDCARD_LOG_CLOSE) { | |
478 | // Close the logfile. | |
479 | if (strlen(sdcard_state->logfile) && (sdcard_state->card_present == true)) { | |
480 | ESP_LOGI(TAG, "Closing logfile"); | |
481 | char destname[64]; | |
482 | sprintf(destname, "/sdcard/w/log"); | |
483 | int rc = mkdir(destname, 0755); | |
484 | if (rc && (errno != EEXIST)) { | |
485 | ESP_LOGE(TAG, "Cannot create %s error %d", destname, errno); | |
486 | } else { | |
487 | sprintf(filename, "/spiffs/log/%s.json", sdcard_state->logfile); | |
488 | // First close the JSON data records | |
489 | FILE *f = fopen(filename, "a+"); | |
490 | if (f) { | |
491 | fprintf(f, " ],\n"); // End of brewdata | |
492 | fprintf(f, " \"annotations\":[\n"); | |
493 | // Insert annotation records | |
494 | sprintf(destname, "/spiffs/log/%s.anno", sdcard_state->logfile); | |
495 | FILE *a = fopen(destname, "r"); | |
496 | char buf[256]; | |
497 | if (a) { | |
498 | while(true) { | |
499 | if (fgets(buf, sizeof(buf), a)) { | |
500 | fprintf(f, "%s", buf); | |
501 | } else { | |
502 | break; | |
503 | } | |
504 | } | |
505 | fclose(a); | |
506 | unlink(destname); | |
507 | } | |
508 | fprintf(f, " ]\n"); // End of annotations | |
509 | fprintf(f, " }]\n"); // End of brew | |
510 | fprintf(f, "}\n"); | |
511 | fclose(f); | |
512 | } | |
513 | sprintf(destname, "/sdcard/w/log/%s.json", sdcard_state->logfile); | |
514 | if (FileCopy(filename, destname) == 0) { | |
515 | ESP_LOGI(TAG, "JSON file copied to %s", destname); | |
516 | unlink(filename); | |
517 | } | |
518 | } | |
519 | } | |
520 | sdcard_state->logfile[0] = '\0'; // Clear logfile name | |
521 | runtime.Logfile[0] = '\0'; | |
522 | xEventGroupClearBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLOSE); | |
523 | } | |
524 | } | |
525 | } | |
526 | ||
527 |