From 6183cd07312abd419208d7730df6683ae415563b Mon Sep 17 00:00:00 2001 From: LinFor Date: Sun, 28 Feb 2021 01:06:48 +0300 Subject: [PATCH] RGB Caselight (#20341) --- Marlin/Configuration.h | 1 + Marlin/Configuration_adv.h | 13 +++++++++---- Marlin/src/MarlinCore.cpp | 5 +---- Marlin/src/feature/caselight.cpp | 22 +++++++++++++++------- Marlin/src/feature/caselight.h | 26 ++++++++++++++++---------- Marlin/src/feature/leds/leds.cpp | 22 +++++++++++++++------- Marlin/src/feature/leds/leds.h | 2 ++ Marlin/src/feature/leds/neopixel.cpp | 2 +- Marlin/src/gcode/host/M115.cpp | 2 +- Marlin/src/inc/Conditionals_adv.h | 9 +++++++++ Marlin/src/inc/SanityCheck.h | 6 ++++-- Marlin/src/lcd/menu/menu_led.cpp | 11 +++++++---- Marlin/src/pins/pins_postprocess.h | 4 ++++ buildroot/tests/DUE-tests | 2 +- 14 files changed, 86 insertions(+), 41 deletions(-) diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h index 34f47bf48..c81309355 100644 --- a/Marlin/Configuration.h +++ b/Marlin/Configuration.h @@ -2650,6 +2650,7 @@ // Use a single NeoPixel LED for static (background) lighting //#define NEOPIXEL_BKGD_LED_INDEX 0 // Index of the LED to use //#define NEOPIXEL_BKGD_COLOR { 255, 255, 255, 0 } // R, G, B, W + //#define NEOPIXEL_BKGD_ALWAYS_ON // Keep the backlight on when other NeoPixels are off #endif /** diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 2fbcf259f..76a6a0bb5 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -519,12 +519,17 @@ #define INVERT_CASE_LIGHT false // Set true if Case Light is ON when pin is LOW #define CASE_LIGHT_DEFAULT_ON true // Set default power-up state on #define CASE_LIGHT_DEFAULT_BRIGHTNESS 105 // Set default power-up brightness (0-255, requires PWM pin) + //#define CASE_LIGHT_NO_BRIGHTNESS // Disable brightness control. Enable for non-PWM lighting. //#define CASE_LIGHT_MAX_PWM 128 // Limit PWM duty cycle (0-255) //#define CASE_LIGHT_MENU // Add Case Light options to the LCD menu - //#define CASE_LIGHT_NO_BRIGHTNESS // Disable brightness control. Enable for non-PWM lighting. - //#define CASE_LIGHT_USE_NEOPIXEL // Use NeoPixel LED as case light, requires NEOPIXEL_LED. - #if ENABLED(CASE_LIGHT_USE_NEOPIXEL) - #define CASE_LIGHT_NEOPIXEL_COLOR { 255, 255, 255, 255 } // { Red, Green, Blue, White } + #if ENABLED(NEOPIXEL_LED) + //#define CASE_LIGHT_USE_NEOPIXEL // Use NeoPixel LED as case light + #endif + #if EITHER(RGB_LED, RGBW_LED) + //#define CASE_LIGHT_USE_RGB_LED // Use RGB / RGBW LED as case light + #endif + #if EITHER(CASE_LIGHT_USE_NEOPIXEL, CASE_LIGHT_USE_RGB_LED) + #define CASE_LIGHT_DEFAULT_COLOR { 255, 255, 255, 255 } // { Red, Green, Blue, White } #endif #endif diff --git a/Marlin/src/MarlinCore.cpp b/Marlin/src/MarlinCore.cpp index aa92c9994..723276da2 100644 --- a/Marlin/src/MarlinCore.cpp +++ b/Marlin/src/MarlinCore.cpp @@ -1136,10 +1136,7 @@ void setup() { #endif #if ENABLED(CASE_LIGHT_ENABLE) - #if DISABLED(CASE_LIGHT_USE_NEOPIXEL) - if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN); - #endif - SETUP_RUN(caselight.update_brightness()); + SETUP_RUN(caselight.init()); #endif #if HAS_PRUSA_MMU1 diff --git a/Marlin/src/feature/caselight.cpp b/Marlin/src/feature/caselight.cpp index 0eba102a0..d4cc6b150 100644 --- a/Marlin/src/feature/caselight.cpp +++ b/Marlin/src/feature/caselight.cpp @@ -28,6 +28,10 @@ CaseLight caselight; +#if CASE_LIGHT_IS_COLOR_LED + #include "leds/leds.h" +#endif + #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 @@ -38,10 +42,10 @@ CaseLight caselight; bool CaseLight::on = CASE_LIGHT_DEFAULT_ON; -#if ENABLED(CASE_LIGHT_USE_NEOPIXEL) +#if CASE_LIGHT_IS_COLOR_LED LEDColor CaseLight::color = - #ifdef CASE_LIGHT_NEOPIXEL_COLOR - CASE_LIGHT_NEOPIXEL_COLOR + #ifdef CASE_LIGHT_DEFAULT_COLOR + CASE_LIGHT_DEFAULT_COLOR #else { 255, 255, 255, 255 } #endif @@ -71,17 +75,17 @@ void CaseLight::update(const bool sflag) { const uint8_t i = on ? brightness : 0, n10ct = INVERT_CASE_LIGHT ? 255 - i : i; #endif - #if ENABLED(CASE_LIGHT_USE_NEOPIXEL) + #if CASE_LIGHT_IS_COLOR_LED leds.set_color( MakeLEDColor(color.r, color.g, color.b, color.w, n10ct), false ); - #else // !CASE_LIGHT_USE_NEOPIXEL + #else // !CASE_LIGHT_IS_COLOR_LED #if CASELIGHT_USES_BRIGHTNESS - if (PWM_PIN(CASE_LIGHT_PIN)) + if (pin_is_pwm()) analogWrite(pin_t(CASE_LIGHT_PIN), ( #if CASE_LIGHT_MAX_PWM == 255 n10ct @@ -96,7 +100,11 @@ void CaseLight::update(const bool sflag) { WRITE(CASE_LIGHT_PIN, s ? HIGH : LOW); } - #endif // !CASE_LIGHT_USE_NEOPIXEL + #endif // !CASE_LIGHT_IS_COLOR_LED + + #if ENABLED(CASE_LIGHT_USE_RGB_LED) + if (leds.lights_on) leds.update(); else leds.set_off(); + #endif } #endif // CASE_LIGHT_ENABLE diff --git a/Marlin/src/feature/caselight.h b/Marlin/src/feature/caselight.h index 2198c85f2..25bcb486f 100644 --- a/Marlin/src/feature/caselight.h +++ b/Marlin/src/feature/caselight.h @@ -21,10 +21,10 @@ */ #pragma once -#include "../inc/MarlinConfigPre.h" +#include "../inc/MarlinConfig.h" -#if ENABLED(CASE_LIGHT_USE_NEOPIXEL) - #include "leds/leds.h" +#if CASE_LIGHT_IS_COLOR_LED + #include "leds/leds.h" // for LEDColor #endif #if DISABLED(CASE_LIGHT_NO_BRIGHTNESS) || ENABLED(CASE_LIGHT_USE_NEOPIXEL) @@ -33,19 +33,25 @@ class CaseLight { public: - #if CASELIGHT_USES_BRIGHTNESS - static uint8_t brightness; - #endif static bool on; + TERN_(CASELIGHT_USES_BRIGHTNESS, static uint8_t brightness); + + static bool pin_is_pwm() { return TERN0(NEED_CASE_LIGHT_PIN, PWM_PIN(CASE_LIGHT_PIN)); } + static bool has_brightness() { return TERN0(CASELIGHT_USES_BRIGHTNESS, TERN(CASE_LIGHT_USE_NEOPIXEL, true, pin_is_pwm())); } + + static void init() { + #if NEED_CASE_LIGHT_PIN + if (pin_is_pwm()) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN); + #endif + update_brightness(); + } static void update(const bool sflag); static inline void update_brightness() { update(false); } - static inline void update_enabled() { update(true); } + static inline void update_enabled() { update(true); } private: - #if ENABLED(CASE_LIGHT_USE_NEOPIXEL) - static LEDColor color; - #endif + TERN_(CASE_LIGHT_IS_COLOR_LED, static LEDColor color); }; extern CaseLight caselight; diff --git a/Marlin/src/feature/leds/leds.cpp b/Marlin/src/feature/leds/leds.cpp index c8cbdec33..ef0561a43 100644 --- a/Marlin/src/feature/leds/leds.cpp +++ b/Marlin/src/feature/leds/leds.cpp @@ -42,6 +42,10 @@ #include "pca9533.h" #endif +#if ENABLED(CASE_LIGHT_USE_RGB_LED) + #include "../../feature/caselight.h" +#endif + #if ENABLED(LED_COLOR_PRESETS) const LEDColor LEDLights::defaultLEDColor = MakeLEDColor( LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE, @@ -85,8 +89,11 @@ void LEDLights::set_color(const LEDColor &incol #ifdef NEOPIXEL_BKGD_LED_INDEX if (NEOPIXEL_BKGD_LED_INDEX == nextLed) { - if (++nextLed >= neo.pixels()) nextLed = 0; - return; + neo.set_color_background(); + if (++nextLed >= neo.pixels()) { + nextLed = 0; + return; + } } #endif @@ -114,12 +121,13 @@ void LEDLights::set_color(const LEDColor &incol // This variant uses 3-4 separate pins for the RGB(W) components. // If the pins can do PWM then their intensity will be set. - #define UPDATE_RGBW(C,c) do { \ - if (PWM_PIN(RGB_LED_##C##_PIN)) \ - analogWrite(pin_t(RGB_LED_##C##_PIN), incol.c); \ - else \ - WRITE(RGB_LED_##C##_PIN, incol.c ? HIGH : LOW); \ + #define _UPDATE_RGBW(C,c) do { \ + if (PWM_PIN(RGB_LED_##C##_PIN)) \ + analogWrite(pin_t(RGB_LED_##C##_PIN), c); \ + else \ + WRITE(RGB_LED_##C##_PIN, c ? HIGH : LOW); \ }while(0) + #define UPDATE_RGBW(C,c) _UPDATE_RGBW(C, TERN1(CASE_LIGHT_USE_RGB_LED, caselight.on) ? incol.c : 0) UPDATE_RGBW(R,r); UPDATE_RGBW(G,g); UPDATE_RGBW(B,b); #if ENABLED(RGBW_LED) UPDATE_RGBW(W,w); diff --git a/Marlin/src/feature/leds/leds.h b/Marlin/src/feature/leds/leds.h index d41c61144..c34eb57f4 100644 --- a/Marlin/src/feature/leds/leds.h +++ b/Marlin/src/feature/leds/leds.h @@ -194,6 +194,8 @@ public: #if ENABLED(LED_CONTROL_MENU) static void toggle(); // swap "off" with color + #endif + #if EITHER(LED_CONTROL_MENU, CASE_LIGHT_USE_RGB_LED) static inline void update() { set_color(color); } #endif diff --git a/Marlin/src/feature/leds/neopixel.cpp b/Marlin/src/feature/leds/neopixel.cpp index 27bbeb348..6f5ea0540 100644 --- a/Marlin/src/feature/leds/neopixel.cpp +++ b/Marlin/src/feature/leds/neopixel.cpp @@ -60,7 +60,7 @@ void Marlin_NeoPixel::set_color(const uint32_t color) { else { for (uint16_t i = 0; i < pixels(); ++i) { #ifdef NEOPIXEL_BKGD_LED_INDEX - if (i == NEOPIXEL_BKGD_LED_INDEX && color != 0x000000) { + if (i == NEOPIXEL_BKGD_LED_INDEX && TERN(ENABLED(NEOPIXEL_BKGD_ALWAYS_ON), true, color != 0x000000)) { set_color_background(); continue; } diff --git a/Marlin/src/gcode/host/M115.cpp b/Marlin/src/gcode/host/M115.cpp index 0501f3f60..40fdbd6d3 100644 --- a/Marlin/src/gcode/host/M115.cpp +++ b/Marlin/src/gcode/host/M115.cpp @@ -106,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, TERN0(CASELIGHT_USES_BRIGHTNESS, TERN(CASE_LIGHT_USE_NEOPIXEL, true, PWM_PIN(CASE_LIGHT_PIN))))); + cap_line(PSTR("CASE_LIGHT_BRIGHTNESS"), TERN0(CASE_LIGHT_ENABLE, caselight.has_brightness())); // EMERGENCY_PARSER (M108, M112, M410, M876) cap_line(PSTR("EMERGENCY_PARSER"), ENABLED(EMERGENCY_PARSER)); diff --git a/Marlin/src/inc/Conditionals_adv.h b/Marlin/src/inc/Conditionals_adv.h index c2ba04f0b..b8464df3d 100644 --- a/Marlin/src/inc/Conditionals_adv.h +++ b/Marlin/src/inc/Conditionals_adv.h @@ -506,6 +506,15 @@ #define NEED_HEX_PRINT 1 #endif +// Flags for Case Light having a color property or a single pin +#if ENABLED(CASE_LIGHT_ENABLE) + #if EITHER(CASE_LIGHT_USE_NEOPIXEL, CASE_LIGHT_USE_RGB_LED) + #define CASE_LIGHT_IS_COLOR_LED 1 + #else + #define NEED_CASE_LIGHT_PIN 1 + #endif +#endif + // Flag whether least_squares_fit.cpp is used #if ANY(AUTO_BED_LEVELING_UBL, AUTO_BED_LEVELING_LINEAR, Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) #define NEED_LSF 1 diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index f606ea5d9..9dc64e6bf 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -437,6 +437,8 @@ #error "DUAL_NOZZLE_DUPLICATION_MODE is now MULTI_NOZZLE_DUPLICATION." #elif defined(MENU_ITEM_CASE_LIGHT) #error "MENU_ITEM_CASE_LIGHT is now CASE_LIGHT_MENU." +#elif defined(CASE_LIGHT_NEOPIXEL_COLOR) + #error "CASE_LIGHT_NEOPIXEL_COLOR is now CASE_LIGHT_DEFAULT_COLOR." #elif defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) #error "ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED is now SD_ABORT_ON_ENDSTOP_HIT." #elif defined(LPC_SD_LCD) || defined(LPC_SD_ONBOARD) || defined(LPC_SD_CUSTOM_CABLE) @@ -1704,9 +1706,9 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal /** * Case Light requirements */ -#if ENABLED(CASE_LIGHT_ENABLE) +#if NEED_CASE_LIGHT_PIN #if !PIN_EXISTS(CASE_LIGHT) - #error "CASE_LIGHT_ENABLE requires CASE_LIGHT_PIN to be defined." + #error "CASE_LIGHT_ENABLE requires CASE_LIGHT_PIN, CASE_LIGHT_USE_NEOPIXEL, or CASE_LIGHT_USE_RGB_LED." #elif CASE_LIGHT_PIN == FAN_PIN #error "CASE_LIGHT_PIN conflicts with FAN_PIN. Resolve before continuing." #endif diff --git a/Marlin/src/lcd/menu/menu_led.cpp b/Marlin/src/lcd/menu/menu_led.cpp index de5750278..5ab5e8a9d 100644 --- a/Marlin/src/lcd/menu/menu_led.cpp +++ b/Marlin/src/lcd/menu/menu_led.cpp @@ -105,12 +105,14 @@ #if ENABLED(CASE_LIGHT_MENU) #include "../../feature/caselight.h" + #define CASELIGHT_TOGGLE_ITEM() EDIT_ITEM(bool, MSG_CASE_LIGHT, (bool*)&caselight.on, caselight.update_enabled) + #if CASELIGHT_USES_BRIGHTNESS void menu_case_light() { START_MENU(); BACK_ITEM(MSG_CONFIGURATION); EDIT_ITEM(percent, MSG_CASE_LIGHT_BRIGHTNESS, &caselight.brightness, 0, 255, caselight.update_brightness, true); - EDIT_ITEM(bool, MSG_CASE_LIGHT, (bool*)&caselight.on, caselight.update_enabled); + CASELIGHT_TOGGLE_ITEM(); END_MENU(); } #endif @@ -155,13 +157,14 @@ void menu_led() { // Set Case light on/off/brightness // #if ENABLED(CASE_LIGHT_MENU) - #if DISABLED(CASE_LIGHT_NO_BRIGHTNESS) - if (TERN1(CASE_LIGHT_USE_NEOPIXEL, PWM_PIN(CASE_LIGHT_PIN))) + #if CASELIGHT_USES_BRIGHTNESS + if (caselight.has_brightness()) SUBMENU(MSG_CASE_LIGHT, menu_case_light); else #endif - EDIT_ITEM(bool, MSG_CASE_LIGHT, (bool*)&caselight.on, caselight.update_enabled); + CASELIGHT_TOGGLE_ITEM(); #endif + END_MENU(); } diff --git a/Marlin/src/pins/pins_postprocess.h b/Marlin/src/pins/pins_postprocess.h index de70248d4..d37dd4352 100644 --- a/Marlin/src/pins/pins_postprocess.h +++ b/Marlin/src/pins/pins_postprocess.h @@ -876,5 +876,9 @@ #undef BOARD_ST7920_DELAY_3 #endif +#if !NEED_CASE_LIGHT_PIN + #undef CASE_LIGHT_PIN +#endif + #undef HAS_FREE_AUX2_PINS #undef DIAG_REMAPPED diff --git a/buildroot/tests/DUE-tests b/buildroot/tests/DUE-tests index d4c49a318..1e22ba337 100755 --- a/buildroot/tests/DUE-tests +++ b/buildroot/tests/DUE-tests @@ -18,7 +18,7 @@ opt_enable S_CURVE_ACCELERATION EEPROM_SETTINGS GCODE_MACROS \ ASSISTED_TRAMMING ASSISTED_TRAMMING_WIZARD REPORT_TRAMMING_MM ASSISTED_TRAMMING_WAIT_POSITION \ EEPROM_SETTINGS SDSUPPORT BINARY_FILE_TRANSFER \ BLINKM PCA9533 PCA9632 RGB_LED RGB_LED_R_PIN RGB_LED_G_PIN RGB_LED_B_PIN LED_CONTROL_MENU \ - NEOPIXEL_LED CASE_LIGHT_ENABLE CASE_LIGHT_USE_NEOPIXEL CASE_LIGHT_MENU \ + NEOPIXEL_LED CASE_LIGHT_ENABLE CASE_LIGHT_USE_NEOPIXEL CASE_LIGHT_USE_RGB_LED CASE_LIGHT_MENU \ NOZZLE_PARK_FEATURE ADVANCED_PAUSE_FEATURE FILAMENT_RUNOUT_DISTANCE_MM FILAMENT_RUNOUT_SENSOR \ AUTO_BED_LEVELING_BILINEAR Z_MIN_PROBE_REPEATABILITY_TEST DEBUG_LEVELING_FEATURE \ SKEW_CORRECTION SKEW_CORRECTION_FOR_Z SKEW_CORRECTION_GCODE CALIBRATION_GCODE \