Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LEDs refactor and extend #21962

Merged
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a4b350e
refactor leds so white is not alway needed.
May 21, 2021
43e9aef
Tidy up
May 22, 2021
48ec033
Remove test configs
May 22, 2021
ad1edbe
config cleanup
May 22, 2021
c720b70
Add NEOPIXEL_BKGD_LED_INDEX sanity check
May 22, 2021
2d4b565
Fix typo
May 22, 2021
30f2513
Merge branch 'bugfix-2.0.x' into bugfix-2.0.x-#refactor-leds
ellensp May 24, 2021
e4eab33
Fix typos
May 24, 2021
10328e0
remove redundant MakeLEDColor
May 24, 2021
bc9a88a
remove ,
May 24, 2021
e537fb4
add more OPTARG marcos
May 24, 2021
d8b95f8
addd OPTARG to caselight.cpp
May 24, 2021
e93fa89
add OPTARG to leds.cpp
May 24, 2021
4e786d6
add OPTARG to neopixels.h
May 24, 2021
aebe537
add OPTARG to printer_event_leds.cpp
May 24, 2021
eaa2752
cleanup
May 24, 2021
53d56ce
Merge 'bugfix-2.0.x' into pr/21962
thinkyhead May 24, 2021
cb3dfcf
tweaks
thinkyhead May 24, 2021
1991163
start/end => first/last
thinkyhead May 24, 2021
fd9f6b8
misc cleanups
thinkyhead May 24, 2021
24da90b
more cleanup
thinkyhead May 24, 2021
9563f19
Apply NEOPIXEL_IS_SEQUENTIAL in pel_set_rgb
thinkyhead May 24, 2021
28f561f
Arg only needed w/ NEOPIXEL_IS_SEQUENTIAL
thinkyhead May 24, 2021
4bb552f
Neo pin off by default
thinkyhead May 24, 2021
5a62c5e
link leds.h to neopixel.h
thinkyhead May 24, 2021
046f7fd
update test
thinkyhead May 24, 2021
8d98c42
fix DUE test
thinkyhead May 24, 2021
099d404
fix rambo test
thinkyhead May 24, 2021
23c43d7
fix sequential
thinkyhead May 24, 2021
d72701a
No nextLed needed
thinkyhead May 24, 2021
5bcad65
Add neo.set_background_color
thinkyhead May 24, 2021
d613c2d
update test
thinkyhead May 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -2708,10 +2708,11 @@
//#define NEOPIXEL2_INSERIES // Default behavior is NeoPixel 2 in parallel
#endif

// 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
// Use some of the NeoPixel LEDs for static (background) lighting
//#define NEOPIXEL_BKGD_INDEX_FIRST 0 // Index of the first background LED
//#define NEOPIXEL_BKGD_INDEX_LAST 5 // Index of the last background LED
//#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

/**
Expand Down
4 changes: 1 addition & 3 deletions Marlin/src/feature/caselight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ void CaseLight::update(const bool sflag) {
#endif

#if CASE_LIGHT_IS_COLOR_LED

leds.set_color(MakeLEDColor(color.r, color.g, color.b, color.w, n10ct));

leds.set_color(LEDColor(color.r, color.g, color.b OPTARG(HAS_WHITE_LED, color.w), n10ct));
#else // !CASE_LIGHT_IS_COLOR_LED

#if CASELIGHT_USES_BRIGHTNESS
Expand Down
22 changes: 12 additions & 10 deletions Marlin/src/feature/leds/leds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
#endif

#if ENABLED(LED_COLOR_PRESETS)
const LEDColor LEDLights::defaultLEDColor = MakeLEDColor(
LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE,
LED_USER_PRESET_WHITE, LED_USER_PRESET_BRIGHTNESS
const LEDColor LEDLights::defaultLEDColor = LEDColor(
LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE
OPTARG(HAS_WHITE_LED, LED_USER_PRESET_WHITE)
OPTARG(NEOPIXEL_LED, LED_USER_PRESET_BRIGHTNESS)
);
#endif

Expand Down Expand Up @@ -82,11 +83,11 @@ void LEDLights::set_color(const LEDColor &incol

const uint32_t neocolor = LEDColorWhite() == incol
? neo.Color(NEO_WHITE)
: neo.Color(incol.r, incol.g, incol.b, incol.w);
: neo.Color(incol.r, incol.g, incol.b OPTARG(HAS_WHITE_LED, incol.w));
static uint16_t nextLed = 0;

#ifdef NEOPIXEL_BKGD_LED_INDEX
if (NEOPIXEL_BKGD_LED_INDEX == nextLed) {
#ifdef NEOPIXEL_BKGD_INDEX_FIRST
while (WITHIN(nextLed, NEOPIXEL_BKGD_INDEX_FIRST, NEOPIXEL_BKGD_INDEX_LAST)) {
neo.set_color_background();
if (++nextLed >= neo.pixels()) {
nextLed = 0;
Expand Down Expand Up @@ -167,9 +168,10 @@ void LEDLights::set_color(const LEDColor &incol
#if ENABLED(NEOPIXEL2_SEPARATE)

#if ENABLED(NEO2_COLOR_PRESETS)
const LEDColor LEDLights2::defaultLEDColor = MakeLEDColor(
NEO2_USER_PRESET_RED, NEO2_USER_PRESET_GREEN, NEO2_USER_PRESET_BLUE,
NEO2_USER_PRESET_WHITE, NEO2_USER_PRESET_BRIGHTNESS
const LEDColor LEDLights2::defaultLEDColor = LEDColor(
LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE
OPTARG(HAS_WHITE_LED2, LED_USER_PRESET_WHITE)
OPTARG(NEOPIXEL_LED, LED_USER_PRESET_BRIGHTNESS)
);
#endif

Expand All @@ -188,7 +190,7 @@ void LEDLights::set_color(const LEDColor &incol
void LEDLights2::set_color(const LEDColor &incol) {
const uint32_t neocolor = LEDColorWhite() == incol
? neo2.Color(NEO2_WHITE)
: neo2.Color(incol.r, incol.g, incol.b, incol.w);
: neo2.Color(incol.r, incol.g, incol.b OPTARG(HAS_WHITE_LED2, incol.w));
neo2.set_brightness(incol.i);
neo2.set_color(neocolor);

Expand Down
17 changes: 11 additions & 6 deletions Marlin/src/feature/leds/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#endif

// A white component can be passed
#if ANY(RGBW_LED, NEOPIXEL_LED, PCA9632_RGBW)
#if EITHER(RGBW_LED, PCA9632_RGBW)
#define HAS_WHITE_LED 1
#endif

Expand Down Expand Up @@ -84,9 +84,8 @@ typedef struct LEDColor {
} LEDColor;

/**
* Color helpers and presets
* Color presets
*/
#define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B OPTARG(HAS_WHITE_LED, W) OPTARG(NEOPIXEL_LED, I))

#define LEDColorOff() LEDColor( 0, 0, 0)
#define LEDColorRed() LEDColor(255, 0, 0)
Expand Down Expand Up @@ -122,7 +121,7 @@ class LEDLights {
OPTARG(NEOPIXEL_LED, uint8_t i=NEOPIXEL_BRIGHTNESS)
OPTARG(NEOPIXEL_LED, bool isSequence=false)
) {
set_color(MakeLEDColor(r, g, b, w, i) OPTARG(NEOPIXEL_LED, isSequence));
set_color(LEDColor(r, g, b OPTARG(HAS_WHITE_LED, w) OPTARG(NEOPIXEL_LED, i)) OPTARG(NEOPIXEL_LED, isSequence));
}

static inline void set_off() { set_color(LEDColorOff()); }
Expand Down Expand Up @@ -180,8 +179,14 @@ extern LEDLights leds;

static void set_color(const LEDColor &color);

inline void set_color(uint8_t r, uint8_t g, uint8_t b, uint8_t w=0, uint8_t i=NEOPIXEL2_BRIGHTNESS) {
set_color(MakeLEDColor(r, g, b, w, i));
static inline void set_color(uint8_t r, uint8_t g, uint8_t b
OPTARG(HAS_WHITE_LED, uint8_t w=0)
OPTARG(NEOPIXEL_LED, uint8_t i=NEOPIXEL_BRIGHTNESS)
) {
set_color(LEDColor(r, g, b
OPTARG(HAS_WHITE_LED, w)
OPTARG(NEOPIXEL_LED, i)
));
}

static inline void set_off() { set_color(LEDColorOff()); }
Expand Down
72 changes: 35 additions & 37 deletions Marlin/src/feature/leds/neopixel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@
Marlin_NeoPixel neo;
int8_t Marlin_NeoPixel::neoindex;

Adafruit_NeoPixel Marlin_NeoPixel::adaneo1(NEOPIXEL_PIXELS, NEOPIXEL_PIN, NEOPIXEL_TYPE + NEO_KHZ800)
#if CONJOINED_NEOPIXEL
, Marlin_NeoPixel::adaneo2(NEOPIXEL_PIXELS, NEOPIXEL2_PIN, NEOPIXEL2_TYPE + NEO_KHZ800)
#endif
;
Adafruit_NeoPixel Marlin_NeoPixel::adaneo1(NEOPIXEL_PIXELS, NEOPIXEL_PIN, NEOPIXEL_TYPE + NEO_KHZ800);
#if CONJOINED_NEOPIXEL
Adafruit_NeoPixel Marlin_NeoPixel::adaneo2(NEOPIXEL_PIXELS, NEOPIXEL2_PIN, NEOPIXEL2_TYPE + NEO_KHZ800);
#endif

#ifdef NEOPIXEL_BKGD_LED_INDEX
#ifdef NEOPIXEL_BKGD_INDEX_FIRST

void Marlin_NeoPixel::set_color_background() {
uint8_t background_color[4] = NEOPIXEL_BKGD_COLOR;
set_pixel_color(NEOPIXEL_BKGD_LED_INDEX, adaneo1.Color(background_color[0], background_color[1], background_color[2], background_color[3]));
for (int background_led = NEOPIXEL_BKGD_INDEX_FIRST; background_led <= NEOPIXEL_BKGD_INDEX_LAST; background_led++) {
set_pixel_color(background_led, adaneo1.Color(background_color[0], background_color[1], background_color[2], background_color[3]));
}
}

#endif
Expand All @@ -59,9 +60,10 @@ 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 && TERN(NEOPIXEL_BKGD_ALWAYS_ON, true, color != 0x000000)) {
#ifdef NEOPIXEL_BKGD_INDEX_FIRST
if (i == NEOPIXEL_BKGD_INDEX_FIRST && TERN(NEOPIXEL_BKGD_ALWAYS_ON, true, color != 0x000000)) {
set_color_background();
i += NEOPIXEL_BKGD_INDEX_LAST - (NEOPIXEL_BKGD_INDEX_FIRST);
continue;
}
#endif
Expand Down Expand Up @@ -90,35 +92,25 @@ void Marlin_NeoPixel::init() {
safe_delay(500);
set_color_startup(adaneo1.Color(0, 0, 255, 0)); // blue
safe_delay(500);
#endif
#if HAS_WHITE_LED
set_color_startup(adaneo1.Color(0, 0, 0, 255)); // white
safe_delay(500);
#endif

#ifdef NEOPIXEL_BKGD_LED_INDEX
set_color_background();
#endif

#if ENABLED(LED_USER_PRESET_STARTUP)
set_color(adaneo1.Color(LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE, LED_USER_PRESET_WHITE));
#else
set_color(adaneo1.Color(0, 0, 0, 0));
#ifdef NEOPIXEL_BKGD_INDEX_FIRST
set_color_background();
#endif
}

#if 0
bool Marlin_NeoPixel::set_led_color(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t w, const uint8_t p) {
const uint32_t color = adaneo1.Color(r, g, b, w);
set_brightness(p);
#if DISABLED(NEOPIXEL_IS_SEQUENTIAL)
set_color(color);
return false;
#else
static uint16_t nextLed = 0;
set_pixel_color(nextLed, color);
show();
if (++nextLed >= pixels()) nextLed = 0;
return true;
#endif
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
set_color(adaneo1.Color(
#if ENABLED(LED_USER_PRESET_STARTUP)
LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE, LED_USER_PRESET_WHITE
#else
0, 0, 0, 0
#endif
));
}
#endif

#if ENABLED(NEOPIXEL2_SEPARATE)

Expand Down Expand Up @@ -158,13 +150,19 @@ bool Marlin_NeoPixel::set_led_color(const uint8_t r, const uint8_t g, const uint
safe_delay(500);
set_color_startup(adaneo.Color(0, 0, 255, 0)); // blue
safe_delay(500);
#if HAS_WHITE_LED2
set_color_startup(adaneo.Color(0, 0, 0, 255)); // white
safe_delay(500);
#endif
#endif

#if ENABLED(NEO2_USER_PRESET_STARTUP)
set_color(adaneo.Color(NEO2_USER_PRESET_RED, NEO2_USER_PRESET_GREEN, NEO2_USER_PRESET_BLUE, NEO2_USER_PRESET_WHITE));
#else
set_color(adaneo.Color(0, 0, 0, 0));
#endif
set_color(adaneo.Color(
#if ENABLED(NEO2_USER_PRESET_STARTUP)
NEO2_USER_PRESET_RED, NEO2_USER_PRESET_GREEN, NEO2_USER_PRESET_BLUE, NEO2_USER_PRESET_WHITE
#else
0, 0, 0, 0
#endif
));
}

#endif // NEOPIXEL2_SEPARATE
Expand Down
53 changes: 23 additions & 30 deletions Marlin/src/feature/leds/neopixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@
#define CONJOINED_NEOPIXEL 1
#endif

#if NEOPIXEL_TYPE == NEO_RGB || NEOPIXEL_TYPE == NEO_RBG || NEOPIXEL_TYPE == NEO_GRB || NEOPIXEL_TYPE == NEO_GBR || NEOPIXEL_TYPE == NEO_BRG || NEOPIXEL_TYPE == NEO_BGR
#define _NEO_IS_RGB(N) (N == NEO_RGB || N == NEO_RBG || N == NEO_GRB || N == NEO_GBR || N == NEO_BRG || N == NEO_BGR)

#if _NEO_IS_RGB(NEOPIXEL_TYPE)
#define NEOPIXEL_IS_RGB 1
#define NEO_WHITE 255, 255, 255
#else
#define NEOPIXEL_IS_RGBW 1
#define NEO_WHITE 0, 0, 0, 255
#endif

#if NEOPIXEL_IS_RGB
#define NEO_WHITE 255, 255, 255, 0
#else
#define NEO_WHITE 0, 0, 0, 255
// A white component can be passed
#if ANY(RGBW_LED, NEOPIXEL_IS_RGBW, PCA9632_RGBW)
#define HAS_WHITE_LED 1
#endif

// ------------------------
Expand All @@ -64,11 +67,10 @@

class Marlin_NeoPixel {
private:
static Adafruit_NeoPixel adaneo1
#if CONJOINED_NEOPIXEL
, adaneo2
#endif
;
static Adafruit_NeoPixel adaneo1;
#if CONJOINED_NEOPIXEL
static Adafruit_NeoPixel adaneo2;
#endif

public:
static int8_t neoindex;
Expand All @@ -78,7 +80,7 @@ class Marlin_NeoPixel {

static void set_color(const uint32_t c);

#ifdef NEOPIXEL_BKGD_LED_INDEX
#ifdef NEOPIXEL_BKGD_INDEX_FIRST
static void set_color_background();
#endif

Expand All @@ -93,9 +95,7 @@ class Marlin_NeoPixel {
else adaneo1.setPixelColor(n, c);
#else
adaneo1.setPixelColor(n, c);
#if MULTIPLE_NEOPIXEL_TYPES
adaneo2.setPixelColor(n, c);
#endif
TERN_(MULTIPLE_NEOPIXEL_TYPES, adaneo2.setPixelColor(n, c));
#endif
}

Expand All @@ -120,15 +120,13 @@ class Marlin_NeoPixel {
TERN_(HAS_PAUSE_SERVO_OUTPUT, RESUME_SERVO_OUTPUT());
}

#if 0
bool set_led_color(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t w, const uint8_t p);
#endif

// Accessors
static inline uint16_t pixels() { TERN(NEOPIXEL2_INSERIES, return adaneo1.numPixels() * 2, return adaneo1.numPixels()); }
static inline uint16_t pixels() { return adaneo1.numPixels() * TERN1(NEOPIXEL2_INSERIES, 2); }

static inline uint8_t brightness() { return adaneo1.getBrightness(); }
static inline uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
return adaneo1.Color(r, g, b, w);

static inline uint32_t Color(uint8_t r, uint8_t g, uint8_t b OPTARG(HAS_WHITE_LED, uint8_t w)) {
return adaneo1.Color(r, g, b OPTARG(HAS_WHITE_LED, w));
}
};

Expand All @@ -137,15 +135,12 @@ extern Marlin_NeoPixel neo;
// Neo pixel channel 2
#if ENABLED(NEOPIXEL2_SEPARATE)

#if NEOPIXEL2_TYPE == NEO_RGB || NEOPIXEL2_TYPE == NEO_RBG || NEOPIXEL2_TYPE == NEO_GRB || NEOPIXEL2_TYPE == NEO_GBR || NEOPIXEL2_TYPE == NEO_BRG || NEOPIXEL2_TYPE == NEO_BGR
#if _NEO_IS_RGB(NEOPIXEL2_TYPE)
#define NEOPIXEL2_IS_RGB 1
#define NEO2_WHITE 255, 255, 255
#else
#define NEOPIXEL2_IS_RGBW 1
#endif

#if NEOPIXEL2_IS_RGB
#define NEO2_WHITE 255, 255, 255, 0
#else
#define HAS_WHITE_LED2 1 // A white component can be passed for NEOPIXEL2
#define NEO2_WHITE 0, 0, 0, 255
#endif

Expand All @@ -172,9 +167,7 @@ extern Marlin_NeoPixel neo;
// Accessors
static inline uint16_t pixels() { return adaneo.numPixels();}
static inline uint8_t brightness() { return adaneo.getBrightness(); }
static inline uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
return adaneo.Color(r, g, b, w);
}
static inline uint32_t Color(uint8_t r, uint8_t g, uint8_t b OPTARG(HAS_WHITE_LED2, uint8_t w)) { return adaneo.Color(r, g, b OPTARG(HAS_WHITE_LED2, w)); }
};

extern Marlin_NeoPixel2 neo2;
Expand Down
15 changes: 5 additions & 10 deletions Marlin/src/feature/leds/printer_event_leds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,8 @@ PrinterEventLEDs printerEventLEDs;
return (uint8_t)map(constrain(current, start, target), start, target, 0, 255);
}

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, neo.brightness())
#if ENABLED(NEOPIXEL_IS_SEQUENTIAL)
, true
#endif
);
inline void pel_set_rgb(const uint8_t r, const uint8_t g, const uint8_t b OPTARG(HAS_WHITE_LED, const uint8_t w)) {
leds.set_color(LEDColor(r, g, b OPTARG(HAS_WHITE_LED, w) OPTARG(NEOPIXEL_LED, neo.brightness())));
}

#endif
Expand All @@ -62,7 +57,7 @@ PrinterEventLEDs printerEventLEDs;
const uint8_t blue = pel_intensity(start, current, target);
if (blue != old_intensity) {
old_intensity = blue;
pel_set_rgb(255, 0, 255 - blue);
pel_set_rgb(255, 0, 255 - blue OPTARG(HAS_WHITE_LED, 0));
}
}

Expand All @@ -74,7 +69,7 @@ PrinterEventLEDs printerEventLEDs;
const uint8_t red = pel_intensity(start, current, target);
if (red != old_intensity) {
old_intensity = red;
pel_set_rgb(red, 0, 255);
pel_set_rgb(red, 0, 255 OPTARG(HAS_WHITE_LED, 0));
}
}

Expand All @@ -86,7 +81,7 @@ PrinterEventLEDs printerEventLEDs;
const uint8_t green = pel_intensity(start, current, target);
if (green != old_intensity) {
old_intensity = green;
pel_set_rgb(255, green, 255);
pel_set_rgb(255, green, 255 OPTARG(HAS_WHITE_LED, 0));
}
}

Expand Down
Loading