From 4e4706d45020d23fffb13774002aad7f893457d1 Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Tue, 18 Aug 2020 14:08:34 -1000 Subject: [PATCH 1/3] Study mods to InputBuffer - not for merge This code is not intended to be merged in this form. This is a proof of concept of some class structure changes that will be useful as a steppingstone for the Client class task. The old InputBuffer class, which was used for two different purposes (macro input and buffering between SerialCheckTask and protocol_main_loop()), has now been split into MacroBuffer and InputBuffer. MacroBuffer inherits from Stream, so it can be a primary data source, while InputBuffer is a standalone class with only the exact set of methods needed for the intermediate buffering job. MacroBuffer has a write() method that discards the data, so when it is used as a data source in the "client" sense, writes to it do nothing. InputBuffer will go away when the Client class appears, as its function will be subsumed by an integrated line buffering mechanism. The Telnet_Server and Serial2Socket classes were converted to inherit from Stream so they can be used as input sources without using different code to read them, compared to other sources. --- Grbl_Esp32/Custom/atari_1020.cpp | 30 +++---- Grbl_Esp32/Custom/polar_coaster.cpp | 8 +- Grbl_Esp32/src/Grbl.cpp | 2 +- Grbl_Esp32/src/Grbl.h | 3 +- Grbl_Esp32/src/InputBuffer.cpp | 54 ++++++++++++ Grbl_Esp32/src/InputBuffer.h | 43 ++++++++++ Grbl_Esp32/src/MacroBuffer.cpp | 61 ++++++++++++++ Grbl_Esp32/src/MacroBuffer.h | 57 +++++++++++++ Grbl_Esp32/src/Report.cpp | 4 +- Grbl_Esp32/src/Serial.cpp | 12 +-- Grbl_Esp32/src/WebUI/InputBuffer.cpp | 115 -------------------------- Grbl_Esp32/src/WebUI/InputBuffer.h | 60 -------------- Grbl_Esp32/src/WebUI/Serial2Socket.h | 11 +-- Grbl_Esp32/src/WebUI/TelnetServer.cpp | 21 +++++ Grbl_Esp32/src/WebUI/TelnetServer.h | 16 +++- 15 files changed, 284 insertions(+), 213 deletions(-) create mode 100644 Grbl_Esp32/src/InputBuffer.cpp create mode 100644 Grbl_Esp32/src/InputBuffer.h create mode 100644 Grbl_Esp32/src/MacroBuffer.cpp create mode 100644 Grbl_Esp32/src/MacroBuffer.h delete mode 100644 Grbl_Esp32/src/WebUI/InputBuffer.cpp delete mode 100644 Grbl_Esp32/src/WebUI/InputBuffer.h diff --git a/Grbl_Esp32/Custom/atari_1020.cpp b/Grbl_Esp32/Custom/atari_1020.cpp index f1e94514d..b39cbbddf 100644 --- a/Grbl_Esp32/Custom/atari_1020.cpp +++ b/Grbl_Esp32/Custom/atari_1020.cpp @@ -124,23 +124,23 @@ void atari_home_task(void* pvParameters) { if (sys.state == STATE_IDLE) { switch (homing_phase) { case HOMING_PHASE_FULL_APPROACH: // a full width move to insure it hits left end - WebUI::inputBuffer.push("G90G0Z1\r"); // lift the pen + macroBuffer.push("G90G0Z1\r"); // lift the pen sprintf(gcode_line, "G91G0X%3.2f\r", -ATARI_PAPER_WIDTH + ATARI_HOME_POS - 3.0); // plus a little extra - WebUI::inputBuffer.push(gcode_line); + macroBuffer.push(gcode_line); homing_attempt = 1; homing_phase = HOMING_PHASE_CHECK; break; case HOMING_PHASE_CHECK: // check the limits switch if (digitalRead(REED_SW_PIN) == 0) { // see if reed switch is grounded - WebUI::inputBuffer.push("G4P0.1\n"); // dramtic pause + macroBuffer.push("G4P0.1\n"); // dramtic pause sys_position[X_AXIS] = ATARI_HOME_POS * axis_settings[X_AXIS]->steps_per_mm->get(); sys_position[Y_AXIS] = 0.0; sys_position[Z_AXIS] = 1.0 * axis_settings[Y_AXIS]->steps_per_mm->get(); gc_sync_position(); plan_sync_position(); sprintf(gcode_line, "G90G0X%3.2f\r", ATARI_PAPER_WIDTH); // alway return to right side to reduce home travel stalls - WebUI::inputBuffer.push(gcode_line); + macroBuffer.push(gcode_line); current_tool = 1; // local copy for reference...until actual M6 change gc_state.tool = current_tool; atari_homing = false; // done with homing sequence @@ -151,9 +151,9 @@ void atari_home_task(void* pvParameters) { break; case HOMING_PHASE_RETRACT: sprintf(gcode_line, "G0X%3.2f\r", -ATARI_HOME_POS); - WebUI::inputBuffer.push(gcode_line); + macroBuffer.push(gcode_line); sprintf(gcode_line, "G0X%3.2f\r", ATARI_HOME_POS); - WebUI::inputBuffer.push(gcode_line); + macroBuffer.push(gcode_line); homing_phase = HOMING_PHASE_CHECK; break; default: @@ -166,7 +166,7 @@ void atari_home_task(void* pvParameters) { if (homing_attempt > ATARI_HOMING_ATTEMPTS) { // try all positions plus 1 grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Atari homing failed"); - WebUI::inputBuffer.push("G90\r"); + macroBuffer.push("G90\r"); atari_homing = false; } } @@ -227,11 +227,11 @@ void user_tool_change(uint8_t new_tool) { else move_count = BUMPS_PER_PEN_CHANGE * ((MAX_PEN_NUMBER - current_tool) + new_tool); sprintf(gcode_line, "G0Z%3.2f\r", ATARI_TOOL_CHANGE_Z); // go to tool change height - WebUI::inputBuffer.push(gcode_line); + macroBuffer.push(gcode_line); for (uint8_t i = 0; i < move_count; i++) { sprintf(gcode_line, "G0X%3.2f\r", ATARI_HOME_POS); // - WebUI::inputBuffer.push(gcode_line); - WebUI::inputBuffer.push("G0X0\r"); + macroBuffer.push(gcode_line); + macroBuffer.push("G0X0\r"); } current_tool = new_tool; grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Change to Pen#%d", current_tool); @@ -250,7 +250,7 @@ void user_defined_macro(uint8_t index) { #ifdef MACRO_BUTTON_0_PIN case CONTROL_PIN_INDEX_MACRO_0: grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Pen switch"); - WebUI::inputBuffer.push("$H\r"); + macroBuffer.push("$H\r"); break; #endif #ifdef MACRO_BUTTON_1_PIN @@ -258,15 +258,15 @@ void user_defined_macro(uint8_t index) { grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Color switch"); atari_next_pen(); sprintf(gcode_line, "G90G0X%3.2f\r", ATARI_PAPER_WIDTH); // alway return to right side to reduce home travel stalls - WebUI::inputBuffer.push(gcode_line); + macroBuffer.push(gcode_line); break; #endif #ifdef MACRO_BUTTON_2_PIN case CONTROL_PIN_INDEX_MACRO_2: // feed out some paper and reset the Y 0 grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Paper switch"); - WebUI::inputBuffer.push("G0Y-25\r"); - WebUI::inputBuffer.push("G4P0.1\r"); // sync...forces wait for planner to clear + macroBuffer.push("G0Y-25\r"); + macroBuffer.push("G4P0.1\r"); // sync...forces wait for planner to clear sys_position[Y_AXIS] = 0.0; // reset the Y position gc_sync_position(); plan_sync_position(); @@ -279,5 +279,5 @@ void user_defined_macro(uint8_t index) { void user_m30() { char gcode_line[20]; sprintf(gcode_line, "G90G0X%3.2f\r", ATARI_PAPER_WIDTH); // - WebUI::inputBuffer.push(gcode_line); + macroBuffer.push(gcode_line); } diff --git a/Grbl_Esp32/Custom/polar_coaster.cpp b/Grbl_Esp32/Custom/polar_coaster.cpp index a9ae8018c..7588e6809 100644 --- a/Grbl_Esp32/Custom/polar_coaster.cpp +++ b/Grbl_Esp32/Custom/polar_coaster.cpp @@ -225,17 +225,17 @@ void user_defined_macro(uint8_t index) { switch (index) { #ifdef MACRO_BUTTON_0_PIN case CONTROL_PIN_INDEX_MACRO_0: - WebUI::inputBuffer.push("$H\r"); // home machine + macroBuffer.push("$H\r"); // home machine break; #endif #ifdef MACRO_BUTTON_1_PIN case CONTROL_PIN_INDEX_MACRO_1: - WebUI::inputBuffer.push("[ESP220]/1.nc\r"); // run SD card file 1.nc + macroBuffer.push("[ESP220]/1.nc\r"); // run SD card file 1.nc break; #endif #ifdef MACRO_BUTTON_2_PIN case CONTROL_PIN_INDEX_MACRO_2: - WebUI::inputBuffer.push("[ESP220]/2.nc\r"); // run SD card file 2.nc + macroBuffer.push("[ESP220]/2.nc\r"); // run SD card file 2.nc break; #endif #ifdef MACRO_BUTTON_3_PIN @@ -247,5 +247,5 @@ void user_defined_macro(uint8_t index) { // handle the M30 command void user_m30() { - WebUI::inputBuffer.push("$H\r"); + macroBuffer.push("$H\r"); } diff --git a/Grbl_Esp32/src/Grbl.cpp b/Grbl_Esp32/src/Grbl.cpp index 04e95d670..d8dfa1cb7 100644 --- a/Grbl_Esp32/src/Grbl.cpp +++ b/Grbl_Esp32/src/Grbl.cpp @@ -79,7 +79,7 @@ void grbl_init() { #ifdef ENABLE_BLUETOOTH WebUI::bt_config.begin(); #endif - WebUI::inputBuffer.begin(); + macroBuffer.begin(); } static void reset_variables() { diff --git a/Grbl_Esp32/src/Grbl.h b/Grbl_Esp32/src/Grbl.h index 8218d2637..6499bcc0b 100644 --- a/Grbl_Esp32/src/Grbl.h +++ b/Grbl_Esp32/src/Grbl.h @@ -60,7 +60,8 @@ #include "Motors/Motors.h" #include "Stepper.h" #include "Jog.h" -#include "WebUI/InputBuffer.h" +#include "MacroBuffer.h" +#include "InputBuffer.h" #include "Settings.h" #include "SettingsDefinitions.h" #include "WebUI/WebSettings.h" diff --git a/Grbl_Esp32/src/InputBuffer.cpp b/Grbl_Esp32/src/InputBuffer.cpp new file mode 100644 index 000000000..c87033245 --- /dev/null +++ b/Grbl_Esp32/src/InputBuffer.cpp @@ -0,0 +1,54 @@ +/* + 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 +*/ + +#include "InputBuffer.h" + +InputBuffer::InputBuffer() { + begin(); +} + +void InputBuffer::begin() { + _bufferSize = 0; + _bufferpos = 0; +} + +void InputBuffer::push(uint8_t c) { + if (_bufferSize == BUFFERSIZE) { + return; + } + int writeIndex = _bufferpos + _bufferSize; + if (writeIndex >= BUFFERSIZE) { + writeIndex -= BUFFERSIZE; + } + _buffer[writeIndex] = c; + _bufferSize++; +} + +int InputBuffer::read(void) { + if (_bufferSize) { + return -1; + } + int data = _buffer[_bufferpos]; + if (++_bufferpos == BUFFERSIZE) { + _bufferpos = 0; + } + _bufferSize--; + return data; +} diff --git a/Grbl_Esp32/src/InputBuffer.h b/Grbl_Esp32/src/InputBuffer.h new file mode 100644 index 000000000..08428c263 --- /dev/null +++ b/Grbl_Esp32/src/InputBuffer.h @@ -0,0 +1,43 @@ +#pragma once + +/* + InputBuffer.h - circular queue for transferring input data from SerialCheckTask + to protocol_main_loop(). There is an instance of this for each client. + + 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 +*/ + +#include +#include + +class InputBuffer { +public: + InputBuffer(); + + void begin(); + int available() { return _bufferSize; } + int availableForPush() { return BUFFERSIZE - _bufferSize; } + int read(void); + void push(uint8_t c); + +private: + static const int BUFFERSIZE = 128; + + uint8_t _buffer[BUFFERSIZE]; + uint16_t _bufferSize; + uint16_t _bufferpos; +}; diff --git a/Grbl_Esp32/src/MacroBuffer.cpp b/Grbl_Esp32/src/MacroBuffer.cpp new file mode 100644 index 000000000..349911280 --- /dev/null +++ b/Grbl_Esp32/src/MacroBuffer.cpp @@ -0,0 +1,61 @@ +/* + 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 +*/ + +// #include "Config.h" +#include "MacroBuffer.h" + +MacroBuffer macroBuffer; + +MacroBuffer::MacroBuffer() { + begin(); +} + +void MacroBuffer::begin() { + _bufferSize = 0; + _bufferpos = 0; +} + +bool MacroBuffer::push(const char* data) { + int data_size = strlen(data); + if ((data_size + _bufferSize) <= BUFFERSIZE) { + int current = _bufferpos + _bufferSize; + for (int i = 0; i < data_size; i++) { + if (current >= BUFFERSIZE) { + current -= BUFFERSIZE; + } + _buffer[current++] = data[i]; + } + _bufferSize += strlen(data); + return true; + } + return false; +} + +int MacroBuffer::read(void) { + if (!_bufferSize) { + return -1; + } + int v = _buffer[_bufferpos]; + if (++_bufferpos > (BUFFERSIZE - 1)) { + _bufferpos = 0; + } + _bufferSize--; + return v; +} diff --git a/Grbl_Esp32/src/MacroBuffer.h b/Grbl_Esp32/src/MacroBuffer.h new file mode 100644 index 000000000..24435bfb2 --- /dev/null +++ b/Grbl_Esp32/src/MacroBuffer.h @@ -0,0 +1,57 @@ +#pragma once + +/* + MacroBuffer.h - for injecting macro data as an input stream + + 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 +*/ + +#include +#include + +// MacroBuffer inherits from Stream so it can be used as the input source for +// a "Client". Clients support input via read() and output via print(). + +class MacroBuffer : public Stream { +public: + MacroBuffer(); + + void begin(); + + // push() injects macro data into the stream + bool push(const char* data); + + // Virtual method of Print class; discards the data + size_t write(uint8_t c) { return 0; } + + // Virtual methods of Stream class + int available() { return _bufferSize; } + int peek(void) { return _bufferSize ? _buffer[_bufferpos] : -1; } + int read(void); + void flush(void) {} + + operator bool() const { return true; } + +private: + static const int BUFFERSIZE = 128; + + uint8_t _buffer[BUFFERSIZE]; + uint16_t _bufferSize; + uint16_t _bufferpos; +}; + +extern MacroBuffer macroBuffer; diff --git a/Grbl_Esp32/src/Report.cpp b/Grbl_Esp32/src/Report.cpp index 42228b272..a4ea23696 100644 --- a/Grbl_Esp32/src/Report.cpp +++ b/Grbl_Esp32/src/Report.cpp @@ -64,11 +64,11 @@ void grbl_send(uint8_t client, const char* text) { #endif #if defined(ENABLE_WIFI) && defined(ENABLE_HTTP) && defined(ENABLE_SERIAL2SOCKET_OUT) if (client == CLIENT_WEBUI || client == CLIENT_ALL) - WebUI::Serial2Socket.write((const uint8_t*)text, strlen(text)); + WebUI::Serial2Socket.print(text); #endif #if defined(ENABLE_WIFI) && defined(ENABLE_TELNET) if (client == CLIENT_TELNET || client == CLIENT_ALL) - WebUI::telnet_server.write((const uint8_t*)text, strlen(text)); + WebUI::telnet_server.print(text); #endif if (client == CLIENT_SERIAL || client == CLIENT_ALL) { Serial.print(text); diff --git a/Grbl_Esp32/src/Serial.cpp b/Grbl_Esp32/src/Serial.cpp index 74cbd611d..83c0eb219 100644 --- a/Grbl_Esp32/src/Serial.cpp +++ b/Grbl_Esp32/src/Serial.cpp @@ -61,11 +61,11 @@ portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED; static TaskHandle_t serialCheckTaskHandle = 0; -WebUI::InputBuffer client_buffer[CLIENT_COUNT]; // create a buffer for each client +InputBuffer client_buffer[CLIENT_COUNT]; // create a buffer for each client // Returns the number of bytes available in a client buffer. uint8_t serial_get_rx_buffer_available(uint8_t client) { - return client_buffer[client].availableforwrite(); + return client_buffer[client].availableForPush(); } void serial_init() { @@ -95,9 +95,9 @@ void serialCheckTask(void* pvParameters) { if (Serial.available()) { client = CLIENT_SERIAL; data = Serial.read(); - } else if (WebUI::inputBuffer.available()) { + } else if (macroBuffer.available()) { client = CLIENT_INPUT; - data = WebUI::inputBuffer.read(); + data = macroBuffer.read(); } else { //currently is wifi or BT but better to prepare both can be live #ifdef ENABLE_BLUETOOTH @@ -132,7 +132,7 @@ void serialCheckTask(void* pvParameters) { execute_realtime_command(data, client); else { vTaskEnterCritical(&myMutex); - client_buffer[client].write(data); + client_buffer[client].push(data); vTaskExitCritical(&myMutex); } } // if something available @@ -179,7 +179,7 @@ uint8_t serial_read(uint8_t client) { } bool any_client_has_data() { - return (Serial.available() || WebUI::inputBuffer.available() + return (Serial.available() || macroBuffer.available() #ifdef ENABLE_BLUETOOTH || (WebUI::SerialBT.hasClient() && WebUI::SerialBT.available()) #endif diff --git a/Grbl_Esp32/src/WebUI/InputBuffer.cpp b/Grbl_Esp32/src/WebUI/InputBuffer.cpp deleted file mode 100644 index a1906e5fe..000000000 --- a/Grbl_Esp32/src/WebUI/InputBuffer.cpp +++ /dev/null @@ -1,115 +0,0 @@ -/* - 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 -*/ - -#include "../Config.h" -#include "InputBuffer.h" - -namespace WebUI { - InputBuffer inputBuffer; - - 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; } - - int InputBuffer::availableforwrite() { return (RXBUFFERSIZE - _RXbufferSize); } - - size_t InputBuffer::write(uint8_t c) { - if ((1 + _RXbufferSize) <= RXBUFFERSIZE) { - int current = _RXbufferpos + _RXbufferSize; - if (current > RXBUFFERSIZE) - current = current - RXBUFFERSIZE; - if (current > (RXBUFFERSIZE - 1)) - current = 0; - _RXbuffer[current] = c; - current++; - _RXbufferSize += 1; - return 1; - } - return 0; - } - - 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 - } - - InputBuffer::~InputBuffer() { - _RXbufferSize = 0; - _RXbufferpos = 0; - } -} diff --git a/Grbl_Esp32/src/WebUI/InputBuffer.h b/Grbl_Esp32/src/WebUI/InputBuffer.h deleted file mode 100644 index 4eec0ca68..000000000 --- a/Grbl_Esp32/src/WebUI/InputBuffer.h +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once - -/* - 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 -*/ - -#include -#include - -namespace WebUI { - class InputBuffer : public Print { - public: - 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 availableforwrite(); - int peek(void); - int read(void); - bool push(const char* data); - void flush(void); - - operator bool() const; - - ~InputBuffer(); - - private: - static const int RXBUFFERSIZE = 128; - - uint8_t _RXbuffer[RXBUFFERSIZE]; - uint16_t _RXbufferSize; - uint16_t _RXbufferpos; - }; - - extern InputBuffer inputBuffer; -} diff --git a/Grbl_Esp32/src/WebUI/Serial2Socket.h b/Grbl_Esp32/src/WebUI/Serial2Socket.h index 7d3375204..dada4dd23 100644 --- a/Grbl_Esp32/src/WebUI/Serial2Socket.h +++ b/Grbl_Esp32/src/WebUI/Serial2Socket.h @@ -26,7 +26,7 @@ class WebSocketsServer; namespace WebUI { - class Serial_2_Socket : public Print { + class Serial_2_Socket : public Stream { static const int TXBUFFERSIZE = 1200; static const int RXBUFFERSIZE = 128; static const int FLUSHTIMEOUT = 500; @@ -46,14 +46,15 @@ namespace WebUI { long baudRate(); void begin(long speed); void end(); - int available(); - int peek(void); - int read(void); bool push(const char* data); - void flush(void); void handle_flush(); bool attachWS(WebSocketsServer* web_socket); bool detachWS(); + // Virtual methods of Stream + int available(); + int peek(void); + int read(void); + void flush(void); operator bool() const; diff --git a/Grbl_Esp32/src/WebUI/TelnetServer.cpp b/Grbl_Esp32/src/WebUI/TelnetServer.cpp index 0829bddb6..3bf48e506 100644 --- a/Grbl_Esp32/src/WebUI/TelnetServer.cpp +++ b/Grbl_Esp32/src/WebUI/TelnetServer.cpp @@ -100,6 +100,27 @@ namespace WebUI { } } + size_t Telnet_Server::write(uint8_t data) { + size_t wsize = 0; + if (!_setupdone || _telnetserver == NULL) { + log_d("[TELNET out blocked]"); + return 0; + } + + clearClients(); + + //log_d("[TELNET out]"); + //push UART data to all connected telnet clients + for (uint8_t i = 0; i < MAX_TLNT_CLIENTS; i++) { + if (_telnetClients[i] && _telnetClients[i].connected()) { + //log_d("[TELNET out connected]"); + wsize = _telnetClients[i].write(&data, 1); + COMMANDS::wait(0); + } + } + return wsize; + } + size_t Telnet_Server::write(const uint8_t* buffer, size_t size) { size_t wsize = 0; if (!_setupdone || _telnetserver == NULL) { diff --git a/Grbl_Esp32/src/WebUI/TelnetServer.h b/Grbl_Esp32/src/WebUI/TelnetServer.h index 83af4e896..43497dfd0 100644 --- a/Grbl_Esp32/src/WebUI/TelnetServer.h +++ b/Grbl_Esp32/src/WebUI/TelnetServer.h @@ -22,11 +22,13 @@ #include "../Config.h" +#include + class WiFiServer; class WiFiClient; namespace WebUI { - class Telnet_Server { + class Telnet_Server : public Stream { //how many clients should be able to telnet to this ESP32 static const int MAX_TLNT_CLIENTS = 1; @@ -39,13 +41,19 @@ namespace WebUI { bool begin(); void end(); void handle(); + int get_rx_buffer_available(); + bool push(uint8_t data); + bool push(const uint8_t* data, int datasize); + + // Overloaded method of Print class size_t write(const uint8_t* buffer, size_t size); + // Virtual method of Print class + size_t write(uint8_t); + // Virtual methods of Stream class int read(void); int peek(void); int available(); - int get_rx_buffer_available(); - bool push(uint8_t data); - bool push(const uint8_t* data, int datasize); + void flush() { } static uint16_t port() { return _port; } From 0e148d451392e432fd8d180e4b3127c65005a76a Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Tue, 18 Aug 2020 14:32:56 -1000 Subject: [PATCH 2/3] Fixed typo --- Grbl_Esp32/src/InputBuffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Grbl_Esp32/src/InputBuffer.cpp b/Grbl_Esp32/src/InputBuffer.cpp index c87033245..63c5bcba0 100644 --- a/Grbl_Esp32/src/InputBuffer.cpp +++ b/Grbl_Esp32/src/InputBuffer.cpp @@ -42,7 +42,7 @@ void InputBuffer::push(uint8_t c) { } int InputBuffer::read(void) { - if (_bufferSize) { + if (!_bufferSize) { return -1; } int data = _buffer[_bufferpos]; From 2b9b45dff9f63022b9dce14208c5ca0b308b37c1 Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Tue, 18 Aug 2020 14:35:21 -1000 Subject: [PATCH 3/3] Another typo in commentary --- Grbl_Esp32/src/MacroBuffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Grbl_Esp32/src/MacroBuffer.cpp b/Grbl_Esp32/src/MacroBuffer.cpp index 349911280..e7d8fdb12 100644 --- a/Grbl_Esp32/src/MacroBuffer.cpp +++ b/Grbl_Esp32/src/MacroBuffer.cpp @@ -1,5 +1,5 @@ /* - InputBuffer.cpp - inputbuffer functions class + MacroBuffer.cpp - for injecting macro data as an input stream Copyright (c) 2014 Luc Lebosse. All rights reserved.