RGB Caselight (#20341)

This commit is contained in:
LinFor 2021-02-28 01:06:48 +03:00 committed by Scott Lahteine
parent 971fb8729a
commit 6183cd0731
14 changed files with 86 additions and 41 deletions

View File

@ -2650,6 +2650,7 @@
// Use a single NeoPixel LED for static (background) lighting // Use a single NeoPixel LED for static (background) lighting
//#define NEOPIXEL_BKGD_LED_INDEX 0 // Index of the LED to use //#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_COLOR { 255, 255, 255, 0 } // R, G, B, W
//#define NEOPIXEL_BKGD_ALWAYS_ON // Keep the backlight on when other NeoPixels are off
#endif #endif
/** /**

View File

@ -519,12 +519,17 @@
#define INVERT_CASE_LIGHT false // Set true if Case Light is ON when pin is LOW #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_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_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_MAX_PWM 128 // Limit PWM duty cycle (0-255)
//#define CASE_LIGHT_MENU // Add Case Light options to the LCD menu //#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. #if ENABLED(NEOPIXEL_LED)
//#define CASE_LIGHT_USE_NEOPIXEL // Use NeoPixel LED as case light, requires NEOPIXEL_LED. //#define CASE_LIGHT_USE_NEOPIXEL // Use NeoPixel LED as case light
#if ENABLED(CASE_LIGHT_USE_NEOPIXEL) #endif
#define CASE_LIGHT_NEOPIXEL_COLOR { 255, 255, 255, 255 } // { Red, Green, Blue, White } #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
#endif #endif

View File

@ -1136,10 +1136,7 @@ void setup() {
#endif #endif
#if ENABLED(CASE_LIGHT_ENABLE) #if ENABLED(CASE_LIGHT_ENABLE)
#if DISABLED(CASE_LIGHT_USE_NEOPIXEL) SETUP_RUN(caselight.init());
if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
#endif
SETUP_RUN(caselight.update_brightness());
#endif #endif
#if HAS_PRUSA_MMU1 #if HAS_PRUSA_MMU1

View File

@ -28,6 +28,10 @@
CaseLight caselight; CaseLight caselight;
#if CASE_LIGHT_IS_COLOR_LED
#include "leds/leds.h"
#endif
#if CASELIGHT_USES_BRIGHTNESS && !defined(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 #define CASE_LIGHT_DEFAULT_BRIGHTNESS 0 // For use on PWM pin as non-PWM just sets a default
#endif #endif
@ -38,10 +42,10 @@ CaseLight caselight;
bool CaseLight::on = CASE_LIGHT_DEFAULT_ON; bool CaseLight::on = CASE_LIGHT_DEFAULT_ON;
#if ENABLED(CASE_LIGHT_USE_NEOPIXEL) #if CASE_LIGHT_IS_COLOR_LED
LEDColor CaseLight::color = LEDColor CaseLight::color =
#ifdef CASE_LIGHT_NEOPIXEL_COLOR #ifdef CASE_LIGHT_DEFAULT_COLOR
CASE_LIGHT_NEOPIXEL_COLOR CASE_LIGHT_DEFAULT_COLOR
#else #else
{ 255, 255, 255, 255 } { 255, 255, 255, 255 }
#endif #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; const uint8_t i = on ? brightness : 0, n10ct = INVERT_CASE_LIGHT ? 255 - i : i;
#endif #endif
#if ENABLED(CASE_LIGHT_USE_NEOPIXEL) #if CASE_LIGHT_IS_COLOR_LED
leds.set_color( leds.set_color(
MakeLEDColor(color.r, color.g, color.b, color.w, n10ct), MakeLEDColor(color.r, color.g, color.b, color.w, n10ct),
false false
); );
#else // !CASE_LIGHT_USE_NEOPIXEL #else // !CASE_LIGHT_IS_COLOR_LED
#if CASELIGHT_USES_BRIGHTNESS #if CASELIGHT_USES_BRIGHTNESS
if (PWM_PIN(CASE_LIGHT_PIN)) if (pin_is_pwm())
analogWrite(pin_t(CASE_LIGHT_PIN), ( analogWrite(pin_t(CASE_LIGHT_PIN), (
#if CASE_LIGHT_MAX_PWM == 255 #if CASE_LIGHT_MAX_PWM == 255
n10ct n10ct
@ -96,7 +100,11 @@ void CaseLight::update(const bool sflag) {
WRITE(CASE_LIGHT_PIN, s ? HIGH : LOW); 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 #endif // CASE_LIGHT_ENABLE

View File

@ -21,10 +21,10 @@
*/ */
#pragma once #pragma once
#include "../inc/MarlinConfigPre.h" #include "../inc/MarlinConfig.h"
#if ENABLED(CASE_LIGHT_USE_NEOPIXEL) #if CASE_LIGHT_IS_COLOR_LED
#include "leds/leds.h" #include "leds/leds.h" // for LEDColor
#endif #endif
#if DISABLED(CASE_LIGHT_NO_BRIGHTNESS) || ENABLED(CASE_LIGHT_USE_NEOPIXEL) #if DISABLED(CASE_LIGHT_NO_BRIGHTNESS) || ENABLED(CASE_LIGHT_USE_NEOPIXEL)
@ -33,19 +33,25 @@
class CaseLight { class CaseLight {
public: public:
#if CASELIGHT_USES_BRIGHTNESS
static uint8_t brightness;
#endif
static bool on; 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 void update(const bool sflag);
static inline void update_brightness() { update(false); } static inline void update_brightness() { update(false); }
static inline void update_enabled() { update(true); } static inline void update_enabled() { update(true); }
private: private:
#if ENABLED(CASE_LIGHT_USE_NEOPIXEL) TERN_(CASE_LIGHT_IS_COLOR_LED, static LEDColor color);
static LEDColor color;
#endif
}; };
extern CaseLight caselight; extern CaseLight caselight;

View File

@ -42,6 +42,10 @@
#include "pca9533.h" #include "pca9533.h"
#endif #endif
#if ENABLED(CASE_LIGHT_USE_RGB_LED)
#include "../../feature/caselight.h"
#endif
#if ENABLED(LED_COLOR_PRESETS) #if ENABLED(LED_COLOR_PRESETS)
const LEDColor LEDLights::defaultLEDColor = MakeLEDColor( const LEDColor LEDLights::defaultLEDColor = MakeLEDColor(
LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE, LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE,
@ -85,9 +89,12 @@ void LEDLights::set_color(const LEDColor &incol
#ifdef NEOPIXEL_BKGD_LED_INDEX #ifdef NEOPIXEL_BKGD_LED_INDEX
if (NEOPIXEL_BKGD_LED_INDEX == nextLed) { if (NEOPIXEL_BKGD_LED_INDEX == nextLed) {
if (++nextLed >= neo.pixels()) nextLed = 0; neo.set_color_background();
if (++nextLed >= neo.pixels()) {
nextLed = 0;
return; return;
} }
}
#endif #endif
neo.set_brightness(incol.i); neo.set_brightness(incol.i);
@ -114,12 +121,13 @@ void LEDLights::set_color(const LEDColor &incol
// This variant uses 3-4 separate pins for the RGB(W) components. // This variant uses 3-4 separate pins for the RGB(W) components.
// If the pins can do PWM then their intensity will be set. // If the pins can do PWM then their intensity will be set.
#define UPDATE_RGBW(C,c) do { \ #define _UPDATE_RGBW(C,c) do { \
if (PWM_PIN(RGB_LED_##C##_PIN)) \ if (PWM_PIN(RGB_LED_##C##_PIN)) \
analogWrite(pin_t(RGB_LED_##C##_PIN), incol.c); \ analogWrite(pin_t(RGB_LED_##C##_PIN), c); \
else \ else \
WRITE(RGB_LED_##C##_PIN, incol.c ? HIGH : LOW); \ WRITE(RGB_LED_##C##_PIN, c ? HIGH : LOW); \
}while(0) }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); UPDATE_RGBW(R,r); UPDATE_RGBW(G,g); UPDATE_RGBW(B,b);
#if ENABLED(RGBW_LED) #if ENABLED(RGBW_LED)
UPDATE_RGBW(W,w); UPDATE_RGBW(W,w);

View File

@ -194,6 +194,8 @@ public:
#if ENABLED(LED_CONTROL_MENU) #if ENABLED(LED_CONTROL_MENU)
static void toggle(); // swap "off" with color 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); } static inline void update() { set_color(color); }
#endif #endif

View File

@ -60,7 +60,7 @@ void Marlin_NeoPixel::set_color(const uint32_t color) {
else { else {
for (uint16_t i = 0; i < pixels(); ++i) { for (uint16_t i = 0; i < pixels(); ++i) {
#ifdef NEOPIXEL_BKGD_LED_INDEX #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(); set_color_background();
continue; continue;
} }

View File

@ -106,7 +106,7 @@ void GcodeSuite::M115() {
// TOGGLE_LIGHTS (M355) // TOGGLE_LIGHTS (M355)
cap_line(PSTR("TOGGLE_LIGHTS"), ENABLED(CASE_LIGHT_ENABLE)); 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) // EMERGENCY_PARSER (M108, M112, M410, M876)
cap_line(PSTR("EMERGENCY_PARSER"), ENABLED(EMERGENCY_PARSER)); cap_line(PSTR("EMERGENCY_PARSER"), ENABLED(EMERGENCY_PARSER));

View File

@ -506,6 +506,15 @@
#define NEED_HEX_PRINT 1 #define NEED_HEX_PRINT 1
#endif #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 // Flag whether least_squares_fit.cpp is used
#if ANY(AUTO_BED_LEVELING_UBL, AUTO_BED_LEVELING_LINEAR, Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) #if ANY(AUTO_BED_LEVELING_UBL, AUTO_BED_LEVELING_LINEAR, Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
#define NEED_LSF 1 #define NEED_LSF 1

View File

@ -437,6 +437,8 @@
#error "DUAL_NOZZLE_DUPLICATION_MODE is now MULTI_NOZZLE_DUPLICATION." #error "DUAL_NOZZLE_DUPLICATION_MODE is now MULTI_NOZZLE_DUPLICATION."
#elif defined(MENU_ITEM_CASE_LIGHT) #elif defined(MENU_ITEM_CASE_LIGHT)
#error "MENU_ITEM_CASE_LIGHT is now CASE_LIGHT_MENU." #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) #elif defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
#error "ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED is now SD_ABORT_ON_ENDSTOP_HIT." #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) #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 * Case Light requirements
*/ */
#if ENABLED(CASE_LIGHT_ENABLE) #if NEED_CASE_LIGHT_PIN
#if !PIN_EXISTS(CASE_LIGHT) #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 #elif CASE_LIGHT_PIN == FAN_PIN
#error "CASE_LIGHT_PIN conflicts with FAN_PIN. Resolve before continuing." #error "CASE_LIGHT_PIN conflicts with FAN_PIN. Resolve before continuing."
#endif #endif

View File

@ -105,12 +105,14 @@
#if ENABLED(CASE_LIGHT_MENU) #if ENABLED(CASE_LIGHT_MENU)
#include "../../feature/caselight.h" #include "../../feature/caselight.h"
#define CASELIGHT_TOGGLE_ITEM() EDIT_ITEM(bool, MSG_CASE_LIGHT, (bool*)&caselight.on, caselight.update_enabled)
#if CASELIGHT_USES_BRIGHTNESS #if CASELIGHT_USES_BRIGHTNESS
void menu_case_light() { void menu_case_light() {
START_MENU(); START_MENU();
BACK_ITEM(MSG_CONFIGURATION); BACK_ITEM(MSG_CONFIGURATION);
EDIT_ITEM(percent, MSG_CASE_LIGHT_BRIGHTNESS, &caselight.brightness, 0, 255, caselight.update_brightness, true); 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(); END_MENU();
} }
#endif #endif
@ -155,13 +157,14 @@ void menu_led() {
// Set Case light on/off/brightness // Set Case light on/off/brightness
// //
#if ENABLED(CASE_LIGHT_MENU) #if ENABLED(CASE_LIGHT_MENU)
#if DISABLED(CASE_LIGHT_NO_BRIGHTNESS) #if CASELIGHT_USES_BRIGHTNESS
if (TERN1(CASE_LIGHT_USE_NEOPIXEL, PWM_PIN(CASE_LIGHT_PIN))) if (caselight.has_brightness())
SUBMENU(MSG_CASE_LIGHT, menu_case_light); SUBMENU(MSG_CASE_LIGHT, menu_case_light);
else else
#endif #endif
EDIT_ITEM(bool, MSG_CASE_LIGHT, (bool*)&caselight.on, caselight.update_enabled); CASELIGHT_TOGGLE_ITEM();
#endif #endif
END_MENU(); END_MENU();
} }

View File

@ -876,5 +876,9 @@
#undef BOARD_ST7920_DELAY_3 #undef BOARD_ST7920_DELAY_3
#endif #endif
#if !NEED_CASE_LIGHT_PIN
#undef CASE_LIGHT_PIN
#endif
#undef HAS_FREE_AUX2_PINS #undef HAS_FREE_AUX2_PINS
#undef DIAG_REMAPPED #undef DIAG_REMAPPED

View File

@ -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 \ ASSISTED_TRAMMING ASSISTED_TRAMMING_WIZARD REPORT_TRAMMING_MM ASSISTED_TRAMMING_WAIT_POSITION \
EEPROM_SETTINGS SDSUPPORT BINARY_FILE_TRANSFER \ 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 \ 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 \ 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 \ AUTO_BED_LEVELING_BILINEAR Z_MIN_PROBE_REPEATABILITY_TEST DEBUG_LEVELING_FEATURE \
SKEW_CORRECTION SKEW_CORRECTION_FOR_Z SKEW_CORRECTION_GCODE CALIBRATION_GCODE \ SKEW_CORRECTION SKEW_CORRECTION_FOR_Z SKEW_CORRECTION_GCODE CALIBRATION_GCODE \