Skip to content

Commit 3fcf3f6

Browse files
ellenspthinkyhead
andcommitted
♻️ LEDs refactor and extend (MarlinFirmware#21962)
Co-authored-by: Scott Lahteine <[email protected]>
1 parent a9fd276 commit 3fcf3f6

File tree

15 files changed

+158
-143
lines changed

15 files changed

+158
-143
lines changed

Marlin/Configuration.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,7 +2685,7 @@
26852685
//#define NEOPIXEL_LED
26862686
#if ENABLED(NEOPIXEL_LED)
26872687
#define NEOPIXEL_TYPE NEO_GRBW // NEO_GRBW / NEO_GRB - four/three channel driver type (defined in Adafruit_NeoPixel.h)
2688-
#define NEOPIXEL_PIN 4 // LED driving pin
2688+
//#define NEOPIXEL_PIN 4 // LED driving pin
26892689
//#define NEOPIXEL2_TYPE NEOPIXEL_TYPE
26902690
//#define NEOPIXEL2_PIN 5
26912691
#define NEOPIXEL_PIXELS 30 // Number of LEDs in the strip. (Longest strip when NEOPIXEL2_SEPARATE is disabled.)
@@ -2703,10 +2703,11 @@
27032703
//#define NEOPIXEL2_INSERIES // Default behavior is NeoPixel 2 in parallel
27042704
#endif
27052705

2706-
// Use a single NeoPixel LED for static (background) lighting
2707-
//#define NEOPIXEL_BKGD_LED_INDEX 0 // Index of the LED to use
2708-
//#define NEOPIXEL_BKGD_COLOR { 255, 255, 255, 0 } // R, G, B, W
2709-
//#define NEOPIXEL_BKGD_ALWAYS_ON // Keep the backlight on when other NeoPixels are off
2706+
// Use some of the NeoPixel LEDs for static (background) lighting
2707+
//#define NEOPIXEL_BKGD_INDEX_FIRST 0 // Index of the first background LED
2708+
//#define NEOPIXEL_BKGD_INDEX_LAST 5 // Index of the last background LED
2709+
//#define NEOPIXEL_BKGD_COLOR { 255, 255, 255, 0 } // R, G, B, W
2710+
//#define NEOPIXEL_BKGD_ALWAYS_ON // Keep the backlight on when other NeoPixels are off
27102711
#endif
27112712

27122713
/**

Marlin/src/feature/caselight.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ void CaseLight::update(const bool sflag) {
6565
#endif
6666

6767
#if CASE_LIGHT_IS_COLOR_LED
68-
69-
leds.set_color(MakeLEDColor(color.r, color.g, color.b, color.w, n10ct));
70-
68+
leds.set_color(LEDColor(color.r, color.g, color.b OPTARG(HAS_WHITE_LED, color.w), n10ct));
7169
#else // !CASE_LIGHT_IS_COLOR_LED
7270

7371
#if CASELIGHT_USES_BRIGHTNESS

Marlin/src/feature/leds/leds.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@
4747
#endif
4848

4949
#if ENABLED(LED_COLOR_PRESETS)
50-
const LEDColor LEDLights::defaultLEDColor = MakeLEDColor(
51-
LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE,
52-
LED_USER_PRESET_WHITE, LED_USER_PRESET_BRIGHTNESS
50+
const LEDColor LEDLights::defaultLEDColor = LEDColor(
51+
LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE
52+
OPTARG(HAS_WHITE_LED, LED_USER_PRESET_WHITE)
53+
OPTARG(NEOPIXEL_LED, LED_USER_PRESET_BRIGHTNESS)
5354
);
5455
#endif
5556

@@ -75,34 +76,35 @@ void LEDLights::setup() {
7576
}
7677

7778
void LEDLights::set_color(const LEDColor &incol
78-
OPTARG(NEOPIXEL_LED, bool isSequence/*=false*/)
79+
OPTARG(NEOPIXEL_IS_SEQUENTIAL, bool isSequence/*=false*/)
7980
) {
8081

8182
#if ENABLED(NEOPIXEL_LED)
8283

8384
const uint32_t neocolor = LEDColorWhite() == incol
8485
? neo.Color(NEO_WHITE)
85-
: neo.Color(incol.r, incol.g, incol.b, incol.w);
86-
static uint16_t nextLed = 0;
87-
88-
#ifdef NEOPIXEL_BKGD_LED_INDEX
89-
if (NEOPIXEL_BKGD_LED_INDEX == nextLed) {
90-
neo.set_color_background();
91-
if (++nextLed >= neo.pixels()) {
92-
nextLed = 0;
93-
return;
86+
: neo.Color(incol.r, incol.g, incol.b OPTARG(HAS_WHITE_LED, incol.w));
87+
88+
#if ENABLED(NEOPIXEL_IS_SEQUENTIAL)
89+
static uint16_t nextLed = 0;
90+
#ifdef NEOPIXEL_BKGD_INDEX_FIRST
91+
while (WITHIN(nextLed, NEOPIXEL_BKGD_INDEX_FIRST, NEOPIXEL_BKGD_INDEX_LAST)) {
92+
neo.reset_background_color();
93+
if (++nextLed >= neo.pixels()) { nextLed = 0; return; }
9494
}
95-
}
95+
#endif
9696
#endif
9797

9898
neo.set_brightness(incol.i);
9999

100-
if (isSequence) {
101-
neo.set_pixel_color(nextLed, neocolor);
102-
neo.show();
103-
if (++nextLed >= neo.pixels()) nextLed = 0;
104-
return;
105-
}
100+
#if ENABLED(NEOPIXEL_IS_SEQUENTIAL)
101+
if (isSequence) {
102+
neo.set_pixel_color(nextLed, neocolor);
103+
neo.show();
104+
if (++nextLed >= neo.pixels()) nextLed = 0;
105+
return;
106+
}
107+
#endif
106108

107109
neo.set_color(neocolor);
108110

@@ -167,9 +169,10 @@ void LEDLights::set_color(const LEDColor &incol
167169
#if ENABLED(NEOPIXEL2_SEPARATE)
168170

169171
#if ENABLED(NEO2_COLOR_PRESETS)
170-
const LEDColor LEDLights2::defaultLEDColor = MakeLEDColor(
171-
NEO2_USER_PRESET_RED, NEO2_USER_PRESET_GREEN, NEO2_USER_PRESET_BLUE,
172-
NEO2_USER_PRESET_WHITE, NEO2_USER_PRESET_BRIGHTNESS
172+
const LEDColor LEDLights2::defaultLEDColor = LEDColor(
173+
LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE
174+
OPTARG(HAS_WHITE_LED2, LED_USER_PRESET_WHITE)
175+
OPTARG(NEOPIXEL_LED, LED_USER_PRESET_BRIGHTNESS)
173176
);
174177
#endif
175178

@@ -188,7 +191,7 @@ void LEDLights::set_color(const LEDColor &incol
188191
void LEDLights2::set_color(const LEDColor &incol) {
189192
const uint32_t neocolor = LEDColorWhite() == incol
190193
? neo2.Color(NEO2_WHITE)
191-
: neo2.Color(incol.r, incol.g, incol.b, incol.w);
194+
: neo2.Color(incol.r, incol.g, incol.b OPTARG(HAS_WHITE_LED2, incol.w));
192195
neo2.set_brightness(incol.i);
193196
neo2.set_color(neocolor);
194197

Marlin/src/feature/leds/leds.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@
2929

3030
#include <string.h>
3131

32-
#if ENABLED(NEOPIXEL_LED)
33-
#include "neopixel.h"
34-
#endif
35-
3632
// A white component can be passed
37-
#if ANY(RGBW_LED, NEOPIXEL_LED, PCA9632_RGBW)
33+
#if EITHER(RGBW_LED, PCA9632_RGBW)
3834
#define HAS_WHITE_LED 1
3935
#endif
4036

37+
#if ENABLED(NEOPIXEL_LED)
38+
#define _NEOPIXEL_INCLUDE_
39+
#include "neopixel.h"
40+
#undef _NEOPIXEL_INCLUDE_
41+
#endif
42+
4143
/**
4244
* LEDcolor type for use with leds.set_color
4345
*/
@@ -84,9 +86,8 @@ typedef struct LEDColor {
8486
} LEDColor;
8587

8688
/**
87-
* Color helpers and presets
89+
* Color presets
8890
*/
89-
#define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B OPTARG(HAS_WHITE_LED, W) OPTARG(NEOPIXEL_LED, I))
9091

9192
#define LEDColorOff() LEDColor( 0, 0, 0)
9293
#define LEDColorRed() LEDColor(255, 0, 0)
@@ -114,15 +115,15 @@ class LEDLights {
114115
static void setup(); // init()
115116

116117
static void set_color(const LEDColor &color
117-
OPTARG(NEOPIXEL_LED, bool isSequence=false)
118+
OPTARG(NEOPIXEL_IS_SEQUENTIAL, bool isSequence=false)
118119
);
119120

120121
static inline void set_color(uint8_t r, uint8_t g, uint8_t b
121122
OPTARG(HAS_WHITE_LED, uint8_t w=0)
122123
OPTARG(NEOPIXEL_LED, uint8_t i=NEOPIXEL_BRIGHTNESS)
123-
OPTARG(NEOPIXEL_LED, bool isSequence=false)
124+
OPTARG(NEOPIXEL_IS_SEQUENTIAL, bool isSequence=false)
124125
) {
125-
set_color(MakeLEDColor(r, g, b, w, i) OPTARG(NEOPIXEL_LED, isSequence));
126+
set_color(LEDColor(r, g, b OPTARG(HAS_WHITE_LED, w) OPTARG(NEOPIXEL_LED, i)) OPTARG(NEOPIXEL_IS_SEQUENTIAL, isSequence));
126127
}
127128

128129
static inline void set_off() { set_color(LEDColorOff()); }
@@ -180,8 +181,14 @@ extern LEDLights leds;
180181

181182
static void set_color(const LEDColor &color);
182183

183-
inline void set_color(uint8_t r, uint8_t g, uint8_t b, uint8_t w=0, uint8_t i=NEOPIXEL2_BRIGHTNESS) {
184-
set_color(MakeLEDColor(r, g, b, w, i));
184+
static inline void set_color(uint8_t r, uint8_t g, uint8_t b
185+
OPTARG(HAS_WHITE_LED, uint8_t w=0)
186+
OPTARG(NEOPIXEL_LED, uint8_t i=NEOPIXEL_BRIGHTNESS)
187+
) {
188+
set_color(LEDColor(r, g, b
189+
OPTARG(HAS_WHITE_LED, w)
190+
OPTARG(NEOPIXEL_LED, i)
191+
));
185192
}
186193

187194
static inline void set_off() { set_color(LEDColorOff()); }

Marlin/src/feature/leds/neopixel.cpp

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#if ENABLED(NEOPIXEL_LED)
3030

31-
#include "neopixel.h"
31+
#include "leds.h"
3232

3333
#if EITHER(NEOPIXEL_STARTUP_TEST, NEOPIXEL2_STARTUP_TEST)
3434
#include "../../core/utility.h"
@@ -37,17 +37,21 @@
3737
Marlin_NeoPixel neo;
3838
int8_t Marlin_NeoPixel::neoindex;
3939

40-
Adafruit_NeoPixel Marlin_NeoPixel::adaneo1(NEOPIXEL_PIXELS, NEOPIXEL_PIN, NEOPIXEL_TYPE + NEO_KHZ800)
41-
#if CONJOINED_NEOPIXEL
42-
, Marlin_NeoPixel::adaneo2(NEOPIXEL_PIXELS, NEOPIXEL2_PIN, NEOPIXEL2_TYPE + NEO_KHZ800)
43-
#endif
44-
;
40+
Adafruit_NeoPixel Marlin_NeoPixel::adaneo1(NEOPIXEL_PIXELS, NEOPIXEL_PIN, NEOPIXEL_TYPE + NEO_KHZ800);
41+
#if CONJOINED_NEOPIXEL
42+
Adafruit_NeoPixel Marlin_NeoPixel::adaneo2(NEOPIXEL_PIXELS, NEOPIXEL2_PIN, NEOPIXEL2_TYPE + NEO_KHZ800);
43+
#endif
4544

46-
#ifdef NEOPIXEL_BKGD_LED_INDEX
45+
#ifdef NEOPIXEL_BKGD_INDEX_FIRST
46+
47+
void Marlin_NeoPixel::set_background_color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
48+
for (int background_led = NEOPIXEL_BKGD_INDEX_FIRST; background_led <= NEOPIXEL_BKGD_INDEX_LAST; background_led++)
49+
set_pixel_color(background_led, adaneo1.Color(r, g, b, w));
50+
}
4751

48-
void Marlin_NeoPixel::set_color_background() {
49-
uint8_t background_color[4] = NEOPIXEL_BKGD_COLOR;
50-
set_pixel_color(NEOPIXEL_BKGD_LED_INDEX, adaneo1.Color(background_color[0], background_color[1], background_color[2], background_color[3]));
52+
void Marlin_NeoPixel::reset_background_color() {
53+
constexpr uint8_t background_color[4] = NEOPIXEL_BKGD_COLOR;
54+
set_background_color(background_color[0], background_color[1], background_color[2], background_color[3]);
5155
}
5256

5357
#endif
@@ -59,9 +63,10 @@ void Marlin_NeoPixel::set_color(const uint32_t color) {
5963
}
6064
else {
6165
for (uint16_t i = 0; i < pixels(); ++i) {
62-
#ifdef NEOPIXEL_BKGD_LED_INDEX
63-
if (i == NEOPIXEL_BKGD_LED_INDEX && TERN(NEOPIXEL_BKGD_ALWAYS_ON, true, color != 0x000000)) {
64-
set_color_background();
66+
#ifdef NEOPIXEL_BKGD_INDEX_FIRST
67+
if (i == NEOPIXEL_BKGD_INDEX_FIRST && TERN(NEOPIXEL_BKGD_ALWAYS_ON, true, color != 0x000000)) {
68+
reset_background_color();
69+
i += NEOPIXEL_BKGD_INDEX_LAST - (NEOPIXEL_BKGD_INDEX_FIRST);
6570
continue;
6671
}
6772
#endif
@@ -90,35 +95,22 @@ void Marlin_NeoPixel::init() {
9095
safe_delay(500);
9196
set_color_startup(adaneo1.Color(0, 0, 255, 0)); // blue
9297
safe_delay(500);
98+
#if HAS_WHITE_LED
99+
set_color_startup(adaneo1.Color(0, 0, 0, 255)); // white
100+
safe_delay(500);
101+
#endif
93102
#endif
94103

95-
#ifdef NEOPIXEL_BKGD_LED_INDEX
96-
set_color_background();
97-
#endif
98-
99-
#if ENABLED(LED_USER_PRESET_STARTUP)
100-
set_color(adaneo1.Color(LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE, LED_USER_PRESET_WHITE));
101-
#else
102-
set_color(adaneo1.Color(0, 0, 0, 0));
104+
#ifdef NEOPIXEL_BKGD_INDEX_FIRST
105+
reset_background_color();
103106
#endif
104-
}
105107

106-
#if 0
107-
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) {
108-
const uint32_t color = adaneo1.Color(r, g, b, w);
109-
set_brightness(p);
110-
#if DISABLED(NEOPIXEL_IS_SEQUENTIAL)
111-
set_color(color);
112-
return false;
113-
#else
114-
static uint16_t nextLed = 0;
115-
set_pixel_color(nextLed, color);
116-
show();
117-
if (++nextLed >= pixels()) nextLed = 0;
118-
return true;
119-
#endif
108+
set_color(adaneo1.Color
109+
TERN(LED_USER_PRESET_STARTUP,
110+
(LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE, LED_USER_PRESET_WHITE),
111+
(0, 0, 0, 0))
112+
);
120113
}
121-
#endif
122114

123115
#if ENABLED(NEOPIXEL2_SEPARATE)
124116

@@ -158,13 +150,17 @@ bool Marlin_NeoPixel::set_led_color(const uint8_t r, const uint8_t g, const uint
158150
safe_delay(500);
159151
set_color_startup(adaneo.Color(0, 0, 255, 0)); // blue
160152
safe_delay(500);
153+
#if HAS_WHITE_LED2
154+
set_color_startup(adaneo.Color(0, 0, 0, 255)); // white
155+
safe_delay(500);
156+
#endif
161157
#endif
162158

163-
#if ENABLED(NEO2_USER_PRESET_STARTUP)
164-
set_color(adaneo.Color(NEO2_USER_PRESET_RED, NEO2_USER_PRESET_GREEN, NEO2_USER_PRESET_BLUE, NEO2_USER_PRESET_WHITE));
165-
#else
166-
set_color(adaneo.Color(0, 0, 0, 0));
167-
#endif
159+
set_color(adaneo.Color
160+
TERN(NEO2_USER_PRESET_STARTUP,
161+
(NEO2_USER_PRESET_RED, NEO2_USER_PRESET_GREEN, NEO2_USER_PRESET_BLUE, NEO2_USER_PRESET_WHITE),
162+
(0, 0, 0, 0))
163+
);
168164
}
169165

170166
#endif // NEOPIXEL2_SEPARATE

0 commit comments

Comments
 (0)