From fe41a4d5168779d211193f6fa7ca3a5c6f48f075 Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Fri, 21 May 2021 13:51:14 -1000 Subject: [PATCH] Improvements to logging, including * Use client_write so data goes to all clients, not just the Uart * Added unsigned int to SimpleOutputStream so uints can be logged * Added MSG: prefix on all log messages for Grbl protocol compatibility * Use log_warn in PWMSpindle to test it --- Grbl_Esp32/src/Config.h | 39 ------------------ Grbl_Esp32/src/Logging.cpp | 18 +++++---- Grbl_Esp32/src/Report.h | 8 ---- Grbl_Esp32/src/Serial.h | 56 +++++++++++++++++++++----- Grbl_Esp32/src/SimpleOutputStream.cpp | 47 +++++++++++++-------- Grbl_Esp32/src/SimpleOutputStream.h | 15 ++++--- Grbl_Esp32/src/Spindles/PWMSpindle.cpp | 25 +++++------- 7 files changed, 108 insertions(+), 100 deletions(-) diff --git a/Grbl_Esp32/src/Config.h b/Grbl_Esp32/src/Config.h index 8b6f845f..e9099d05 100644 --- a/Grbl_Esp32/src/Config.h +++ b/Grbl_Esp32/src/Config.h @@ -160,45 +160,6 @@ const int DEFAULT_RADIO_MODE = ESP_RADIO_OFF; # endif #endif -// Define realtime command special characters. These characters are 'picked-off' directly from the -// serial read data stream and are not passed to the grbl line execution parser. Select characters -// that do not and must not exist in the streamed GCode program. ASCII control characters may be -// used, if they are available per user setup. Also, extended ASCII codes (>127), which are never in -// GCode programs, maybe selected for interface programs. -// NOTE: If changed, manually update help message in report.c. - -// NOTE: All override realtime commands must be in the extended ASCII character set, starting -// at character value 128 (0x80) and up to 255 (0xFF). If the normal set of realtime commands, -// such as status reports, feed hold, reset, and cycle start, are moved to the extended set -// space, serial.c's RX ISR will need to be modified to accommodate the change. - -enum class Cmd : uint8_t { - Reset = 0x18, // Ctrl-X - StatusReport = '?', - CycleStart = '~', - FeedHold = '!', - SafetyDoor = 0x84, - JogCancel = 0x85, - DebugReport = 0x86, // Only when DEBUG enabled, sends debug report in '{}' braces. - FeedOvrReset = 0x90, // Restores feed override value to 100%. - FeedOvrCoarsePlus = 0x91, - FeedOvrCoarseMinus = 0x92, - FeedOvrFinePlus = 0x93, - FeedOvrFineMinus = 0x94, - RapidOvrReset = 0x95, // Restores rapid override value to 100%. - RapidOvrMedium = 0x96, - RapidOvrLow = 0x97, - RapidOvrExtraLow = 0x98, // *NOT SUPPORTED* - SpindleOvrReset = 0x99, // Restores spindle override value to 100%. - SpindleOvrCoarsePlus = 0x9A, // 154 - SpindleOvrCoarseMinus = 0x9B, - SpindleOvrFinePlus = 0x9C, - SpindleOvrFineMinus = 0x9D, - SpindleOvrStop = 0x9E, - CoolantFloodOvrToggle = 0xA0, - CoolantMistOvrToggle = 0xA1, -}; - // If homing is enabled, homing init lock sets Grbl into an alarm state upon power up. This forces // the user to perform the homing cycle (or override the locks) before doing anything else. This is // mainly a safety feature to remind the user to home, since position is unknown to Grbl. diff --git a/Grbl_Esp32/src/Logging.cpp b/Grbl_Esp32/src/Logging.cpp index aec8cfb8..ce4fe77d 100644 --- a/Grbl_Esp32/src/Logging.cpp +++ b/Grbl_Esp32/src/Logging.cpp @@ -5,7 +5,7 @@ # include DebugStream::DebugStream(const char* name) { - std::cout << '[' << name << ": "; + std::cout << '[MSG:' << name << ": "; } void DebugStream::add(char c) { std::cout << c; @@ -17,20 +17,24 @@ DebugStream::~DebugStream() { #else -# include "Uart.h" +# include "Serial.h" DebugStream::DebugStream(const char* name) { - Uart0.print("["); - Uart0.print(name); - Uart0.print(": "); + client_write(CLIENT_ALL, "[MSG:"); + client_write(CLIENT_ALL, name); + client_write(CLIENT_ALL, ": "); } void DebugStream::add(char c) { - Uart0.print(c); + char txt[2]; + txt[0] = c; + txt[1] = '\0'; + client_write(CLIENT_ALL, txt); } DebugStream::~DebugStream() { - Uart0.println("]"); + client_write(CLIENT_ALL, "]"); + client_write(CLIENT_ALL, "\r\n"); } #endif diff --git a/Grbl_Esp32/src/Report.h b/Grbl_Esp32/src/Report.h index cf7cb6c8..e2fc9e6c 100644 --- a/Grbl_Esp32/src/Report.h +++ b/Grbl_Esp32/src/Report.h @@ -46,14 +46,6 @@ enum class Message : uint8_t { SdFileQuit = 60, // mc_reset was called during an SD job }; -#define CLIENT_SERIAL 0 -#define CLIENT_BT 1 -#define CLIENT_WEBUI 2 -#define CLIENT_TELNET 3 -#define CLIENT_INPUT 4 -#define CLIENT_ALL 0xFF -#define CLIENT_COUNT 5 // total number of client types regardless if they are used - enum class MsgLevel : int8_t { // Use $Message/Level None = 0, Error = 1, diff --git a/Grbl_Esp32/src/Serial.h b/Grbl_Esp32/src/Serial.h index ec290050..b599ab46 100644 --- a/Grbl_Esp32/src/Serial.h +++ b/Grbl_Esp32/src/Serial.h @@ -22,16 +22,13 @@ #include "stdint.h" -#ifndef RX_BUFFER_SIZE -# define RX_BUFFER_SIZE 256 -#endif -#ifndef TX_BUFFER_SIZE -# ifdef USE_LINE_NUMBERS -# define TX_BUFFER_SIZE 112 -# else -# define TX_BUFFER_SIZE 104 -# endif -#endif +#define CLIENT_SERIAL 0 +#define CLIENT_BT 1 +#define CLIENT_WEBUI 2 +#define CLIENT_TELNET 3 +#define CLIENT_INPUT 4 +#define CLIENT_ALL 0xFF +#define CLIENT_COUNT 5 // total number of client types regardless if they are used // a task to read for incoming data from serial port void clientCheckTask(void* pvParameters); @@ -50,5 +47,44 @@ void client_reset_read_buffer(uint8_t client); // Returns the number of bytes available in the RX serial buffer. uint8_t client_get_rx_buffer_available(uint8_t client); +// Define realtime command special characters. These characters are 'picked-off' directly from the +// serial read data stream and are not passed to the grbl line execution parser. Select characters +// that do not and must not exist in the streamed GCode program. ASCII control characters may be +// used, if they are available per user setup. Also, extended ASCII codes (>127), which are never in +// GCode programs, maybe selected for interface programs. +// NOTE: If changed, manually update help message in report.c. + +// NOTE: All override realtime commands must be in the extended ASCII character set, starting +// at character value 128 (0x80) and up to 255 (0xFF). If the normal set of realtime commands, +// such as status reports, feed hold, reset, and cycle start, are moved to the extended set +// space, serial.c's RX ISR will need to be modified to accommodate the change. + +enum class Cmd : uint8_t { + Reset = 0x18, // Ctrl-X + StatusReport = '?', + CycleStart = '~', + FeedHold = '!', + SafetyDoor = 0x84, + JogCancel = 0x85, + DebugReport = 0x86, // Only when DEBUG enabled, sends debug report in '{}' braces. + FeedOvrReset = 0x90, // Restores feed override value to 100%. + FeedOvrCoarsePlus = 0x91, + FeedOvrCoarseMinus = 0x92, + FeedOvrFinePlus = 0x93, + FeedOvrFineMinus = 0x94, + RapidOvrReset = 0x95, // Restores rapid override value to 100%. + RapidOvrMedium = 0x96, + RapidOvrLow = 0x97, + RapidOvrExtraLow = 0x98, // *NOT SUPPORTED* + SpindleOvrReset = 0x99, // Restores spindle override value to 100%. + SpindleOvrCoarsePlus = 0x9A, // 154 + SpindleOvrCoarseMinus = 0x9B, + SpindleOvrFinePlus = 0x9C, + SpindleOvrFineMinus = 0x9D, + SpindleOvrStop = 0x9E, + CoolantFloodOvrToggle = 0xA0, + CoolantMistOvrToggle = 0xA1, +}; + void execute_realtime_command(Cmd command, uint8_t client); bool is_realtime_command(uint8_t data); diff --git a/Grbl_Esp32/src/SimpleOutputStream.cpp b/Grbl_Esp32/src/SimpleOutputStream.cpp index 819447ce..f0d66c24 100644 --- a/Grbl_Esp32/src/SimpleOutputStream.cpp +++ b/Grbl_Esp32/src/SimpleOutputStream.cpp @@ -2,54 +2,69 @@ #include -char* SimpleOutputStream::intToBuf(int value, char* dst) -{ +char* SimpleOutputStream::intToBuf(int value, char* dst) { #ifdef ESP32 return itoa(value, dst, 10); -#else +#else _itoa_s(value, dst, 10, 10); return dst + strlen(dst); #endif } void SimpleOutputStream::add(const char* s) { - for (; *s; ++s) { add(*s); } + for (; *s; ++s) { + add(*s); + } } void SimpleOutputStream::add(int value) { - char buf[10]; + char buf[12]; // Up to 10 digits, possibly a -, plus a null intToBuf(value, buf); add(buf); } -void SimpleOutputStream::add(double value, int numberDigits, int precision) -{ +void SimpleOutputStream::add(unsigned int value) { + if (value == 0) { + add('0'); + return; + } + char reverse[11]; // Up to 10 digits plus a null + char* p = reverse; + while (value) { + *p++ = value % 10 + '0'; + value /= 10; + } + while (p > reverse) { + add(*--p); + } +} + +void SimpleOutputStream::add(double value, int numberDigits, int precision) { if (isnan(value)) { add("NaN"); - } - else if (isinf(value)) { + } else if (isinf(value)) { add("Inf"); } - char buf[30]; // that's already quite big + char buf[30]; // that's already quite big char fmt[10]; fmt[0] = '%'; fmt[1] = '0'; char* next = intToBuf(numberDigits, fmt + 2); - *next++ = '.'; + *next++ = '.'; intToBuf(precision, next); snprintf(buf, sizeof(buf) - 1, fmt, value); add(buf); } -void SimpleOutputStream::add(StringRange range) -{ - for (auto ch : range) { add(ch); } +void SimpleOutputStream::add(StringRange range) { + for (auto ch : range) { + add(ch); + } } -void SimpleOutputStream::add(const Pin& pin) -{ +void SimpleOutputStream::add(const Pin& pin) { add(pin.name()); } diff --git a/Grbl_Esp32/src/SimpleOutputStream.h b/Grbl_Esp32/src/SimpleOutputStream.h index 0904b472..a9d24068 100644 --- a/Grbl_Esp32/src/SimpleOutputStream.h +++ b/Grbl_Esp32/src/SimpleOutputStream.h @@ -5,15 +5,15 @@ #include "StringRange.h" #include "Pin.h" -class SimpleOutputStream -{ +class SimpleOutputStream { static char* intToBuf(int value, char* dst); + static char* uintToBuf(unsigned int value, char* dst); public: SimpleOutputStream() = default; SimpleOutputStream(const SimpleOutputStream& o) = delete; - SimpleOutputStream(SimpleOutputStream&& o) = delete; + SimpleOutputStream(SimpleOutputStream&& o) = delete; SimpleOutputStream& operator=(const SimpleOutputStream& o) = delete; SimpleOutputStream& operator=(SimpleOutputStream&& o) = delete; @@ -23,12 +23,12 @@ public: void add(const char* s); void add(int value); + void add(unsigned int value); void add(double value, int numberDigits, int precision); void add(StringRange range); void add(const Pin& pin); - virtual ~SimpleOutputStream() { - } + virtual ~SimpleOutputStream() {} }; inline SimpleOutputStream& operator<<(SimpleOutputStream& lhs, char c) { @@ -46,6 +46,11 @@ inline SimpleOutputStream& operator<<(SimpleOutputStream& lhs, int v) { return lhs; } +inline SimpleOutputStream& operator<<(SimpleOutputStream& lhs, unsigned int v) { + lhs.add(v); + return lhs; +} + inline SimpleOutputStream& operator<<(SimpleOutputStream& lhs, double v) { lhs.add(v, 4, 3); return lhs; diff --git a/Grbl_Esp32/src/Spindles/PWMSpindle.cpp b/Grbl_Esp32/src/Spindles/PWMSpindle.cpp index 1571b054..feddd629 100644 --- a/Grbl_Esp32/src/Spindles/PWMSpindle.cpp +++ b/Grbl_Esp32/src/Spindles/PWMSpindle.cpp @@ -21,6 +21,7 @@ */ #include "PWMSpindle.h" #include "soc/ledc_struct.h" +#include "../Logging.h" // ======================= PWM ============================== /* @@ -35,12 +36,12 @@ namespace Spindles { get_pins_and_settings(); if (_output_pin.undefined()) { - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Warning: Spindle output pin not defined"); + log_warn("Spindle output pin not defined"); return; // We cannot continue without the output pin } if (!_output_pin.capabilities().has(Pin::Capabilities::PWM)) { - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Warning: Spindle output pin %s cannot do PWM", _output_pin.name().c_str()); + log_warn("Spindle output pin %s cannot do PWM" << _output_pin.name().c_str()); return; } @@ -67,11 +68,11 @@ namespace Spindles { is_reversable = _direction_pin.defined(); - _pwm_precision = calc_pwm_precision(_pwm_freq); // detewrmine the best precision + _pwm_precision = calc_pwm_precision(_pwm_freq); // determine the best precision _pwm_period = (1 << _pwm_precision); if (_pwm_min_value > _pwm_max_value) { - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Warning: Spindle min pwm is greater than max. Check $35 and $36"); + log_warn("Spindle min pwm is greater than max."); } // pre-calculate some PWM count values @@ -96,8 +97,6 @@ namespace Spindles { return rpm; } - //grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "set_rpm(%d)", rpm); - // apply override rpm = rpm * sys.spindle_speed_ovr / 100; // Scale by spindle speed override value (uint8_t percent) @@ -113,7 +112,7 @@ namespace Spindles { if (_piecewise_linear) { //pwm_value = piecewise_linear_fit(rpm); TODO pwm_value = 0; - grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Warning: Linear fit not implemented yet."); + log_warn("Linear fit not implemented yet."); } else { if (rpm == 0) { @@ -172,14 +171,10 @@ namespace Spindles { // prints the startup message of the spindle config void PWM::config_message() { - grbl_msg_sendf(CLIENT_ALL, - MsgLevel::Info, - "PWM spindle Output:%s, Enbl:%s, Dir:%s, Freq:%dHz, Res:%dbits", - _output_pin.name().c_str(), - _enable_pin.name().c_str(), - _direction_pin.name().c_str(), - _pwm_freq, - _pwm_precision); + log_info("PWM spindle Output:" << _output_pin.name().c_str() << ", Enbl:" << _enable_pin.name().c_str() << ", Dir:" + << _direction_pin.name().c_str() << ", Freq:" << _pwm_freq << "Hz, Res:" << _pwm_precision << "bits" + + ); } void PWM::set_output(uint32_t duty) {