From f9b04af650be066d981bc09f3511552760d65c67 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 22 Oct 2020 22:31:48 -0500 Subject: [PATCH] Case light brightness cleanup (#19856) Co-authored-by: Chris --- Marlin/src/feature/caselight.cpp | 37 ++++++++++++--------- Marlin/src/feature/caselight.h | 8 ++++- Marlin/src/gcode/feature/caselight/M355.cpp | 19 +++++++---- Marlin/src/gcode/host/M115.cpp | 6 +++- Marlin/src/lcd/extui/ui_api.cpp | 2 +- Marlin/src/lcd/menu/menu_led.cpp | 2 +- Marlin/src/module/settings.cpp | 15 ++++----- 7 files changed, 56 insertions(+), 33 deletions(-) diff --git a/Marlin/src/feature/caselight.cpp b/Marlin/src/feature/caselight.cpp index 8a128bb12d..0eba102a04 100644 --- a/Marlin/src/feature/caselight.cpp +++ b/Marlin/src/feature/caselight.cpp @@ -28,7 +28,14 @@ CaseLight caselight; -uint8_t CaseLight::brightness = CASE_LIGHT_DEFAULT_BRIGHTNESS; +#if CASELIGHT_USES_BRIGHTNESS && !defined(CASE_LIGHT_DEFAULT_BRIGHTNESS) + #define CASE_LIGHT_DEFAULT_BRIGHTNESS 0 // For use on PWM pin as non-PWM just sets a default +#endif + +#if CASELIGHT_USES_BRIGHTNESS + uint8_t CaseLight::brightness = CASE_LIGHT_DEFAULT_BRIGHTNESS; +#endif + bool CaseLight::on = CASE_LIGHT_DEFAULT_ON; #if ENABLED(CASE_LIGHT_USE_NEOPIXEL) @@ -46,21 +53,21 @@ bool CaseLight::on = CASE_LIGHT_DEFAULT_ON; #endif void CaseLight::update(const bool sflag) { - /** - * The brightness_sav (and sflag) is needed because ARM chips ignore - * a "WRITE(CASE_LIGHT_PIN,x)" command to the pins that are directly - * controlled by the PWM module. In order to turn them off the brightness - * level needs to be set to OFF. Since we can't use the PWM register to - * save the last brightness level we need a variable to save it. - */ - static uint8_t brightness_sav; // Save brightness info for restore on "M355 S1" + #if CASELIGHT_USES_BRIGHTNESS + /** + * The brightness_sav (and sflag) is needed because ARM chips ignore + * a "WRITE(CASE_LIGHT_PIN,x)" command to the pins that are directly + * controlled by the PWM module. In order to turn them off the brightness + * level needs to be set to OFF. Since we can't use the PWM register to + * save the last brightness level we need a variable to save it. + */ + static uint8_t brightness_sav; // Save brightness info for restore on "M355 S1" - if (on || !sflag) - brightness_sav = brightness; // Save brightness except for M355 S0 - if (sflag && on) - brightness = brightness_sav; // Restore last brightness for M355 S1 + if (on || !sflag) + brightness_sav = brightness; // Save brightness except for M355 S0 + if (sflag && on) + brightness = brightness_sav; // Restore last brightness for M355 S1 - #if ENABLED(CASE_LIGHT_USE_NEOPIXEL) || DISABLED(CASE_LIGHT_NO_BRIGHTNESS) const uint8_t i = on ? brightness : 0, n10ct = INVERT_CASE_LIGHT ? 255 - i : i; #endif @@ -73,7 +80,7 @@ void CaseLight::update(const bool sflag) { #else // !CASE_LIGHT_USE_NEOPIXEL - #if DISABLED(CASE_LIGHT_NO_BRIGHTNESS) + #if CASELIGHT_USES_BRIGHTNESS if (PWM_PIN(CASE_LIGHT_PIN)) analogWrite(pin_t(CASE_LIGHT_PIN), ( #if CASE_LIGHT_MAX_PWM == 255 diff --git a/Marlin/src/feature/caselight.h b/Marlin/src/feature/caselight.h index 9d1e76da26..2198c85f2a 100644 --- a/Marlin/src/feature/caselight.h +++ b/Marlin/src/feature/caselight.h @@ -27,9 +27,15 @@ #include "leds/leds.h" #endif +#if DISABLED(CASE_LIGHT_NO_BRIGHTNESS) || ENABLED(CASE_LIGHT_USE_NEOPIXEL) + #define CASELIGHT_USES_BRIGHTNESS 1 +#endif + class CaseLight { public: - static uint8_t brightness; + #if CASELIGHT_USES_BRIGHTNESS + static uint8_t brightness; + #endif static bool on; static void update(const bool sflag); diff --git a/Marlin/src/gcode/feature/caselight/M355.cpp b/Marlin/src/gcode/feature/caselight/M355.cpp index bb6b57f666..12ae5cff1d 100644 --- a/Marlin/src/gcode/feature/caselight/M355.cpp +++ b/Marlin/src/gcode/feature/caselight/M355.cpp @@ -41,10 +41,12 @@ */ void GcodeSuite::M355() { bool didset = false; - if (parser.seenval('P')) { - didset = true; - caselight.brightness = parser.value_byte(); - } + #if CASELIGHT_USES_BRIGHTNESS + if (parser.seenval('P')) { + didset = true; + caselight.brightness = parser.value_byte(); + } + #endif const bool sflag = parser.seenval('S'); if (sflag) { didset = true; @@ -58,8 +60,13 @@ void GcodeSuite::M355() { if (!caselight.on) SERIAL_ECHOLNPGM(STR_OFF); else { - if (!PWM_PIN(CASE_LIGHT_PIN)) SERIAL_ECHOLNPGM(STR_ON); - else SERIAL_ECHOLN(int(caselight.brightness)); + #if CASELIGHT_USES_BRIGHTNESS + if (PWM_PIN(CASE_LIGHT_PIN)) { + SERIAL_ECHOLN(int(caselight.brightness)); + return; + } + #endif + SERIAL_ECHOLNPGM(STR_ON); } } diff --git a/Marlin/src/gcode/host/M115.cpp b/Marlin/src/gcode/host/M115.cpp index 53c5163bba..529b1dd6d0 100644 --- a/Marlin/src/gcode/host/M115.cpp +++ b/Marlin/src/gcode/host/M115.cpp @@ -27,6 +27,10 @@ #include "../../module/motion.h" #endif +#if ENABLED(CASE_LIGHT_ENABLE) + #include "../../feature/caselight.h" +#endif + #if ENABLED(EXTENDED_CAPABILITIES_REPORT) static void cap_line(PGM_P const name, bool ena=false) { SERIAL_ECHOPGM("Cap:"); @@ -102,7 +106,7 @@ void GcodeSuite::M115() { // TOGGLE_LIGHTS (M355) cap_line(PSTR("TOGGLE_LIGHTS"), ENABLED(CASE_LIGHT_ENABLE)); - cap_line(PSTR("CASE_LIGHT_BRIGHTNESS"), TERN0(CASE_LIGHT_ENABLE, PWM_PIN(CASE_LIGHT_PIN))); + cap_line(PSTR("CASE_LIGHT_BRIGHTNESS"), TERN0(CASE_LIGHT_ENABLE, TERN0(CASELIGHT_USES_BRIGHTNESS, TERN(CASE_LIGHT_USE_NEOPIXEL, true, PWM_PIN(CASE_LIGHT_PIN))))); // EMERGENCY_PARSER (M108, M112, M410, M876) cap_line(PSTR("EMERGENCY_PARSER"), ENABLED(EMERGENCY_PARSER)); diff --git a/Marlin/src/lcd/extui/ui_api.cpp b/Marlin/src/lcd/extui/ui_api.cpp index 8151828a08..33b5d5458e 100644 --- a/Marlin/src/lcd/extui/ui_api.cpp +++ b/Marlin/src/lcd/extui/ui_api.cpp @@ -610,7 +610,7 @@ namespace ExtUI { caselight.update_enabled(); } - #if DISABLED(CASE_LIGHT_NO_BRIGHTNESS) + #if CASELIGHT_USES_BRIGHTNESS float getCaseLightBrightness_percent() { return ui8_to_percent(caselight.brightness); } void setCaseLightBrightness_percent(const float value) { caselight.brightness = map(constrain(value, 0, 100), 0, 100, 0, 255); diff --git a/Marlin/src/lcd/menu/menu_led.cpp b/Marlin/src/lcd/menu/menu_led.cpp index 386a4d799a..d9540592d0 100644 --- a/Marlin/src/lcd/menu/menu_led.cpp +++ b/Marlin/src/lcd/menu/menu_led.cpp @@ -105,7 +105,7 @@ #if ENABLED(CASE_LIGHT_MENU) #include "../../feature/caselight.h" - #if DISABLED(CASE_LIGHT_NO_BRIGHTNESS) + #if CASELIGHT_USES_BRIGHTNESS void menu_case_light() { START_MENU(); BACK_ITEM(MSG_CONFIGURATION); diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index 972d375848..d01486a80d 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -137,9 +137,8 @@ void M710_report(const bool forReplay); #endif -#if ENABLED(CASE_LIGHT_ENABLE) && DISABLED(CASE_LIGHT_NO_BRIGHTNESS) +#if ENABLED(CASE_LIGHT_ENABLE) #include "../feature/caselight.h" - #define HAS_CASE_LIGHT_BRIGHTNESS 1 #endif #if ENABLED(PASSWORD_FEATURE) @@ -422,9 +421,9 @@ typedef struct SettingsDataStruct { #endif // - // HAS_CASE_LIGHT_BRIGHTNESS + // CASELIGHT_USES_BRIGHTNESS // - #if HAS_CASE_LIGHT_BRIGHTNESS + #if CASELIGHT_USES_BRIGHTNESS uint8_t caselight_brightness; // M355 P #endif @@ -503,7 +502,7 @@ void MarlinSettings::postprocess() { TERN_(HAS_LINEAR_E_JERK, planner.recalculate_max_e_jerk()); - TERN_(HAS_CASE_LIGHT_BRIGHTNESS, caselight.update_brightness()); + TERN_(CASELIGHT_USES_BRIGHTNESS, caselight.update_brightness()); // Refresh steps_to_mm with the reciprocal of axis_steps_per_mm // and init stepper.count[], planner.position[] with current_position @@ -1385,7 +1384,7 @@ void MarlinSettings::postprocess() { // // Case Light Brightness // - #if HAS_CASE_LIGHT_BRIGHTNESS + #if CASELIGHT_USES_BRIGHTNESS EEPROM_WRITE(caselight.brightness); #endif @@ -2259,7 +2258,7 @@ void MarlinSettings::postprocess() { // // Case Light Brightness // - #if HAS_CASE_LIGHT_BRIGHTNESS + #if CASELIGHT_USES_BRIGHTNESS _FIELD_TEST(caselight_brightness); EEPROM_READ(caselight.brightness); #endif @@ -2597,7 +2596,7 @@ void MarlinSettings::reset() { // // Case Light Brightness // - TERN_(HAS_CASE_LIGHT_BRIGHTNESS, caselight.brightness = CASE_LIGHT_DEFAULT_BRIGHTNESS); + TERN_(CASELIGHT_USES_BRIGHTNESS, caselight.brightness = CASE_LIGHT_DEFAULT_BRIGHTNESS); // // TOUCH_SCREEN_CALIBRATION