From 676df60b91922c4858950740b40f632739b94c2f Mon Sep 17 00:00:00 2001 From: Luc Date: Thu, 1 Aug 2019 16:31:56 +0200 Subject: [PATCH] Allow to start/stop serial with [ESP900] --- esp3d/src/core/commands.cpp | 9 ++- esp3d/src/core/commands.h | 1 + esp3d/src/core/espcmd/ESP900.cpp | 65 +++++++++++++++++++++ esp3d/src/include/version.h | 2 +- esp3d/src/modules/serial/serial_service.cpp | 3 + esp3d/src/modules/serial/serial_service.h | 6 +- 6 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 esp3d/src/core/espcmd/ESP900.cpp diff --git a/esp3d/src/core/commands.cpp b/esp3d/src/core/commands.cpp index b485dd3c..c6a9236c 100644 --- a/esp3d/src/core/commands.cpp +++ b/esp3d/src/core/commands.cpp @@ -436,12 +436,19 @@ bool Commands::execute_internal_command (int cmd, const char* cmd_params, level_ break; #endif //FILESYSTEM_FEATURE - //get fw version firmare target and fw version + //Get fw version firmare target and fw version //output is JSON or plain text according parameter //[ESP800] case 800: response = ESP800(cmd_params, auth_type, output); break; + + //Get state / Set Enable / Disable Serial Communication + //[ESP900] + case 900: + response = ESP900(cmd_params, auth_type, output); + break; + default: output->printERROR ("Invalid Command"); response = false; diff --git a/esp3d/src/core/commands.h b/esp3d/src/core/commands.h index bc70f0fd..a8dd4a71 100644 --- a/esp3d/src/core/commands.h +++ b/esp3d/src/core/commands.h @@ -107,6 +107,7 @@ public: bool ESP720(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); #endif //FILESYSTEM_FEATURE bool ESP800(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); + bool ESP900(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); }; extern Commands esp3d_commands; diff --git a/esp3d/src/core/espcmd/ESP900.cpp b/esp3d/src/core/espcmd/ESP900.cpp new file mode 100644 index 00000000..8ad4ffac --- /dev/null +++ b/esp3d/src/core/espcmd/ESP900.cpp @@ -0,0 +1,65 @@ +/* + ESP900.cpp - ESP3D command 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/esp3d_config.h" +#include "../commands.h" +#include "../esp3doutput.h" +#include "../settings_esp3d.h" +#include "../../modules/authentication/authentication_service.h" +#include "../../modules/serial/serial_service.h" +//Get state / Set Enable / Disable Serial Communication +//[ESP900][pwd=] +bool Commands::ESP900(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) +{ + bool response = true; + String parameter; +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) { + output->printERROR("Wrong authentication!", 401); + return false; + } +#else + (void)auth_type; +#endif //AUTHENTICATION_FEATURE + parameter = get_param (cmd_params, ""); + //get + if (parameter.length() == 0) { + if (serial_service.started()){ + output->printMSG("ENABLED"); + } else { + output->printMSG("DISABLED"); + } + } else { //set + if (parameter == "ENABLE" ) { + if (!serial_service.begin()) { + output->printMSG ("Serial communication enabled"); + } else { + output->printERROR("Cannot enable serial communication!", 500); + response = false; + } + } else if (parameter == "DISABLE" ) { + output->printMSG ("Serial communication disabled"); + serial_service.end(); + } else { + output->printERROR("Cannot enable serial communication!", 500); + response = false; + } + } + return response; +} diff --git a/esp3d/src/include/version.h b/esp3d/src/include/version.h index c87e60d0..0286acab 100644 --- a/esp3d/src/include/version.h +++ b/esp3d/src/include/version.h @@ -22,7 +22,7 @@ #define _VERSION_ESP3D_H //version and sources location -#define FW_VERSION "3.0.0.a8" +#define FW_VERSION "3.0.0.a9" #define REPOSITORY "https://github.com/luc-github/ESP3D" #endif //_VERSION_ESP3D_H diff --git a/esp3d/src/modules/serial/serial_service.cpp b/esp3d/src/modules/serial/serial_service.cpp index 9ffd7b64..4f2d45e7 100644 --- a/esp3d/src/modules/serial/serial_service.cpp +++ b/esp3d/src/modules/serial/serial_service.cpp @@ -45,6 +45,7 @@ const long SupportedBaudList[] = {9600, 19200, 38400, 57600, 74880, 115200, 2304 SerialService::SerialService() { _buffer_size = 0; + _started = false; } //Destructor @@ -76,6 +77,7 @@ bool SerialService::begin() #endif //ARDUINO_ARCH_ESP32 } ESP3D_SERIAL.setRxBufferSize (SERIAL_RX_BUFFER_SIZE); + _started = true; return true; } //End serial @@ -86,6 +88,7 @@ bool SerialService::end() swap(); ESP3D_SERIAL.end(); _buffer_size = 0; + _started = false; return true; } diff --git a/esp3d/src/modules/serial/serial_service.h b/esp3d/src/modules/serial/serial_service.h index a1c96e18..9bdbfaa6 100644 --- a/esp3d/src/modules/serial/serial_service.h +++ b/esp3d/src/modules/serial/serial_service.h @@ -65,8 +65,12 @@ public: } int read(); size_t readBytes (uint8_t * sbuf, size_t len); - + inline bool started() + { + return _started; + } private: + bool _started; uint32_t _lastflush; uint8_t _buffer[ESP3D_SERIAL_BUFFER_SIZE + 1]; //keep space of 0x0 terminal size_t _buffer_size;