1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-08 21:30:54 +02:00

replace execute_command by inputBuffer

New input buffer with no output
- usage inputBuffer.push (const char * data);
no output but errors

- some code formating
This commit is contained in:
Luc
2019-09-08 19:19:47 +02:00
parent eddd737998
commit 7baf850414
11 changed files with 211 additions and 106 deletions

View File

@@ -81,6 +81,7 @@ void setup() {
#ifdef ENABLE_BLUETOOTH
bt_config.begin();
#endif
inputBuffer.begin();
}
void loop() {

View File

@@ -68,93 +68,6 @@ 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,7 +42,6 @@ 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

@@ -42,7 +42,8 @@ ESPResponseStream::ESPResponseStream(){
#endif
}
ESPResponseStream::ESPResponseStream(uint8_t client){
ESPResponseStream::ESPResponseStream(uint8_t client, bool byid){
(void)byid; //fake parameter to avoid confusion with pointer one (NULL == 0)
_client = client;
#if defined (ENABLE_HTTP) && defined(ENABLE_WIFI)
_header_sent=false;

View File

@@ -36,7 +36,7 @@ class ESPResponseStream{
#if defined (ENABLE_HTTP) && defined(ENABLE_WIFI)
ESPResponseStream(WebServer * webserver);
#endif
ESPResponseStream(uint8_t client);
ESPResponseStream(uint8_t client, bool byid = true);
ESPResponseStream();
private:
uint8_t _client;

View File

@@ -20,7 +20,7 @@
// Grbl versioning system
#define GRBL_VERSION "1.1f"
#define GRBL_VERSION_BUILD "20190905"
#define GRBL_VERSION_BUILD "20190908"
//#include <sdkconfig.h>
#include <Arduino.h>
@@ -55,6 +55,7 @@
#include "spindle_control.h"
#include "stepper.h"
#include "jog.h"
#include "inputbuffer.h"
#ifdef ENABLE_BLUETOOTH
#include "BTconfig.h"

108
Grbl_Esp32/inputbuffer.cpp Normal file
View File

@@ -0,0 +1,108 @@
/*
inputbuffer.cpp - inputbuffer functions class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef ARDUINO_ARCH_ESP32
#include "config.h"
#include "inputbuffer.h"
InputBuffer inputBuffer;
InputBuffer::InputBuffer(){
_RXbufferSize = 0;
_RXbufferpos = 0;
}
InputBuffer::~InputBuffer(){
_RXbufferSize = 0;
_RXbufferpos = 0;
}
void InputBuffer::begin(){
_RXbufferSize = 0;
_RXbufferpos = 0;
}
void InputBuffer::end(){
_RXbufferSize = 0;
_RXbufferpos = 0;
}
InputBuffer::operator bool() const
{
return true;
}
int InputBuffer::available(){
return _RXbufferSize;
}
size_t InputBuffer::write(uint8_t c)
{
//No need currently
//keep for compatibility
return 1;
}
size_t InputBuffer::write(const uint8_t *buffer, size_t size)
{
//No need currently
//keep for compatibility
return size;
}
int InputBuffer::peek(void){
if (_RXbufferSize > 0)return _RXbuffer[_RXbufferpos];
else return -1;
}
bool InputBuffer::push (const char * data){
int data_size = strlen(data);
if ((data_size + _RXbufferSize) <= RXBUFFERSIZE){
int current = _RXbufferpos + _RXbufferSize;
if (current > RXBUFFERSIZE) current = current - RXBUFFERSIZE;
for (int i = 0; i < data_size; i++){
if (current > (RXBUFFERSIZE-1)) current = 0;
_RXbuffer[current] = data[i];
current ++;
}
_RXbufferSize+=strlen(data);
return true;
}
return false;
}
int InputBuffer::read(void){
if (_RXbufferSize > 0) {
int v = _RXbuffer[_RXbufferpos];
_RXbufferpos++;
if (_RXbufferpos > (RXBUFFERSIZE-1))_RXbufferpos = 0;
_RXbufferSize--;
return v;
} else return -1;
}
void InputBuffer::flush(void){
//No need currently
//keep for compatibility
}
#endif // ARDUINO_ARCH_ESP32

71
Grbl_Esp32/inputbuffer.h Normal file
View File

@@ -0,0 +1,71 @@
/*
inputbuffer.h - inputbuffer functions class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _INPUT_BUFFER_H_
#define _INPUT_BUFFER_H_
#include "Print.h"
#define RXBUFFERSIZE 128
class InputBuffer: public Print{
public:
InputBuffer();
~InputBuffer();
size_t write(uint8_t c);
size_t write(const uint8_t *buffer, size_t size);
inline size_t write(const char * s)
{
return write((uint8_t*) s, strlen(s));
}
inline size_t write(unsigned long n)
{
return write((uint8_t) n);
}
inline size_t write(long n)
{
return write((uint8_t) n);
}
inline size_t write(unsigned int n)
{
return write((uint8_t) n);
}
inline size_t write(int n)
{
return write((uint8_t) n);
}
void begin();
void end();
int available();
int peek(void);
int read(void);
bool push (const char * data);
void flush(void);
operator bool() const;
private:
uint8_t _RXbuffer[RXBUFFERSIZE];
uint16_t _RXbufferSize;
uint16_t _RXbufferpos;
};
extern InputBuffer inputBuffer;
#endif

View File

@@ -133,8 +133,10 @@ void protocol_main_loop()
int cmd = 0;
String cmd_params;
if (COMMANDS::check_command (line, &cmd, cmd_params)) {
ESPResponseStream espresponse(client);
COMMANDS::execute_internal_command (cmd, cmd_params, LEVEL_GUEST, &espresponse);
ESPResponseStream espresponse(client, true);
if (!COMMANDS::execute_internal_command (cmd, cmd_params, LEVEL_GUEST, &espresponse)) {
report_status_message(STATUS_GCODE_UNSUPPORTED_COMMAND, CLIENT_ALL);
}
} else grbl_sendf(client, "[MSG: Unknow Command...%s]\r\n", line);
} else if (sys.state & (STATE_ALARM | STATE_JOG)) {
// Everything else is gcode. Block if in alarm or jog mode.

View File

@@ -104,7 +104,7 @@
#define CLIENT_WEBUI 3
#define CLIENT_TELNET 4
#define CLIENT_ALL 0xFF
#define CLIENT_COUNT 4 // total number of client types regardless if they are used
#define CLIENT_COUNT 5 // total number of client types regardless if they are used
// functions to send data to the user.
void grbl_send(uint8_t client, const char *text);

View File

@@ -70,7 +70,7 @@ void serialCheckTask(void *pvParameters)
while(true) // run continuously
{
while (Serial.available()
while (Serial.available() || inputBuffer.available()
#ifdef ENABLE_BLUETOOTH
|| (SerialBT.hasClient() && SerialBT.available())
#endif
@@ -86,14 +86,18 @@ void serialCheckTask(void *pvParameters)
{
client = CLIENT_SERIAL;
data = Serial.read();
}
}
else if (inputBuffer.available()){
client = CLIENT_NONE;
data = inputBuffer.read();
}
else
{ //currently is wifi or BT but better to prepare both can be live
#ifdef ENABLE_BLUETOOTH
if(SerialBT.hasClient() && SerialBT.available()){
client = CLIENT_BT;
data = SerialBT.read();
//Serial.write(data); // echo all data to serial
client = CLIENT_BT;
data = SerialBT.read();
//Serial.write(data); // echo all data to serial
} else {
#endif
#if defined (ENABLE_WIFI) && defined(ENABLE_HTTP) && defined(ENABLE_SERIAL2SOCKET_IN)
@@ -207,7 +211,7 @@ void serialCheck()
uint8_t client_idx = 0; // index of data buffer
while (Serial.available()
while (Serial.available() || inputBuffer.available()
#ifdef ENABLE_BLUETOOTH
|| (SerialBT.hasClient() && SerialBT.available())
#endif
@@ -220,19 +224,24 @@ void serialCheck()
{
client = CLIENT_SERIAL;
data = Serial.read();
}
}
else if (inputBuffer.available())
{
client = CLIENT_NONE;
data = inputBuffer.read();
}
#if defined (ENABLE_BLUETOOTH) || (defined (ENABLE_WIFI) && defined(ENABLE_HTTP) && defined(ENABLE_SERIAL2SOCKET_IN))
else
{ //currently is wifi or BT but better to prepare both can be live
#ifdef ENABLE_BLUETOOTH
if(SerialBT.hasClient() && SerialBT.available()){
client = CLIENT_BT;
data = SerialBT.read();
} else {
client = CLIENT_BT;
data = SerialBT.read();
} else {
#endif
#if defined (ENABLE_WIFI) && defined(ENABLE_HTTP) && defined(ENABLE_SERIAL2SOCKET_IN)
client = CLIENT_WEBUI;
data = Serial2Socket.read();
client = CLIENT_WEBUI;
data = Serial2Socket.read();
#endif
#ifdef ENABLE_BLUETOOTH
}