From f04d83fc4cdc1d5390feb2da22f9c76341a244ad Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Fri, 27 May 2022 07:27:57 +0200 Subject: [PATCH] Update printer's type (MMU/non-MMU) at runtime + reformat utils.cpp --- Firmware/Marlin_main.cpp | 3 +- Firmware/util.cpp | 378 ++++++++++++++++++--------------------- 2 files changed, 181 insertions(+), 200 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 1c09e52ec..2046a420d 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -1621,7 +1621,6 @@ void setup() #endif //UVLO_SUPPORT fCheckModeInit(); - fSetMmuMode(MMU2::mmu2.Enabled()); KEEPALIVE_STATE(NOT_BUSY); #ifdef WATCHDOG wdt_enable(WDTO_4S); @@ -8140,6 +8139,8 @@ Sigma_Exit: { uint16_t nPrinterModel; nPrinterModel=(uint16_t)code_value_long(); + // based on current state of MMU (active/stopped/connecting) perform a runtime update of the printer type + fSetMmuMode(MMU2::mmu2.Enabled()); printer_model_check(nPrinterModel); } else if(code_seen('Q')) diff --git a/Firmware/util.cpp b/Firmware/util.cpp index 3a0203b19..2576967eb 100644 --- a/Firmware/util.cpp +++ b/Firmware/util.cpp @@ -227,7 +227,6 @@ void update_current_firmware_version_to_eeprom() } -//-// #define MSG_PRINT_CHECKING_FAILED_TIMEOUT 30 ClNozzleDiameter oNozzleDiameter=ClNozzleDiameter::_Diameter_400; @@ -236,207 +235,192 @@ ClCheckModel oCheckModel=ClCheckModel::_None; ClCheckVersion oCheckVersion=ClCheckVersion::_None; ClCheckGcode oCheckGcode=ClCheckGcode::_None; -void fCheckModeInit() -{ -oCheckMode=(ClCheckMode)eeprom_read_byte((uint8_t*)EEPROM_CHECK_MODE); -if(oCheckMode==ClCheckMode::_Undef) - { - oCheckMode=ClCheckMode::_Warn; - eeprom_update_byte((uint8_t*)EEPROM_CHECK_MODE,(uint8_t)oCheckMode); - } -if(farm_mode) - { - oCheckMode=ClCheckMode::_Strict; - if(eeprom_read_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM)==EEPROM_EMPTY_VALUE16) - eeprom_update_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM,EEPROM_NOZZLE_DIAMETER_uM_DEFAULT); - } -oNozzleDiameter=(ClNozzleDiameter)eeprom_read_byte((uint8_t*)EEPROM_NOZZLE_DIAMETER); -if((oNozzleDiameter==ClNozzleDiameter::_Diameter_Undef)&& !farm_mode) - { - oNozzleDiameter=ClNozzleDiameter::_Diameter_400; - eeprom_update_byte((uint8_t*)EEPROM_NOZZLE_DIAMETER,(uint8_t)oNozzleDiameter); - eeprom_update_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM,EEPROM_NOZZLE_DIAMETER_uM_DEFAULT); - } -oCheckModel=(ClCheckModel)eeprom_read_byte((uint8_t*)EEPROM_CHECK_MODEL); -if(oCheckModel==ClCheckModel::_Undef) - { - oCheckModel=ClCheckModel::_Warn; - eeprom_update_byte((uint8_t*)EEPROM_CHECK_MODEL,(uint8_t)oCheckModel); - } -oCheckVersion=(ClCheckVersion)eeprom_read_byte((uint8_t*)EEPROM_CHECK_VERSION); -if(oCheckVersion==ClCheckVersion::_Undef) - { - oCheckVersion=ClCheckVersion::_Warn; - eeprom_update_byte((uint8_t*)EEPROM_CHECK_VERSION,(uint8_t)oCheckVersion); - } -oCheckGcode=(ClCheckGcode)eeprom_read_byte((uint8_t*)EEPROM_CHECK_GCODE); -if(oCheckGcode==ClCheckGcode::_Undef) - { - oCheckGcode=ClCheckGcode::_Warn; - eeprom_update_byte((uint8_t*)EEPROM_CHECK_GCODE,(uint8_t)oCheckGcode); - } +void fCheckModeInit() { + oCheckMode = (ClCheckMode)eeprom_read_byte((uint8_t *)EEPROM_CHECK_MODE); + if (oCheckMode == ClCheckMode::_Undef) { + oCheckMode = ClCheckMode::_Warn; + eeprom_update_byte((uint8_t *)EEPROM_CHECK_MODE, (uint8_t)oCheckMode); + } + if (farm_mode) { + oCheckMode = ClCheckMode::_Strict; + if (eeprom_read_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM) == EEPROM_EMPTY_VALUE16) + eeprom_update_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM, EEPROM_NOZZLE_DIAMETER_uM_DEFAULT); + } + oNozzleDiameter = (ClNozzleDiameter)eeprom_read_byte((uint8_t *)EEPROM_NOZZLE_DIAMETER); + if ((oNozzleDiameter == ClNozzleDiameter::_Diameter_Undef) && !farm_mode) { + oNozzleDiameter = ClNozzleDiameter::_Diameter_400; + eeprom_update_byte((uint8_t *)EEPROM_NOZZLE_DIAMETER, (uint8_t)oNozzleDiameter); + eeprom_update_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM, EEPROM_NOZZLE_DIAMETER_uM_DEFAULT); + } + oCheckModel = (ClCheckModel)eeprom_read_byte((uint8_t *)EEPROM_CHECK_MODEL); + if (oCheckModel == ClCheckModel::_Undef) { + oCheckModel = ClCheckModel::_Warn; + eeprom_update_byte((uint8_t *)EEPROM_CHECK_MODEL, (uint8_t)oCheckModel); + } + oCheckVersion = (ClCheckVersion)eeprom_read_byte((uint8_t *)EEPROM_CHECK_VERSION); + if (oCheckVersion == ClCheckVersion::_Undef) { + oCheckVersion = ClCheckVersion::_Warn; + eeprom_update_byte((uint8_t *)EEPROM_CHECK_VERSION, (uint8_t)oCheckVersion); + } + oCheckGcode = (ClCheckGcode)eeprom_read_byte((uint8_t *)EEPROM_CHECK_GCODE); + if (oCheckGcode == ClCheckGcode::_Undef) { + oCheckGcode = ClCheckGcode::_Warn; + eeprom_update_byte((uint8_t *)EEPROM_CHECK_GCODE, (uint8_t)oCheckGcode); + } } -void nozzle_diameter_check(uint16_t nDiameter) -{ -uint16_t nDiameter_um; +void nozzle_diameter_check(uint16_t nDiameter) { + uint16_t nDiameter_um; -if(oCheckMode==ClCheckMode::_None) - return; -nDiameter_um=eeprom_read_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM); -if(nDiameter==nDiameter_um) - return; -//SERIAL_ECHO_START; -//SERIAL_ECHOLNPGM("Printer nozzle diameter differs from the G-code ..."); -//SERIAL_ECHOPGM("actual : "); -//SERIAL_ECHOLN((float)(nDiameter_um/1000.0)); -//SERIAL_ECHOPGM("expected: "); -//SERIAL_ECHOLN((float)(nDiameter/1000.0)); -switch(oCheckMode) - { - case ClCheckMode::_Warn: -// lcd_show_fullscreen_message_and_wait_P(_i("Printer nozzle diameter differs from the G-code. Continue?")); -lcd_display_message_fullscreen_P(_i("Printer nozzle diameter differs from the G-code. Continue?"));////MSG_NOZZLE_DIFFERS_CONTINUE c=20 r=5 -lcd_wait_for_click_delay(MSG_PRINT_CHECKING_FAILED_TIMEOUT); -//???custom_message_type=CUSTOM_MSG_TYPE_STATUS; // display / status-line recovery -lcd_update_enable(true); // display / status-line recovery - break; - case ClCheckMode::_Strict: - lcd_show_fullscreen_message_and_wait_P(_i("Printer nozzle diameter differs from the G-code. Please check the value in settings. Print cancelled."));////MSG_NOZZLE_DIFFERS_CANCELLED c=20 r=9 - lcd_print_stop(); - break; - case ClCheckMode::_None: - case ClCheckMode::_Undef: - break; - } -if(!farm_mode) - { - bSettings=false; // flag ('fake parameter') for 'lcd_hw_setup_menu()' function - menu_submenu(lcd_hw_setup_menu); - } + if (oCheckMode == ClCheckMode::_None) + return; + nDiameter_um = eeprom_read_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM); + if (nDiameter == nDiameter_um) + return; + // SERIAL_ECHO_START; + // SERIAL_ECHOLNPGM("Printer nozzle diameter differs from the G-code ..."); + // SERIAL_ECHOPGM("actual : "); + // SERIAL_ECHOLN((float)(nDiameter_um/1000.0)); + // SERIAL_ECHOPGM("expected: "); + // SERIAL_ECHOLN((float)(nDiameter/1000.0)); + switch (oCheckMode) { + case ClCheckMode::_Warn: + // lcd_show_fullscreen_message_and_wait_P(_i("Printer nozzle diameter differs from the G-code. Continue?")); + lcd_display_message_fullscreen_P(_i("Printer nozzle diameter differs from the G-code. Continue?")); ////MSG_NOZZLE_DIFFERS_CONTINUE c=20 r=5 + lcd_wait_for_click_delay(MSG_PRINT_CHECKING_FAILED_TIMEOUT); + //???custom_message_type=CUSTOM_MSG_TYPE_STATUS; // display / status-line recovery + lcd_update_enable(true); // display / status-line recovery + break; + case ClCheckMode::_Strict: + lcd_show_fullscreen_message_and_wait_P(_i( + "Printer nozzle diameter differs from the G-code. Please check the value in settings. Print cancelled.")); ////MSG_NOZZLE_DIFFERS_CANCELLED c=20 r=9 + lcd_print_stop(); + break; + case ClCheckMode::_None: + case ClCheckMode::_Undef: + break; + } + if (!farm_mode) { + bSettings = false; // flag ('fake parameter') for 'lcd_hw_setup_menu()' function + menu_submenu(lcd_hw_setup_menu); + } } -void printer_model_check(uint16_t nPrinterModel) -{ -if(oCheckModel==ClCheckModel::_None) - return; -if(nPrinterModel==nPrinterType) - return; -//SERIAL_ECHO_START; -//SERIAL_ECHOLNPGM("Printer model differs from the G-code ..."); -//SERIAL_ECHOPGM("actual : "); -//SERIAL_ECHOLN(nPrinterType); -//SERIAL_ECHOPGM("expected: "); -//SERIAL_ECHOLN(nPrinterModel); -switch(oCheckModel) - { - case ClCheckModel::_Warn: -// lcd_show_fullscreen_message_and_wait_P(_i("Printer model differs from the G-code. Continue?")); -lcd_display_message_fullscreen_P(_T(MSG_GCODE_DIFF_PRINTER_CONTINUE)); -lcd_wait_for_click_delay(MSG_PRINT_CHECKING_FAILED_TIMEOUT); -//???custom_message_type=CUSTOM_MSG_TYPE_STATUS; // display / status-line recovery -lcd_update_enable(true); // display / status-line recovery - break; - case ClCheckModel::_Strict: - lcd_show_fullscreen_message_and_wait_P(_T(MSG_GCODE_DIFF_PRINTER_CANCELLED)); - lcd_print_stop(); - break; - case ClCheckModel::_None: - case ClCheckModel::_Undef: - break; - } +void printer_model_check(uint16_t nPrinterModel) { + if (oCheckModel == ClCheckModel::_None) + return; + if (nPrinterModel == nPrinterType) + return; + // SERIAL_ECHO_START; + // SERIAL_ECHOLNPGM("Printer model differs from the G-code ..."); + // SERIAL_ECHOPGM("actual : "); + // SERIAL_ECHOLN(nPrinterType); + // SERIAL_ECHOPGM("expected: "); + // SERIAL_ECHOLN(nPrinterModel); + switch (oCheckModel) { + case ClCheckModel::_Warn: + // lcd_show_fullscreen_message_and_wait_P(_i("Printer model differs from the G-code. Continue?")); + lcd_display_message_fullscreen_P(_T(MSG_GCODE_DIFF_PRINTER_CONTINUE)); + lcd_wait_for_click_delay(MSG_PRINT_CHECKING_FAILED_TIMEOUT); + //???custom_message_type=CUSTOM_MSG_TYPE_STATUS; // display / status-line recovery + lcd_update_enable(true); // display / status-line recovery + break; + case ClCheckModel::_Strict: + lcd_show_fullscreen_message_and_wait_P(_T(MSG_GCODE_DIFF_PRINTER_CANCELLED)); + lcd_print_stop(); + break; + case ClCheckModel::_None: + case ClCheckModel::_Undef: + break; + } } -uint8_t mCompareValue(uint16_t nX,uint16_t nY) -{ -if(nX>nY) - return((uint8_t)ClCompareValue::_Greater); -if(nX nY) + return ((uint8_t)ClCompareValue::_Greater); + if (nX < nY) + return ((uint8_t)ClCompareValue::_Less); + return ((uint8_t)ClCompareValue::_Equal); } -void fw_version_check(const char *pVersion) -{ -uint16_t aVersion[4]; -uint8_t nCompareValueResult; +void fw_version_check(const char *pVersion) { + uint16_t aVersion[4]; + uint8_t nCompareValueResult; -if(oCheckVersion==ClCheckVersion::_None) - return; -parse_version(pVersion,aVersion); -nCompareValueResult=mCompareValue(aVersion[0],eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MAJOR))<<6; -nCompareValueResult+=mCompareValue(aVersion[1],eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MINOR))<<4; -nCompareValueResult+=mCompareValue(aVersion[2],eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_REVISION))<<2; -nCompareValueResult+=mCompareValue(aVersion[3],eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_FLAVOR)); -if(nCompareValueResult==COMPARE_VALUE_EQUAL) - return; -if((nCompareValueResult cmdqueue ??? -#define PRINTER_NAME_LENGTH ((sizeof(PRINTER_MMU_NAME)>sizeof(PRINTER_NAME))?(sizeof(PRINTER_MMU_NAME)-1):(sizeof(PRINTER_NAME)-1)) +#define PRINTER_NAME_LENGTH ((sizeof(PRINTER_MMU_NAME) > sizeof(PRINTER_NAME)) ? (sizeof(PRINTER_MMU_NAME) - 1) : (sizeof(PRINTER_NAME) - 1)) #define GCODE_DELIMITER '"' #define ELLIPSIS "..." -char* code_string(const char* pStr,size_t* nLength) -{ +char *code_string(char *pStr, size_t *nLength) { char* pStrBegin; char* pStrEnd; @@ -448,16 +432,16 @@ pStrEnd=strchr(pStrBegin,GCODE_DELIMITER); if(!pStrEnd) return(NULL); *nLength=pStrEnd-pStrBegin; +pStrBegin[*nLength] = '\0'; return pStrBegin; } -void printer_smodel_check(const char* pStrPos) -{ +void printer_smodel_check(char *pStrPos) { char* pResult; size_t nLength,nPrinterNameLength; nPrinterNameLength = strlen_P(sPrinterName); -pResult = code_string(pStrPos,&nLength); +pResult=code_string(pStrPos,&nLength); if(pResult != NULL && nLength == nPrinterNameLength) { // Only compare them if the lengths match @@ -483,20 +467,16 @@ lcd_update_enable(true); // display / status-line recovery } } -void fSetMmuMode(bool bMMu) -{ -if(bMMu) - { - nPrinterType=pgm_read_word(&_nPrinterMmuType); - sPrinterName=_sPrinterMmuName; - } -else { - nPrinterType=pgm_read_word(&_nPrinterType); - sPrinterName=_sPrinterName; - } +void fSetMmuMode(bool bMMu) { + if (bMMu) { + nPrinterType = pgm_read_word(&_nPrinterMmuType); + sPrinterName = _sPrinterMmuName; + } else { + nPrinterType = pgm_read_word(&_nPrinterType); + sPrinterName = _sPrinterName; + } } - void ip4_to_str(char* dest, uint8_t* IP) { sprintf_P(dest, PSTR("%u.%u.%u.%u"), IP[0], IP[1], IP[2], IP[3]);