From 717754eb3e3a49d5b69792f6128f5c87a626fc3f Mon Sep 17 00:00:00 2001 From: Luc Date: Thu, 1 Aug 2019 10:31:30 +0200 Subject: [PATCH] Fixes #349 allow to disable serial (tested on esp8266 not yet esp32) --- docs/Commands.txt | 3 +++ esp3d/command.cpp | 31 +++++++++++++++++++++++++++++++ esp3d/config.cpp | 18 +++++++++++++++++- esp3d/config.h | 4 +++- 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/docs/Commands.txt b/docs/Commands.txt index 5a5fd437..fb7265b8 100644 --- a/docs/Commands.txt +++ b/docs/Commands.txt @@ -165,5 +165,8 @@ Get will give type and settings only not the protected T1/T2 * Get fw target [ESP801]
+* Get state / Set Enable / Disable Serial Communication +[ESP900] + diff --git a/esp3d/command.cpp b/esp3d/command.cpp index 2159a444..34441e4e 100644 --- a/esp3d/command.cpp +++ b/esp3d/command.cpp @@ -1909,6 +1909,37 @@ bool COMMAND::execute_command (int cmd, String cmd_params, tpipe output, level_a case 810: web_interface->blockserial = false; break; + case 900: + parameter = get_param (cmd_params, "", true); +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) { + ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse); + response = false; + } +#endif + if (parameter.length() == 0) { + if (CONFIG::is_com_enabled) { + ESPCOM::print (F ("ENABLED"), output, espresponse); + } else { + ESPCOM::print (F ("DISABLED"), output, espresponse); + } + } else { + if (parameter == "ENABLE") { + CONFIG::DisableSerial(); + if (!CONFIG::InitBaudrate()){ + ESPCOM::print (F ("Cannot enable serial communication"), output, espresponse); + } else { + ESPCOM::print (F ("Enable serial communication"), output, espresponse); + } + } else if (parameter == "DISABLE") { + ESPCOM::print (F ("Disable serial communication"), output, espresponse); + CONFIG::DisableSerial(); + } else { + ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse); + response = false; + } + } + break; default: ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse); diff --git a/esp3d/config.cpp b/esp3d/config.cpp index 62a9bf2f..6e0451e1 100644 --- a/esp3d/config.cpp +++ b/esp3d/config.cpp @@ -49,7 +49,7 @@ extern DHTesp dht; uint8_t CONFIG::FirmwareTarget = UNKNOWN_FW; byte CONFIG::output_flag = DEFAULT_OUTPUT_FLAG; - +bool CONFIG::is_com_enabled = false; #ifdef DHT_FEATURE byte CONFIG::DHT_type = DEFAULT_DHT_TYPE; int CONFIG::DHT_interval = DEFAULT_DHT_INTERVAL; @@ -162,6 +162,21 @@ void CONFIG::InitDirectSD() } +bool CONFIG::DisableSerial() +{ +#ifdef USE_SERIAL_0 +Serial.end(); +#endif +#ifdef USE_SERIAL_1 +Serial1.end(); +#endif +#ifdef USE_SERIAL_2 +Serial2.end(); +#endif +CONFIG::is_com_enabled = false; +return true; +} + bool CONFIG::InitBaudrate(long value) { long baud_rate = 0; @@ -214,6 +229,7 @@ bool CONFIG::InitBaudrate(long value) wifi_config.baud_rate = baud_rate; delay (100); + CONFIG::is_com_enabled = true; return true; } diff --git a/esp3d/config.h b/esp3d/config.h index 3a3f90c9..53f146b4 100644 --- a/esp3d/config.h +++ b/esp3d/config.h @@ -19,7 +19,7 @@ */ //version and sources location -#define FW_VERSION "2.1.0.b32" +#define FW_VERSION "2.1.0.b33" #define REPOSITORY "https://github.com/luc-github/ESP3D" //Customize ESP3D //////////////////////////////////////////////////////////////////////// @@ -530,6 +530,7 @@ public: static int DHT_interval; static void InitDHT(bool refresh = false); #endif + static bool is_com_enabled; static bool is_locked(byte flag); static bool is_direct_sd; static bool read_string (int pos, char byte_buffer[], int size_max); @@ -548,6 +549,7 @@ public: static void InitDirectSD(); static void InitPins(); static bool InitBaudrate(long value = 0); + static bool DisableSerial(); static bool InitExternalPorts(); static uint8_t GetFirmwareTarget(); static const char* GetFirmwareTargetName();