Skip to content

Commit b409371

Browse files
authored
Add Usb Serial otg feature (#1055)
* Update esp3d_version.h * Fix GCode client is not processed * Update lua engine to 1.0.3 * Fix HOOKS and Init script conflicting at boot * Add a queue for multiple scripts (max 5) * Fix compilation failed on SERIAL_MKS on ESP32 * Explain better sanity check on SERIAL_MKS and DISPLAY * Implement USB Serial OTG
1 parent 9bac144 commit b409371

File tree

185 files changed

+9986
-94
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+9986
-94
lines changed

.github/ci/prepare-libs.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ cp -r ./libraries/DHT_sensor_library_for_ESPx-1.0.6 $HOME/arduino_ide/libraries/
1212
cp -r ./libraries/esp8266-oled-ssd1306-4.3.0 $HOME/arduino_ide/libraries/
1313
cp -r ./libraries/TFT_eSPI-2.5.43 $HOME/arduino_ide/libraries/
1414
cp -r ./libraries/lvgl-8.2.0 $HOME/arduino_ide/libraries/
15-
cp -r ./libraries/EspLuaEngine-1.0.2 $HOME/arduino_ide/libraries/
15+
cp -r ./libraries/EspLuaEngine-1.0.3 $HOME/arduino_ide/libraries/
1616
cp -r ./libraries/BMx280MI-1.2.0 $HOME/arduino_ide/libraries/
17+
cp -r ./libraries/esp32-usb-serial-1.0.1 $HOME/arduino_ide/libraries/
1718
#TODO add SDFat libraries according version and target
1819

docs/Module-client.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Module client
2+
3+
## Summary
4+
5+
A module client is a class that represent a client, it handle commands from outside to the ESP3D, and handle commands from ESP3D to outside.
6+
7+
WebDav, FTP ,SSDP , MDNS are not module client, they are modules that handle in/out data differently.
8+
9+
The main clients are : Serial, USB Serial, Telnet, WebSocket are kind of full duplex, the http client is a half duplex client for everything but ESP3D commands, in that case it is a full duplex client.
10+
11+
## General description of data flow
12+
13+
The module client fill a buffer char by char until char `\n` or `r` is found or buffer is full, the data come from outside, serial port, usb port, telnet, websocket.
14+
The catch of the commands is done by specific library, and the library inform the module client that a command is received, the module client will then dispatch the command to the ESP3D.
15+
16+
To do that the module client will add some context data to the command, the context data is the client type, the authentication level, the origin of the command, the target of the command, the size of the data, and the data itself. It is called a message.
17+
18+
there 3 cases:
19+
20+
* If a command is not recognized as ESP3D command and origin is not the printer/cnc the message will be transfered to the communication module client with the printer/cnc.
21+
22+
* If the command is recognized as ESP3D command what ever the origin, the message will be transfered to the ESP3D, and the message will change the origin as the target to answer the command.
23+
24+
* If a command is not recognized as ESP3D command and origin is the printer/cnc the message will be dispatched all clients.
25+
26+
The messages are stored in a FIFO which can be accessed from different thread on the ESP32 when it is done by differents functions on ESP8266 sequentially.
27+
28+
Note: Each message is a unique object, it is not possible to send the same message twice, in case of multiple client the message will be duplicated.
29+
30+
Note 2: message are dynamicaly allocated in memory, which means that when dispatch is done or failed the message must be freed to avoid any memory leak.
31+
32+
## Serial
33+
34+
Serial is a full duplex client, it is the default output client for printer/cnc. It use the native Serial library of the ESP32 and ESP8266.
35+
### Output -> ESP3D
36+
The Serial API allow to hook a function to be called when some data is received.
37+
`Serials[_serialIndex]->onReceive(receiveSerialCb);`
38+
39+
When called the function read the data char by char until char `\n` or `r` is found or buffer is full, if it is the case the data is packaged as message and dispatched to the FIFO list by the flush function.
40+
41+
The FIFO list is accessed from different thread/function on the ESP32, it is done by differents functions sequentially on ESP8266 .
42+
43+
The handle function is the one that will dispatch the message to the defined target.
44+
45+
### ESP3D -> Output
46+
47+
When dispatched the message is packaged and sent to the targeted output client. It is delivered to client module by the function `dispatch(ESP3DMessage *message)`.
48+
49+
The message data will be delivered to the client module by the function `writeBytes(const uint8_t *buffer, size_t size)` and using the available function `availableForWrite()` to know how many data can be sent will dispatch the data to Serial.
50+
51+
## USB Serial
52+
53+
USB Serial is a full duplex client, it is optional output client for ESP32 S2/S3 only. It use the esp32-usb-serial library.
54+
The Library has 2 intialization functions: `usb_serial_init()` and `usb_serial_create_task()` which handle usb events.
55+
if no need to handle usb events just call `usb_serial_init()` and do not call `usb_serial_create_task()`.
56+
Additionaly USB Serial client has a dedicated task that check if there is any supported USB Serial device connected, it try all drivers one by one until it find one that match the supported ones.
57+
If device is found it willhook a usb event function and a onreceive function to handle data.

docs/esp3dcnf.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,15 @@ SENSOR_INTERVAL = 30000
158158
#Target Firmware Marlin / Repetier / MarlinKimbra / Smoothieware / GRBL
159159
TargetFW=Marlin
160160

161+
#Output: SERIAL / USB
162+
output=SERIAL
163+
161164
#Baud Rate
162165
Baud_rate = 115200
163166

167+
#Baud Rate
168+
USB_Serial_Baud_rate = 115200
169+
164170
#Boot delay in ms
165171
Boot_delay = 5000
166172

esp3d/configuration.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
*/
6565
#define COMMUNICATION_PROTOCOL RAW_SERIAL
6666

67-
/* Main Serial port
67+
/* Main Serial port / Ouptut
6868
* which serial ESP use to communicate to printer (ESP32 has 3 serials
6969
* available, ESP8266 only 2) USE_SERIAL_0 //for ESP8266/32, also used by
7070
* bootloader output, so consider to make it quiet USE_SERIAL_1 //for ESP8266/32
@@ -73,6 +73,13 @@
7373
// Main serial port
7474
#define ESP_SERIAL_OUTPUT USE_SERIAL_0
7575

76+
77+
/* Optional Output
78+
* Instead of Serial, you can use USB Serial
79+
* USE_USB_SERIAL //for ESP32 S2/S3 Only
80+
*/
81+
//#define USB_SERIAL_FEATURE
82+
7683
/* Bridge Serial port (deprecated on esp8266 as second serial is)
7784
* which serial ESP use to bridge to another device (ESP32 has 3 serials
7885
* available, ESP8266 only 2) USE_SERIAL_0 //for ESP8266/32, also used by
@@ -579,6 +586,7 @@
579586
* Commands to run on startup
580587
* Separate commands with ';' or use file
581588
*/
589+
// #define ESP_AUTOSTART_SCRIPT "[ESP300]/FS/init.lua\n"
582590
// #define ESP_AUTOSTART_SCRIPT "M117 Mounting SD;M21"
583591
// #define ESP_AUTOSTART_SCRIPT_FILE "autoscript.gco"
584592

@@ -625,9 +633,9 @@
625633
// LOG_OUTPUT_SERIAL2
626634
// LOG_OUTPUT_TELNET
627635
// LOG_OUTPUT_WEBSOCKET
628-
//#define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0
636+
// #define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0
629637

630-
//#define ESP3D_DEBUG_LEVEL LOG_LEVEL_DEBUG
638+
// #define ESP3D_DEBUG_LEVEL LOG_LEVEL_DEBUG
631639

632640
#ifdef ESP_LOG_FEATURE
633641
#define LOG_ESP3D_BAUDRATE 115200

esp3d/src/core/commands/ESP0.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,19 @@ const char* help[] = {
179179
"[ESP900](ENABLE/DISABLE) - display/set serial state",
180180
"[ESP901]<BAUD RATE> - display/set serial baud rate",
181181
#endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL
182+
#if defined(USB_SERIAL_FEATURE)
183+
"[ESP902]<BAUD RATE> - display/set USB Serial baud rate",
184+
#endif // defined(USB_SERIAL_FEATURE)
182185
#ifdef BUZZER_DEVICE
183186
"[ESP910](ENABLE/DISABLE) - display/set buzzer state",
184187
#endif // BUZZER_DEVICE
185188
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
186189
"[ESP930](ENABLE/DISABLE/CLOSE) - display/set serial bridge state",
187190
"[ESP931]<BAUD RATE> - display/set serial bridge baud rate",
188191
#endif // defined(ESP_SERIAL_BRIDGE_OUTPUT)
192+
#if defined(USB_SERIAL_FEATURE)
193+
"[ESP950]<SERIAL/USB> - display/set client output",
194+
#endif // defined(USB_SERIAL_FEATURE)
189195
#if defined(ARDUINO_ARCH_ESP32) && \
190196
(CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || \
191197
CONFIG_IDF_TARGET_ESP32C3)
@@ -304,13 +310,18 @@ const uint cmdlist[] = {
304310
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL
305311
900, 901,
306312
#endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL
313+
#if defined(USB_SERIAL_FEATURE)
314+
902,
315+
#endif // defined(USB_SERIAL_FEATURE)
307316
#ifdef BUZZER_DEVICE
308317
910,
309-
310318
#endif // BUZZER_DEVICE
311319
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
312320
930, 931,
313321
#endif // defined(ESP_SERIAL_BRIDGE_OUTPUT)
322+
#if defined(USB_SERIAL_FEATURE)
323+
950,
324+
#endif // defined(USB_SERIAL_FEATURE)
314325
#if defined(ARDUINO_ARCH_ESP32) && \
315326
(CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || \
316327
CONFIG_IDF_TARGET_ESP32C3)

esp3d/src/core/commands/ESP400.cpp

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@
3232
#endif // TIMESTAMP_FEATURE
3333
#define COMMAND_ID 400
3434

35+
#ifndef STRINGIFY
36+
#define STRINGIFY(x) #x
37+
#endif // STRINGIFY
38+
#define STRING(x) STRINGIFY(x)
39+
40+
#if defined(USB_SERIAL_FEATURE)
41+
const char* OutputClientsLabels[] = {
42+
"serial port",
43+
"usb port",
44+
};
45+
const char* OutputClientsValues[] = {"1",
46+
"2"};
47+
#endif // USB_SERIAL_FEATURE
48+
3549
const char* YesNoLabels[] = {"no", "yes"};
3650
const char* YesNoValues[] = {"0", "1"};
3751
const char* RadioModeLabels[] = {"none"
@@ -93,14 +107,14 @@ const char* FallbackValues[] = {"0"
93107

94108
const char* EthFallbackValues[] = {"0"
95109
#ifdef BLUETOOTH_FEATURE
96-
,
97-
"3"
110+
,
111+
"3"
98112
#endif // BLUETOOTH_FEATURE
99113
};
100114
const char* EthFallbackLabels[] = {"none"
101115
#ifdef BLUETOOTH_FEATURE
102-
,
103-
"bt"
116+
,
117+
"bt"
104118
#endif // BLUETOOTH_FEATURE
105119
};
106120

@@ -121,10 +135,14 @@ const char* IpModeValues[] = {"0", "1"};
121135
const char* SupportedApChannelsStr[] = {"1", "2", "3", "4", "5", "6", "7",
122136
"8", "9", "10", "11", "12", "13", "14"};
123137

138+
139+
/// Note Currently the Serial and USB Serial baud rate list is the same
140+
// if this change this part need to be updated
124141
const char* SupportedBaudListSizeStr[] = {
125142
"9600", "19200", "38400", "57600", "74880", "115200", "230400",
126143
"250000", "500000", "921600", "1000000", "1958400", "2000000"};
127144

145+
128146
#ifdef SENSOR_DEVICE
129147

130148
const char* SensorLabels[] = {"none"
@@ -218,28 +236,32 @@ void ESP3DCommands::ESP400(int cmd_params_pos, ESP3DMessage* msg) {
218236
YesNoValues, YesNoLabels, sizeof(YesNoValues) / sizeof(char*),
219237
-1, -1, -1, NULL, true, target, requestId);
220238
#if defined(ETH_FEATURE)
221-
// Ethernet STA IP mode
222-
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_IP_MODE, "ip mode", IpModeValues,
223-
IpModeLabels, sizeof(IpModeLabels) / sizeof(char*), -1, -1,
224-
-1, nullptr, true, target, requestId);
239+
// Ethernet STA IP mode
240+
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_IP_MODE, "ip mode",
241+
IpModeValues, IpModeLabels,
242+
sizeof(IpModeLabels) / sizeof(char*), -1, -1, -1, nullptr,
243+
true, target, requestId);
225244
// Ethernet STA static IP
226-
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_IP_VALUE, "ip", nullptr, nullptr,
227-
-1, -1, -1, -1, nullptr, true, target, requestId);
245+
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_IP_VALUE, "ip", nullptr,
246+
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);
228247

229248
// Ethernet STA static Gateway
230-
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_GATEWAY_VALUE, "gw", nullptr,
231-
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);
249+
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_GATEWAY_VALUE, "gw",
250+
nullptr, nullptr, -1, -1, -1, -1, nullptr, true, target,
251+
requestId);
232252
// Ethernet STA static Mask
233-
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_MASK_VALUE, "msk", nullptr,
234-
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);
253+
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_MASK_VALUE, "msk",
254+
nullptr, nullptr, -1, -1, -1, -1, nullptr, true, target,
255+
requestId);
235256
// Ethernet STA static DNS
236-
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_DNS_VALUE, "DNS", nullptr,
237-
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);
238-
// Ethernet Sta fallback mode
257+
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_DNS_VALUE, "DNS",
258+
nullptr, nullptr, -1, -1, -1, -1, nullptr, true, target,
259+
requestId);
260+
// Ethernet Sta fallback mode
239261
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_FALLBACK_MODE,
240262
"sta fallback mode", EthFallbackValues, EthFallbackLabels,
241-
sizeof(EthFallbackValues) / sizeof(char*), -1, -1, -1, nullptr,
242-
true, target, requestId);
263+
sizeof(EthFallbackValues) / sizeof(char*), -1, -1, -1,
264+
nullptr, true, target, requestId);
243265
#endif // ETH_FEATURE
244266
#ifdef WIFI_FEATURE
245267
// STA SSID network/sta
@@ -251,7 +273,7 @@ void ESP3DCommands::ESP400(int cmd_params_pos, ESP3DMessage* msg) {
251273
nullptr, 64, 8, 0, -1, nullptr, true, target, requestId);
252274

253275
#endif // WIFI_FEATURE
254-
#if defined(WIFI_FEATURE)
276+
#if defined(WIFI_FEATURE)
255277
// STA IP mode
256278
dispatchSetting(json, "network/sta", ESP_STA_IP_MODE, "ip mode", IpModeValues,
257279
IpModeLabels, sizeof(IpModeLabels) / sizeof(char*), -1, -1,
@@ -270,15 +292,15 @@ void ESP3DCommands::ESP400(int cmd_params_pos, ESP3DMessage* msg) {
270292
dispatchSetting(json, "network/sta", ESP_STA_DNS_VALUE, "DNS", nullptr,
271293
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);
272294

273-
#endif // WIFI_FEATURE
295+
#endif // WIFI_FEATURE
274296

275-
#if defined(WIFI_FEATURE)
297+
#if defined(WIFI_FEATURE)
276298
// Sta fallback mode
277299
dispatchSetting(json, "network/sta", ESP_STA_FALLBACK_MODE,
278300
"sta fallback mode", FallbackValues, FallbackLabels,
279301
sizeof(FallbackValues) / sizeof(char*), -1, -1, -1, nullptr,
280302
true, target, requestId);
281-
#endif // WIFI_FEATURE
303+
#endif // WIFI_FEATURE
282304
#if defined(WIFI_FEATURE)
283305
// AP SSID network/ap
284306
dispatchSetting(json, "network/ap", ESP_AP_SSID, "SSID", nullptr, nullptr, 32,
@@ -497,7 +519,20 @@ void ESP3DCommands::ESP400(int cmd_params_pos, ESP3DMessage* msg) {
497519
true, target, requestId);
498520
#endif // FIXED_FW_TARGET
499521
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
500-
// Baud Rate
522+
#if defined(USB_SERIAL_FEATURE)
523+
dispatchSetting(json, "system/system", ESP_OUTPUT_CLIENT, "output",
524+
OutputClientsValues, OutputClientsLabels,
525+
sizeof(OutputClientsValues) / sizeof(char*), -1, -1, -1,
526+
nullptr, true, target, requestId);
527+
// Usb-Serial Baud Rate
528+
dispatchSetting(json, "system/system", ESP_USB_SERIAL_BAUD_RATE,
529+
"usb-serial baud", SupportedBaudListSizeStr,
530+
SupportedBaudListSizeStr,
531+
sizeof(SupportedBaudListSizeStr) / sizeof(char*), -1, -1, -1,
532+
nullptr, true, target, requestId);
533+
534+
#endif // defined(USB_SERIAL_FEATURE)
535+
// Serial Baud Rate
501536
dispatchSetting(json, "system/system", ESP_BAUD_RATE, "baud",
502537
SupportedBaudListSizeStr, SupportedBaudListSizeStr,
503538
sizeof(SupportedBaudListSizeStr) / sizeof(char*), -1, -1, -1,

0 commit comments

Comments
 (0)