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 6.7KB

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