My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

leds.h 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * leds.h - Marlin general RGB LED support
  25. */
  26. #include "../../inc/MarlinConfigPre.h"
  27. #include <string.h>
  28. #if ENABLED(NEOPIXEL_LED)
  29. #include "neopixel.h"
  30. #endif
  31. // A white component can be passed
  32. #if EITHER(RGBW_LED, NEOPIXEL_LED)
  33. #define HAS_WHITE_LED 1
  34. #endif
  35. /**
  36. * LEDcolor type for use with leds.set_color
  37. */
  38. typedef struct LEDColor {
  39. uint8_t r, g, b
  40. #if HAS_WHITE_LED
  41. , w
  42. #if ENABLED(NEOPIXEL_LED)
  43. , i
  44. #endif
  45. #endif
  46. ;
  47. LEDColor() : r(255), g(255), b(255)
  48. #if HAS_WHITE_LED
  49. , w(255)
  50. #if ENABLED(NEOPIXEL_LED)
  51. , i(NEOPIXEL_BRIGHTNESS)
  52. #endif
  53. #endif
  54. {}
  55. LEDColor(uint8_t r, uint8_t g, uint8_t b
  56. #if HAS_WHITE_LED
  57. , uint8_t w=0
  58. #if ENABLED(NEOPIXEL_LED)
  59. , uint8_t i=NEOPIXEL_BRIGHTNESS
  60. #endif
  61. #endif
  62. ) : r(r), g(g), b(b)
  63. #if HAS_WHITE_LED
  64. , w(w)
  65. #if ENABLED(NEOPIXEL_LED)
  66. , i(i)
  67. #endif
  68. #endif
  69. {}
  70. LEDColor(const uint8_t (&rgbw)[4]) : r(rgbw[0]), g(rgbw[1]), b(rgbw[2])
  71. #if HAS_WHITE_LED
  72. , w(rgbw[3])
  73. #if ENABLED(NEOPIXEL_LED)
  74. , i(NEOPIXEL_BRIGHTNESS)
  75. #endif
  76. #endif
  77. {}
  78. LEDColor& operator=(const uint8_t (&rgbw)[4]) {
  79. r = rgbw[0]; g = rgbw[1]; b = rgbw[2];
  80. TERN_(HAS_WHITE_LED, w = rgbw[3]);
  81. return *this;
  82. }
  83. LEDColor& operator=(const LEDColor &right) {
  84. if (this != &right) memcpy(this, &right, sizeof(LEDColor));
  85. return *this;
  86. }
  87. bool operator==(const LEDColor &right) {
  88. if (this == &right) return true;
  89. return 0 == memcmp(this, &right, sizeof(LEDColor));
  90. }
  91. bool operator!=(const LEDColor &right) { return !operator==(right); }
  92. bool is_off() const {
  93. return 3 > r + g + b + TERN0(HAS_WHITE_LED, w);
  94. }
  95. } LEDColor;
  96. /**
  97. * Color helpers and presets
  98. */
  99. #if HAS_WHITE_LED
  100. #if ENABLED(NEOPIXEL_LED)
  101. #define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B, W, I)
  102. #else
  103. #define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B, W)
  104. #endif
  105. #else
  106. #define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B)
  107. #endif
  108. #define LEDColorOff() LEDColor( 0, 0, 0)
  109. #define LEDColorRed() LEDColor(255, 0, 0)
  110. #if ENABLED(LED_COLORS_REDUCE_GREEN)
  111. #define LEDColorOrange() LEDColor(255, 25, 0)
  112. #define LEDColorYellow() LEDColor(255, 75, 0)
  113. #else
  114. #define LEDColorOrange() LEDColor(255, 80, 0)
  115. #define LEDColorYellow() LEDColor(255, 255, 0)
  116. #endif
  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. #if HAS_WHITE_LED && DISABLED(RGB_LED)
  122. #define LEDColorWhite() LEDColor( 0, 0, 0, 255)
  123. #else
  124. #define LEDColorWhite() LEDColor(255, 255, 255)
  125. #endif
  126. class LEDLights {
  127. public:
  128. LEDLights() {} // ctor
  129. static void setup(); // init()
  130. static void set_color(const LEDColor &color
  131. #if ENABLED(NEOPIXEL_LED)
  132. , bool isSequence=false
  133. #endif
  134. );
  135. static inline void set_color(uint8_t r, uint8_t g, uint8_t b
  136. #if HAS_WHITE_LED
  137. , uint8_t w=0
  138. #if ENABLED(NEOPIXEL_LED)
  139. , uint8_t i=NEOPIXEL_BRIGHTNESS
  140. #endif
  141. #endif
  142. #if ENABLED(NEOPIXEL_LED)
  143. , bool isSequence=false
  144. #endif
  145. ) {
  146. set_color(MakeLEDColor(r, g, b, w, i)
  147. #if ENABLED(NEOPIXEL_LED)
  148. , isSequence
  149. #endif
  150. );
  151. }
  152. static inline void set_off() { set_color(LEDColorOff()); }
  153. static inline void set_green() { set_color(LEDColorGreen()); }
  154. static inline void set_white() { set_color(LEDColorWhite()); }
  155. #if ENABLED(LED_COLOR_PRESETS)
  156. static const LEDColor defaultLEDColor;
  157. static inline void set_default() { set_color(defaultLEDColor); }
  158. static inline void set_red() { set_color(LEDColorRed()); }
  159. static inline void set_orange() { set_color(LEDColorOrange()); }
  160. static inline void set_yellow() { set_color(LEDColorYellow()); }
  161. static inline void set_blue() { set_color(LEDColorBlue()); }
  162. static inline void set_indigo() { set_color(LEDColorIndigo()); }
  163. static inline void set_violet() { set_color(LEDColorViolet()); }
  164. #endif
  165. #if ENABLED(PRINTER_EVENT_LEDS)
  166. static inline LEDColor get_color() { return lights_on ? color : LEDColorOff(); }
  167. #endif
  168. #if EITHER(LED_CONTROL_MENU, PRINTER_EVENT_LEDS)
  169. static LEDColor color; // last non-off color
  170. static bool lights_on; // the last set color was "on"
  171. #endif
  172. #if ENABLED(LED_CONTROL_MENU)
  173. static void toggle(); // swap "off" with color
  174. static inline void update() { set_color(color); }
  175. #endif
  176. #ifdef LED_BACKLIGHT_TIMEOUT
  177. private:
  178. static millis_t led_off_time;
  179. public:
  180. static inline void reset_timeout(const millis_t &ms) {
  181. led_off_time = ms + LED_BACKLIGHT_TIMEOUT;
  182. if (!lights_on) set_default();
  183. }
  184. static void update_timeout(const bool power_on);
  185. #endif
  186. };
  187. extern LEDLights leds;