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.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /**
  23. * leds.cpp - Marlin RGB LED general support
  24. */
  25. #include "../../inc/MarlinConfig.h"
  26. #if HAS_COLOR_LEDS
  27. #include "leds.h"
  28. #if ENABLED(BLINKM)
  29. #include "blinkm.h"
  30. #endif
  31. #if ENABLED(PCA9632)
  32. #include "pca9632.h"
  33. #endif
  34. #if ENABLED(PCA9533)
  35. #include "pca9533.h"
  36. #endif
  37. #if EITHER(CASE_LIGHT_USE_RGB_LED, CASE_LIGHT_USE_NEOPIXEL)
  38. #include "../../feature/caselight.h"
  39. #endif
  40. #if ENABLED(LED_COLOR_PRESETS)
  41. const LEDColor LEDLights::defaultLEDColor = LEDColor(
  42. LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE
  43. OPTARG(HAS_WHITE_LED, LED_USER_PRESET_WHITE)
  44. OPTARG(NEOPIXEL_LED, LED_USER_PRESET_BRIGHTNESS)
  45. );
  46. #endif
  47. #if ANY(LED_CONTROL_MENU, PRINTER_EVENT_LEDS, CASE_LIGHT_IS_COLOR_LED)
  48. LEDColor LEDLights::color;
  49. bool LEDLights::lights_on;
  50. #endif
  51. LEDLights leds;
  52. void LEDLights::setup() {
  53. #if EITHER(RGB_LED, RGBW_LED)
  54. if (PWM_PIN(RGB_LED_R_PIN)) SET_PWM(RGB_LED_R_PIN); else SET_OUTPUT(RGB_LED_R_PIN);
  55. if (PWM_PIN(RGB_LED_G_PIN)) SET_PWM(RGB_LED_G_PIN); else SET_OUTPUT(RGB_LED_G_PIN);
  56. if (PWM_PIN(RGB_LED_B_PIN)) SET_PWM(RGB_LED_B_PIN); else SET_OUTPUT(RGB_LED_B_PIN);
  57. #if ENABLED(RGBW_LED)
  58. if (PWM_PIN(RGB_LED_W_PIN)) SET_PWM(RGB_LED_W_PIN); else SET_OUTPUT(RGB_LED_W_PIN);
  59. #endif
  60. #endif
  61. TERN_(NEOPIXEL_LED, neo.init());
  62. TERN_(PCA9533, PCA9533_init());
  63. TERN_(LED_USER_PRESET_STARTUP, set_default());
  64. }
  65. void LEDLights::set_color(const LEDColor &incol
  66. OPTARG(NEOPIXEL_IS_SEQUENTIAL, bool isSequence/*=false*/)
  67. ) {
  68. #if ENABLED(NEOPIXEL_LED)
  69. const uint32_t neocolor = LEDColorWhite() == incol
  70. ? neo.Color(NEO_WHITE)
  71. : neo.Color(incol.r, incol.g, incol.b OPTARG(HAS_WHITE_LED, incol.w));
  72. #if ENABLED(NEOPIXEL_IS_SEQUENTIAL)
  73. static uint16_t nextLed = 0;
  74. #ifdef NEOPIXEL_BKGD_INDEX_FIRST
  75. while (WITHIN(nextLed, NEOPIXEL_BKGD_INDEX_FIRST, NEOPIXEL_BKGD_INDEX_LAST)) {
  76. neo.reset_background_color();
  77. if (++nextLed >= neo.pixels()) { nextLed = 0; return; }
  78. }
  79. #endif
  80. #endif
  81. #if BOTH(CASE_LIGHT_MENU, CASE_LIGHT_USE_NEOPIXEL)
  82. // Update brightness only if caselight is ON or switching leds off
  83. if (caselight.on || incol.is_off())
  84. #endif
  85. neo.set_brightness(incol.i);
  86. #if ENABLED(NEOPIXEL_IS_SEQUENTIAL)
  87. if (isSequence) {
  88. neo.set_pixel_color(nextLed, neocolor);
  89. neo.show();
  90. if (++nextLed >= neo.pixels()) nextLed = 0;
  91. return;
  92. }
  93. #endif
  94. #if BOTH(CASE_LIGHT_MENU, CASE_LIGHT_USE_NEOPIXEL)
  95. // Update color only if caselight is ON or switching leds off
  96. if (caselight.on || incol.is_off())
  97. #endif
  98. neo.set_color(neocolor);
  99. #endif
  100. #if ENABLED(BLINKM)
  101. // This variant uses i2c to send the RGB components to the device.
  102. blinkm_set_led_color(incol);
  103. #endif
  104. #if EITHER(RGB_LED, RGBW_LED)
  105. // This variant uses 3-4 separate pins for the RGB(W) components.
  106. // If the pins can do PWM then their intensity will be set.
  107. #define _UPDATE_RGBW(C,c) do { \
  108. if (PWM_PIN(RGB_LED_##C##_PIN)) \
  109. set_pwm_duty(pin_t(RGB_LED_##C##_PIN), c); \
  110. else \
  111. WRITE(RGB_LED_##C##_PIN, c ? HIGH : LOW); \
  112. }while(0)
  113. #define UPDATE_RGBW(C,c) _UPDATE_RGBW(C, TERN1(CASE_LIGHT_USE_RGB_LED, caselight.on) ? incol.c : 0)
  114. UPDATE_RGBW(R,r); UPDATE_RGBW(G,g); UPDATE_RGBW(B,b);
  115. #if ENABLED(RGBW_LED)
  116. UPDATE_RGBW(W,w);
  117. #endif
  118. #endif
  119. // Update I2C LED driver
  120. TERN_(PCA9632, PCA9632_set_led_color(incol));
  121. TERN_(PCA9533, PCA9533_set_rgb(incol.r, incol.g, incol.b));
  122. #if EITHER(LED_CONTROL_MENU, PRINTER_EVENT_LEDS)
  123. // Don't update the color when OFF
  124. lights_on = !incol.is_off();
  125. if (lights_on) color = incol;
  126. #endif
  127. }
  128. #if ENABLED(LED_CONTROL_MENU)
  129. void LEDLights::toggle() { if (lights_on) set_off(); else update(); }
  130. #endif
  131. #if LED_POWEROFF_TIMEOUT > 0
  132. millis_t LEDLights::led_off_time; // = 0
  133. void LEDLights::update_timeout(const bool power_on) {
  134. if (lights_on) {
  135. const millis_t ms = millis();
  136. if (power_on)
  137. reset_timeout(ms);
  138. else if (ELAPSED(ms, led_off_time))
  139. set_off();
  140. }
  141. }
  142. #endif
  143. #if ENABLED(NEOPIXEL2_SEPARATE)
  144. #if ENABLED(NEO2_COLOR_PRESETS)
  145. const LEDColor LEDLights2::defaultLEDColor = LEDColor(
  146. NEO2_USER_PRESET_RED, NEO2_USER_PRESET_GREEN, NEO2_USER_PRESET_BLUE
  147. OPTARG(HAS_WHITE_LED2, NEO2_USER_PRESET_WHITE)
  148. OPTARG(NEOPIXEL_LED, NEO2_USER_PRESET_BRIGHTNESS)
  149. );
  150. #endif
  151. #if ENABLED(LED_CONTROL_MENU)
  152. LEDColor LEDLights2::color;
  153. bool LEDLights2::lights_on;
  154. #endif
  155. LEDLights2 leds2;
  156. void LEDLights2::setup() {
  157. neo2.init();
  158. TERN_(NEO2_USER_PRESET_STARTUP, set_default());
  159. }
  160. void LEDLights2::set_color(const LEDColor &incol) {
  161. const uint32_t neocolor = LEDColorWhite() == incol
  162. ? neo2.Color(NEO2_WHITE)
  163. : neo2.Color(incol.r, incol.g, incol.b OPTARG(HAS_WHITE_LED2, incol.w));
  164. neo2.set_brightness(incol.i);
  165. neo2.set_color(neocolor);
  166. #if ENABLED(LED_CONTROL_MENU)
  167. // Don't update the color when OFF
  168. lights_on = !incol.is_off();
  169. if (lights_on) color = incol;
  170. #endif
  171. }
  172. #if ENABLED(LED_CONTROL_MENU)
  173. void LEDLights2::toggle() { if (lights_on) set_off(); else update(); }
  174. #endif
  175. #endif // NEOPIXEL2_SEPARATE
  176. #endif // HAS_COLOR_LEDS