2017-09-08 14:47:47 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2019-02-12 15:06:53 -06:00
|
|
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-08 14:47:47 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-10-10 21:25:43 -05:00
|
|
|
#pragma once
|
2017-09-08 14:47:47 -05:00
|
|
|
|
|
|
|
/**
|
2017-11-28 01:07:10 -06:00
|
|
|
* leds.h - Marlin general RGB LED support
|
2017-09-08 14:47:47 -05:00
|
|
|
*/
|
|
|
|
|
2018-10-10 21:25:43 -05:00
|
|
|
#include "../../inc/MarlinConfigPre.h"
|
2017-10-25 15:50:36 -05:00
|
|
|
|
2018-10-20 16:48:28 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-10-05 06:45:36 -05:00
|
|
|
#if ENABLED(NEOPIXEL_LED)
|
2017-09-08 14:47:47 -05:00
|
|
|
#include "neopixel.h"
|
|
|
|
#endif
|
|
|
|
|
2019-03-16 23:43:06 -05:00
|
|
|
#define HAS_WHITE_LED EITHER(RGBW_LED, NEOPIXEL_LED)
|
2017-09-08 14:47:47 -05:00
|
|
|
|
2017-11-28 01:07:10 -06:00
|
|
|
/**
|
|
|
|
* LEDcolor type for use with leds.set_color
|
|
|
|
*/
|
|
|
|
typedef struct LEDColor {
|
|
|
|
uint8_t r, g, b
|
|
|
|
#if HAS_WHITE_LED
|
|
|
|
, w
|
|
|
|
#if ENABLED(NEOPIXEL_LED)
|
|
|
|
, i
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
;
|
2018-10-20 16:48:28 -05:00
|
|
|
|
2017-11-28 01:07:10 -06:00
|
|
|
LEDColor() : r(255), g(255), b(255)
|
|
|
|
#if HAS_WHITE_LED
|
|
|
|
, w(255)
|
|
|
|
#if ENABLED(NEOPIXEL_LED)
|
|
|
|
, i(NEOPIXEL_BRIGHTNESS)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
{}
|
2018-10-20 16:48:28 -05:00
|
|
|
|
2017-11-28 01:07:10 -06:00
|
|
|
LEDColor(uint8_t r, uint8_t g, uint8_t b
|
|
|
|
#if HAS_WHITE_LED
|
|
|
|
, uint8_t w=0
|
|
|
|
#if ENABLED(NEOPIXEL_LED)
|
|
|
|
, uint8_t i=NEOPIXEL_BRIGHTNESS
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
) : r(r), g(g), b(b)
|
|
|
|
#if HAS_WHITE_LED
|
|
|
|
, w(w)
|
|
|
|
#if ENABLED(NEOPIXEL_LED)
|
|
|
|
, i(i)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
{}
|
2018-10-20 16:48:28 -05:00
|
|
|
|
2018-01-20 16:58:41 -06:00
|
|
|
LEDColor(const uint8_t (&rgbw)[4]) : r(rgbw[0]), g(rgbw[1]), b(rgbw[2])
|
|
|
|
#if HAS_WHITE_LED
|
|
|
|
, w(rgbw[3])
|
|
|
|
#if ENABLED(NEOPIXEL_LED)
|
|
|
|
, i(NEOPIXEL_BRIGHTNESS)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
{}
|
2018-10-20 16:48:28 -05:00
|
|
|
|
2018-01-20 16:58:41 -06:00
|
|
|
LEDColor& operator=(const uint8_t (&rgbw)[4]) {
|
|
|
|
r = rgbw[0]; g = rgbw[1]; b = rgbw[2];
|
|
|
|
#if HAS_WHITE_LED
|
|
|
|
w = rgbw[3];
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
2018-10-20 16:48:28 -05:00
|
|
|
|
2017-11-28 01:07:10 -06:00
|
|
|
LEDColor& operator=(const LEDColor &right) {
|
|
|
|
if (this != &right) memcpy(this, &right, sizeof(LEDColor));
|
|
|
|
return *this;
|
|
|
|
}
|
2018-10-20 16:48:28 -05:00
|
|
|
|
2017-11-28 01:07:10 -06:00
|
|
|
bool operator==(const LEDColor &right) {
|
|
|
|
if (this == &right) return true;
|
|
|
|
return 0 == memcmp(this, &right, sizeof(LEDColor));
|
|
|
|
}
|
2018-10-20 16:48:28 -05:00
|
|
|
|
2017-11-28 01:07:10 -06:00
|
|
|
bool operator!=(const LEDColor &right) { return !operator==(right); }
|
2018-10-20 16:48:28 -05:00
|
|
|
|
2017-11-28 01:07:10 -06:00
|
|
|
bool is_off() const {
|
|
|
|
return 3 > r + g + b
|
|
|
|
#if HAS_WHITE_LED
|
|
|
|
+ w
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
}
|
|
|
|
} LEDColor;
|
2017-10-05 06:45:36 -05:00
|
|
|
|
2017-11-28 01:07:10 -06:00
|
|
|
/**
|
|
|
|
* Color helpers and presets
|
|
|
|
*/
|
|
|
|
#if HAS_WHITE_LED
|
|
|
|
#if ENABLED(NEOPIXEL_LED)
|
|
|
|
#define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B, W, I)
|
|
|
|
#else
|
|
|
|
#define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B, W)
|
|
|
|
#endif
|
2018-11-01 17:44:41 -05:00
|
|
|
#define LEDColorWhite() LEDColor(0, 0, 0, 255)
|
2017-10-05 06:45:36 -05:00
|
|
|
#else
|
2017-11-28 01:07:10 -06:00
|
|
|
#define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B)
|
|
|
|
#define LEDColorWhite() LEDColor(255, 255, 255)
|
2017-10-05 06:45:36 -05:00
|
|
|
#endif
|
2017-11-28 01:07:10 -06:00
|
|
|
#define LEDColorOff() LEDColor( 0, 0, 0)
|
|
|
|
#define LEDColorRed() LEDColor(255, 0, 0)
|
|
|
|
#define LEDColorOrange() LEDColor(255, 80, 0)
|
|
|
|
#define LEDColorYellow() LEDColor(255, 255, 0)
|
|
|
|
#define LEDColorGreen() LEDColor( 0, 255, 0)
|
|
|
|
#define LEDColorBlue() LEDColor( 0, 0, 255)
|
|
|
|
#define LEDColorIndigo() LEDColor( 0, 255, 255)
|
|
|
|
#define LEDColorViolet() LEDColor(255, 0, 255)
|
|
|
|
|
|
|
|
class LEDLights {
|
|
|
|
public:
|
|
|
|
LEDLights() {} // ctor
|
|
|
|
|
|
|
|
static void setup(); // init()
|
2017-10-05 06:45:36 -05:00
|
|
|
|
2017-11-28 01:07:10 -06:00
|
|
|
static void set_color(const LEDColor &color
|
|
|
|
#if ENABLED(NEOPIXEL_LED)
|
|
|
|
, bool isSequence=false
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
2018-11-11 07:51:39 -06:00
|
|
|
inline void set_color(uint8_t r, uint8_t g, uint8_t b
|
2017-11-28 01:07:10 -06:00
|
|
|
#if HAS_WHITE_LED
|
|
|
|
, uint8_t w=0
|
2017-10-25 15:50:36 -05:00
|
|
|
#if ENABLED(NEOPIXEL_LED)
|
2017-11-28 01:07:10 -06:00
|
|
|
, uint8_t i=NEOPIXEL_BRIGHTNESS
|
2017-10-25 15:50:36 -05:00
|
|
|
#endif
|
|
|
|
#endif
|
2017-11-28 01:07:10 -06:00
|
|
|
#if ENABLED(NEOPIXEL_LED)
|
|
|
|
, bool isSequence=false
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
set_color(MakeLEDColor(r, g, b, w, i)
|
|
|
|
#if ENABLED(NEOPIXEL_LED)
|
|
|
|
, isSequence
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-11 07:51:39 -06:00
|
|
|
static inline void set_off() { set_color(LEDColorOff()); }
|
|
|
|
static inline void set_green() { set_color(LEDColorGreen()); }
|
|
|
|
static inline void set_white() { set_color(LEDColorWhite()); }
|
2017-11-28 01:07:10 -06:00
|
|
|
|
|
|
|
#if ENABLED(LED_COLOR_PRESETS)
|
|
|
|
static const LEDColor defaultLEDColor;
|
2018-11-11 07:51:39 -06:00
|
|
|
static inline void set_default() { set_color(defaultLEDColor); }
|
|
|
|
static inline void set_red() { set_color(LEDColorRed()); }
|
|
|
|
static inline void set_orange() { set_color(LEDColorOrange()); }
|
|
|
|
static inline void set_yellow() { set_color(LEDColorYellow()); }
|
|
|
|
static inline void set_blue() { set_color(LEDColorBlue()); }
|
|
|
|
static inline void set_indigo() { set_color(LEDColorIndigo()); }
|
|
|
|
static inline void set_violet() { set_color(LEDColorViolet()); }
|
2017-11-28 01:07:10 -06:00
|
|
|
#endif
|
|
|
|
|
2018-11-01 17:44:41 -05:00
|
|
|
#if ENABLED(PRINTER_EVENT_LEDS)
|
2018-11-11 07:51:39 -06:00
|
|
|
static inline LEDColor get_color() { return lights_on ? color : LEDColorOff(); }
|
2018-11-01 17:44:41 -05:00
|
|
|
#endif
|
|
|
|
|
2019-03-16 23:43:06 -05:00
|
|
|
#if EITHER(LED_CONTROL_MENU, PRINTER_EVENT_LEDS)
|
2017-11-28 01:07:10 -06:00
|
|
|
static LEDColor color; // last non-off color
|
|
|
|
static bool lights_on; // the last set color was "on"
|
2018-11-01 17:44:41 -05:00
|
|
|
#endif
|
2019-01-14 14:29:55 -06:00
|
|
|
|
2018-11-01 17:44:41 -05:00
|
|
|
#if ENABLED(LED_CONTROL_MENU)
|
2017-11-28 01:07:10 -06:00
|
|
|
static void toggle(); // swap "off" with color
|
2018-11-11 07:51:39 -06:00
|
|
|
static inline void update() { set_color(color); }
|
2017-11-28 01:07:10 -06:00
|
|
|
#endif
|
|
|
|
};
|
2017-09-08 14:47:47 -05:00
|
|
|
|
2017-11-28 01:07:10 -06:00
|
|
|
extern LEDLights leds;
|