Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grbl grblHAL better support for realtime commands #1064

Merged
merged 10 commits into from
Dec 8, 2024
8 changes: 4 additions & 4 deletions esp3d/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@
/* Printer screen
* If your printer has a display
*/
#define PRINTER_HAS_DISPLAY
//#define PRINTER_HAS_DISPLAY

/* ESP3D screen
* Screen connected to ESP board
Expand Down Expand Up @@ -644,9 +644,9 @@
// LOG_OUTPUT_SERIAL2
// LOG_OUTPUT_TELNET
// LOG_OUTPUT_WEBSOCKET
// #define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0
#define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0

// #define ESP3D_DEBUG_LEVEL LOG_LEVEL_DEBUG
#define ESP3D_DEBUG_LEVEL LOG_LEVEL_DEBUG

#ifdef ESP_LOG_FEATURE
#define LOG_ESP3D_BAUDRATE 115200
Expand All @@ -657,7 +657,7 @@
// #define ESP_BENCHMARK_FEATURE

// Disable sanity check at compilation
// #define ESP_NO_SANITY_CHECK
#define ESP_NO_SANITY_CHECK

/************************************
*
Expand Down
2 changes: 1 addition & 1 deletion esp3d/src/core/esp3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ void Esp3D::restart_now() {
#if defined(FILESYSTEM_FEATURE)
ESP_FileSystem::end();
#endif // FILESYSTEM_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
#if (COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL) & defined(ARDUINO_ARCH_ESP8266)
esp3d_serial_service.swap();
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
Expand Down
23 changes: 11 additions & 12 deletions esp3d/src/core/esp3d_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ ESP3DCommands::ESP3DCommands() {

ESP3DCommands::~ESP3DCommands() {}

bool ESP3DCommands::isRealTimeCommand(char *cmd, size_t len) { return false; }

// check if current line is an [ESPXXX] command
bool ESP3DCommands::is_esp_command(uint8_t *sbuf, size_t len) {
// TODO
Expand Down Expand Up @@ -1156,10 +1154,6 @@ bool ESP3DCommands::dispatchIdValue(bool json, const char *Id,
}

bool ESP3DCommands::formatCommand(char *cmd, size_t len) {
if (isRealTimeCommand(cmd, len)) {
// TODO: what if is realtime command ?
return true;
}
uint sizestr = strlen(cmd);
if (len > sizestr + 2) {
cmd[sizestr] = '\n';
Expand Down Expand Up @@ -1205,10 +1199,6 @@ void ESP3DCommands::process(ESP3DMessage *msg) {
esp3d_log("Execute internal command %d", cmdId);
execute_internal_command(cmdId, espcmdpos, msg);
} else {
/*esp3d_log("Dispatch command, len %d, from %d(%s) to %d(%s)", msg->size,
static_cast<uint8_t>(msg->origin), GETCLIENTSTR(msg->origin),
static_cast<uint8_t>(msg->target), GETCLIENTSTR(msg->target));*/

// Work around to avoid to dispatch single \n or \r to everyone as it is
// part of previous ESP3D command
if (msg->size == 1 &&
Expand Down Expand Up @@ -1237,7 +1227,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg, uint8_t *sbuf, size_t len) {
// check is need \n at the end of the command
if (msg->type == ESP3DMessageType::unique ||
msg->type == ESP3DMessageType::tail) {
esp3d_log("unique or tail message :*%s*", (char *)sbuf);
esp3d_log_d("unique or tail message :*%s*", (char *)sbuf);
if (!formatCommand((char *)sbuf, len)) {
esp3d_log("format command failed");
String tmpstr = "";
Expand All @@ -1254,7 +1244,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg, uint8_t *sbuf, size_t len) {
return false;
}
} else {
esp3d_log("format command success, no need to update");
esp3d_log_d("format command success, no need to update");
if (!esp3d_message_manager.setDataContent(msg, sbuf, len)) {
esp3d_log_e("set data content failed");
esp3d_message_manager.deleteMsg(msg);
Expand All @@ -1263,6 +1253,9 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg, uint8_t *sbuf, size_t len) {
}
} else {
esp3d_log("not unique or tail message");
if (msg->type == ESP3DMessageType::realtimecmd){
esp3d_log_d("realtime command");
}
if (!esp3d_message_manager.setDataContent(msg, sbuf, len)) {
esp3d_log_e("set data content failed");
esp3d_message_manager.deleteMsg(msg);
Expand Down Expand Up @@ -1472,6 +1465,12 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {

#ifdef PRINTER_HAS_DISPLAY
case ESP3DClientType::remote_screen:
if (ESP3DSettings::GetFirmwareTarget() == GRBL ||
ESP3DSettings::GetFirmwareTarget() == GRBLHAL) {
esp3d_log_e("Remote screen message is not supported for GRBL");
sendOk = false;
break;
}
esp3d_log("Remote screen message");
// change target to output client
msg->target = getOutputClient();
Expand Down
1 change: 0 additions & 1 deletion esp3d/src/core/esp3d_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ class ESP3DCommands {
bool isFirst = false);
bool dispatchAuthenticationError(ESP3DMessage* msg, uint cmdid, bool json);
bool formatCommand(char* cmd, size_t len);
bool isRealTimeCommand(char* cmd, size_t len);
ESP3DClientType getOutputClient(bool fromSettings = false);
void setOutputClient(ESP3DClientType output_client) {
_output_client = output_client;
Expand Down
2 changes: 1 addition & 1 deletion esp3d/src/core/esp3d_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class WebServer;
#endif //pdTRUE
#endif //ESP8266

enum class ESP3DMessageType : uint8_t { head, core, tail, unique };
enum class ESP3DMessageType : uint8_t { head, core, tail, unique, realtimecmd };

union ESP3DRequest {
uint id;
Expand Down
36 changes: 27 additions & 9 deletions esp3d/src/core/esp3d_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <Arduino.h>

#include "../include/esp3d_config.h"
#include "esp3d_settings.h"

#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
#include "../modules/network/netconfig.h"
Expand Down Expand Up @@ -174,16 +175,16 @@ const char* esp3d_string::urlEncode(const char* s) {
static String encoded;
encoded = "";
char temp[4];
for (int i = 0; i < strlen(s); i++) {
temp[0] =s[i];
if (temp[0] == 32) { //space
for (size_t i = 0; i < strlen(s); i++) {
temp[0] = s[i];
if (temp[0] == 32) { // space
encoded.concat('+');
} else if ((temp[0] >= 48 && temp[0] <= 57) /*0-9*/
|| (temp[0] >= 65 && temp[0] <= 90) /*A-Z*/
|| (temp[0] >= 97 && temp[0] <= 122) /*a-z*/
} else if ((temp[0] >= 48 && temp[0] <= 57) /*0-9*/
|| (temp[0] >= 65 && temp[0] <= 90) /*A-Z*/
|| (temp[0] >= 97 && temp[0] <= 122) /*a-z*/
) {
encoded.concat(temp[0]);
} else { //character needs encoding
} else { // character needs encoding
snprintf(temp, 4, "%%%02X", temp[0]);
encoded.concat(temp);
}
Expand All @@ -208,7 +209,7 @@ const char* esp3d_string::formatBytes(uint64_t bytes) {

bool esp3d_string::isPrintableChar(char ch) {
int c = static_cast<int>(ch);
if (c == 9 || (c >= 32 && c <= 126) || c >= 128) {
if (c == '\t' || c == '\r' || (c >= ' ' && c <= '~') || c >= 128) {
return true;
}
return false;
Expand Down Expand Up @@ -254,7 +255,7 @@ const char* esp3d_string::formatDuration(uint64_t duration) {
display = true;
}
if (hours > 0 || display) {
result+= String(hours) + "h ";
result += String(hours) + "h ";
display = true;
}
if (minutes > 0 || display) {
Expand All @@ -264,4 +265,21 @@ const char* esp3d_string::formatDuration(uint64_t duration) {
result += String(seconds) + "s";

return result.c_str();
}

bool esp3d_string::isRealTimeCommand(char c) {
if (ESP3DSettings::GetFirmwareTarget() == GRBL ||
ESP3DSettings::GetFirmwareTarget() == GRBLHAL) {
// Standard characters
if (c == '?' || c == '!' || c == '~' || c == 0x18) { // 0x18 is ^X
return true;
}

// Range >= 0x80 et <= 0xA4
const unsigned char uc = static_cast<unsigned char>(c);
if (uc >= 0x80 && uc <= 0xA4) {
return true;
}
}
return false;
}
1 change: 1 addition & 0 deletions esp3d/src/core/esp3d_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const char* formatBytes(uint64_t bytes);
bool isPrintableChar(char c);
const char* expandString(const char* s, bool formatspace = false);
const char * urlEncode(const char* s);
bool isRealTimeCommand(char c);
} // namespace esp3d_string

#endif //_ESP3D_STRING_H
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.a244"
#define FW_VERSION "3.0.0.a245"
#define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0"

#endif //_VERSION_ESP3D_H
63 changes: 30 additions & 33 deletions esp3d/src/modules/bluetooth/BT_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void BTService::handle() {
// we cannot left data in buffer too long
// in case some commands "forget" to add \n
if (((millis() - _lastflush) > TIMEOUT_BT_FLUSH) && (_buffer_size > 0)) {
flushbuffer();
flushBuffer();
}
}

Expand All @@ -189,51 +189,48 @@ void BTService::initAuthentication() {
}
ESP3DAuthenticationLevel BTService::getAuthentication() { return _auth; }

void BTService::flushbuffer() {
_buffer[_buffer_size] = 0x0;
// dispatch command
ESP3DMessage *msg = esp3d_message_manager.newMsg(
ESP3DClientType::bluetooth, esp3d_commands.getOutputClient(), _buffer,
_buffer_size, _auth);
if (msg) {
// process command
esp3d_commands.process(msg);
void BTService::flushData(const uint8_t *data, size_t size,
ESP3DMessageType type) {
ESP3DMessage *message = esp3d_message_manager.newMsg(
ESP3DClientType::bluetooth, esp3d_commands.getOutputClient(), data,
size, _auth);

if (message) {
message->type = type;
esp3d_log("Process Message");
esp3d_commands.process(message);
} else {
esp3d_log_e("Cannot create message");
}
_lastflush = millis();
}

void BTService::flushChar(char c) {
flushData((uint8_t *)&c, 1, ESP3DMessageType::realtimecmd);
}

void BTService::flushBuffer() {
_buffer[_buffer_size] = 0x0;
flushData((uint8_t *)_buffer, _buffer_size, ESP3DMessageType::unique);
_buffer_size = 0;
}

// push collected data to buffer and proceed accordingly
void BTService::push2buffer(uint8_t *sbuf, size_t len) {
if (!_buffer || !_started) {
return;
}
for (size_t i = 0; i < len; i++) {
_lastflush = millis();
// command is defined
if ((char(sbuf[i]) == '\n') || (char(sbuf[i]) == '\r')) {
if (_buffer_size < ESP3D_BT_BUFFER_SIZE) {
_buffer[_buffer_size] = sbuf[i];
_buffer_size++;
}
flushbuffer();
} else if (esp3d_string::isPrintableChar(char(sbuf[i]))) {
if (_buffer_size < ESP3D_BT_BUFFER_SIZE) {
_buffer[_buffer_size] = sbuf[i];
_buffer_size++;
} else {
flushbuffer();
_buffer[_buffer_size] = sbuf[i];
_buffer_size++;
}
} else { // it is not printable char
// clean buffer first
if (_buffer_size > 0) {
flushbuffer();
}
// process char
if (esp3d_string::isRealTimeCommand(sbuf[i])) {
flushChar(sbuf[i]);
} else {
_buffer[_buffer_size] = sbuf[i];
_buffer_size++;
flushbuffer();
if (_buffer_size > ESP3D_BT_BUFFER_SIZE ||
_buffer[_buffer_size - 1] == '\n') {
flushBuffer();
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion esp3d/src/modules/bluetooth/BT_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class BTService {
uint8_t _buffer[ESP3D_BT_BUFFER_SIZE + 1]; // keep space of 0x0 terminal
size_t _buffer_size;
void push2buffer(uint8_t* sbuf, size_t len);
void flushbuffer();
void flushBuffer();
void flushChar(char c);
void flushData(const uint8_t* data, size_t size, ESP3DMessageType type);
bool _started;
};

Expand Down
6 changes: 3 additions & 3 deletions esp3d/src/modules/gcode_host/gcode_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ bool GcodeHost::push(const uint8_t *sbuf, size_t len) {
esp3d_log("Push got %d bytes", len);
for (size_t i = 0; i < len; i++) {
// it is a line process it
if (sbuf[i] == '\n' || sbuf[i] == '\r') {
if (sbuf[i] == '\n') {
flush();
} else {
// fill buffer until it is full
Expand Down Expand Up @@ -263,7 +263,7 @@ void GcodeHost::readNextCommand() {
} else {
_processedSize++;
_currentPosition++;
if (!(((char)c == '\n') || ((char)c == '\r'))) {
if (!((char)c == '\n')) {
_currentCommand += (char)c;
} else {
processing = false;
Expand All @@ -290,7 +290,7 @@ void GcodeHost::readNextCommand() {
} else {
_processedSize++;
_currentPosition++;
if (!(((char)c == '\n') || ((char)c == '\r'))) {
if (!(char)c == '\n' ) {
_currentCommand += (char)c;
} else {
processing = false;
Expand Down
Loading
Loading