From 44c02e7e5b1cd77ac75af5709e074b6e4c1f08b9 Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Tue, 25 May 2021 08:11:31 -1000 Subject: [PATCH] Move the pinReportStarted logic into Report.cpp --- Grbl_Esp32/src/Control.h | 2 +- Grbl_Esp32/src/ControlPin.cpp | 10 ++-------- Grbl_Esp32/src/ControlPin.h | 2 +- Grbl_Esp32/src/Report.cpp | 29 ++++++++++++++++++----------- Grbl_Esp32/src/Report.h | 2 ++ 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/Grbl_Esp32/src/Control.h b/Grbl_Esp32/src/Control.h index 891cea48..751b947e 100644 --- a/Grbl_Esp32/src/Control.h +++ b/Grbl_Esp32/src/Control.h @@ -52,7 +52,7 @@ public: void handle(Configuration::HandlerBase& handler) override; bool system_check_safety_door_ajar(); - void report(char* status, bool& pinReportStarted); + void report(char* status); ~Control() = default; }; diff --git a/Grbl_Esp32/src/ControlPin.cpp b/Grbl_Esp32/src/ControlPin.cpp index 5175a8e5..0d72ca4d 100644 --- a/Grbl_Esp32/src/ControlPin.cpp +++ b/Grbl_Esp32/src/ControlPin.cpp @@ -23,15 +23,9 @@ void ControlPin::init() { } } -void ControlPin::report(char* status, bool& pinReportStarted) { +void ControlPin::report(char* status) { if (!_pin.undefined()) { - if (!pinReportStarted) { - strcat(status, "|Pn:"); - pinReportStarted = true; - } - size_t pos = strlen(status); - status[pos] = letter(); - status[pos + 1] = '\0'; + addPinReport(status, _letter); } } diff --git a/Grbl_Esp32/src/ControlPin.h b/Grbl_Esp32/src/ControlPin.h index 4c36aa18..3620df6a 100644 --- a/Grbl_Esp32/src/ControlPin.h +++ b/Grbl_Esp32/src/ControlPin.h @@ -28,7 +28,7 @@ public: // char invertBitNum() { return _invertBitNum; } char letter() { return _letter; } - void report(char* status, bool& pinReportStarted); + void report(char* status); ~ControlPin(); }; diff --git a/Grbl_Esp32/src/Report.cpp b/Grbl_Esp32/src/Report.cpp index 14ce8d51..3b1c76ee 100644 --- a/Grbl_Esp32/src/Report.cpp +++ b/Grbl_Esp32/src/Report.cpp @@ -578,11 +578,7 @@ void report_echo_line_received(char* line, uint8_t client) { // float wco = returns the work coordinate offset // bool wpos = true for work position compensation -void addPinReport(char* status, char pinLetter, bool& pinReportStarted) { - if (!pinReportStarted) { - strcat(status, "|Pn:"); - pinReportStarted = true; - } +void addPinReport(char* status, char pinLetter) { size_t pos = strlen(status); status[pos] = pinLetter; status[pos + 1] = '\0'; @@ -653,22 +649,33 @@ void report_realtime_status(uint8_t client) { strcat(status, temp); #endif #ifdef REPORT_FIELD_PIN_STATE - AxisMask lim_pin_state = limits_get_state(); - bool prb_pin_state = config->_probe->get_state(); - bool pinReportStarted = false; + AxisMask lim_pin_state = limits_get_state(); + bool prb_pin_state = config->_probe->get_state(); + const char* pinReportPrefix = "|Pn:"; + + // Remember the current length so we know whether something was added + size_t saved_length = strlen(status); + + strcat(status, pinReportPrefix); + if (prb_pin_state) { - addPinReport(status, 'P', pinReportStarted); + addPinReport(status, 'P'); } if (lim_pin_state) { auto n_axis = config->_axes->_numberAxis; for (int i = 0; i < n_axis; i++) { if (bit_istrue(lim_pin_state, bit(i))) { - addPinReport(status, "XYZABC"[i], pinReportStarted); + addPinReport(status, "XYZABC"[i]); } } } - config->_control->report(status, pinReportStarted); + config->_control->report(status); + + if (strlen(status) == (saved_length + strlen(pinReportPrefix))) { + // Erase the "|Pn:" prefix because there is nothing after it + status[saved_length] = '\0'; + } #endif #ifdef REPORT_FIELD_WORK_COORD_OFFSET diff --git a/Grbl_Esp32/src/Report.h b/Grbl_Esp32/src/Report.h index e2fc9e6c..567a68a3 100644 --- a/Grbl_Esp32/src/Report.h +++ b/Grbl_Esp32/src/Report.h @@ -126,3 +126,5 @@ void reportTaskStackSize(UBaseType_t& saved); char* report_state_text(); float* get_wco(); void mpos_to_wpos(float* position); + +void addPinReport(char* status, char pinLetter);