Skip to content

Commit 613a157

Browse files
committed
wip: split code
1 parent 313fbb1 commit 613a157

6 files changed

Lines changed: 123 additions & 138 deletions

File tree

Lines changed: 72 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
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"
@@ -8,7 +7,7 @@
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

2524
static char cdc_accum_buf[CDC_ACCUM_BUF_SIZE];
2625
static 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

3799
static 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-
199175
void 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
}

firmware/esp32-s3/main/main.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
#include "cJSON.h"
2-
#include "class/hid/hid_device.h"
31
#include "config_storage.h"
42
#include "esp_log.h"
5-
#include "esp_spiffs.h"
63
#include "freertos/FreeRTOS.h"
7-
#include "freertos/queue.h"
8-
#include "freertos/task.h"
9-
#include "macrolev.h"
10-
#include "sdkconfig.h"
11-
#include "tinyusb.h"
12-
#include "tusb_cdc_acm.h"
134
#include "usb.h"
145
#include <stdio.h>
156
#include <string.h>

firmware/esp32-s3/main/sensors.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "freertos/FreeRTOS.h"
55
#include "freertos/queue.h"
66
#include "freertos/task.h"
7+
#include "macrolev.h"
78
#include "sdkconfig.h"
89
#include <stdio.h>
910
#include <string.h>

firmware/esp32-s3/main/usb.c

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1+
#include "usb.h"
2+
#include "class/hid/hid_device.h"
3+
#include "esp_log.h"
4+
#include "freertos/FreeRTOS.h"
5+
#include "freertos/queue.h"
6+
#include "freertos/task.h"
17
#include "tinyusb.h"
8+
#include "tusb_cdc_acm.h"
29

310
static const char *TAG = "MACROLEV";
411

5-
// Interface definitions
12+
#define TUSB_DESC_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN * CFG_TUD_HID + TUD_CDC_DESC_LEN * CFG_TUD_CDC)
13+
14+
QueueHandle_t usb_cdc_rx_queue;
15+
616
enum {
717
ITF_NUM_HID = 0,
818
ITF_NUM_CDC,
919
ITF_NUM_CDC_DATA,
1020
ITF_NUM_TOTAL
1121
};
1222

13-
#define TUSB_DESC_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN * CFG_TUD_HID + TUD_CDC_DESC_LEN * CFG_TUD_CDC)
14-
15-
/**
16-
* @brief HID report descriptor
17-
*
18-
* In this example we implement Keyboard + Mouse HID device,
19-
* so we must define both report descriptors
20-
*/
2123
const uint8_t hid_report_descriptor[] = {
2224
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(HID_ITF_PROTOCOL_KEYBOARD)),
2325
};
@@ -32,11 +34,6 @@ const char *string_descriptor[] = {
3234
"Macrolev HID Device", // 5: HID Interface
3335
};
3436

35-
/**
36-
* @brief Configuration descriptor
37-
*
38-
* This is a simple configuration descriptor that defines 1 configuration and 1 HID interface
39-
*/
4037
static uint8_t configuration_descriptor[] = {
4138
// Configuration number, interface count, string index, total length, attribute, power in mA
4239
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, TUSB_DESC_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
@@ -80,9 +77,41 @@ uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_t
8077
void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const *buffer, uint16_t bufsize) {
8178
};
8279

80+
void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event) {
81+
usb_message_t msg = { 0 };
82+
msg.buf_len = 0;
83+
84+
// Read data from CDC
85+
esp_err_t ret = tinyusb_cdcacm_read(itf, msg.buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &msg.buf_len);
86+
if (ret == ESP_OK && msg.buf_len > 0) {
87+
// Send message to queue
88+
if (xQueueSend(usb_cdc_rx_queue, &msg, portMAX_DELAY) != pdTRUE) {
89+
ESP_LOGE(TAG, "Failed to send message to queue");
90+
}
91+
}
92+
}
93+
8394
void usb_init(void) {
8495
// Initialize TinyUSB
8596
ESP_LOGI(TAG, "USB initialization");
8697
extern const tinyusb_config_t tusb_cfg;
8798
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
99+
100+
// Create the CDC RX queue
101+
usb_cdc_rx_queue = xQueueCreate(5, sizeof(usb_message_t));
102+
assert(usb_cdc_rx_queue);
103+
104+
// Initialize CDC
105+
tinyusb_config_cdcacm_t acm_cfg = {
106+
.usb_dev = TINYUSB_USBDEV_0,
107+
.cdc_port = TINYUSB_CDC_ACM_0,
108+
.callback_rx = &tinyusb_cdc_rx_callback,
109+
.callback_rx_wanted_char = NULL,
110+
.callback_line_state_changed = NULL,
111+
.callback_line_coding_changed = NULL,
112+
.rx_unread_buf_sz = CONFIG_TINYUSB_CDC_RX_BUFSIZE
113+
};
114+
ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg));
115+
116+
ESP_LOGI(TAG, "USB initialization DONE");
88117
};

firmware/esp32-s3/main/usb.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
#include "tinyusb.h"
2+
3+
typedef struct usb_message {
4+
uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1];
5+
size_t buf_len;
6+
} usb_message_t;
7+
18
void usb_init(void);

firmware/esp32-s3/sdkconfig.defaults

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ CONFIG_TINYUSB_DEBUG_LEVEL=3
1010
CONFIG_TINYUSB_DESC_MANUFACTURER_STRING="Heiso"
1111
CONFIG_TINYUSB_DESC_PRODUCT_STRING="Macrolev Rev.2"
1212
CONFIG_TINYUSB_DESC_CDC_STRING="Macrolev CDC Device"
13-
CONFIG_TINYUSB_MSC_ENABLED=y
1413
CONFIG_TINYUSB_CDC_ENABLED=y
1514
CONFIG_TINYUSB_CDC_RX_BUFSIZE=1024
1615
CONFIG_TINYUSB_CDC_TX_BUFSIZE=1024

0 commit comments

Comments
 (0)