Wed, 19 May 2021 17:21:33 +0200
Version 0.3.14. Log brewing essentials at brew start.
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; | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
34 | static uint8_t pdrv = FF_DRV_NOT_USED; |
0 | 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; |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
51 | extern bool System_TimeOk; |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
52 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
53 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
54 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
55 | void log_msg(const char *tag, const char *format, ...) |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
56 | { |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
57 | char *outstr, logfn[64], stamp[32]; |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
58 | va_list va_ptr; |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
59 | FILE *fp; |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
60 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
61 | outstr = calloc(1024, sizeof(char)); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
62 | va_start(va_ptr, format); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
63 | vsnprintf(outstr, 1023, format, va_ptr); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
64 | ESP_LOGI(tag, "%s", outstr); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
65 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
66 | if (System_TimeOk && sdcard_state->card_present == true) { |
64
326c38d3681b
Added more plain logfile entries. Change some message details. Added anohe function to eliminate duplicated code. Moved the logfile to the web so it can be downloaded or viewed.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
67 | snprintf(logfn, 64, "/sdcard/w/log/sys%04d%02d%02d.log", timeinfo.tm_year+1900, timeinfo.tm_mon+1, timeinfo.tm_mday); |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
68 | snprintf(stamp, 31, "%02d-%02d %02d:%02d:%02d", timeinfo.tm_mday, timeinfo.tm_mon+1, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
69 | fp = fopen(logfn, "a+"); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
70 | if (fp) { |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
71 | fprintf(fp, "%s %s: %s\n", stamp, tag, outstr); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
72 | fclose(fp); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
73 | } |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
74 | } |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
75 | |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
76 | va_end(va_ptr); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
77 | free(outstr); |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
78 | } |
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
79 | |
0 | 80 | |
81 | ||
82 | void log_begin(time_t t) | |
83 | { | |
84 | struct tm timeinfo; | |
85 | ||
86 | /* | |
87 | * If there is an open logfile from an crashed brew, open that one. | |
88 | */ | |
89 | if (! strlen(sdcard_state->logfile) && strlen(runtime.Logfile)) { | |
90 | sprintf(sdcard_state->logfile, "%s", runtime.Logfile); | |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
91 | log_msg(TAG, "Resumed %s", sdcard_state->logfile); |
0 | 92 | } else { |
93 | localtime_r(&t, &timeinfo); | |
94 | if (timeinfo.tm_year > (2016 - 1900)) { | |
95 | // Valid time, construct the filename. | |
96 | strftime(sdcard_state->logfile, sizeof(sdcard_state->logfile), "br%y%m%d%H%M", &timeinfo); | |
97 | } else { | |
98 | sprintf(sdcard_state->logfile, "brewlog"); | |
99 | } | |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
100 | log_msg(TAG, "Create %s", sdcard_state->logfile); |
0 | 101 | sprintf(runtime.Logfile, "%s", sdcard_state->logfile); |
102 | } | |
103 | } | |
104 | ||
105 | ||
106 | ||
107 | void log_close(void) | |
108 | { | |
109 | xEventGroupSetBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLOSE); | |
110 | } | |
111 | ||
112 | ||
113 | ||
114 | void log_clean(void) | |
115 | { | |
116 | xEventGroupSetBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLEAN); | |
117 | } | |
118 | ||
119 | ||
120 | ||
121 | void log_json(void) | |
122 | { | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
33
diff
changeset
|
123 | char filename[64], strftime_buf[64]; |
0 | 124 | FILE *f; |
125 | bool addcomma = true; | |
126 | ||
127 | if (strlen(sdcard_state->logfile)) { | |
128 | sprintf(filename, "/spiffs/log/%s.json", sdcard_state->logfile); | |
129 | f = fopen(filename, "r"); | |
130 | if (f == NULL) { | |
131 | // Create the file and add the header | |
132 | f = fopen(filename, "a"); | |
133 | if (f) { | |
134 | strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
135 | fprintf(f, "{\"brew\":[{\"Recipe\":\"%s %s\",\"Date\":\"%s\",\"brewdata\":[\n", recipe.Code, recipe.Name, strftime_buf); |
0 | 136 | addcomma = false; |
137 | fclose(f); | |
138 | } else { | |
139 | ESP_LOGE(TAG, "Failed to create %s error %d", filename, errno); | |
140 | return; | |
141 | } | |
142 | } else { | |
143 | fclose(f); // Was open for reading. | |
144 | } | |
145 | ||
146 | f = fopen(filename, "a"); | |
147 | if (f) { | |
148 | if (addcomma) { | |
149 | fprintf(f, ",\n"); | |
150 | } | |
151 | addcomma = true; | |
152 | fprintf(f, " {\"MLT_sp\":\"%.3f\",\"MLT_pv\":\"%.3f\",\"MLT_pwm\":\"%d\",\"MLT_tr\":\"%d\",\"Pump\":\"%d\",", | |
153 | json_log->mlt_sp, json_log->mlt_pv, json_log->mlt_power, json_log->mlt_tempreached, json_log->pump_run); | |
154 | if (json_log->hlt_sp > 0.0) { | |
155 | fprintf(f, "\"HLT_sp\":\"%.3f\",\"HLT_pv\":\"%.3f\",\"HLT_pwm\":\"%d\",", json_log->hlt_sp, json_log->hlt_pv, json_log->hlt_power); | |
156 | } | |
157 | fprintf(f, "\"Label\":\"%s\"}", json_log->time); | |
158 | fclose(f); | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | ||
164 | ||
165 | void log_annotation(int annotation_type, char *label) | |
166 | { | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
33
diff
changeset
|
167 | char filename[64]; |
0 | 168 | char bordercolor[9], color[9], pos[8]; |
169 | FILE *f; | |
170 | 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
|
171 | 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
|
172 | int Minute = ((TimeBrewing % 3600) / 60); |
0 | 173 | |
174 | if (strlen(sdcard_state->logfile)) { | |
175 | sprintf(filename, "/spiffs/log/%s.anno", sdcard_state->logfile); | |
176 | ||
177 | switch (annotation_type) { | |
178 | case ANNOTATION_STAGE: snprintf(bordercolor, 8, "#8942f4"); | |
179 | snprintf(color, 8, "#00215b"); | |
180 | snprintf(pos, 7, "bottom"); | |
181 | break; | |
182 | ||
183 | case ANNOTATION_EVENT: snprintf(bordercolor, 8, "#42f445"); | |
184 | snprintf(color, 8, "#00215b"); | |
185 | snprintf(pos, 7, "top"); | |
186 | break; | |
187 | ||
188 | case ANNOTATION_SYSTEM: snprintf(bordercolor, 8, "black"); | |
189 | snprintf(color, 8, "red"); | |
190 | snprintf(pos, 7, "center"); | |
191 | break; | |
192 | } | |
193 | ||
194 | // Check if the file exists to see if we need to insert a comma. | |
195 | f = fopen(filename, "r"); | |
196 | if (f == NULL) { | |
197 | addcomma = false; | |
198 | } else { | |
199 | fclose(f); | |
200 | } | |
201 | ||
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
33
diff
changeset
|
202 | snprintf(json_log->time, 11, "%02d:%02d", Hour, Minute); |
0 | 203 | f = fopen(filename, "a"); |
204 | if (f) { | |
205 | if (addcomma) { | |
206 | fprintf(f, ",\n"); | |
207 | } | |
208 | addcomma = true; | |
209 | 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); | |
210 | fclose(f); | |
211 | } | |
212 | } | |
213 | } | |
214 | ||
215 | ||
216 | ||
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
217 | static esp_err_t my_init_sdspi_host(int slot, const void *slot_config, int *out_slot) |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
218 | { |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
219 | esp_err_t err = sdspi_host_init_device((const sdspi_device_config_t*)slot_config, out_slot); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
220 | if (err != ESP_OK) { |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
221 | ESP_LOGE(TAG, "Failed to attach sdspi device onto an SPI bus (rc=0x%x), please initialize the \ |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
222 | bus first and check the device parameters.", err); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
223 | } |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
224 | return err; |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
225 | } |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
226 | |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
227 | |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
228 | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
229 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
230 | * @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
|
231 | * 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
|
232 | * for our application. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
233 | * @param base_path The mount path |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
234 | * @param host_config SPI host configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
235 | * @param slot_config Slot configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
236 | * @return Error condition. |
0 | 237 | */ |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
238 | esp_err_t my_vfs_fat_sdmmc_init(const char* base_path, const sdmmc_host_t* host_config, const void* slot_config, int *card_handle) |
0 | 239 | { |
240 | if (s_card != NULL) { | |
241 | return ESP_ERR_INVALID_STATE; | |
242 | } | |
243 | ||
244 | // connect SDMMC driver to FATFS | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
245 | pdrv = FF_DRV_NOT_USED; |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
246 | if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == FF_DRV_NOT_USED) { |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
247 | ESP_LOGE(TAG, "the maximum count of volumes is already mounted"); |
0 | 248 | return ESP_ERR_NO_MEM; |
249 | } | |
250 | ||
251 | s_base_path = strdup(base_path); | |
252 | if (!s_base_path) { | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
253 | ESP_LOGE(TAG, "could not copy base_path"); |
0 | 254 | return ESP_ERR_NO_MEM; |
255 | } | |
256 | esp_err_t err = ESP_OK; | |
257 | s_card = malloc(sizeof(sdmmc_card_t)); | |
258 | if (s_card == NULL) { | |
259 | err = ESP_ERR_NO_MEM; | |
260 | goto fail; | |
261 | } | |
262 | ||
263 | err = (*host_config->init)(); | |
264 | if (err != ESP_OK) { | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
265 | ESP_LOGE(TAG, "host init returned rc=0x%x", err); |
0 | 266 | goto fail; |
267 | } | |
268 | ||
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
269 | // configure SD host |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
270 | err = my_init_sdspi_host(host_config->slot, slot_config, &card_handle); |
0 | 271 | if (err != ESP_OK) { |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
272 | ESP_LOGE(TAG, "init_sdspi_host() rc=0x%x", err); |
0 | 273 | goto fail; |
274 | } | |
275 | return ESP_OK; | |
276 | ||
277 | fail: | |
278 | host_config->deinit(); | |
279 | free(s_card); | |
280 | s_card = NULL; | |
281 | return err; | |
282 | } | |
283 | ||
284 | ||
285 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
286 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
287 | * @brief Mount SD card. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
288 | * @param base_path The mountpoint |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
289 | * @param host_config SPI host configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
290 | * @param slot_config Slot configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
291 | * @param mount_config Mount configuration |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
292 | * @param[out] out_card Card information |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
293 | * @return Error condition |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
294 | */ |
0 | 295 | esp_err_t my_esp_vfs_fat_sdmmc_mount(const char* base_path, |
296 | const sdmmc_host_t* host_config, | |
297 | const void* slot_config, | |
298 | const esp_vfs_fat_mount_config_t* mount_config, | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
299 | sdmmc_card_t** out_card, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
300 | int card_handle) |
0 | 301 | { |
302 | FATFS* fs = NULL; | |
303 | esp_err_t err = ESP_OK; | |
304 | ||
305 | if (s_card == NULL) { | |
306 | return ESP_ERR_INVALID_STATE; | |
307 | } | |
308 | ||
309 | // probe and initialize card | |
310 | err = sdmmc_card_init(host_config, s_card); | |
311 | if (err != ESP_OK) { | |
312 | if (err != ESP_ERR_INVALID_RESPONSE) { // No card present, do not log | |
313 | ESP_LOGI(TAG, "sdmmc_card_init failed 0x(%x)", err); | |
314 | } | |
315 | goto fail; | |
316 | } | |
317 | if (out_card != NULL) { | |
318 | *out_card = s_card; | |
319 | } | |
320 | ||
321 | ff_diskio_register_sdmmc(pdrv, s_card); | |
322 | s_pdrv = pdrv; | |
323 | ESP_LOGD(TAG, "using pdrv=%i", pdrv); | |
324 | char drv[3] = {(char)('0' + pdrv), ':', 0}; | |
325 | ||
326 | // connect FATFS to VFS | |
327 | err = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs); | |
328 | if (err == ESP_ERR_INVALID_STATE) { | |
329 | // it's okay, already registered with VFS | |
330 | } else if (err != ESP_OK) { | |
331 | ESP_LOGI(TAG, "esp_vfs_fat_register failed 0x(%x)", err); | |
332 | goto fail; | |
333 | } | |
334 | ||
335 | // Try to mount partition | |
336 | FRESULT res = f_mount(fs, drv, 1); | |
337 | if (res != FR_OK) { | |
338 | err = ESP_FAIL; | |
339 | ESP_LOGD(TAG, "f_mount failed (%d)", res); | |
340 | goto fail; | |
341 | } | |
342 | return ESP_OK; | |
343 | ||
344 | fail: | |
345 | if (fs) { | |
346 | f_mount(NULL, drv, 0); | |
347 | } | |
348 | esp_vfs_fat_unregister_path(base_path); | |
349 | ff_diskio_unregister(pdrv); | |
350 | return err; | |
351 | } | |
352 | ||
353 | ||
354 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
355 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
356 | * @brief Unmount a mounted SD card, |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
357 | * @return Error condition |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
358 | */ |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
359 | esp_err_t my_esp_vfs_fat_sdmmc_unmount(void) |
0 | 360 | { |
361 | if (s_card == NULL) { | |
362 | return ESP_ERR_INVALID_STATE; | |
363 | } | |
364 | // unmount | |
365 | char drv[3] = {(char)('0' + s_pdrv), ':', 0}; | |
366 | f_mount(0, drv, 0); | |
367 | return ESP_OK; | |
368 | } | |
369 | ||
370 | ||
371 | ||
372 | int FileCopy(char *ff, char *tf) | |
373 | { | |
374 | FILE *f, *t; | |
375 | uint8_t buf[512]; | |
376 | size_t bytes; | |
377 | ||
378 | f = fopen(ff, "r"); | |
379 | if (f == NULL) { | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
380 | ESP_LOGW(TAG, "FileCopy cannot open %s for read, error %d", ff, errno); |
0 | 381 | return 1; |
382 | } | |
383 | ||
384 | t = fopen(tf, "w+"); | |
385 | if (t == NULL) { | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
386 | ESP_LOGW(TAG, "FileCopy cannot open %s for create/write, error %d", tf, errno); |
0 | 387 | fclose(f); |
388 | return 1; | |
389 | } | |
390 | ||
391 | while ((bytes = fread(&buf, 1, 512, f))) { | |
392 | fwrite(&buf, 1, bytes, t); | |
393 | vTaskDelay(10 / portTICK_PERIOD_MS); | |
394 | } | |
395 | ||
396 | fclose(f); | |
397 | fclose(t); | |
398 | return 0; | |
399 | } | |
400 | ||
401 | ||
402 | ||
403 | void task_sdcard(void *pvParameter) | |
404 | { | |
405 | sdmmc_card_t* card; | |
406 | esp_err_t ret; | |
407 | EventBits_t uxBits; | |
408 | char filename[64]; | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
409 | int card_handle = -1; //uninitialized |
0 | 410 | |
411 | sdcard_state = malloc(sizeof(SDCARD_State)); | |
412 | sdcard_state->host_ok = false; | |
413 | sdcard_state->card_present = false; | |
414 | sdcard_state->logfile[0] = '\0'; | |
415 | ||
416 | json_log = malloc(sizeof(JSON_log)); | |
417 | ||
88
7f02dbee58d0
Fix missing log entries. More code space saving.
Michiel Broek <mbroek@mbse.eu>
parents:
87
diff
changeset
|
418 | ESP_LOGI(TAG, "Start SD card"); |
0 | 419 | sdmmc_host_t host = SDSPI_HOST_DEFAULT(); |
420 | host.slot = SDCARD_HOST_SLOT; // HSPI_HOST is in use by the TFT. | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
421 | spi_bus_config_t bus_cfg = { |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
422 | .mosi_io_num = SDCARD_PIN_NUM_MOSI, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
423 | .miso_io_num = SDCARD_PIN_NUM_MISO, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
424 | .sclk_io_num = SDCARD_PIN_NUM_CLK, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
425 | .quadwp_io_num = -1, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
426 | .quadhd_io_num = -1, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
427 | .max_transfer_sz = 4000, |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
428 | }; |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
429 | ret = spi_bus_initialize(host.slot, &bus_cfg, SDCARD_DMA_CHANNEL); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
430 | if (ret != ESP_OK) { |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
431 | ESP_LOGE(TAG, "Failed to initialize bus."); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
432 | vTaskDelete(NULL); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
433 | return; |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
434 | } |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
435 | |
0 | 436 | // This initializes the slot without card detect (CD) and write protect (WP) signals. |
437 | // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
438 | sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT(); |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
439 | slot_config.gpio_cs = SDCARD_PIN_NUM_CS; |
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
440 | slot_config.host_id = host.slot; |
0 | 441 | |
442 | /* | |
92
bac0a860f5dd
No repeated logs when the SD card is removed. Supress wifi_init logs.
Michiel Broek <mbroek@mbse.eu>
parents:
91
diff
changeset
|
443 | * No errors from the sdspi_transaction driver. |
0 | 444 | */ |
92
bac0a860f5dd
No repeated logs when the SD card is removed. Supress wifi_init logs.
Michiel Broek <mbroek@mbse.eu>
parents:
91
diff
changeset
|
445 | esp_log_level_set("sdspi_transaction", ESP_LOG_NONE); |
0 | 446 | |
447 | /* | |
448 | * Options for mounting the filesystem. | |
449 | * If format_if_mount_failed is set to true, SD card will be partitioned and | |
450 | * formatted in case when mounting fails. | |
451 | */ | |
452 | esp_vfs_fat_sdmmc_mount_config_t mount_config = { | |
453 | .format_if_mount_failed = false, | |
454 | .max_files = 5, | |
455 | .allocation_unit_size = 16 * 1024 | |
456 | }; | |
457 | ||
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
458 | ret = my_vfs_fat_sdmmc_init("/sdcard", &host, &slot_config, &card_handle); |
0 | 459 | if (ret == ESP_OK) { |
460 | ESP_LOGI(TAG, "SPI card interface ready"); | |
461 | sdcard_state->host_ok = true; | |
462 | } else { | |
463 | ESP_LOGE(TAG, "SPI card interface failed, abort task"); | |
464 | vTaskDelete(NULL); | |
465 | return; | |
466 | } | |
467 | ||
468 | xEventGroupSDcard = xEventGroupCreate(); | |
469 | ||
470 | /* | |
471 | * Task loop, continues check of the inserted cards. | |
472 | */ | |
473 | while (1) { | |
474 | ||
475 | if (sdcard_state->card_present == false) { | |
476 | /* | |
477 | * If the card is not mounted, try it. | |
478 | */ | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
479 | ret = my_esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card, card_handle); |
0 | 480 | if (ret == ESP_OK) { |
481 | ESP_LOGI(TAG, "SD card mounted on /sdcard"); | |
482 | sdcard_state->card_present = true; | |
483 | ||
484 | DIR* dir = opendir("/sdcard/w/log"); | |
485 | if (dir == NULL) { | |
486 | ret = mkdir("/sdcard/w/log", 0755); | |
487 | } else { | |
488 | closedir(dir); | |
489 | } | |
92
bac0a860f5dd
No repeated logs when the SD card is removed. Supress wifi_init logs.
Michiel Broek <mbroek@mbse.eu>
parents:
91
diff
changeset
|
490 | esp_log_level_set("sdmmc_sd", ESP_LOG_INFO); |
0 | 491 | } |
492 | } else { | |
493 | /* | |
494 | * Check if the mounted card is still in the slot. | |
495 | */ | |
496 | DIR* dir = opendir("/sdcard/w/log"); | |
497 | if (dir == NULL) { | |
498 | ESP_LOGI(TAG, "SD card missing, unmount"); | |
499 | my_esp_vfs_fat_sdmmc_unmount(); | |
500 | sdcard_state->card_present = false; | |
92
bac0a860f5dd
No repeated logs when the SD card is removed. Supress wifi_init logs.
Michiel Broek <mbroek@mbse.eu>
parents:
91
diff
changeset
|
501 | esp_log_level_set("sdmmc_sd", ESP_LOG_NONE); |
0 | 502 | } else { |
503 | closedir(dir); | |
504 | } | |
505 | } | |
506 | ||
507 | uxBits = xEventGroupWaitBits(xEventGroupSDcard, | |
508 | TASK_SDCARD_LOG_CLEAN | TASK_SDCARD_LOG_CLOSE, pdFALSE, pdFALSE, 1000 / portTICK_PERIOD_MS); | |
509 | ||
510 | if (uxBits & TASK_SDCARD_LOG_CLEAN) { | |
511 | DIR *dir = opendir("/spiffs/log"); | |
512 | char lf[32]; | |
513 | ||
514 | if (dir != NULL) { | |
515 | struct dirent *de = readdir(dir); | |
516 | 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
|
517 | 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
|
518 | strncat(lf, de->d_name, 31 - strlen(lf)); |
0 | 519 | if (unlink(lf) == ESP_OK) { |
520 | ESP_LOGI(TAG, "Removed old %s", lf); | |
521 | } | |
522 | de = readdir(dir); | |
523 | vTaskDelay(2 / portTICK_PERIOD_MS); | |
524 | } | |
525 | closedir(dir); | |
526 | } | |
527 | xEventGroupClearBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLEAN); | |
528 | } | |
529 | ||
530 | if (uxBits & TASK_SDCARD_LOG_CLOSE) { | |
531 | // Close the logfile. | |
532 | if (strlen(sdcard_state->logfile) && (sdcard_state->card_present == true)) { | |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
533 | log_msg(TAG, "Closing logfile"); |
0 | 534 | char destname[64]; |
535 | sprintf(destname, "/sdcard/w/log"); | |
536 | int rc = mkdir(destname, 0755); | |
537 | if (rc && (errno != EEXIST)) { | |
538 | ESP_LOGE(TAG, "Cannot create %s error %d", destname, errno); | |
539 | } else { | |
540 | sprintf(filename, "/spiffs/log/%s.json", sdcard_state->logfile); | |
541 | // First close the JSON data records | |
542 | FILE *f = fopen(filename, "a+"); | |
543 | if (f) { | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
544 | fprintf(f, "],\"annotations\":[\n"); // End of brewdata |
0 | 545 | // Insert annotation records |
546 | sprintf(destname, "/spiffs/log/%s.anno", sdcard_state->logfile); | |
547 | FILE *a = fopen(destname, "r"); | |
548 | char buf[256]; | |
549 | if (a) { | |
550 | while(true) { | |
551 | if (fgets(buf, sizeof(buf), a)) { | |
552 | fprintf(f, "%s", buf); | |
553 | } else { | |
554 | break; | |
555 | } | |
556 | } | |
557 | fclose(a); | |
558 | unlink(destname); | |
559 | } | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
560 | fprintf(f, "]}]}\n"); // End of annotations and brew |
0 | 561 | fclose(f); |
562 | } | |
563 | sprintf(destname, "/sdcard/w/log/%s.json", sdcard_state->logfile); | |
564 | if (FileCopy(filename, destname) == 0) { | |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
565 | log_msg(TAG, "JSON file copied to %s", destname); |
0 | 566 | unlink(filename); |
567 | } | |
568 | } | |
569 | } | |
570 | sdcard_state->logfile[0] = '\0'; // Clear logfile name | |
571 | runtime.Logfile[0] = '\0'; | |
572 | xEventGroupClearBits(xEventGroupSDcard, TASK_SDCARD_LOG_CLOSE); | |
573 | } | |
574 | } | |
575 | } | |
576 | ||
577 |