Skip to content

Commit

Permalink
add command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-github committed Sep 5, 2019
1 parent dd03459 commit c89e6b7
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
87 changes: 87 additions & 0 deletions Grbl_Esp32/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Grbl_Esp32/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions Grbl_Esp32/espresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Grbl_Esp32/espresponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Grbl_Esp32/report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
{
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Grbl_Esp32/report.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c89e6b7

Please sign in to comment.