Skip to content

Commit

Permalink
Fix GCode client is not processed
Browse files Browse the repository at this point in the history
Remove debug log
Change Gcode Error code to avoid GCode Host Code overlap
  • Loading branch information
luc-github committed Sep 28, 2024
1 parent 632051a commit 80ff484
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 45 deletions.
2 changes: 1 addition & 1 deletion esp3d/src/core/commands/ESP420.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ void ESP3DCommands::ESP420(int cmd_params_pos, ESP3DMessage* msg) {
return;
}
// IP mode
esp3d_log_d("IP mode %d", NetConfig::isIPModeDHCP(ESP_ETH_STA));
esp3d_log("IP mode %d", NetConfig::isIPModeDHCP(ESP_ETH_STA));
tmpstr = (NetConfig::isIPModeDHCP(ESP_ETH_STA)) ? "dhcp" : "static";
if (!dispatchIdValue(json, "ip mode", tmpstr.c_str(), target, requestId,
false)) {
Expand Down
32 changes: 32 additions & 0 deletions esp3d/src/core/esp3d_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ const char *esp3dmsgstr[] = {"head", "core", "tail", "unique"};
#include "../modules/display/display.h"
#endif // DISPLAY_DEVICE

#if defined(GCODE_HOST_FEATURE)
#include "../modules/gcode_host/gcode_host.h"
#endif // GCODE_HOST_FEATURE

ESP3DCommands esp3d_commands;

ESP3DCommands::ESP3DCommands() {
Expand Down Expand Up @@ -1437,6 +1441,16 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
break;
#endif // COMMUNICATION_PROTOCOL == MKS_SERIAL

#if defined(GCODE_HOST_FEATURE)
case ESP3DClientType::stream:
esp3d_log("Gcode host message");
if (!esp3d_gcode_host.dispatch(msg)) {
sendOk = false;
esp3d_log_e("Gcode host dispatch failed");
}
break;
#endif // GCODE_HOST_FEATURE

#ifdef PRINTER_HAS_DISPLAY
case ESP3DClientType::remote_screen:
esp3d_log("Remote screen message");
Expand Down Expand Up @@ -1498,6 +1512,24 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
}
#endif // ESP_LUA_INTERPRETER_FEATURE

#if defined(GCODE_HOST_FEATURE)
if (msg->origin != ESP3DClientType::stream && esp3d_gcode_host.getStatus() != HOST_NO_STREAM) {
if (msg->target == ESP3DClientType::all_clients) {
// become the reference message
msg->target = ESP3DClientType::stream;
} else {
// duplicate message because current is already pending
ESP3DMessage *copy_msg = esp3d_message_manager.copyMsg(*msg);
if (copy_msg) {
copy_msg->target = ESP3DClientType::stream;
dispatch(copy_msg);
} else {
esp3d_log_e("Cannot duplicate message for gcode host");
}
}
}
#endif

#if defined(DISPLAY_DEVICE)
if (msg->origin != ESP3DClientType::rendering &&
msg->origin != getOutputClient()) {
Expand Down
2 changes: 1 addition & 1 deletion esp3d/src/include/esp3d_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define _VERSION_ESP3D_H

// version and sources location
#define FW_VERSION "3.0.0.a238"
#define FW_VERSION "3.0.0.a239"
#define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0"

#endif //_VERSION_ESP3D_H
6 changes: 3 additions & 3 deletions esp3d/src/modules/ethernet/ethconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool EthConfig::begin(int8_t& espMode) {
ipMode(true);
end();
#if ESP3D_ETH_PHY_TYPE == TYPE_ETH_PHY_LAN8720
esp3d_log_d("ETH PHY Type %d", ESP3D_ETH_PHY_TYPE);
esp3d_log("ETH PHY Type %d", ESP3D_ETH_PHY_TYPE);
_started = ETH.begin();
#endif // ESP3D_ETH_PHY_TYPE == TYPE_ETH_PHY_LAN8720
#if ESP3D_ETH_PHY_TYPE == TYPE_ETH_PHY_TLK110 || \
Expand All @@ -101,7 +101,7 @@ bool EthConfig::begin(int8_t& espMode) {
if (ESP3D_ETH_PHY_TYPE == TYPE_ETH_PHY_KSZ8081) {
phytype = ETH_PHY_KSZ8081;
}
esp3d_log_d("ETH PHY Type %d", phytype);
esp3d_log("ETH PHY Type %d", phytype);
_started = ETH.begin(phytype, ESP3D_ETH_PHY_ADDR,
ESP3D_ETH_PHY_POWER_PIN, ESP3D_ETH_PHY_MDC_PIN,
ESP3D_ETH_PHY_MDIO_PIN, ESP3D_ETH_CLK_MODE_PIN);
Expand All @@ -110,7 +110,7 @@ bool EthConfig::begin(int8_t& espMode) {
// ESP3D_ETH_PHY_TYPE == TYPE_ETH_PHY_KSZ8041 || ESP3D_ETH_PHY_TYPE ==
// TYPE_ETH_PHY_KSZ8081
#if ESP3D_ETH_PHY_TYPE == TYPE_ETH_PHY_W5500
esp3d_log_d("ETH spi PHY Type %d", ESP3D_ETH_PHY_TYPE);
esp3d_log("ETH spi PHY Type %d", ESP3D_ETH_PHY_TYPE);
ETH_SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
_started = ETH.begin(ETH_PHY_W5500, ESP3D_ETH_PHY_ADDR, ETH_PHY_CS,
ETH_PHY_IRQ, ETH_PHY_RST, ETH_SPI);
Expand Down
26 changes: 19 additions & 7 deletions esp3d/src/modules/gcode_host/gcode_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ bool GcodeHost::begin() {
return true;
}

bool GcodeHost::dispatch(ESP3DMessage* message) {
if (!message || _step == HOST_NO_STREAM) {
return false;
}
if (message->size > 0 && message->data) {
push(message->data, message->size);
esp3d_message_manager.deleteMsg(message);
return true;
}
return false;
}

void GcodeHost::end() {
_commandNumber = 0;
_error = ERROR_NO_ERROR;
Expand Down Expand Up @@ -313,7 +325,9 @@ void GcodeHost::processCommand() {
_step = HOST_READ_LINE;
} else {
esp3d_log("Command %s is valid", _currentCommand.c_str());
String cmd = _currentCommand + "\n";
if (!_currentCommand.endsWith("\n")) {
_currentCommand += "\n";
}
bool isESPcmd = esp3d_commands.is_esp_command(
(uint8_t *)_currentCommand.c_str(), _currentCommand.length());
if (isESPcmd) {
Expand All @@ -323,7 +337,7 @@ void GcodeHost::processCommand() {
if (msg) {
// process command
esp3d_commands.process(msg);
esp3d_log("Command is ESP command: %s", cmd.c_str());
esp3d_log("Command is ESP command: %s", _currentCommand.c_str());
_step = HOST_READ_LINE;
} else {
esp3d_log_e("Cannot create message");
Expand All @@ -337,7 +351,7 @@ void GcodeHost::processCommand() {
if (msg) {
// process command
esp3d_commands.process(msg);
esp3d_log("Command is GCODE command: %s", cmd.c_str());
esp3d_log("Command is GCODE command: %s", _currentCommand.c_str());
_startTimeOut = millis();
if (isAckNeeded()) {
_step = HOST_WAIT4_ACK;
Expand Down Expand Up @@ -494,10 +508,8 @@ uint32_t GcodeHost::getCommandNumber(String &response) {
bool GcodeHost::processScript(const char *line,
ESP3DAuthenticationLevel auth_type) {
if (_step != HOST_NO_STREAM) {
esp3d_log("Streaming already in progress");
while (_step != HOST_NO_STREAM) {
handle();
}
esp3d_log("Streaming already in progress");
return false;
}
_script = line;
_script.trim();
Expand Down
24 changes: 13 additions & 11 deletions esp3d/src/modules/gcode_host/gcode_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define _GCODE_HOST_H

#include <Arduino.h>

#include "../../core/esp3d_message.h"
#include "../authentication/authentication_service.h"

#define ERROR_NO_ERROR 0
Expand All @@ -40,16 +40,16 @@
#define ERROR_FILE_NOT_FOUND 12
#define ERROR_STREAM_ABORTED 13

#define HOST_NO_STREAM 0
#define HOST_START_STREAM 1
#define HOST_READ_LINE 2
#define HOST_PROCESS_LINE 3
#define HOST_WAIT4_ACK 4
#define HOST_PAUSE_STREAM 5
#define HOST_RESUME_STREAM 6
#define HOST_STOP_STREAM 7
#define HOST_ERROR_STREAM 8
#define HOST_ABORT_STREAM 9
#define HOST_NO_STREAM 100
#define HOST_START_STREAM 101
#define HOST_READ_LINE 102
#define HOST_PROCESS_LINE 103
#define HOST_WAIT4_ACK 104
#define HOST_PAUSE_STREAM 105
#define HOST_RESUME_STREAM 106
#define HOST_STOP_STREAM 107
#define HOST_ERROR_STREAM 108
#define HOST_ABORT_STREAM 109

#define TYPE_SCRIPT_STREAM 0
#define TYPE_FS_STREAM 1
Expand All @@ -74,6 +74,8 @@ class GcodeHost {
uint8_t Checksum(const char* command, uint32_t commandSize);
String CheckSumCommand(const char* command, uint32_t commandnb);

bool dispatch(ESP3DMessage* message);

/*bool wait_for_ack(uint32_t timeout = DEFAULT_TIMOUT, bool checksum=false,
* const char * ack=nullptr);*/

Expand Down
4 changes: 2 additions & 2 deletions esp3d/src/modules/serial/serial_service_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void ESP3DSerialService::handle() {
len = 10;
}
while (len > 0) {
esp3d_log_d("Serial in fifo size %d", _messagesInFIFO.size());
esp3d_log("Serial in fifo size %d", _messagesInFIFO.size());
ESP3DMessage *message = _messagesInFIFO.pop();
if (message) {
esp3d_commands.process(message);
Expand Down Expand Up @@ -274,7 +274,7 @@ void ESP3DSerialService::flushbuffer() {
if (message) {
// process command
message->type = ESP3DMessageType::unique;
esp3d_log_d("Message sent to fifo list");
esp3d_log("Message sent to fifo list");
_messagesInFIFO.push(message);
} else {
esp3d_log_e("Cannot create message");
Expand Down
2 changes: 1 addition & 1 deletion esp3d/src/modules/time/time_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ bool TimeService::updateTimeZone(bool fromsettings) {
_time_zone_config += ">";
_time_zone_config += _time_zone[0]=='+' ? "-" : "+";
_time_zone_config += &_time_zone[1];
esp3d_log_d("Time zone is %s", _time_zone_config.c_str());
esp3d_log("Time zone is %s", _time_zone_config.c_str());
return true;
}

Expand Down
64 changes: 51 additions & 13 deletions esp3d/src/modules/usb-serial/usb_serial_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,34 @@ const size_t SupportedUsbSerialBaudListSize = sizeof(SupportedUsbSerialBaudList)
ESP3DUsbSerialService esp3d_usb_serial_service;

#define SERIAL_COMMUNICATION_TIMEOUT 500


// Serial Parameters
#define ESP_USBSERIAL_PARAM SERIAL_8N1
#define ESP_USB_SERIAL_DATA_BITS (8)
#define ESP_USB_SERIAL_PARITY \
(0) // 0: 1 stopbit, 1: 1.5 stopbits, 2: 2 stopbits
#define ESP_USB_SERIAL_STOP_BITS \
(0) // 0: None, 1: Odd, 2: Even, 3: Mark, 4: Space
// Task parameters
#define ESP_USB_SERIAL_RX_BUFFER_SIZE 512
#define ESP_USB_SERIAL_TX_BUFFER_SIZE 128

#define ESP_USB_SERIAL_TASK_SIZE 4096
#if CONFIG_FREERTOS_UNICORE
#define ESP_USB_SERIAL_TASK_CORE 0
#else
#define ESP_USB_SERIAL_TASK_CORE 1
#endif // CONFIG_FREERTOS_UNICORE

#define ESP_USB_SERIAL_TASK_PRIORITY 10

#define TIMEOUT_USB_SERIAL_FLUSH 1500
// Constructor
ESP3DUsbSerialService::ESP3DUsbSerialService() {
_buffer_size = 0;
_mutex = NULL;
_buffer_mutex = NULL;
_device_disconnected_mutex = NULL;
_is_connected = false;
_xHandle = NULL;
_vcp_ptr = NULL;
_started = false;
#if defined(AUTHENTICATION_FEATURE)
_needauthentication = true;
Expand Down Expand Up @@ -96,7 +113,7 @@ void ESP3DUsbSerialService::receiveCb() {
if (!started()) {
return;
}
/* if (xSemaphoreTake(_mutex, portMAX_DELAY)) {
/* if (xSemaphoreTake(_buffer_mutex, portMAX_DELAY)) {
uint32_t now = millis();
while ((millis() - now) < SERIAL_COMMUNICATION_TIMEOUT) {
if (Serials[_serialIndex]->available()) {
Expand All @@ -114,16 +131,21 @@ void ESP3DUsbSerialService::receiveCb() {
}
}
xSemaphoreGive(_mutex);
xSemaphoreGive(_buffer_mutex);
} else {
esp3d_log_e("Mutex not taken");
}*/
}

// Setup Serial
bool ESP3DUsbSerialService::begin() {
_mutex = xSemaphoreCreateMutex();
if (_mutex == NULL) {
_buffer_mutex = xSemaphoreCreateMutex();
if (_buffer_mutex == NULL) {
esp3d_log_e("Mutex creation failed");
return false;
}
_device_disconnected_mutex = xSemaphoreCreateMutex();
if (_device_disconnected_mutex == NULL) {
esp3d_log_e("Mutex creation failed");
return false;
}
Expand Down Expand Up @@ -158,13 +180,29 @@ uint32_t defaultBr = ESP3DSettings::getDefaultIntegerSetting(ESP_USB_SERIAL_BAUD
bool ESP3DUsbSerialService::end() {
flush();
delay(100);
if (_mutex != NULL) {
vSemaphoreDelete(_mutex);
_mutex = NULL;
if (_buffer_mutex != NULL) {
vSemaphoreDelete(_buffer_mutex);
_buffer_mutex = NULL;
}
if (_device_disconnected_mutex != NULL) {
vSemaphoreDelete(_device_disconnected_mutex);
_device_disconnected_mutex = NULL;
}
//Serials[_serialIndex]->end();
_buffer_size = 0;
_started = false;
_is_connected = false;
if (_xHandle != NULL) {
vTaskDelete(_xHandle);
_xHandle = NULL;
esp3d_log("Task deleted");
}
if (_vcp_ptr != NULL) {
_vcp_ptr = NULL;
esp3d_log("VCP deleted");
}
usb_serial_deinit();
usb_serial_delete_task();
initAuthentication();
return true;
}
Expand All @@ -186,7 +224,7 @@ void ESP3DUsbSerialService::handle() {
len = 10;
}
while (len > 0) {
esp3d_log_d("Serial in fifo size %d", _messagesInFIFO.size());
esp3d_log("Serial in fifo size %d", _messagesInFIFO.size());
ESP3DMessage *message = _messagesInFIFO.pop();
if (message) {
esp3d_commands.process(message);
Expand All @@ -212,7 +250,7 @@ void ESP3DUsbSerialService::flushbuffer() {
if (message) {
// process command
message->type = ESP3DMessageType::unique;
esp3d_log_d("Message sent to fifo list");
esp3d_log("Message sent to fifo list");
_messagesInFIFO.push(message);
} else {
esp3d_log_e("Cannot create message");
Expand Down
7 changes: 6 additions & 1 deletion esp3d/src/modules/usb-serial/usb_serial_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "../../core/esp3d_client_types.h"
#include "../../core/esp3d_message.h"
#include "../../core/esp3d_messageFifo.h"
#include <esp32_usb_serial.h>

#define ESP3D_USB_SERIAL_BUFFER_SIZE 1024

Expand Down Expand Up @@ -59,10 +60,14 @@ class ESP3DUsbSerialService final {
ESP3DClientType _origin;
bool _started;
bool _needauthentication;
bool _is_connected;
uint32_t _lastflush;
uint8_t _buffer[ESP3D_USB_SERIAL_BUFFER_SIZE + 1]; // keep space of 0x0 terminal
size_t _buffer_size;
SemaphoreHandle_t _mutex;
SemaphoreHandle_t _buffer_mutex;
SemaphoreHandle_t _device_disconnected_mutex;
std::unique_ptr<CdcAcmDevice> _vcp_ptr;
TaskHandle_t _xHandle;
ESP3DMessageFIFO _messagesInFIFO;
void push2buffer(uint8_t *sbuf, size_t len);
void flushbuffer();
Expand Down
Loading

0 comments on commit 80ff484

Please sign in to comment.