Prusa MINI Firmware overview
leds.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4  *
5  * Based on Sprinter and grbl.
6  * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #pragma once
23 
24 /**
25  * leds.h - Marlin general RGB LED support
26  */
27 
28 #include "../../inc/MarlinConfigPre.h"
29 
30 #include <string.h>
31 
32 #if ENABLED(NEOPIXEL_LED)
33  #include "neopixel.h"
34 #endif
35 
36 #define HAS_WHITE_LED EITHER(RGBW_LED, NEOPIXEL_LED)
37 
38 /**
39  * LEDcolor type for use with leds.set_color
40  */
41 typedef struct LEDColor {
43  #if HAS_WHITE_LED
44  , w
45  #if ENABLED(NEOPIXEL_LED)
46  , i
47  #endif
48  #endif
49  ;
50 
51  LEDColor() : r(255), g(255), b(255)
53  , w(255)
54  #if ENABLED(NEOPIXEL_LED)
55  , i(NEOPIXEL_BRIGHTNESS)
56  #endif
57  #endif
58  {}
59 
61  #if HAS_WHITE_LED
62  , uint8_t w=0
63  #if ENABLED(NEOPIXEL_LED)
64  , uint8_t i=NEOPIXEL_BRIGHTNESS
65  #endif
66  #endif
67  ) : r(r), g(g), b(b)
69  , w(w)
70  #if ENABLED(NEOPIXEL_LED)
71  , i(i)
72  #endif
73  #endif
74  {}
75 
76  LEDColor(const uint8_t (&rgbw)[4]) : r(rgbw[0]), g(rgbw[1]), b(rgbw[2])
78  , w(rgbw[3])
79  #if ENABLED(NEOPIXEL_LED)
80  , i(NEOPIXEL_BRIGHTNESS)
81  #endif
82  #endif
83  {}
84 
85  LEDColor& operator=(const uint8_t (&rgbw)[4]) {
86  r = rgbw[0]; g = rgbw[1]; b = rgbw[2];
87  #if HAS_WHITE_LED
88  w = rgbw[3];
89  #endif
90  return *this;
91  }
92 
93  LEDColor& operator=(const LEDColor &right) {
94  if (this != &right) memcpy(this, &right, sizeof(LEDColor));
95  return *this;
96  }
97 
98  bool operator==(const LEDColor &right) {
99  if (this == &right) return true;
100  return 0 == memcmp(this, &right, sizeof(LEDColor));
101  }
102 
103  bool operator!=(const LEDColor &right) { return !operator==(right); }
104 
105  bool is_off() const {
106  return 3 > r + g + b
107  #if HAS_WHITE_LED
108  + w
109  #endif
110  ;
111  }
112 } LEDColor;
113 
114 /**
115  * Color helpers and presets
116  */
117 #if HAS_WHITE_LED
118  #if ENABLED(NEOPIXEL_LED)
119  #define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B, W, I)
120  #else
121  #define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B, W)
122  #endif
123 #else
124  #define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B)
125 #endif
126 
127 #define LEDColorOff() LEDColor( 0, 0, 0)
128 #define LEDColorRed() LEDColor(255, 0, 0)
129 #if ENABLED(LED_COLORS_REDUCE_GREEN)
130  #define LEDColorOrange() LEDColor(255, 25, 0)
131  #define LEDColorYellow() LEDColor(255, 75, 0)
132 #else
133  #define LEDColorOrange() LEDColor(255, 80, 0)
134  #define LEDColorYellow() LEDColor(255, 255, 0)
135 #endif
136 #define LEDColorGreen() LEDColor( 0, 255, 0)
137 #define LEDColorBlue() LEDColor( 0, 0, 255)
138 #define LEDColorIndigo() LEDColor( 0, 255, 255)
139 #define LEDColorViolet() LEDColor(255, 0, 255)
140 #if HAS_WHITE_LED
141  #define LEDColorWhite() LEDColor( 0, 0, 0, 255)
142 #else
143  #define LEDColorWhite() LEDColor(255, 255, 255)
144 #endif
145 
146 class LEDLights {
147 public:
148  LEDLights() {} // ctor
149 
150  static void setup(); // init()
151 
152  static void set_color(const LEDColor &color
153  #if ENABLED(NEOPIXEL_LED)
154  , bool isSequence=false
155  #endif
156  );
157 
158  inline void set_color(uint8_t r, uint8_t g, uint8_t b
159  #if HAS_WHITE_LED
160  , uint8_t w=0
161  #if ENABLED(NEOPIXEL_LED)
162  , uint8_t i=NEOPIXEL_BRIGHTNESS
163  #endif
164  #endif
165  #if ENABLED(NEOPIXEL_LED)
166  , bool isSequence=false
167  #endif
168  ) {
169  set_color(MakeLEDColor(r, g, b, w, i)
170  #if ENABLED(NEOPIXEL_LED)
171  , isSequence
172  #endif
173  );
174  }
175 
176  static inline void set_off() { set_color(LEDColorOff()); }
177  static inline void set_green() { set_color(LEDColorGreen()); }
178  static inline void set_white() { set_color(LEDColorWhite()); }
179 
180  #if ENABLED(LED_COLOR_PRESETS)
181  static const LEDColor defaultLEDColor;
182  static inline void set_default() { set_color(defaultLEDColor); }
183  static inline void set_red() { set_color(LEDColorRed()); }
184  static inline void set_orange() { set_color(LEDColorOrange()); }
185  static inline void set_yellow() { set_color(LEDColorYellow()); }
186  static inline void set_blue() { set_color(LEDColorBlue()); }
187  static inline void set_indigo() { set_color(LEDColorIndigo()); }
188  static inline void set_violet() { set_color(LEDColorViolet()); }
189  #endif
190 
191  #if ENABLED(PRINTER_EVENT_LEDS)
192  static inline LEDColor get_color() { return lights_on ? color : LEDColorOff(); }
193  #endif
194 
195  #if EITHER(LED_CONTROL_MENU, PRINTER_EVENT_LEDS)
196  static LEDColor color; // last non-off color
197  static bool lights_on; // the last set color was "on"
198  #endif
199 
200  #if ENABLED(LED_CONTROL_MENU)
201  static void toggle(); // swap "off" with color
202  static inline void update() { set_color(color); }
203  #endif
204 
205  #ifdef LED_BACKLIGHT_TIMEOUT
206  private:
207  static millis_t led_off_time;
208  public:
209  static inline void reset_timeout(const millis_t &ms) {
210  led_off_time = ms + LED_BACKLIGHT_TIMEOUT;
211  if (!lights_on) set_default();
212  }
213  static void update_timeout(const bool power_on);
214  #endif
215 };
216 
217 extern LEDLights leds;
Marlin_NeoPixel::init
static void init()
LEDLights::set_color
static void set_color(const LEDColor &color)
LEDColor::operator=
LEDColor & operator=(const LEDColor &right)
Definition: leds.h:93
MakeLEDColor
#define MakeLEDColor(R, G, B, W, I)
Definition: leds.h:124
blinkm_set_led_color
void blinkm_set_led_color(const LEDColor &color)
LEDLights::set_color
void set_color(uint8_t r, uint8_t g, uint8_t b)
Definition: leds.h:158
Marlin_NeoPixel::Color
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w)
Definition: neopixel.h:115
LEDColor::r
uint8_t r
Definition: leds.h:42
Marlin_NeoPixel::set_brightness
static void set_brightness(const uint8_t b)
Definition: neopixel.h:88
LEDColorOrange
#define LEDColorOrange()
Definition: leds.h:133
LEDLights
Definition: leds.h:146
LEDColorGreen
#define LEDColorGreen()
Definition: leds.h:136
LEDColorRed
#define LEDColorRed()
Definition: leds.h:128
pca9632.h
LEDColorBlue
#define LEDColorBlue()
Definition: leds.h:137
LEDColor::operator==
bool operator==(const LEDColor &right)
Definition: leds.h:98
NEO_WHITE
#define NEO_WHITE
Definition: neopixel.h:47
LEDLights::set_white
static void set_white()
Definition: leds.h:178
Marlin_NeoPixel::set_pixel_color
static void set_pixel_color(const uint16_t n, const uint32_t c)
Definition: neopixel.h:81
SET_PWM
#define SET_PWM(IO)
Definition: fastio.h:103
LEDColor::LEDColor
LEDColor(const uint8_t(&rgbw)[4])
Definition: leds.h:76
i
uint8_t i
Definition: screen_test_graph.c:72
LEDColor
struct LEDColor LEDColor
leds.h
Marlin_NeoPixel::pixels
static uint16_t pixels()
Definition: neopixel.h:113
millis
uint32_t millis(void)
Definition: wiring_time.c:29
SET_OUTPUT
#define SET_OUTPUT(IO)
Definition: fastio.h:101
LEDColor::is_off
bool is_off() const
Definition: leds.h:105
blinkm.h
LEDLights::set_off
static void set_off()
Definition: leds.h:176
PWM_PIN
bool PWM_PIN(const pin_t p)
LEDColor::operator!=
bool operator!=(const LEDColor &right)
Definition: leds.h:103
LEDLights::LEDLights
LEDLights()
Definition: leds.h:148
LEDColor::b
uint8_t b
Definition: leds.h:42
LEDColor
Definition: leds.h:41
LEDColor::LEDColor
LEDColor(uint8_t r, uint8_t g, uint8_t b)
Definition: leds.h:60
RGB_LED_R_PIN
#define RGB_LED_R_PIN
Definition: pins_FYSETC_F6_13.h:243
LEDColor::LEDColor
LEDColor()
Definition: leds.h:51
LEDColorYellow
#define LEDColorYellow()
Definition: leds.h:134
neo
Marlin_NeoPixel neo
RGB_LED_G_PIN
#define RGB_LED_G_PIN
Definition: pins_FYSETC_F6_13.h:246
HAS_WHITE_LED
#define HAS_WHITE_LED
Definition: leds.h:36
LEDColorWhite
#define LEDColorWhite()
Definition: leds.h:143
LEDColorIndigo
#define LEDColorIndigo()
Definition: leds.h:138
LEDColorViolet
#define LEDColorViolet()
Definition: leds.h:139
if
if(size<=((png_alloc_size_t) -1) - ob)
Definition: pngwrite.c:2176
Marlin_NeoPixel::show
static void show()
Definition: neopixel.h:95
ELAPSED
#define ELAPSED(NOW, SOON)
Definition: millis_t.h:29
uint8_t
const uint8_t[]
Definition: 404_html.c:3
neopixel.h
leds
LEDLights leds
RGB_LED_W_PIN
#define RGB_LED_W_PIN
Definition: pins_FYSETC_F6_13.h:252
LEDColor::operator=
LEDColor & operator=(const uint8_t(&rgbw)[4])
Definition: leds.h:85
LEDLights::setup
static void setup()
RGB_LED_B_PIN
#define RGB_LED_B_PIN
Definition: pins_FYSETC_F6_13.h:249
LEDColorOff
#define LEDColorOff()
Definition: leds.h:127
LEDColor::g
uint8_t g
Definition: leds.h:42
createSpeedLookupTable.b
list b
Definition: createSpeedLookupTable.py:30
LEDLights::set_green
static void set_green()
Definition: leds.h:177
pca9632_set_led_color
void pca9632_set_led_color(const LEDColor &color)
Marlin_NeoPixel::set_color
static void set_color(const uint32_t c)
millis_t
uint32_t millis_t
Definition: millis_t.h:26
ENABLED
#define ENABLED(V...)
Definition: macros.h:177