1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-03 19:32:39 +02:00

Merge pull request #208 from luc-github/Devt

add command handler
This commit is contained in:
bdring
2019-09-06 11:29:43 -05:00
committed by GitHub
6 changed files with 101 additions and 0 deletions

View File

@@ -68,6 +68,93 @@ void COMMANDS::wait(uint32_t milliseconds){
}
}
bool COMMANDS::execute_command (String cmd_params)
{
bool res = true;
String currentline ="";
//parse all line in case of multi commands
for (uint p = 0; p < cmd_params.length(); p++){
//separator is \n or \r or nothing for last command (just in case)
if ((cmd_params[p] == '\r') || (cmd_params[p] == '\n') || (p == (cmd_params.length()-1))) {
//if last char is not a \r neither \n
if ((p == (cmd_params.length()-1)) && !((cmd_params[p] == '\r') || (cmd_params[p] == '\n'))) {
currentline+=cmd_params[p]; //add char to line
}
//remove space
currentline.trim();
//still have a content ?
if (currentline.length() > 0) { //process the line
int ESPpos = currentline.indexOf ("[ESP");
if (ESPpos > -1) {
//is there the second part?
int ESPpos2 = currentline.indexOf ("]", ESPpos);
if (ESPpos2 > -1) {
//Split in command and parameters
String cmd_part1 = currentline.substring (ESPpos + 4, ESPpos2);
String cmd_part2 = "";
//is there space for parameters?
if (ESPpos2 < currentline.length() ) {
cmd_part2 = currentline.substring (ESPpos2 + 1);
}
//if command is a valid number then execute command
if(cmd_part1.toInt()!=0) {
ESPResponseStream espresponse;
if (!execute_internal_command(cmd_part1.toInt(),cmd_part2, LEVEL_ADMIN, &espresponse)){
report_status_message(STATUS_GCODE_UNSUPPORTED_COMMAND, CLIENT_ALL);
res = false;
}
}
//if not is not a valid [ESPXXX] command ignore it
}
} else {
//preprocess line
String processedline = "";
char c;
uint8_t line_flags = 0;
for (uint16_t index=0; index < currentline.length(); index++){
c = currentline[index];
if (c == '\r' || c == ' ' || c == '\n') {
// ignore these whitespace items
}
else if (c == '(') {
line_flags |= LINE_FLAG_COMMENT_PARENTHESES;
}
else if (c == ')') {
// End of '()' comment. Resume line allowed.
if (line_flags & LINE_FLAG_COMMENT_PARENTHESES) { line_flags &= ~(LINE_FLAG_COMMENT_PARENTHESES); }
}
else if (c == ';') {
// NOTE: ';' comment to EOL is a LinuxCNC definition. Not NIST.
if (!(line_flags & LINE_FLAG_COMMENT_PARENTHESES)) // semi colon inside parentheses do not mean anything
line_flags |= LINE_FLAG_COMMENT_SEMICOLON;
}
else { // add characters to the line
if (!line_flags) {
c = toupper(c); // make upper case
processedline += c;
}
}
}
if (processedline.length() > 0) {
uint8_t r = gc_execute_line((char *)processedline.c_str(), CLIENT_NONE);
if (STATUS_OK != r) {
report_status_message(r, CLIENT_ALL);
res=false;
}
}
wait (1);
}
wait (1);
currentline="";
}
} else { //add char to line
currentline+=cmd_params[p];
}
}
return res;
}
bool COMMANDS::execute_internal_command (int cmd, String cmd_params, level_authenticate_type auth_level, ESPResponseStream *espresponse)
{
bool response = true;

View File

@@ -42,6 +42,7 @@ class COMMANDS
public:
static bool check_command (const char *, int * cmd, String & cmd_params);
static String get_param (String & cmd_params, const char * id, bool withspace);
static bool execute_command (String cmd_params);
static bool execute_internal_command (int cmd, String cmd_params, level_authenticate_type auth_level = LEVEL_GUEST , ESPResponseStream *espresponse= NULL);
static void wait(uint32_t milliseconds);
static void handle();

View File

@@ -34,6 +34,14 @@ ESPResponseStream::ESPResponseStream(WebServer * webserver){
}
#endif
ESPResponseStream::ESPResponseStream(){
_client = CLIENT_NONE;
#if defined (ENABLE_HTTP) && defined(ENABLE_WIFI)
_header_sent=false;
_webserver = NULL;
#endif
}
ESPResponseStream::ESPResponseStream(uint8_t client){
_client = client;
#if defined (ENABLE_HTTP) && defined(ENABLE_WIFI)
@@ -63,6 +71,7 @@ String ESPResponseStream::formatBytes (uint64_t bytes)
}
void ESPResponseStream::print(const char *data){
if (_client == CLIENT_NONE) return;
#if defined (ENABLE_HTTP) && defined(ENABLE_WIFI)
if (_webserver) {
if (!_header_sent) {

View File

@@ -37,6 +37,7 @@ class ESPResponseStream{
ESPResponseStream(WebServer * webserver);
#endif
ESPResponseStream(uint8_t client);
ESPResponseStream();
private:
uint8_t _client;
#if defined (ENABLE_HTTP) && defined(ENABLE_WIFI)

View File

@@ -53,6 +53,7 @@
// this is a generic send function that everything should use, so interfaces could be added (Bluetooth, etc)
void grbl_send(uint8_t client, const char *text)
{
if (client == CLIENT_NONE) return;
#ifdef ENABLE_BLUETOOTH
if (SerialBT.hasClient() && ( client == CLIENT_BT || client == CLIENT_ALL ) )
{
@@ -80,6 +81,7 @@ void grbl_send(uint8_t client, const char *text)
// This is a formating version of the grbl_send(CLIENT_ALL,...) function that work like printf
void grbl_sendf(uint8_t client, const char *format, ...)
{
if (client == CLIENT_NONE) return;
char loc_buf[64];
char * temp = loc_buf;
va_list arg;

View File

@@ -98,6 +98,7 @@
#define MESSAGE_SLEEP_MODE 11
#define MESSAGE_SD_FILE_QUIT 60 // mc_reset was called during an SD job
#define CLIENT_NONE 0
#define CLIENT_SERIAL 1
#define CLIENT_BT 2
#define CLIENT_WEBUI 3