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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #if ENABLED(RGB_STARTUP_TEST)
  61. int8_t led_pin_count = 0;
  62. if (PWM_PIN(RGB_LED_R_PIN) && PWM_PIN(RGB_LED_G_PIN) && PWM_PIN(RGB_LED_B_PIN)) led_pin_count = 3;
  63. #if ENABLED(RGBW_LED)
  64. if (PWM_PIN(RGB_LED_W_PIN) && led_pin_count) led_pin_count++;
  65. #endif
  66. // Startup animation
  67. if (led_pin_count) {
  68. // blackout
  69. if (PWM_PIN(RGB_LED_R_PIN)) hal.set_pwm_duty(pin_t(RGB_LED_R_PIN), 0); else WRITE(RGB_LED_R_PIN, LOW);
  70. if (PWM_PIN(RGB_LED_G_PIN)) hal.set_pwm_duty(pin_t(RGB_LED_G_PIN), 0); else WRITE(RGB_LED_G_PIN, LOW);
  71. if (PWM_PIN(RGB_LED_B_PIN)) hal.set_pwm_duty(pin_t(RGB_LED_B_PIN), 0); else WRITE(RGB_LED_B_PIN, LOW);
  72. #if ENABLED(RGBW_LED)
  73. if (PWM_PIN(RGB_LED_W_PIN)) hal.set_pwm_duty(pin_t(RGB_LED_W_PIN), 0);
  74. else WRITE(RGB_LED_W_PIN, LOW);
  75. #endif
  76. delay(200);
  77. LOOP_L_N(i, led_pin_count) {
  78. LOOP_LE_N(b, 200) {
  79. const uint16_t led_pwm = b <= 100 ? b : 200 - b;
  80. if (i == 0 && PWM_PIN(RGB_LED_R_PIN)) hal.set_pwm_duty(pin_t(RGB_LED_R_PIN), led_pwm); else WRITE(RGB_LED_R_PIN, b < 100 ? HIGH : LOW);
  81. if (i == 1 && PWM_PIN(RGB_LED_G_PIN)) hal.set_pwm_duty(pin_t(RGB_LED_G_PIN), led_pwm); else WRITE(RGB_LED_G_PIN, b < 100 ? HIGH : LOW);
  82. if (i == 2 && PWM_PIN(RGB_LED_B_PIN)) hal.set_pwm_duty(pin_t(RGB_LED_B_PIN), led_pwm); else WRITE(RGB_LED_B_PIN, b < 100 ? HIGH : LOW);
  83. #if ENABLED(RGBW_LED)
  84. if (i == 3){
  85. if (PWM_PIN(RGB_LED_W_PIN)) hal.set_pwm_duty(pin_t(RGB_LED_W_PIN), led_pwm);
  86. else WRITE(RGB_LED_W_PIN, b < 100 ? HIGH : LOW);
  87. delay(RGB_STARTUP_TEST_INNER_MS);//More slowing for ending
  88. }
  89. #endif
  90. delay(RGB_STARTUP_TEST_INNER_MS);
  91. }
  92. }
  93. delay(500);
  94. }
  95. #endif // RGB_STARTUP_TEST
  96. #endif
  97. TERN_(NEOPIXEL_LED, neo.init());
  98. TERN_(PCA9533, PCA9533_init());
  99. TERN_(LED_USER_PRESET_STARTUP, set_default());
  100. }
  101. void LEDLights::set_color(const LEDColor &incol
  102. OPTARG(NEOPIXEL_IS_SEQUENTIAL, bool isSequence/*=false*/)
  103. ) {
  104. #if ENABLED(NEOPIXEL_LED)
  105. const uint32_t neocolor = LEDColorWhite() == incol
  106. ? neo.Color(NEO_WHITE)
  107. : neo.Color(incol.r, incol.g, incol.b OPTARG(HAS_WHITE_LED, incol.w));
  108. #if ENABLED(NEOPIXEL_IS_SEQUENTIAL)
  109. static uint16_t nextLed = 0;
  110. #ifdef NEOPIXEL_BKGD_INDEX_FIRST
  111. while (WITHIN(nextLed, NEOPIXEL_BKGD_INDEX_FIRST, NEOPIXEL_BKGD_INDEX_LAST)) {
  112. neo.reset_background_color();
  113. if (++nextLed >= neo.pixels()) { nextLed = 0; return; }
  114. }
  115. #endif
  116. #endif
  117. #if BOTH(CASE_LIGHT_MENU, CASE_LIGHT_USE_NEOPIXEL)
  118. // Update brightness only if caselight is ON or switching leds off
  119. if (caselight.on || incol.is_off())
  120. #endif
  121. neo.set_brightness(incol.i);
  122. #if ENABLED(NEOPIXEL_IS_SEQUENTIAL)
  123. if (isSequence) {
  124. neo.set_pixel_color(nextLed, neocolor);
  125. neo.show();
  126. if (++nextLed >= neo.pixels()) nextLed = 0;
  127. return;
  128. }
  129. #endif
  130. #if BOTH(CASE_LIGHT_MENU, CASE_LIGHT_USE_NEOPIXEL)
  131. // Update color only if caselight is ON or switching leds off
  132. if (caselight.on || incol.is_off())
  133. #endif
  134. neo.set_color(neocolor);
  135. #endif
  136. #if ENABLED(BLINKM)
  137. // This variant uses i2c to send the RGB components to the device.
  138. blinkm_set_led_color(incol);
  139. #endif
  140. #if EITHER(RGB_LED, RGBW_LED)
  141. // This variant uses 3-4 separate pins for the RGB(W) components.
  142. // If the pins can do PWM then their intensity will be set.
  143. #define _UPDATE_RGBW(C,c) do { \
  144. if (PWM_PIN(RGB_LED_##C##_PIN)) \
  145. hal.set_pwm_duty(pin_t(RGB_LED_##C##_PIN), c); \
  146. else \
  147. WRITE(RGB_LED_##C##_PIN, c ? HIGH : LOW); \
  148. }while(0)
  149. #define UPDATE_RGBW(C,c) _UPDATE_RGBW(C, TERN1(CASE_LIGHT_USE_RGB_LED, caselight.on) ? incol.c : 0)
  150. UPDATE_RGBW(R,r); UPDATE_RGBW(G,g); UPDATE_RGBW(B,b);
  151. #if ENABLED(RGBW_LED)
  152. UPDATE_RGBW(W,w);
  153. #endif
  154. #endif
  155. // Update I2C LED driver
  156. TERN_(PCA9632, PCA9632_set_led_color(incol));
  157. TERN_(PCA9533, PCA9533_set_rgb(incol.r, incol.g, incol.b));
  158. #if EITHER(LED_CONTROL_MENU, PRINTER_EVENT_LEDS)
  159. // Don't update the color when OFF
  160. lights_on = !incol.is_off();
  161. if (lights_on) color = incol;
  162. #endif
  163. }
  164. #if ENABLED(LED_CONTROL_MENU)
  165. void LEDLights::toggle() { if (lights_on) set_off(); else update(); }
  166. #endif
  167. #if LED_POWEROFF_TIMEOUT > 0
  168. millis_t LEDLights::led_off_time; // = 0
  169. void LEDLights::update_timeout(const bool power_on) {
  170. if (lights_on) {
  171. const millis_t ms = millis();
  172. if (power_on)
  173. reset_timeout(ms);
  174. else if (ELAPSED(ms, led_off_time))
  175. set_off();
  176. }
  177. }
  178. #endif
  179. #if ENABLED(NEOPIXEL2_SEPARATE)
  180. #if ENABLED(NEO2_COLOR_PRESETS)
  181. const LEDColor LEDLights2::defaultLEDColor = LEDColor(
  182. NEO2_USER_PRESET_RED, NEO2_USER_PRESET_GREEN, NEO2_USER_PRESET_BLUE
  183. OPTARG(HAS_WHITE_LED2, NEO2_USER_PRESET_WHITE)
  184. OPTARG(NEOPIXEL_LED, NEO2_USER_PRESET_BRIGHTNESS)
  185. );
  186. #endif
  187. #if ENABLED(LED_CONTROL_MENU)
  188. LEDColor LEDLights2::color;
  189. bool LEDLights2::lights_on;
  190. #endif
  191. LEDLights2 leds2;
  192. void LEDLights2::setup() {
  193. neo2.init();
  194. TERN_(NEO2_USER_PRESET_STARTUP, set_default());
  195. }
  196. void LEDLights2::set_color(const LEDColor &incol) {
  197. const uint32_t neocolor = LEDColorWhite() == incol
  198. ? neo2.Color(NEO2_WHITE)
  199. : neo2.Color(incol.r, incol.g, incol.b OPTARG(HAS_WHITE_LED2, incol.w));
  200. neo2.set_brightness(incol.i);
  201. neo2.set_color(neocolor);
  202. #if ENABLED(LED_CONTROL_MENU)
  203. // Don't update the color when OFF
  204. lights_on = !incol.is_off();
  205. if (lights_on) color = incol;
  206. #endif
  207. }
  208. #if ENABLED(LED_CONTROL_MENU)
  209. void LEDLights2::toggle() { if (lights_on) set_off(); else update(); }
  210. #endif
  211. #endif // NEOPIXEL2_SEPARATE
  212. #endif // HAS_COLOR_LEDS