From d43d4e421977e333f1a7fbff8b939e0288b3616c Mon Sep 17 00:00:00 2001 From: Giuliano Zaro <3684609+GMagician@users.noreply.github.com> Date: Thu, 11 Oct 2018 04:25:43 +0200 Subject: [PATCH] Refactor PRINTER_EVENT_LEDS, apply to M303 (#12038) Co-Authored-By: Giuliano Zaro --- Marlin/src/feature/leds/leds.h | 8 +- .../src/feature/leds/printer_event_leds.cpp | 81 +++++++++++++++++++ Marlin/src/feature/leds/printer_event_leds.h | 79 ++++++++++++++++++ Marlin/src/feature/leds/tempstat.cpp | 10 +-- Marlin/src/gcode/gcode.h | 4 - Marlin/src/gcode/lcd/M0_M1.cpp | 12 +-- Marlin/src/gcode/queue.cpp | 11 +-- Marlin/src/gcode/temperature/M104_M109.cpp | 4 - Marlin/src/inc/Conditionals_LCD.h | 1 + Marlin/src/inc/SanityCheck.h | 6 +- Marlin/src/inc/Version.h | 6 +- Marlin/src/module/temperature.cpp | 53 ++++++------ buildroot/share/tests/megaatmega2560_tests | 3 +- 13 files changed, 205 insertions(+), 73 deletions(-) create mode 100644 Marlin/src/feature/leds/printer_event_leds.cpp create mode 100644 Marlin/src/feature/leds/printer_event_leds.h diff --git a/Marlin/src/feature/leds/leds.h b/Marlin/src/feature/leds/leds.h index fe00cd3c48..220de6d15d 100644 --- a/Marlin/src/feature/leds/leds.h +++ b/Marlin/src/feature/leds/leds.h @@ -19,15 +19,13 @@ * along with this program. If not, see . * */ +#pragma once /** * leds.h - Marlin general RGB LED support */ -#ifndef __LEDS_H__ -#define __LEDS_H__ - -#include "../../inc/MarlinConfig.h" +#include "../../inc/MarlinConfigPre.h" #if ENABLED(NEOPIXEL_LED) #include "neopixel.h" @@ -180,5 +178,3 @@ public: }; extern LEDLights leds; - -#endif // __LEDS_H__ diff --git a/Marlin/src/feature/leds/printer_event_leds.cpp b/Marlin/src/feature/leds/printer_event_leds.cpp new file mode 100644 index 0000000000..3d974e7b3e --- /dev/null +++ b/Marlin/src/feature/leds/printer_event_leds.cpp @@ -0,0 +1,81 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * printer_event_leds.cpp - LED color changing based on printer status + */ + +#include "../../inc/MarlinConfigPre.h" + +#if ENABLED(PRINTER_EVENT_LEDS) + +#include "printer_event_leds.h" + +PrinterEventLEDs printerEventLEDs; + +#if HAS_LEDS_OFF_FLAG + bool PrinterEventLEDs::leds_off_after_print; // = false +#endif + +#if HAS_TEMP_HOTEND || HAS_HEATED_BED + + uint8_t PrinterEventLEDs::old_intensity = 0; + + inline uint8_t pel_intensity(const float &start, const float ¤t, const float &target) { + return (uint8_t)map(constrain(current, start, target), start, target, 0.f, 255.f); + } + + inline void pel_set_rgb(const uint8_t r, const uint8_t g, const uint8_t b) { + leds.set_color( + MakeLEDColor(r, g, b, 0, pixels.getBrightness()) + #if ENABLED(NEOPIXEL_IS_SEQUENTIAL) + , true + #endif + ); + } + +#endif + +#if HAS_TEMP_HOTEND + + void PrinterEventLEDs::onHotendHeating(const float &start, const float ¤t, const float &target) { + const uint8_t blue = pel_intensity(start, current, target); + if (blue != old_intensity) { + old_intensity = blue; + pel_set_rgb(255, 0, 255 - blue); + } + } + +#endif + +#if HAS_HEATED_BED + + void PrinterEventLEDs::onBedHeating(const float &start, const float ¤t, const float &target) { + const uint8_t red = pel_intensity(start, current, target); + if (red != old_intensity) { + old_intensity = red; + pel_set_rgb(red, 0, 255); + } + } +#endif + +#endif // PRINTER_EVENT_LEDS diff --git a/Marlin/src/feature/leds/printer_event_leds.h b/Marlin/src/feature/leds/printer_event_leds.h new file mode 100644 index 0000000000..679eddfbc9 --- /dev/null +++ b/Marlin/src/feature/leds/printer_event_leds.h @@ -0,0 +1,79 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +/** + * printer_event_leds.h - LED color changing based on printer status + */ + +#include "leds.h" +#include "../../inc/MarlinConfig.h" + +class PrinterEventLEDs { +private: + static uint8_t old_intensity; + + #if HAS_LEDS_OFF_FLAG + static bool leds_off_after_print; + #endif + +public: + #if HAS_TEMP_HOTEND + FORCE_INLINE static void onHotendHeatingStart() { old_intensity = 0; } + static void onHotendHeating(const float &start, const float ¤t, const float &target); + #endif + + #if HAS_HEATED_BED + FORCE_INLINE static void onBedHeatingStart() { old_intensity = 127; } + static void onBedHeating(const float &start, const float ¤t, const float &target); + #endif + + #if HAS_TEMP_HOTEND || HAS_HEATED_BED + FORCE_INLINE static void onHeated() { leds.set_white(); } + FORCE_INLINE static void onHeatersOff() { leds.set_off(); } + #endif + + #if ENABLED(SDSUPPORT) + + FORCE_INLINE static void onPrintCompleted() { + leds.set_green(); + #if HAS_LEDS_OFF_FLAG + leds_off_after_print = true; + #else + safe_delay(2000); + leds.set_off(); + #endif + } + + FORCE_INLINE static void onResumeAfterWait() { + #if HAS_LEDS_OFF_FLAG + if (leds_off_after_print) { + leds.set_off(); + leds_off_after_print = false; + } + #endif + } + + #endif // SDSUPPORT +}; + +extern PrinterEventLEDs printerEventLEDs; diff --git a/Marlin/src/feature/leds/tempstat.cpp b/Marlin/src/feature/leds/tempstat.cpp index a7aa6a4d43..8a5a4a0b70 100644 --- a/Marlin/src/feature/leds/tempstat.cpp +++ b/Marlin/src/feature/leds/tempstat.cpp @@ -32,7 +32,7 @@ #include "../../module/temperature.h" void handle_status_leds(void) { - static bool red_led = false; + static uint8_t red_led = LOW; static millis_t next_status_led_update_ms = 0; if (ELAPSED(millis(), next_status_led_update_ms)) { next_status_led_update_ms += 500; // Update every 0.5s @@ -42,16 +42,16 @@ void handle_status_leds(void) { #endif HOTEND_LOOP() max_temp = MAX(max_temp, thermalManager.degHotend(e), thermalManager.degTargetHotend(e)); - const bool new_led = (max_temp > 55.0) ? true : (max_temp < 54.0) ? false : red_led; + const uint8_t new_led = (max_temp > 55.0) ? HIGH : (max_temp < 54.0) ? LOW : red_led; if (new_led != red_led) { red_led = new_led; #if PIN_EXISTS(STAT_LED_RED) - WRITE(STAT_LED_RED_PIN, new_led ? HIGH : LOW); + WRITE(STAT_LED_RED_PIN, new_led); #if PIN_EXISTS(STAT_LED_BLUE) - WRITE(STAT_LED_BLUE_PIN, new_led ? LOW : HIGH); + WRITE(STAT_LED_BLUE_PIN, !new_led); #endif #else - WRITE(STAT_LED_BLUE_PIN, new_led ? HIGH : LOW); + WRITE(STAT_LED_BLUE_PIN, new_led); #endif } } diff --git a/Marlin/src/gcode/gcode.h b/Marlin/src/gcode/gcode.h index a25c6c14eb..7cc94ce8d2 100644 --- a/Marlin/src/gcode/gcode.h +++ b/Marlin/src/gcode/gcode.h @@ -336,10 +336,6 @@ public: #define KEEPALIVE_STATE(n) NOOP #endif - #if ENABLED(PRINTER_EVENT_LEDS) && ENABLED(SDSUPPORT) && HAS_RESUME_CONTINUE - static bool lights_off_after_print; - #endif - static void dwell(millis_t time); private: diff --git a/Marlin/src/gcode/lcd/M0_M1.cpp b/Marlin/src/gcode/lcd/M0_M1.cpp index 7563f85ff6..b2b4ccd135 100644 --- a/Marlin/src/gcode/lcd/M0_M1.cpp +++ b/Marlin/src/gcode/lcd/M0_M1.cpp @@ -33,9 +33,8 @@ #include "../../sd/cardreader.h" -#if ENABLED(PRINTER_EVENT_LEDS) && ENABLED(SDSUPPORT) - bool GcodeSuite::lights_off_after_print; - #include "../../feature/leds/leds.h" +#if HAS_LEDS_OFF_FLAG + #include "../../feature/leds/printer_event_leds.h" #endif /** @@ -90,11 +89,8 @@ void GcodeSuite::M0_M1() { else while (wait_for_user) idle(); - #if ENABLED(PRINTER_EVENT_LEDS) && ENABLED(SDSUPPORT) - if (lights_off_after_print) { - leds.set_off(); - lights_off_after_print = false; - } + #if HAS_LEDS_OFF_FLAG + printerEventLEDs.onResumeAfterWait(); #endif #if ENABLED(ULTIPANEL) diff --git a/Marlin/src/gcode/queue.cpp b/Marlin/src/gcode/queue.cpp index 61d7c1d158..0c1b406f32 100644 --- a/Marlin/src/gcode/queue.cpp +++ b/Marlin/src/gcode/queue.cpp @@ -33,8 +33,8 @@ #include "../module/temperature.h" #include "../Marlin.h" -#if HAS_COLOR_LEDS - #include "../feature/leds/leds.h" +#if ENABLED(PRINTER_EVENT_LEDS) + #include "../feature/leds/printer_event_leds.h" #endif #if ENABLED(POWER_LOSS_RECOVERY) @@ -484,10 +484,8 @@ inline void get_serial_commands() { else { SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED); #if ENABLED(PRINTER_EVENT_LEDS) - LCD_MESSAGEPGM(MSG_INFO_COMPLETED_PRINTS); - leds.set_green(); + printerEventLEDs.onPrintCompleted(); #if HAS_RESUME_CONTINUE - gcode.lights_off_after_print = true; enqueue_and_echo_commands_P(PSTR("M0 S" #if ENABLED(NEWPANEL) "1800" @@ -495,9 +493,6 @@ inline void get_serial_commands() { "60" #endif )); - #else - safe_delay(2000); - leds.set_off(); #endif #endif // PRINTER_EVENT_LEDS } diff --git a/Marlin/src/gcode/temperature/M104_M109.cpp b/Marlin/src/gcode/temperature/M104_M109.cpp index 7ce80d9ae7..4868f2b4d3 100644 --- a/Marlin/src/gcode/temperature/M104_M109.cpp +++ b/Marlin/src/gcode/temperature/M104_M109.cpp @@ -31,10 +31,6 @@ #include "../../module/printcounter.h" #endif -#if ENABLED(PRINTER_EVENT_LEDS) - #include "../../feature/leds/leds.h" -#endif - #if ENABLED(SINGLENOZZLE) #include "../../module/tool_change.h" #endif diff --git a/Marlin/src/inc/Conditionals_LCD.h b/Marlin/src/inc/Conditionals_LCD.h index 0680306fd3..f5c086fed9 100644 --- a/Marlin/src/inc/Conditionals_LCD.h +++ b/Marlin/src/inc/Conditionals_LCD.h @@ -545,6 +545,7 @@ #define HAS_SOFTWARE_ENDSTOPS (ENABLED(MIN_SOFTWARE_ENDSTOPS) || ENABLED(MAX_SOFTWARE_ENDSTOPS)) #define HAS_RESUME_CONTINUE (ENABLED(EXTENSIBLE_UI) || ENABLED(NEWPANEL) || ENABLED(EMERGENCY_PARSER)) #define HAS_COLOR_LEDS (ENABLED(BLINKM) || ENABLED(RGB_LED) || ENABLED(RGBW_LED) || ENABLED(PCA9632) || ENABLED(NEOPIXEL_LED)) +#define HAS_LEDS_OFF_FLAG (ENABLED(PRINTER_EVENT_LEDS) && ENABLED(SDSUPPORT) && HAS_RESUME_CONTINUE) #define Z_MULTI_STEPPER_DRIVERS (ENABLED(Z_DUAL_STEPPER_DRIVERS) || ENABLED(Z_TRIPLE_STEPPER_DRIVERS)) #define Z_MULTI_ENDSTOPS (ENABLED(Z_DUAL_ENDSTOPS) || ENABLED(Z_TRIPLE_ENDSTOPS)) diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index 766d3e3132..ff0159c0d5 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -1571,7 +1571,9 @@ static_assert(X_MAX_LENGTH >= X_BED_SIZE && Y_MAX_LENGTH >= Y_BED_SIZE, * RGB_LED Requirements */ #define _RGB_TEST (PIN_EXISTS(RGB_LED_R) && PIN_EXISTS(RGB_LED_G) && PIN_EXISTS(RGB_LED_B)) -#if ENABLED(RGB_LED) +#if ENABLED(PRINTER_EVENT_LEDS) && !HAS_COLOR_LEDS + #error "PRINTER_EVENT_LEDS requires BLINKM, PCA9632, RGB_LED, RGBW_LED or NEOPIXEL_LED." +#elif ENABLED(RGB_LED) #if !_RGB_TEST #error "RGB_LED requires RGB_LED_R_PIN, RGB_LED_G_PIN, and RGB_LED_B_PIN." #elif ENABLED(RGBW_LED) @@ -1585,8 +1587,6 @@ static_assert(X_MAX_LENGTH >= X_BED_SIZE && Y_MAX_LENGTH >= Y_BED_SIZE, #if !(PIN_EXISTS(NEOPIXEL) && NEOPIXEL_PIXELS > 0) #error "NEOPIXEL_LED requires NEOPIXEL_PIN and NEOPIXEL_PIXELS." #endif -#elif ENABLED(PRINTER_EVENT_LEDS) && DISABLED(BLINKM) && DISABLED(PCA9632) && DISABLED(NEOPIXEL_LED) - #error "PRINTER_EVENT_LEDS requires BLINKM, PCA9632, RGB_LED, RGBW_LED or NEOPIXEL_LED." #endif /** diff --git a/Marlin/src/inc/Version.h b/Marlin/src/inc/Version.h index 161411774a..5b023079fc 100644 --- a/Marlin/src/inc/Version.h +++ b/Marlin/src/inc/Version.h @@ -19,9 +19,7 @@ * along with this program. If not, see . * */ - -#ifndef _VERSION_H_ -#define _VERSION_H_ +#pragma once #include "../core/macros.h" // for ENABLED @@ -97,5 +95,3 @@ #define WEBSITE_URL "http://marlinfw.org" #endif // USE_AUTOMATIC_VERSIONING - -#endif // _VERSION_H_ diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index 22f21809c5..322ba1c978 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -52,7 +52,7 @@ #endif #if ENABLED(PRINTER_EVENT_LEDS) - #include "../feature/leds/leds.h" + #include "../feature/leds/printer_event_leds.h" #endif #if HOTEND_USES_THERMISTOR @@ -250,13 +250,19 @@ uint8_t Temperature::soft_pwm_amount[HOTENDS]; #if HAS_PID_FOR_BOTH #define GHV(B,H) (hotend < 0 ? (B) : (H)) - #define SHV(S,B,H) if (hotend < 0) S##_bed = B; else S [hotend] = H; + #define SHV(S,B,H) do{ if (hotend < 0) S##_bed = B; else S [hotend] = H; }while(0) + #define ONHEATINGSTART() do{ if (hotend < 0) printerEventLEDs.onBedHeatingStart(); else printerEventLEDs.onHotendHeatingStart(); }while(0) + #define ONHEATING(S,C,T) do{ if (hotend < 0) printerEventLEDs.onBedHeating(S,C,T); else printerEventLEDs.onHotendHeating(S,C,T); }while(0) #elif ENABLED(PIDTEMPBED) #define GHV(B,H) B #define SHV(S,B,H) (S##_bed = B) + #define ONHEATINGSTART() printerEventLEDs.onBedHeatingStart() + #define ONHEATING(S,C,T) printerEventLEDs.onBedHeating(S,C,T) #else #define GHV(B,H) H #define SHV(S,B,H) (S [hotend] = H) + #define ONHEATINGSTART() printerEventLEDs.onHotendHeatingStart() + #define ONHEATING(S,C,T) printerEventLEDs.onHotendHeating(S,C,T) #endif #if WATCH_THE_BED || WATCH_HOTENDS @@ -303,6 +309,10 @@ uint8_t Temperature::soft_pwm_amount[HOTENDS]; SHV(soft_pwm_amount, bias = d = (MAX_BED_POWER) >> 1, bias = d = (PID_MAX) >> 1); wait_for_heatup = true; // Can be interrupted with M108 + #if ENABLED(PRINTER_EVENT_LEDS) + const float start_temp = GHV(current_temperature_bed, current_temperature[hotend]); + ONHEATINGSTART(); + #endif // PID Tuning loop while (wait_for_heatup) { @@ -317,6 +327,10 @@ uint8_t Temperature::soft_pwm_amount[HOTENDS]; NOLESS(max, current); NOMORE(min, current); + #if ENABLED(PRINTER_EVENT_LEDS) + ONHEATING(start_temp, current, target); + #endif + #if HAS_AUTO_FAN if (ELAPSED(ms, next_auto_fan_check_ms)) { checkExtruderAutoFans(); @@ -483,6 +497,9 @@ uint8_t Temperature::soft_pwm_amount[HOTENDS]; lcd_update(); } disable_all_heaters(); + #if ENABLED(PRINTER_EVENT_LEDS) + printerEventLEDs.onHeatersOff(); + #endif } #endif // HAS_PID_HEATING @@ -2439,7 +2456,7 @@ void Temperature::isr() { #if ENABLED(PRINTER_EVENT_LEDS) const float start_temp = degHotend(target_extruder); - uint8_t old_blue = 0; + printerEventLEDs.onHotendHeatingStart(); #endif float target_temp = -1.0, old_temp = 9999.0; @@ -2477,18 +2494,7 @@ void Temperature::isr() { #if ENABLED(PRINTER_EVENT_LEDS) // Gradually change LED strip from violet to red as nozzle heats up - if (!wants_to_cool) { - const uint8_t blue = map(constrain(temp, start_temp, target_temp), start_temp, target_temp, 255, 0); - if (blue != old_blue) { - old_blue = blue; - leds.set_color( - MakeLEDColor(255, 0, blue, 0, pixels.getBrightness()) - #if ENABLED(NEOPIXEL_IS_SEQUENTIAL) - , true - #endif - ); - } - } + if (!wants_to_cool) printerEventLEDs.onHotendHeating(start_temp, temp, target_temp); #endif #if TEMP_RESIDENCY_TIME > 0 @@ -2522,7 +2528,7 @@ void Temperature::isr() { if (wait_for_heatup) { lcd_reset_status(); #if ENABLED(PRINTER_EVENT_LEDS) - leds.set_white(); + printerEventLEDs.onHeated(); #endif } @@ -2568,7 +2574,7 @@ void Temperature::isr() { #if ENABLED(PRINTER_EVENT_LEDS) const float start_temp = degBed(); - uint8_t old_red = 127; + printerEventLEDs.onBedHeatingStart(); #endif do { @@ -2602,18 +2608,7 @@ void Temperature::isr() { #if ENABLED(PRINTER_EVENT_LEDS) // Gradually change LED strip from blue to violet as bed heats up - if (!wants_to_cool) { - const uint8_t red = map(constrain(temp, start_temp, target_temp), start_temp, target_temp, 0, 255); - if (red != old_red) { - old_red = red; - leds.set_color( - MakeLEDColor(red, 0, 255, 0, pixels.getBrightness()) - #if ENABLED(NEOPIXEL_IS_SEQUENTIAL) - , true - #endif - ); - } - } + if (!wants_to_cool) printerEventLEDs.onBedHeating(start_temp, temp, target_temp); #endif #if TEMP_BED_RESIDENCY_TIME > 0 diff --git a/buildroot/share/tests/megaatmega2560_tests b/buildroot/share/tests/megaatmega2560_tests index 81b6e843c8..8533d4f5ba 100755 --- a/buildroot/share/tests/megaatmega2560_tests +++ b/buildroot/share/tests/megaatmega2560_tests @@ -31,7 +31,8 @@ opt_set POWER_SUPPLY 1 opt_set GRID_MAX_POINTS_X 16 opt_enable PIDTEMPBED FIX_MOUNTED_PROBE Z_SAFE_HOMING \ REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT EEPROM_SETTINGS PINS_DEBUGGING \ - BLINKM PCA9632 RGB_LED NEOPIXEL_LED AUTO_POWER_CONTROL NOZZLE_PARK_FEATURE FILAMENT_RUNOUT_SENSOR \ + BLINKM PCA9632 RGB_LED NEOPIXEL_LED AUTO_POWER_CONTROL \ + NOZZLE_PARK_FEATURE FILAMENT_RUNOUT_SENSOR \ AUTO_BED_LEVELING_LINEAR Z_MIN_PROBE_REPEATABILITY_TEST DEBUG_LEVELING_FEATURE \ SKEW_CORRECTION SKEW_CORRECTION_FOR_Z SKEW_CORRECTION_GCODE opt_enable_adv FWRETRACT ARC_P_CIRCLES ADVANCED_PAUSE_FEATURE CNC_WORKSPACE_PLANES CNC_COORDINATE_SYSTEMS \