From cdb559040cf98243c960a38b6d0216495851ec40 Mon Sep 17 00:00:00 2001 From: gudnimg Date: Sat, 4 Nov 2023 17:06:43 +0000 Subject: [PATCH 1/6] PFW-1523 Refactor parsing quoted strings My plan is to re-use this function in M79 in a later commit. The firmware doesn't have a dedicated parser like Marlin 2.1 so this is my attempt to not duplicate the parsing of a quoted string in G-codes Change in memory (MK3S+ Multilang): Flash: -50 bytes SRAM: 0 bytes --- Firmware/util.cpp | 36 ++++++++---------------------------- Firmware/util.h | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/Firmware/util.cpp b/Firmware/util.cpp index 6d955bd5e2..0e0bdbb7bb 100644 --- a/Firmware/util.cpp +++ b/Firmware/util.cpp @@ -385,36 +385,16 @@ void gcode_level_check(uint16_t nGcodeLevel) { ); } -#define GCODE_DELIMITER '"' - -char *code_string(const char *pStr, size_t *nLength) { -char* pStrBegin; -char* pStrEnd; - -pStrBegin=strchr(pStr,GCODE_DELIMITER); -if(!pStrBegin) - return(NULL); -pStrBegin++; -pStrEnd=strchr(pStrBegin,GCODE_DELIMITER); -if(!pStrEnd) - return(NULL); -*nLength=pStrEnd-pStrBegin; -return pStrBegin; -} void printer_smodel_check(const char *pStrPos, const char *actualPrinterSModel) { - char* pResult; - size_t nLength; - size_t aLength; - - pResult=code_string(pStrPos,&nLength); - if(pResult != NULL) { - aLength=strlen_P(actualPrinterSModel); - if(aLength > nLength) nLength = aLength; - - // Only compare first 6 chars on MK3|MK3S if string longer than 4 characters - if (nLength > 4 && strncmp_P(pResult, PSTR("MK3"), 3) == 0) nLength = 6; - if (strncmp_P(pResult, actualPrinterSModel, nLength) == 0) return; + unquoted_string smodel = unquoted_string(pStrPos); + + if(smodel.WasFound()) { + const uint8_t compareLength = strlen_P(actualPrinterSModel); + + if(compareLength == smodel.GetLength()) { + if (strncmp_P(smodel.GetUnquotedString(), actualPrinterSModel, compareLength) == 0) return; + } } render_M862_warnings( diff --git a/Firmware/util.h b/Firmware/util.h index d06d466dbf..0e041a8f43 100644 --- a/Firmware/util.h +++ b/Firmware/util.h @@ -1,5 +1,6 @@ #pragma once #include +#include extern const uint16_t FW_VERSION_NR[4]; const char* FW_VERSION_STR_P(); @@ -84,6 +85,45 @@ enum class ClCompareValue:uint_least8_t _Greater=2 }; +struct unquoted_string { +public: + /// @brief Given a pointer to a quoted string, filter out the quotes + /// @param pStr A constant pointer to a constant string to be searched/filtered. Modifying the pointer is strictly forbidden. + unquoted_string(const char * const pStr) + : len(0) + , found(false) + { + char * pStrEnd = NULL; + + // Start of the string + this->ptr = strchr(pStr, '"'); + if (!this->ptr) { + // First quote not found + return; + } + + // Skip the leading quote + this->ptr++; + + // End of the string + pStrEnd = strchr(this->ptr, '"'); + if(!pStrEnd) { + // Second quote not found + return; + } + this->len = pStrEnd - this->ptr; + this->found = true; + } + + bool WasFound() { return found; } + uint8_t GetLength() { return len; } + const char * GetUnquotedString() { return ptr; } +private: + const char * ptr = NULL; + uint8_t len; + bool found; +}; + extern ClNozzleDiameter oNozzleDiameter; extern ClCheckMode oCheckMode; extern ClCheckModel oCheckModel; From 20a2216623c2d54eb8c58d9a27a6cec6e0000624 Mon Sep 17 00:00:00 2001 From: gudnimg Date: Sat, 4 Nov 2023 17:43:13 +0000 Subject: [PATCH 2/6] PFW-1523 Implement S parameter for M79 Change in memory: Flash: +112 bytes SRAM: +3 bytes --- Firmware/Marlin_main.cpp | 14 ++++++++++++-- Firmware/host.cpp | 15 +++++++++++++++ Firmware/host.h | 10 ++++++++++ Firmware/ultralcd.cpp | 19 ++++++++++++++----- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index eb703187a4..3bcc36b3b4 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -5857,12 +5857,22 @@ SERIAL_PROTOCOLPGM("\n\n"); Restart the printer-host enable keepalive timer. While the timer has not expired, the printer will enable host specific features. #### Usage - M79 + M79 [ S ] #### Parameters - None + - `S` - Quoted string containing two characters e.g. "PL" */ case 79: M79_timer_restart(); + + if (code_seen('S')) + { + unquoted_string str = unquoted_string(strchr_pointer); + if (str.WasFound()) + { + ResetHostStatusScreenName(); + SetHostStatusScreenName(str.GetUnquotedString()); + } + } break; /*! diff --git a/Firmware/host.cpp b/Firmware/host.cpp index bd3c7c1e37..0f26c70685 100644 --- a/Firmware/host.cpp +++ b/Firmware/host.cpp @@ -1,8 +1,23 @@ +#include #include "Configuration_adv.h" #include "host.h" #include "Timer.h" static LongTimer M79_timer; +static char host_status_screen_name[3]; + +void SetHostStatusScreenName(const char * name) { + strncpy(host_status_screen_name, name, 2); + host_status_screen_name[2] = '\0'; +} + +char * GetHostStatusScreenName() { + return host_status_screen_name; +} + +void ResetHostStatusScreenName() { + memset(host_status_screen_name, 0, sizeof(host_status_screen_name)); +} void M79_timer_restart() { M79_timer.start(); diff --git a/Firmware/host.h b/Firmware/host.h index 373d6a28fd..d759d7d221 100644 --- a/Firmware/host.h +++ b/Firmware/host.h @@ -1,5 +1,15 @@ #pragma once +/// Assigns host name with up to two characters which will be shown on +/// the UI when printing. The function forces the third byte to be null delimiter. +void SetHostStatusScreenName(const char * name); + +/// Returns a pointer to the host name +char * GetHostStatusScreenName(); + +/// Reset the memory to NULL when the host name should not be used +void ResetHostStatusScreenName(); + /// Restart the M79 timer void M79_timer_restart(); diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp index 33f289785e..c0aab9fe8e 100644 --- a/Firmware/ultralcd.cpp +++ b/Firmware/ultralcd.cpp @@ -19,7 +19,7 @@ #include "menu.h" #include "backlight.h" - +#include "host.h" #include "util.h" #include "mesh_bed_leveling.h" #include "mesh_bed_calibration.h" @@ -359,8 +359,7 @@ void lcdui_print_feedrate(void) // Print percent done in form "USB---%", " SD---%", " ---%" (7 chars total) void lcdui_print_percent_done(void) { - const char* src = usb_timer.running()?_N("USB"):(IS_SD_PRINTING?_N(" SD"):_N(" ")); - char per[4]; + const char* src = usb_timer.running()?_N(" HO"):(IS_SD_PRINTING?_N(" SD"):_N(" ")); bool num = IS_SD_PRINTING || (printer_active() && (print_percent_done_normal != PRINT_PERCENT_DONE_INIT)); if (!num || heating_status != HeatingStatus::NO_HEATING) // either not printing or heating { @@ -375,8 +374,18 @@ void lcdui_print_percent_done(void) return; //do not also print the percentage } } - sprintf_P(per, num?_N("%3d"):_N("---"), calc_percent_done()); - lcd_printf_P(_N("%3S%3s%%"), src, per); + + if (M79_timer_get_status() && GetHostStatusScreenName()) + { + // Overwrite the name + char * hostName = GetHostStatusScreenName(); + lcd_space(1); // Blank space + lcd_print(hostName); // Two characters + } else { + lcd_printf_P(PSTR("%3S"), src); + } + + lcd_printf_P(num ? _N("%3d%%"):_N("---%%"), calc_percent_done()); } // Print extruder status (5 chars total) From ed376009aa0fe128fc0b43c1211be6c7c0d2cfe6 Mon Sep 17 00:00:00 2001 From: gudnimg Date: Sat, 4 Nov 2023 20:57:31 +0000 Subject: [PATCH 3/6] PFW-1523 Force inline unquoted_string constructor Saves 36 bytes of flash --- Firmware/util.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Firmware/util.h b/Firmware/util.h index 0e041a8f43..02c7208b7d 100644 --- a/Firmware/util.h +++ b/Firmware/util.h @@ -89,7 +89,8 @@ struct unquoted_string { public: /// @brief Given a pointer to a quoted string, filter out the quotes /// @param pStr A constant pointer to a constant string to be searched/filtered. Modifying the pointer is strictly forbidden. - unquoted_string(const char * const pStr) + /// NOTE: Forcing inline saves ~36 bytes of flash + inline __attribute__((always_inline)) unquoted_string(const char * const pStr) : len(0) , found(false) { From 890bfab946b07fec8b0d391fd8058d40bd68a9c2 Mon Sep 17 00:00:00 2001 From: gudnimg Date: Tue, 7 Nov 2023 07:26:35 +0000 Subject: [PATCH 4/6] PFW-1523 Update M79 description --- Firmware/Marlin_main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 3bcc36b3b4..0ca4fe70aa 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -5853,8 +5853,8 @@ SERIAL_PROTOCOLPGM("\n\n"); } /*! - ### M79 - TODO - Restart the printer-host enable keepalive timer. While the timer has not expired, the printer will enable host specific features. + ### M79 - Start host timer M79: Start host timer + Start the printer-host enable keep-alive timer. While the timer has not expired, the printer will enable host specific features. #### Usage M79 [ S ] From bcc7a16e1c8b527f328278e9da9d48137bc6ce83 Mon Sep 17 00:00:00 2001 From: gudnimg Date: Tue, 7 Nov 2023 07:27:08 +0000 Subject: [PATCH 5/6] PFW-1523 Trim trailing whitespace in util.h --- Firmware/util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Firmware/util.h b/Firmware/util.h index 02c7208b7d..f7c2596eda 100644 --- a/Firmware/util.h +++ b/Firmware/util.h @@ -104,7 +104,7 @@ struct unquoted_string { } // Skip the leading quote - this->ptr++; + this->ptr++; // End of the string pStrEnd = strchr(this->ptr, '"'); From 4e6b64986585677185d45da2de7507f8c845b3e2 Mon Sep 17 00:00:00 2001 From: gudnimg Date: Sat, 11 Nov 2023 16:56:14 +0000 Subject: [PATCH 6/6] PFW-1523 Only overwrite name if not SD printing --- Firmware/ultralcd.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp index c0aab9fe8e..c15b1ef906 100644 --- a/Firmware/ultralcd.cpp +++ b/Firmware/ultralcd.cpp @@ -375,7 +375,7 @@ void lcdui_print_percent_done(void) } } - if (M79_timer_get_status() && GetHostStatusScreenName()) + if (!IS_SD_PRINTING && M79_timer_get_status() && GetHostStatusScreenName()) { // Overwrite the name char * hostName = GetHostStatusScreenName();