11#include "cJSON.h"
2- #include "class/hid/hid_device.h"
32#include "esp_log.h"
43#include "esp_spiffs.h"
54#include "freertos/FreeRTOS.h"
87#include "macrolev.h"
98#include "sdkconfig.h"
109#include "tinyusb.h"
11- #include "tusb_cdc_acm .h"
10+ #include "usb .h"
1211#include <stdio.h>
1312#include <string.h>
1413
@@ -17,25 +16,89 @@ static const char *TAG = "MACROLEV";
1716#define BOOT_MODE_PIN GPIO_NUM_0
1817#define STORAGE_NAMESPACE "storage"
1918#define JSON_FILENAME "config.json"
20- #define CDC_ACCUM_BUF_SIZE ( 1024 * 1024) // 1MB buffer for large JSON payloads
19+ #define CDC_ACCUM_BUF_SIZE 1024
2120
2221#define MARKER "[EOF]"
2322#define MARKER_LEN (sizeof(MARKER) - 1)
2423
2524static char cdc_accum_buf [CDC_ACCUM_BUF_SIZE ];
2625static size_t cdc_accum_len = 0 ;
2726
28- static uint8_t rx_buffer [CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1 ];
27+ esp_err_t save_json_to_file (const char * filename , cJSON * json ) {
28+ char * json_string = cJSON_PrintUnformatted (json );
29+ if (json_string == NULL ) {
30+ ESP_LOGE (TAG , "Failed to print json" );
31+ return ESP_FAIL ;
32+ }
33+
34+ char filepath [64 ];
35+ // snprintf(filepath, sizeof(filepath), "/%s", filename);
36+ snprintf (filepath , sizeof (filepath ), filename );
37+
38+ FILE * f = fopen (filepath , "w" );
39+ if (f == NULL ) {
40+ ESP_LOGE (TAG , "Failed to open file for writing" );
41+ free (json_string );
42+ return ESP_FAIL ;
43+ }
44+
45+ fprintf (f , "%s" , json_string );
46+ fclose (f );
47+ free (json_string );
2948
30- static QueueHandle_t usb_cdc_rx_queue ;
49+ ESP_LOGI (TAG , "JSON saved to file: %s" , filepath );
50+ ESP_LOGI (TAG , "JSON: %s" , cJSON_Print (json ));
51+ return ESP_OK ;
52+ }
3153
32- typedef struct usb_message {
33- uint8_t buf [CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1 ];
34- size_t buf_len ;
35- } usb_message_t ;
54+ // Load JSON from file
55+ cJSON * load_json_from_file (const char * filename ) {
56+ char filepath [64 ];
57+ // snprintf(filepath, sizeof(filepath), "/%s", filename);
58+ snprintf (filepath , sizeof (filepath ), filename );
59+
60+ FILE * f = fopen (filepath , "r" );
61+ if (f == NULL ) {
62+ ESP_LOGE (TAG , "Failed to open file for reading" );
63+ return NULL ;
64+ }
65+
66+ // Get file size
67+ fseek (f , 0 , SEEK_END );
68+ long fsize = ftell (f );
69+ fseek (f , 0 , SEEK_SET );
70+
71+ // Read file content
72+ char * content = malloc (fsize + 1 );
73+ if (content == NULL ) {
74+ ESP_LOGE (TAG , "Failed to allocate memory" );
75+ fclose (f );
76+ return NULL ;
77+ }
78+
79+ size_t read_size = fread (content , 1 , fsize , f );
80+ content [read_size ] = '\0' ;
81+ fclose (f );
82+
83+ // Parse JSON
84+ cJSON * json = cJSON_Parse (content );
85+ free (content );
86+
87+ if (json == NULL ) {
88+ const char * error_ptr = cJSON_GetErrorPtr ();
89+ if (error_ptr != NULL ) {
90+ ESP_LOGE (TAG , "Error parsing JSON before: %s" , error_ptr );
91+ }
92+ return NULL ;
93+ }
94+
95+ ESP_LOGI (TAG , "JSON loaded from file: %s" , filepath );
96+ return json ;
97+ }
3698
3799static void usb_cdc_rx_queue_handler_task (void * pvParameters ) {
38100 usb_message_t msg ;
101+ extern QueueHandle_t usb_cdc_rx_queue ;
39102
40103 while (1 ) {
41104 if (xQueueReceive (usb_cdc_rx_queue , & msg , portMAX_DELAY ) == pdTRUE ) {
@@ -109,115 +172,10 @@ static esp_err_t init_spiffs(void) {
109172 return ESP_OK ;
110173}
111174
112- // Save JSON to file
113- esp_err_t save_json_to_file (const char * filename , cJSON * json ) {
114- char * json_string = cJSON_PrintUnformatted (json );
115- if (json_string == NULL ) {
116- ESP_LOGE (TAG , "Failed to print json" );
117- return ESP_FAIL ;
118- }
119-
120- char filepath [64 ];
121- // snprintf(filepath, sizeof(filepath), "/%s", filename);
122- snprintf (filepath , sizeof (filepath ), filename );
123-
124- FILE * f = fopen (filepath , "w" );
125- if (f == NULL ) {
126- ESP_LOGE (TAG , "Failed to open file for writing" );
127- free (json_string );
128- return ESP_FAIL ;
129- }
130-
131- fprintf (f , "%s" , json_string );
132- fclose (f );
133- free (json_string );
134-
135- ESP_LOGI (TAG , "JSON saved to file: %s" , filepath );
136- ESP_LOGI (TAG , "JSON: %s" , cJSON_Print (json ));
137- return ESP_OK ;
138- }
139-
140- // Load JSON from file
141- cJSON * load_json_from_file (const char * filename ) {
142- char filepath [64 ];
143- // snprintf(filepath, sizeof(filepath), "/%s", filename);
144- snprintf (filepath , sizeof (filepath ), filename );
145-
146- FILE * f = fopen (filepath , "r" );
147- if (f == NULL ) {
148- ESP_LOGE (TAG , "Failed to open file for reading" );
149- return NULL ;
150- }
151-
152- // Get file size
153- fseek (f , 0 , SEEK_END );
154- long fsize = ftell (f );
155- fseek (f , 0 , SEEK_SET );
156-
157- // Read file content
158- char * content = malloc (fsize + 1 );
159- if (content == NULL ) {
160- ESP_LOGE (TAG , "Failed to allocate memory" );
161- fclose (f );
162- return NULL ;
163- }
164-
165- size_t read_size = fread (content , 1 , fsize , f );
166- content [read_size ] = '\0' ;
167- fclose (f );
168-
169- // Parse JSON
170- cJSON * json = cJSON_Parse (content );
171- free (content );
172-
173- if (json == NULL ) {
174- const char * error_ptr = cJSON_GetErrorPtr ();
175- if (error_ptr != NULL ) {
176- ESP_LOGE (TAG , "Error parsing JSON before: %s" , error_ptr );
177- }
178- return NULL ;
179- }
180-
181- ESP_LOGI (TAG , "JSON loaded from file: %s" , filepath );
182- return json ;
183- }
184-
185- void tinyusb_cdc_rx_callback (int itf , cdcacm_event_t * event ) {
186- usb_message_t msg = { 0 };
187- msg .buf_len = 0 ;
188-
189- // Read data from CDC
190- esp_err_t ret = tinyusb_cdcacm_read (itf , msg .buf , CONFIG_TINYUSB_CDC_RX_BUFSIZE , & msg .buf_len );
191- if (ret == ESP_OK && msg .buf_len > 0 ) {
192- // Send message to queue
193- if (xQueueSend (usb_cdc_rx_queue , & msg , portMAX_DELAY ) != pdTRUE ) {
194- ESP_LOGE (TAG , "Failed to send message to queue" );
195- }
196- }
197- }
198-
199175void config_storage_init (void ) {
200176 // Initialize SPIFFS
201177 ESP_ERROR_CHECK (init_spiffs ());
202178
203- // Create the CDC RX queue
204- usb_cdc_rx_queue = xQueueCreate (5 , sizeof (usb_message_t ));
205- assert (usb_cdc_rx_queue );
206-
207179 // Create the CDC RX queue handler task
208180 xTaskCreate (usb_cdc_rx_queue_handler_task , "usb_cdc_rx_queue_handler" , 4096 , NULL , 5 , NULL );
209-
210- // Initialize CDC
211- tinyusb_config_cdcacm_t acm_cfg = {
212- .usb_dev = TINYUSB_USBDEV_0 ,
213- .cdc_port = TINYUSB_CDC_ACM_0 ,
214- .callback_rx = & tinyusb_cdc_rx_callback ,
215- .callback_rx_wanted_char = NULL ,
216- .callback_line_state_changed = NULL ,
217- .callback_line_coding_changed = NULL ,
218- .rx_unread_buf_sz = CONFIG_TINYUSB_CDC_RX_BUFSIZE
219- };
220- ESP_ERROR_CHECK (tusb_cdc_acm_init (& acm_cfg ));
221-
222- ESP_LOGI (TAG , "USB initialization DONE" );
223181}
0 commit comments