From 5df89d19ccef2e39341d506d991c9a1f60a56232 Mon Sep 17 00:00:00 2001 From: Stefan de Bruijn Date: Thu, 29 Oct 2020 15:28:05 +0100 Subject: [PATCH] Fixed DebugPinDetail flooding/messages --- Grbl_Esp32/src/Pin.cpp | 5 +- Grbl_Esp32/src/Pin.h | 2 +- Grbl_Esp32/src/PinSettingsDefinitions.cpp | 81 ----------------------- Grbl_Esp32/src/Pins/DebugPinDetail.cpp | 27 ++++++-- Grbl_Esp32/src/Pins/DebugPinDetail.h | 9 +-- Grbl_Esp32/src/Report.cpp | 7 ++ Grbl_Esp32/src/Settings.cpp | 6 +- 7 files changed, 40 insertions(+), 97 deletions(-) diff --git a/Grbl_Esp32/src/Pin.cpp b/Grbl_Esp32/src/Pin.cpp index b11b6342..d94a2417 100644 --- a/Grbl_Esp32/src/Pin.cpp +++ b/Grbl_Esp32/src/Pin.cpp @@ -98,7 +98,10 @@ bool Pin::parse(String str, Pins::PinDetail*& pinImplementation) { } #if defined PIN_DEBUG && defined ESP32 - pinImplementation = new Pins::DebugPinDetail(pinImplementation); + // We make an exception for gpio.1: Serial.TX will create an infinite recursion. + if (prefix != "gpio" || pinNumber != 1) { + pinImplementation = new Pins::DebugPinDetail(pinImplementation); + } #endif return true; } diff --git a/Grbl_Esp32/src/Pin.h b/Grbl_Esp32/src/Pin.h index 7d54e3fe..8cbde78b 100644 --- a/Grbl_Esp32/src/Pin.h +++ b/Grbl_Esp32/src/Pin.h @@ -9,7 +9,7 @@ #include #include -// #define PIN_DEBUG // Pin debugging. WILL spam you with a lot of data! +#define PIN_DEBUG // Pin debugging. WILL spam you with a lot of data! // Forward declarations: class String; diff --git a/Grbl_Esp32/src/PinSettingsDefinitions.cpp b/Grbl_Esp32/src/PinSettingsDefinitions.cpp index b60d887a..0d71672e 100644 --- a/Grbl_Esp32/src/PinSettingsDefinitions.cpp +++ b/Grbl_Esp32/src/PinSettingsDefinitions.cpp @@ -17,46 +17,6 @@ #include #include "Config.h" -#define GPIO_NUM_0 "GPIO.0" -#define GPIO_NUM_1 "GPIO.1" -#define GPIO_NUM_2 "GPIO.2" -#define GPIO_NUM_3 "GPIO.3" -#define GPIO_NUM_4 "GPIO.4" -#define GPIO_NUM_5 "GPIO.5" -#define GPIO_NUM_6 "GPIO.6" -#define GPIO_NUM_7 "GPIO.7" -#define GPIO_NUM_8 "GPIO.8" -#define GPIO_NUM_9 "GPIO.9" -#define GPIO_NUM_10 "GPIO.10" -#define GPIO_NUM_11 "GPIO.11" -#define GPIO_NUM_12 "GPIO.12" -#define GPIO_NUM_13 "GPIO.13" -#define GPIO_NUM_14 "GPIO.14" -#define GPIO_NUM_15 "GPIO.15" -#define GPIO_NUM_16 "GPIO.16" -#define GPIO_NUM_17 "GPIO.17" -#define GPIO_NUM_18 "GPIO.18" -#define GPIO_NUM_19 "GPIO.19" - -#define GPIO_NUM_21 "GPIO.21" -#define GPIO_NUM_22 "GPIO.22" -#define GPIO_NUM_23 "GPIO.23" - -#define GPIO_NUM_25 "GPIO.25" -#define GPIO_NUM_26 "GPIO.26" -#define GPIO_NUM_27 "GPIO.27" - -#define GPIO_NUM_32 "GPIO.32" -#define GPIO_NUM_33 "GPIO.33" -#define GPIO_NUM_34 "GPIO.34" -#define GPIO_NUM_35 "GPIO.35" -#define GPIO_NUM_36 "GPIO.36" -#define GPIO_NUM_37 "GPIO.37" -#define GPIO_NUM_38 "GPIO.38" -#define GPIO_NUM_39 "GPIO.39" - -#define I2SO(n) "I2S." #n - // Include the file that loads the machine-specific config file. // machine.h must be edited to choose the desired file. #include "Machine.h" @@ -756,47 +716,6 @@ const char* C_PIN_PHASE_3_DEFAULT = C_PIN_PHASE_3; #endif const char* C2_PIN_PHASE_3_DEFAULT = C2_PIN_PHASE_3; -// We have all the defaults we need at this point. Settings will include Arduino, so we have to get -// rid of the defines we made earlier: -#undef GPIO_NUM_0 -#undef GPIO_NUM_1 -#undef GPIO_NUM_2 -#undef GPIO_NUM_3 -#undef GPIO_NUM_4 -#undef GPIO_NUM_5 -#undef GPIO_NUM_6 -#undef GPIO_NUM_7 -#undef GPIO_NUM_8 -#undef GPIO_NUM_9 -#undef GPIO_NUM_10 -#undef GPIO_NUM_11 -#undef GPIO_NUM_12 -#undef GPIO_NUM_13 -#undef GPIO_NUM_14 -#undef GPIO_NUM_15 -#undef GPIO_NUM_16 -#undef GPIO_NUM_17 -#undef GPIO_NUM_18 -#undef GPIO_NUM_19 - -#undef GPIO_NUM_21 -#undef GPIO_NUM_22 -#undef GPIO_NUM_23 - -#undef GPIO_NUM_25 -#undef GPIO_NUM_26 -#undef GPIO_NUM_27 - -#undef GPIO_NUM_32 -#undef GPIO_NUM_33 -#undef GPIO_NUM_34 -#undef GPIO_NUM_35 -#undef GPIO_NUM_36 -#undef GPIO_NUM_37 -#undef GPIO_NUM_38 -#undef GPIO_NUM_39 -#undef I2SO - // We need settings.h for the settings classes #include "Grbl.h" #include "Settings.h" diff --git a/Grbl_Esp32/src/Pins/DebugPinDetail.cpp b/Grbl_Esp32/src/Pins/DebugPinDetail.cpp index a8b1f16d..5446f2ff 100644 --- a/Grbl_Esp32/src/Pins/DebugPinDetail.cpp +++ b/Grbl_Esp32/src/Pins/DebugPinDetail.cpp @@ -6,13 +6,20 @@ namespace Pins { // I/O: void DebugPinDetail::write(int high) { - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Writing pin %s = %d", toString().c_str(), high); + if (high != _isHigh) { + _isHigh = high; + if (shouldEvent()) { + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Writing pin %s = %d", toString().c_str(), high); + } + } _implementation->write(high); } int DebugPinDetail::read() { auto result = _implementation->read(); - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Reading pin %s = %d", toString().c_str(), result); + if (shouldEvent()) { + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Reading pin %s = %d", toString().c_str(), result); + } return result; } void DebugPinDetail::setAttr(PinAttributes value) { @@ -42,7 +49,7 @@ namespace Pins { buf[n++] = 0; if (shouldEvent()) { - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Setting pin attr %s = %s", toString().c_str(), buf); + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Setting pin attr %s = %s", toString().c_str(), buf); } _implementation->setAttr(value); } @@ -50,7 +57,7 @@ namespace Pins { void DebugPinDetail::CallbackHandler::handle(void* arg) { auto handler = static_cast(arg); if (handler->_myPin->shouldEvent()) { - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Received ISR on pin %s", handler->_myPin->toString().c_str()); + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Received ISR on pin %s", handler->_myPin->toString().c_str()); } handler->callback(handler->argument); } @@ -62,7 +69,7 @@ namespace Pins { _isrHandler.callback = callback; if (shouldEvent()) { - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Attaching interrupt to pin %s, mode %d", toString().c_str(), mode); + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Attaching interrupt to pin %s, mode %d", toString().c_str(), mode); } _implementation->attachInterrupt(_isrHandler.handle, &_isrHandler, mode); } @@ -72,15 +79,21 @@ namespace Pins { // This method basically ensures we don't flood users: auto time = millis(); - if (_lastEvent + 1000 > time) { + if (_lastEvent + 1000 < time) { _lastEvent = time; _eventCount = 1; return true; - } else if (_eventCount < 20) { + } else if (_eventCount < 10) { _lastEvent = time; ++_eventCount; return true; + } else if (_eventCount == 10) { + _lastEvent = time; + ++_eventCount; + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Suppressing events..."); + return false; } else { + _lastEvent = time; return false; } } diff --git a/Grbl_Esp32/src/Pins/DebugPinDetail.h b/Grbl_Esp32/src/Pins/DebugPinDetail.h index f503cd12..eadf03c5 100644 --- a/Grbl_Esp32/src/Pins/DebugPinDetail.h +++ b/Grbl_Esp32/src/Pins/DebugPinDetail.h @@ -6,8 +6,9 @@ namespace Pins { class DebugPinDetail : public PinDetail { PinDetail* _implementation; - int _lastEvent; - int _eventCount; + int _lastEvent; + int _eventCount; + bool _isHigh; struct CallbackHandler { void (*callback)(void* arg); @@ -23,8 +24,8 @@ namespace Pins { public: DebugPinDetail(PinDetail* implementation) : - PinDetail(implementation->number()), _implementation(implementation), _lastEvent(0), _eventCount(0), _isrHandler({ 0 }) { - } + PinDetail(implementation->number()), _implementation(implementation), _lastEvent(0), _eventCount(0), _isHigh(false), _isrHandler({ 0 }) + {} PinCapabilities capabilities() const override { return _implementation->capabilities(); } diff --git a/Grbl_Esp32/src/Report.cpp b/Grbl_Esp32/src/Report.cpp index 46cb05a3..15d4eb8b 100644 --- a/Grbl_Esp32/src/Report.cpp +++ b/Grbl_Esp32/src/Report.cpp @@ -54,11 +54,16 @@ EspClass esp; #endif const int DEFAULTBUFFERSIZE = 64; +portMUX_TYPE mmux = portMUX_INITIALIZER_UNLOCKED; + // 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_INPUT) { return; } + + portENTER_CRITICAL(&mmux); + #ifdef ENABLE_BLUETOOTH if (WebUI::SerialBT.hasClient() && (client == CLIENT_BT || client == CLIENT_ALL)) { WebUI::SerialBT.print(text); @@ -78,6 +83,8 @@ void grbl_send(uint8_t client, const char* text) { if (client == CLIENT_SERIAL || client == CLIENT_ALL) { Serial.print(text); } + + portEXIT_CRITICAL(&mmux); } // This is a formating version of the grbl_send(CLIENT_ALL,...) function that work like printf diff --git a/Grbl_Esp32/src/Settings.cpp b/Grbl_Esp32/src/Settings.cpp index bde6fc49..96ba9690 100644 --- a/Grbl_Esp32/src/Settings.cpp +++ b/Grbl_Esp32/src/Settings.cpp @@ -450,7 +450,7 @@ void PinSetting::load() { esp_err_t err = nvs_get_str(_handle, _keyName, NULL, &len); if (err) { #ifdef PIN_DEBUG - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Initializing pin %s as '%s'", _fullName, _defaultValue); + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Initializing pin %s as '%s' (default)", _fullName, _defaultValue); #endif _storedValue = _defaultValue; _currentValue = Pin::create(_defaultValue); @@ -460,7 +460,7 @@ void PinSetting::load() { err = nvs_get_str(_handle, _keyName, buf, &len); if (err) { #ifdef PIN_DEBUG - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Initializing pin %s as '%s'", _fullName, _defaultValue); + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Initializing pin %s as '%s' (default)", _fullName, _defaultValue); #endif _storedValue = _defaultValue; _currentValue = Pin::create(_defaultValue); @@ -468,7 +468,7 @@ void PinSetting::load() { } #ifdef PIN_DEBUG - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Initializing pin %s as '%s'", _fullName, _storedValue); + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Initializing pin %s as '%s'", _fullName, _storedValue); #endif _storedValue = String(buf); _currentValue = Pin::create(_storedValue);