Wed, 08 May 2019 13:46:30 +0200
Merge branch novnc
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; | |
33
dd52aecfc19d
Fix for annotation log before normal log in the json brewlog.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
50 | extern uint32_t TimeBrewing; |
0 | 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"); | |
26
373a33699605
Graph title includes beer code. Hop additions now only display the additions themselfes.
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
108 | fprintf(f, " \"Recipe\":\"%s %s\",\n", recipe.Code, recipe.Name); |
0 | 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; | |
33
dd52aecfc19d
Fix for annotation log before normal log in the json brewlog.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
146 | int Hour = (TimeBrewing / 3600); |
dd52aecfc19d
Fix for annotation log before normal log in the json brewlog.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
147 | int Minute = ((TimeBrewing % 3600) / 60); |
0 | 148 | |
149 | if (strlen(sdcard_state->logfile)) { | |
150 | sprintf(filename, "/spiffs/log/%s.anno", sdcard_state->logfile); | |
151 | ||
152 | switch (annotation_type) { | |
153 | case ANNOTATION_STAGE: snprintf(bordercolor, 8, "#8942f4"); | |
154 | snprintf(color, 8, "#00215b"); | |
155 | snprintf(pos, 7, "bottom"); | |
156 | break; | |
157 | ||
158 | case ANNOTATION_EVENT: snprintf(bordercolor, 8, "#42f445"); | |
159 | snprintf(color, 8, "#00215b"); | |
160 | snprintf(pos, 7, "top"); | |
161 | break; | |
162 | ||
163 | case ANNOTATION_SYSTEM: snprintf(bordercolor, 8, "black"); | |
164 | snprintf(color, 8, "red"); | |
165 | snprintf(pos, 7, "center"); | |
166 | break; | |
167 | } | |
168 | ||
169 | // Check if the file exists to see if we need to insert a comma. | |
170 | f = fopen(filename, "r"); | |
171 | if (f == NULL) { | |
172 | addcomma = false; | |
173 | } else { | |
174 | fclose(f); | |
175 | } | |
176 | ||
33
dd52aecfc19d
Fix for annotation log before normal log in the json brewlog.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
177 | snprintf(json_log->time, 8, "%02d:%02d", Hour, Minute); |
0 | 178 | f = fopen(filename, "a"); |
179 | if (f) { | |
180 | if (addcomma) { | |
181 | fprintf(f, ",\n"); | |
182 | } | |
183 | addcomma = true; | |
184 | 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); | |
185 | fclose(f); | |
186 | } | |
187 | } | |
188 | } | |
189 | ||
190 | ||
191 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
192 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
193 | * @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
|
194 | * 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
|
195 | * for our application. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
196 | * @param base_path The mount path |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
197 | * @param host_config SPI host configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
198 | * @param slot_config Slot configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
199 | * @return Error condition. |
0 | 200 | */ |
201 | esp_err_t my_vfs_fat_sdmmc_init(const char* base_path, const sdmmc_host_t* host_config, const void* slot_config) | |
202 | { | |
203 | if (s_card != NULL) { | |
204 | return ESP_ERR_INVALID_STATE; | |
205 | } | |
206 | ||
207 | // connect SDMMC driver to FATFS | |
208 | pdrv = 0xFF; | |
209 | if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == 0xFF) { | |
210 | ESP_LOGI(TAG, "the maximum count of volumes is already mounted"); | |
211 | return ESP_ERR_NO_MEM; | |
212 | } | |
213 | ||
214 | s_base_path = strdup(base_path); | |
215 | if (!s_base_path) { | |
216 | ESP_LOGI(TAG, "could not copy base_path"); | |
217 | return ESP_ERR_NO_MEM; | |
218 | } | |
219 | esp_err_t err = ESP_OK; | |
220 | s_card = malloc(sizeof(sdmmc_card_t)); | |
221 | if (s_card == NULL) { | |
222 | err = ESP_ERR_NO_MEM; | |
223 | goto fail; | |
224 | } | |
225 | ||
226 | err = (*host_config->init)(); | |
227 | if (err != ESP_OK) { | |
228 | ESP_LOGI(TAG, "host init returned rc=0x%x", err); | |
229 | goto fail; | |
230 | } | |
231 | ||
232 | // configure SD slot | |
233 | if (host_config->flags == SDMMC_HOST_FLAG_SPI) { | |
234 | err = sdspi_host_init_slot(host_config->slot, (const sdspi_slot_config_t*) slot_config); | |
235 | } else { | |
236 | err = sdmmc_host_init_slot(host_config->slot, (const sdmmc_slot_config_t*) slot_config); | |
237 | } | |
238 | if (err != ESP_OK) { | |
239 | ESP_LOGI(TAG, "slot_config returned rc=0x%x", err); | |
240 | goto fail; | |
241 | } | |
242 | return ESP_OK; | |
243 | ||
244 | fail: | |
245 | host_config->deinit(); | |
246 | free(s_card); | |
247 | s_card = NULL; | |
248 | return err; | |
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 Mount SD card. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
255 | * @param base_path The mountpoint |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
256 | * @param host_config SPI host configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
257 | * @param slot_config Slot configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
258 | * @param mount_config Mount configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
259 | * @param[out] out_card Card information |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
260 | * @return Error condition |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
261 | */ |
0 | 262 | esp_err_t my_esp_vfs_fat_sdmmc_mount(const char* base_path, |
263 | const sdmmc_host_t* host_config, | |
264 | const void* slot_config, | |
265 | const esp_vfs_fat_mount_config_t* mount_config, | |
266 | sdmmc_card_t** out_card) | |
267 | { | |
268 | FATFS* fs = NULL; | |
269 | esp_err_t err = ESP_OK; | |
270 | ||
271 | if (s_card == NULL) { | |
272 | return ESP_ERR_INVALID_STATE; | |
273 | } | |
274 | ||
275 | // probe and initialize card | |
276 | err = sdmmc_card_init(host_config, s_card); | |
277 | if (err != ESP_OK) { | |
278 | if (err != ESP_ERR_INVALID_RESPONSE) { // No card present, do not log | |
279 | ESP_LOGI(TAG, "sdmmc_card_init failed 0x(%x)", err); | |
280 | } | |
281 | goto fail; | |
282 | } | |
283 | if (out_card != NULL) { | |
284 | *out_card = s_card; | |
285 | } | |
286 | ||
287 | ff_diskio_register_sdmmc(pdrv, s_card); | |
288 | s_pdrv = pdrv; | |
289 | ESP_LOGD(TAG, "using pdrv=%i", pdrv); | |
290 | char drv[3] = {(char)('0' + pdrv), ':', 0}; | |
291 | ||
292 | // connect FATFS to VFS | |
293 | err = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs); | |
294 | if (err == ESP_ERR_INVALID_STATE) { | |
295 | // it's okay, already registered with VFS | |
296 | } else if (err != ESP_OK) { | |
297 | ESP_LOGI(TAG, "esp_vfs_fat_register failed 0x(%x)", err); | |
298 | goto fail; | |
299 | } | |
300 | ||
301 | // Try to mount partition | |
302 | FRESULT res = f_mount(fs, drv, 1); | |
303 | if (res != FR_OK) { | |
304 | err = ESP_FAIL; | |
305 | ESP_LOGD(TAG, "f_mount failed (%d)", res); | |
306 | goto fail; | |
307 | } | |
308 | return ESP_OK; | |
309 | ||
310 | fail: | |
311 | if (fs) { | |
312 | f_mount(NULL, drv, 0); | |
313 | } | |
314 | esp_vfs_fat_unregister_path(base_path); | |
315 | ff_diskio_unregister(pdrv); | |
316 | return err; | |
317 | } | |
318 | ||
319 | ||
320 | ||
1
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 | * @brief Unmount a mounted SD card, |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
323 | * @return Error condition |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
324 | */ |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
325 | esp_err_t my_esp_vfs_fat_sdmmc_unmount(void) |
0 | 326 | { |
327 | if (s_card == NULL) { | |
328 | return ESP_ERR_INVALID_STATE; | |
329 | } | |
330 | // unmount | |
331 | char drv[3] = {(char)('0' + s_pdrv), ':', 0}; | |
332 | f_mount(0, drv, 0); | |
333 | return ESP_OK; | |
334 | } | |
335 | ||
336 | ||
337 | ||
338 | int FileCopy(char *ff, char *tf) | |
339 | { | |
340 | FILE *f, *t; | |
341 | uint8_t buf[512]; | |
342 | size_t bytes; | |
343 | ||
344 | f = fopen(ff, "r"); | |
345 | if (f == NULL) { | |
346 | ESP_LOGE(TAG, "FileCopy cannot open %s for read, error %d", ff, errno); | |
347 | return 1; | |
348 | } | |
349 | ||
350 | t = fopen(tf, "w+"); | |
351 | if (t == NULL) { | |
352 | ESP_LOGE(TAG, "FileCopy cannot open %s for create/write, error %d", tf, errno); | |
353 | fclose(f); | |
354 | return 1; | |
355 | } | |
356 | ||
357 | while ((bytes = fread(&buf, 1, 512, f))) { | |
358 | fwrite(&buf, 1, bytes, t); | |
359 | vTaskDelay(10 / portTICK_PERIOD_MS); | |
360 | } | |
361 | ||
362 | fclose(f); | |
363 | fclose(t); | |
364 | return 0; | |
365 | } | |
366 | ||
367 | ||
368 | ||
369 | void task_sdcard(void *pvParameter) | |
370 | { | |
371 | sdmmc_card_t* card; | |
372 | esp_err_t ret; | |
373 | EventBits_t uxBits; | |
374 | char filename[64]; | |
375 | ||
376 | sdcard_state = malloc(sizeof(SDCARD_State)); | |
377 | sdcard_state->host_ok = false; | |
378 | sdcard_state->card_present = false; | |
379 | sdcard_state->logfile[0] = '\0'; | |
380 | ||
381 | json_log = malloc(sizeof(JSON_log)); | |
382 | ||
383 | ESP_LOGI(TAG, "Starting SD card"); | |
384 | sdmmc_host_t host = SDSPI_HOST_DEFAULT(); | |
385 | host.slot = SDCARD_HOST_SLOT; // HSPI_HOST is in use by the TFT. | |
386 | sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT(); | |
387 | slot_config.gpio_miso = SDCARD_PIN_NUM_MISO; | |
388 | slot_config.gpio_mosi = SDCARD_PIN_NUM_MOSI; | |
389 | slot_config.gpio_sck = SDCARD_PIN_NUM_CLK; | |
390 | slot_config.gpio_cs = SDCARD_PIN_NUM_CS; | |
391 | slot_config.dma_channel = SDCARD_DMA_CHANNEL; | |
392 | // This initializes the slot without card detect (CD) and write protect (WP) signals. | |
393 | // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. | |
394 | ||
395 | /* | |
396 | * No errors from the sdmmc_cmd driver. | |
397 | */ | |
398 | esp_log_level_set("sdmmc_cmd", ESP_LOG_NONE); | |
399 | ||
400 | /* | |
401 | * Options for mounting the filesystem. | |
402 | * If format_if_mount_failed is set to true, SD card will be partitioned and | |
403 | * formatted in case when mounting fails. | |
404 | */ | |
405 | esp_vfs_fat_sdmmc_mount_config_t mount_config = { | |
406 | .format_if_mount_failed = false, | |
407 | .max_files = 5, | |
408 | .allocation_unit_size = 16 * 1024 | |
409 | }; | |
410 | ||
411 | ret = my_vfs_fat_sdmmc_init("/sdcard", &host, &slot_config); | |
412 | if (ret == ESP_OK) { | |
413 | ESP_LOGI(TAG, "SPI card interface ready"); | |
414 | sdcard_state->host_ok = true; | |
415 | } else { | |
416 | ESP_LOGE(TAG, "SPI card interface failed, abort task"); | |
417 | vTaskDelete(NULL); | |
418 | return; | |
419 | } | |
420 | ||
421 | xEventGroupSDcard = xEventGroupCreate(); | |
422 | ||
423 | /* | |
424 | * Task loop, continues check of the inserted cards. | |
425 | */ | |
426 | while (1) { | |
427 | ||
428 | if (sdcard_state->card_present == false) { | |
429 | /* | |
430 | * If the card is not mounted, try it. | |
431 | */ | |
432 | ret = my_esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card); | |
433 | if (ret == ESP_OK) { | |
434 | ESP_LOGI(TAG, "SD card mounted on /sdcard"); | |
435 | sdcard_state->card_present = true; | |
436 | ||
437 | DIR* dir = opendir("/sdcard/w/log"); | |
438 | if (dir == NULL) { | |
439 | ret = mkdir("/sdcard/w/log", 0755); | |
440 | } else { | |
441 | closedir(dir); | |
442 | } | |
443 | } | |
444 | } else { | |
445 | /* | |
446 | * Check if the mounted card is still in the slot. | |
447 | */ | |
448 | DIR* dir = opendir("/sdcard/w/log"); | |
449 | if (dir == NULL) { | |
450 | ESP_LOGI(TAG, "SD card missing, unmount"); | |
451 | my_esp_vfs_fat_sdmmc_unmount(); | |
452 | sdcard_state->card_present = false; | |
453 | } else { | |
454 | closedir(dir); | |
455 | } | |
456 | } | |
457 | ||
458 | uxBits = xEventGroupWaitBits(xEventGroupSDcard, | |
459 | TASK_SDCARD_LOG_CLEAN | TASK_SDCARD_LOG_CLOSE, pdFALSE, pdFALSE, 1000 / portTICK_PERIOD_MS); | |
460 | ||
461 | if (uxBits & TASK_SDCARD_LOG_CLEAN) { | |
462 | DIR *dir = opendir("/spiffs/log"); | |
463 | char lf[32]; | |
464 | ||
465 | if (dir != NULL) { | |
466 | struct dirent *de = readdir(dir); | |
467 | while (de) { | |
468 | sprintf(lf, "/spiffs/log/%s", de->d_name); | |
469 | if (unlink(lf) == ESP_OK) { | |
470 | ESP_LOGI(TAG, "Removed old %s", lf); | |
471 | } | |
472 | de = readdir(dir); | |
473 | vTaskDelay(2 / portTICK_PERIOD_MS); | |
474 | } | |
475 | closedir(dir); | |
476 | } | |
477 | xEventGroupClearBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLEAN); | |
478 | } | |
479 | ||
480 | if (uxBits & TASK_SDCARD_LOG_CLOSE) { | |
481 | // Close the logfile. | |
482 | if (strlen(sdcard_state->logfile) && (sdcard_state->card_present == true)) { | |
483 | ESP_LOGI(TAG, "Closing logfile"); | |
484 | char destname[64]; | |
485 | sprintf(destname, "/sdcard/w/log"); | |
486 | int rc = mkdir(destname, 0755); | |
487 | if (rc && (errno != EEXIST)) { | |
488 | ESP_LOGE(TAG, "Cannot create %s error %d", destname, errno); | |
489 | } else { | |
490 | sprintf(filename, "/spiffs/log/%s.json", sdcard_state->logfile); | |
491 | // First close the JSON data records | |
492 | FILE *f = fopen(filename, "a+"); | |
493 | if (f) { | |
494 | fprintf(f, " ],\n"); // End of brewdata | |
495 | fprintf(f, " \"annotations\":[\n"); | |
496 | // Insert annotation records | |
497 | sprintf(destname, "/spiffs/log/%s.anno", sdcard_state->logfile); | |
498 | FILE *a = fopen(destname, "r"); | |
499 | char buf[256]; | |
500 | if (a) { | |
501 | while(true) { | |
502 | if (fgets(buf, sizeof(buf), a)) { | |
503 | fprintf(f, "%s", buf); | |
504 | } else { | |
505 | break; | |
506 | } | |
507 | } | |
508 | fclose(a); | |
509 | unlink(destname); | |
510 | } | |
511 | fprintf(f, " ]\n"); // End of annotations | |
512 | fprintf(f, " }]\n"); // End of brew | |
513 | fprintf(f, "}\n"); | |
514 | fclose(f); | |
515 | } | |
516 | sprintf(destname, "/sdcard/w/log/%s.json", sdcard_state->logfile); | |
517 | if (FileCopy(filename, destname) == 0) { | |
518 | ESP_LOGI(TAG, "JSON file copied to %s", destname); | |
519 | unlink(filename); | |
520 | } | |
521 | } | |
522 | } | |
523 | sdcard_state->logfile[0] = '\0'; // Clear logfile name | |
524 | runtime.Logfile[0] = '\0'; | |
525 | xEventGroupClearBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLOSE); | |
526 | } | |
527 | } | |
528 | } | |
529 | ||
530 |