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