My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

leds.h 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * 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. #define HAS_WHITE_LED EITHER(RGBW_LED, NEOPIXEL_LED)
  33. /**
  34. * LEDcolor type for use with leds.set_color
  35. */
  36. typedef struct LEDColor {
  37. uint8_t r, g, b
  38. #if HAS_WHITE_LED
  39. , w
  40. #if ENABLED(NEOPIXEL_LED)
  41. , i
  42. #endif
  43. #endif
  44. ;
  45. LEDColor() : r(255), g(255), b(255)
  46. #if HAS_WHITE_LED
  47. , w(255)
  48. #if ENABLED(NEOPIXEL_LED)
  49. , i(NEOPIXEL_BRIGHTNESS)
  50. #endif
  51. #endif
  52. {}
  53. LEDColor(uint8_t r, uint8_t g, uint8_t b
  54. #if HAS_WHITE_LED
  55. , uint8_t w=0
  56. #if ENABLED(NEOPIXEL_LED)
  57. , uint8_t i=NEOPIXEL_BRIGHTNESS
  58. #endif
  59. #endif
  60. ) : r(r), g(g), b(b)
  61. #if HAS_WHITE_LED
  62. , w(w)
  63. #if ENABLED(NEOPIXEL_LED)
  64. , i(i)
  65. #endif
  66. #endif
  67. {}
  68. LEDColor(const uint8_t (&rgbw)[4]) : r(rgbw[0]), g(rgbw[1]), b(rgbw[2])
  69. #if HAS_WHITE_LED
  70. , w(rgbw[3])
  71. #if ENABLED(NEOPIXEL_LED)
  72. , i(NEOPIXEL_BRIGHTNESS)
  73. #endif
  74. #endif
  75. {}
  76. LEDColor& operator=(const uint8_t (&rgbw)[4]) {
  77. r = rgbw[0]; g = rgbw[1]; b = rgbw[2];
  78. #if HAS_WHITE_LED
  79. w = rgbw[3];
  80. #endif
  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
  94. #if HAS_WHITE_LED
  95. + w
  96. #endif
  97. ;
  98. }
  99. } LEDColor;
  100. /**
  101. * Color helpers and presets
  102. */
  103. #if HAS_WHITE_LED
  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. #endif
  112. #define LEDColorOff() LEDColor( 0, 0, 0)
  113. #define LEDColorRed() LEDColor(255, 0, 0)
  114. #if ENABLED(LED_COLORS_REDUCE_GREEN)
  115. #define LEDColorOrange() LEDColor(255, 25, 0)
  116. #define LEDColorYellow() LEDColor(255, 75, 0)
  117. #else
  118. #define LEDColorOrange() LEDColor(255, 80, 0)
  119. #define LEDColorYellow() LEDColor(255, 255, 0)
  120. #endif
  121. #define LEDColorGreen() LEDColor( 0, 255, 0)
  122. #define LEDColorBlue() LEDColor( 0, 0, 255)
  123. #define LEDColorIndigo() LEDColor( 0, 255, 255)
  124. #define LEDColorViolet() LEDColor(255, 0, 255)
  125. #if HAS_WHITE_LED && DISABLED(RGB_LED)
  126. #define LEDColorWhite() LEDColor( 0, 0, 0, 255)
  127. #else
  128. #define LEDColorWhite() LEDColor(255, 255, 255)
  129. #endif
  130. class LEDLights {
  131. public:
  132. LEDLights() {} // ctor
  133. static void setup(); // init()
  134. static void set_color(const LEDColor &color
  135. #if ENABLED(NEOPIXEL_LED)
  136. , bool isSequence=false
  137. #endif
  138. );
  139. inline void set_color(uint8_t r, uint8_t g, uint8_t b
  140. #if HAS_WHITE_LED
  141. , uint8_t w=0
  142. #if ENABLED(NEOPIXEL_LED)
  143. , uint8_t i=NEOPIXEL_BRIGHTNESS
  144. #endif
  145. #endif
  146. #if ENABLED(NEOPIXEL_LED)
  147. , bool isSequence=false
  148. #endif
  149. ) {
  150. set_color(MakeLEDColor(r, g, b, w, i)
  151. #if ENABLED(NEOPIXEL_LED)
  152. , isSequence
  153. #endif
  154. );
  155. }
  156. static inline void set_off() { set_color(LEDColorOff()); }
  157. static inline void set_green() { set_color(LEDColorGreen()); }
  158. static inline void set_white() { set_color(LEDColorWhite()); }
  159. #if ENABLED(LED_COLOR_PRESETS)
  160. static const LEDColor defaultLEDColor;
  161. static inline void set_default() { set_color(defaultLEDColor); }
  162. static inline void set_red() { set_color(LEDColorRed()); }
  163. static inline void set_orange() { set_color(LEDColorOrange()); }
  164. static inline void set_yellow() { set_color(LEDColorYellow()); }
  165. static inline void set_blue() { set_color(LEDColorBlue()); }
  166. static inline void set_indigo() { set_color(LEDColorIndigo()); }
  167. static inline void set_violet() { set_color(LEDColorViolet()); }
  168. #endif
  169. #if ENABLED(PRINTER_EVENT_LEDS)
  170. static inline LEDColor get_color() { return lights_on ? color : LEDColorOff(); }
  171. #endif
  172. #if EITHER(LED_CONTROL_MENU, PRINTER_EVENT_LEDS)
  173. static LEDColor color; // last non-off color
  174. static bool lights_on; // the last set color was "on"
  175. #endif
  176. #if ENABLED(LED_CONTROL_MENU)
  177. static void toggle(); // swap "off" with color
  178. static inline void update() { set_color(color); }
  179. #endif
  180. #ifdef LED_BACKLIGHT_TIMEOUT
  181. private:
  182. static millis_t led_off_time;
  183. public:
  184. static inline void reset_timeout(const millis_t &ms) {
  185. led_off_time = ms + LED_BACKLIGHT_TIMEOUT;
  186. if (!lights_on) set_default();
  187. }
  188. static void update_timeout(const bool power_on);
  189. #endif
  190. };
  191. extern LEDLights leds;