leds.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. /**
  23. * leds.h - Marlin general RGB LED support
  24. */
  25. #ifndef __LEDS_H__
  26. #define __LEDS_H__
  27. #include "MarlinConfig.h"
  28. #if ENABLED(NEOPIXEL_LED)
  29. #include "neopixel.h"
  30. #endif
  31. #define HAS_WHITE_LED (ENABLED(RGBW_LED) || ENABLED(NEOPIXEL_LED))
  32. /**
  33. * LEDcolor type for use with leds.set_color
  34. */
  35. typedef struct LEDColor {
  36. uint8_t r, g, b
  37. #if HAS_WHITE_LED
  38. , w
  39. #if ENABLED(NEOPIXEL_LED)
  40. , i
  41. #endif
  42. #endif
  43. ;
  44. LEDColor() : r(255), g(255), b(255)
  45. #if HAS_WHITE_LED
  46. , w(255)
  47. #if ENABLED(NEOPIXEL_LED)
  48. , i(NEOPIXEL_BRIGHTNESS)
  49. #endif
  50. #endif
  51. {}
  52. LEDColor(uint8_t r, uint8_t g, uint8_t b
  53. #if HAS_WHITE_LED
  54. , uint8_t w=0
  55. #if ENABLED(NEOPIXEL_LED)
  56. , uint8_t i=NEOPIXEL_BRIGHTNESS
  57. #endif
  58. #endif
  59. ) : r(r), g(g), b(b)
  60. #if HAS_WHITE_LED
  61. , w(w)
  62. #if ENABLED(NEOPIXEL_LED)
  63. , i(i)
  64. #endif
  65. #endif
  66. {}
  67. LEDColor(const uint8_t (&rgbw)[4]) : r(rgbw[0]), g(rgbw[1]), b(rgbw[2])
  68. #if HAS_WHITE_LED
  69. , w(rgbw[3])
  70. #if ENABLED(NEOPIXEL_LED)
  71. , i(NEOPIXEL_BRIGHTNESS)
  72. #endif
  73. #endif
  74. {}
  75. LEDColor& operator=(const uint8_t (&rgbw)[4]) {
  76. r = rgbw[0]; g = rgbw[1]; b = rgbw[2];
  77. #if HAS_WHITE_LED
  78. w = rgbw[3];
  79. #endif
  80. return *this;
  81. }
  82. LEDColor& operator=(const LEDColor &right) {
  83. if (this != &right) memcpy(this, &right, sizeof(LEDColor));
  84. return *this;
  85. }
  86. bool operator==(const LEDColor &right) {
  87. if (this == &right) return true;
  88. return 0 == memcmp(this, &right, sizeof(LEDColor));
  89. }
  90. bool operator!=(const LEDColor &right) { return !operator==(right); }
  91. bool is_off() const {
  92. return 3 > r + g + b
  93. #if HAS_WHITE_LED
  94. + w
  95. #endif
  96. ;
  97. }
  98. } LEDColor;
  99. /**
  100. * Color helpers and presets
  101. */
  102. #if HAS_WHITE_LED
  103. #define LEDColorWhite() LEDColor(0, 0, 0, 255)
  104. #if ENABLED(NEOPIXEL_LED)
  105. #define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B, W, I)
  106. #else
  107. #define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B, W)
  108. #endif
  109. #else
  110. #define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B)
  111. #define LEDColorWhite() LEDColor(255, 255, 255)
  112. #endif
  113. #define LEDColorOff() LEDColor( 0, 0, 0)
  114. #define LEDColorRed() LEDColor(255, 0, 0)
  115. #define LEDColorOrange() LEDColor(255, 80, 0)
  116. #define LEDColorYellow() LEDColor(255, 255, 0)
  117. #define LEDColorGreen() LEDColor( 0, 255, 0)
  118. #define LEDColorBlue() LEDColor( 0, 0, 255)
  119. #define LEDColorIndigo() LEDColor( 0, 255, 255)
  120. #define LEDColorViolet() LEDColor(255, 0, 255)
  121. class LEDLights {
  122. public:
  123. LEDLights() {} // ctor
  124. static void setup(); // init()
  125. static void set_color(const LEDColor &color
  126. #if ENABLED(NEOPIXEL_LED)
  127. , bool isSequence=false
  128. #endif
  129. );
  130. FORCE_INLINE void set_color(uint8_t r, uint8_t g, uint8_t b
  131. #if HAS_WHITE_LED
  132. , uint8_t w=0
  133. #if ENABLED(NEOPIXEL_LED)
  134. , uint8_t i=NEOPIXEL_BRIGHTNESS
  135. #endif
  136. #endif
  137. #if ENABLED(NEOPIXEL_LED)
  138. , bool isSequence=false
  139. #endif
  140. ) {
  141. set_color(MakeLEDColor(r, g, b, w, i)
  142. #if ENABLED(NEOPIXEL_LED)
  143. , isSequence
  144. #endif
  145. );
  146. }
  147. static void set_white();
  148. FORCE_INLINE static void set_off() { set_color(LEDColorOff()); }
  149. FORCE_INLINE static void set_green() { set_color(LEDColorGreen()); }
  150. #if ENABLED(LED_COLOR_PRESETS)
  151. static const LEDColor defaultLEDColor;
  152. FORCE_INLINE static void set_default() { set_color(defaultLEDColor); }
  153. FORCE_INLINE static void set_red() { set_color(LEDColorRed()); }
  154. FORCE_INLINE static void set_orange() { set_color(LEDColorOrange()); }
  155. FORCE_INLINE static void set_yellow() { set_color(LEDColorYellow()); }
  156. FORCE_INLINE static void set_blue() { set_color(LEDColorBlue()); }
  157. FORCE_INLINE static void set_indigo() { set_color(LEDColorIndigo()); }
  158. FORCE_INLINE static void set_violet() { set_color(LEDColorViolet()); }
  159. #endif
  160. #if ENABLED(LED_CONTROL_MENU)
  161. static LEDColor color; // last non-off color
  162. static bool lights_on; // the last set color was "on"
  163. static void toggle(); // swap "off" with color
  164. FORCE_INLINE static void update() { set_color(color); }
  165. #endif
  166. };
  167. extern LEDLights leds;
  168. #endif // __LEDS_H__