From 61437d988ada581ff9d8c27bcf39484fa98b36f4 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 27 Nov 2016 23:51:51 -0600 Subject: [PATCH] Fix and optimize case-light code --- Marlin/Marlin_main.cpp | 38 ++++++++++---------------------------- Marlin/ultralcd.cpp | 23 +++++++++++++++++------ 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index ef17e7159f..a2f04dc112 100755 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -881,27 +881,6 @@ void setup_homepin(void) { #endif } -#if HAS_CASE_LIGHT - - void setup_case_light() { - digitalWrite(CASE_LIGHT_PIN, - #if ENABLED(CASE_LIGHT_DEFAULT_ON) - 255 - #else - 0 - #endif - ); - analogWrite(CASE_LIGHT_PIN, - #if ENABLED(CASE_LIGHT_DEFAULT_ON) - 255 - #else - 0 - #endif - ); - } - -#endif - void setup_powerhold() { #if HAS_SUICIDE OUT_WRITE(SUICIDE_PIN, HIGH); @@ -7279,6 +7258,13 @@ inline void gcode_M907() { #if HAS_CASE_LIGHT + uint8_t case_light_brightness = 255; + + void update_case_light() { + digitalWrite(CASE_LIGHT_PIN, case_light_on ? HIGH : LOW); + analogWrite(CASE_LIGHT_PIN, case_light_on ? case_light_brightness : 0); + } + /** * M355: Turn case lights on/off and set brightness * @@ -7286,13 +7272,9 @@ inline void gcode_M907() { * P Set case light brightness (PWM pin required) */ inline void gcode_M355() { - static uint8_t case_light_brightness = 255; if (code_seen('P')) case_light_brightness = code_value_byte(); - if (code_seen('S')) { - case_light_on = code_value_bool(); - digitalWrite(CASE_LIGHT_PIN, case_light_on ? HIGH : LOW); - analogWrite(CASE_LIGHT_PIN, case_light_on ? case_light_brightness : 0); - } + if (code_seen('S')) case_light_on = code_value_bool(); + update_case_light(); SERIAL_ECHO_START; SERIAL_ECHOPGM("Case lights "); case_light_on ? SERIAL_ECHOLNPGM("on") : SERIAL_ECHOLNPGM("off"); @@ -10020,7 +10002,7 @@ void setup() { #endif #if HAS_CASE_LIGHT - setup_case_light(); + update_case_light(); #endif #if HAS_BED_PROBE diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index c4fcf65e62..0e2fd9bedf 100755 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -110,9 +110,7 @@ uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to #if HAS_POWER_SWITCH extern bool powersupply; #endif - #if HAS_CASE_LIGHT - extern bool case_light_on; - #endif + const float manual_feedrate_mm_m[] = MANUAL_FEEDRATE; void lcd_main_menu(); void lcd_tune_menu(); @@ -576,6 +574,19 @@ void kill_screen(const char* lcd_msg) { #endif //SDSUPPORT + #if HAS_CASE_LIGHT && ENABLED(MENU_ITEM_CASE_LIGHT) + + extern bool case_light_on; + extern void update_case_light(); + + void toggle_case_light() { + case_light_on = !case_light_on; + lcdDrawUpdate = LCDVIEW_CALL_REDRAW_NEXT; + update_case_light(); + } + + #endif // MENU_ITEM_CASE_LIGHT + /** * * "Main" menu @@ -590,10 +601,10 @@ void kill_screen(const char* lcd_msg) { // Switch case light on/off // #if HAS_CASE_LIGHT && ENABLED(MENU_ITEM_CASE_LIGHT) - if (case_light_on == 0) - MENU_ITEM(gcode, MSG_LIGHTS_ON, PSTR("M355 S1")); + if (case_light_on) + MENU_ITEM(function, MSG_LIGHTS_OFF, toggle_case_light); else - MENU_ITEM(gcode, MSG_LIGHTS_OFF, PSTR("M355 S0")); + MENU_ITEM(function, MSG_LIGHTS_ON, toggle_case_light); #endif #if ENABLED(BLTOUCH)