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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 ENABLED(LED_COLOR_PRESETS)
  38. const LEDColor LEDLights::defaultLEDColor = MakeLEDColor(
  39. LED_USER_PRESET_RED, LED_USER_PRESET_GREEN, LED_USER_PRESET_BLUE,
  40. LED_USER_PRESET_WHITE, LED_USER_PRESET_BRIGHTNESS
  41. );
  42. #endif
  43. #if EITHER(LED_CONTROL_MENU, PRINTER_EVENT_LEDS)
  44. LEDColor LEDLights::color;
  45. bool LEDLights::lights_on;
  46. #endif
  47. LEDLights leds;
  48. void LEDLights::setup() {
  49. #if EITHER(RGB_LED, RGBW_LED)
  50. if (PWM_PIN(RGB_LED_R_PIN)) SET_PWM(RGB_LED_R_PIN); else SET_OUTPUT(RGB_LED_R_PIN);
  51. if (PWM_PIN(RGB_LED_G_PIN)) SET_PWM(RGB_LED_G_PIN); else SET_OUTPUT(RGB_LED_G_PIN);
  52. if (PWM_PIN(RGB_LED_B_PIN)) SET_PWM(RGB_LED_B_PIN); else SET_OUTPUT(RGB_LED_B_PIN);
  53. #if ENABLED(RGBW_LED)
  54. if (PWM_PIN(RGB_LED_W_PIN)) SET_PWM(RGB_LED_W_PIN); else SET_OUTPUT(RGB_LED_W_PIN);
  55. #endif
  56. #endif
  57. TERN_(NEOPIXEL_LED, neo.init());
  58. TERN_(PCA9533, PCA9533_init());
  59. TERN_(LED_USER_PRESET_STARTUP, set_default());
  60. }
  61. void LEDLights::set_color(const LEDColor &incol
  62. #if ENABLED(NEOPIXEL_LED)
  63. , bool isSequence/*=false*/
  64. #endif
  65. ) {
  66. #if ENABLED(NEOPIXEL_LED)
  67. const uint32_t neocolor = LEDColorWhite() == incol
  68. ? neo.Color(NEO_WHITE)
  69. : neo.Color(incol.r, incol.g, incol.b, incol.w);
  70. static uint16_t nextLed = 0;
  71. #ifdef NEOPIXEL_BKGD_LED_INDEX
  72. if (NEOPIXEL_BKGD_LED_INDEX == nextLed) {
  73. if (++nextLed >= neo.pixels()) nextLed = 0;
  74. return;
  75. }
  76. #endif
  77. neo.set_brightness(incol.i);
  78. if (isSequence) {
  79. neo.set_pixel_color(nextLed, neocolor);
  80. neo.show();
  81. if (++nextLed >= neo.pixels()) nextLed = 0;
  82. return;
  83. }
  84. neo.set_color(neocolor);
  85. #endif
  86. #if ENABLED(BLINKM)
  87. // This variant uses i2c to send the RGB components to the device.
  88. blinkm_set_led_color(incol);
  89. #endif
  90. #if EITHER(RGB_LED, RGBW_LED)
  91. // This variant uses 3-4 separate pins for the RGB(W) components.
  92. // If the pins can do PWM then their intensity will be set.
  93. #define UPDATE_RGBW(C,c) do { \
  94. if (PWM_PIN(RGB_LED_##C##_PIN)) \
  95. analogWrite(pin_t(RGB_LED_##C##_PIN), incol.c); \
  96. else \
  97. WRITE(RGB_LED_##C##_PIN, incol.c ? HIGH : LOW); \
  98. }while(0)
  99. UPDATE_RGBW(R,r); UPDATE_RGBW(G,g); UPDATE_RGBW(B,b);
  100. #if ENABLED(RGBW_LED)
  101. UPDATE_RGBW(W,w);
  102. #endif
  103. #endif
  104. // Update I2C LED driver
  105. TERN_(PCA9632, PCA9632_set_led_color(incol));
  106. TERN_(PCA9533, PCA9533_set_rgb(incol.r, incol.g, incol.b));
  107. #if EITHER(LED_CONTROL_MENU, PRINTER_EVENT_LEDS)
  108. // Don't update the color when OFF
  109. lights_on = !incol.is_off();
  110. if (lights_on) color = incol;
  111. #endif
  112. }
  113. #if ENABLED(LED_CONTROL_MENU)
  114. void LEDLights::toggle() { if (lights_on) set_off(); else update(); }
  115. #endif
  116. #ifdef LED_BACKLIGHT_TIMEOUT
  117. millis_t LEDLights::led_off_time; // = 0
  118. void LEDLights::update_timeout(const bool power_on) {
  119. const millis_t ms = millis();
  120. if (power_on)
  121. reset_timeout(ms);
  122. else if (ELAPSED(ms, led_off_time))
  123. set_off();
  124. }
  125. #endif
  126. #if ENABLED(NEOPIXEL2_SEPARATE)
  127. #if ENABLED(NEO2_COLOR_PRESETS)
  128. const LEDColor LEDLights2::defaultLEDColor = MakeLEDColor(
  129. NEO2_USER_PRESET_RED, NEO2_USER_PRESET_GREEN, NEO2_USER_PRESET_BLUE,
  130. NEO2_USER_PRESET_WHITE, NEO2_USER_PRESET_BRIGHTNESS
  131. );
  132. #endif
  133. #if ENABLED(LED_CONTROL_MENU)
  134. LEDColor LEDLights2::color;
  135. bool LEDLights2::lights_on;
  136. #endif
  137. LEDLights2 leds2;
  138. void LEDLights2::setup() {
  139. neo2.init();
  140. TERN_(NEO2_USER_PRESET_STARTUP, set_default());
  141. }
  142. void LEDLights2::set_color(const LEDColor &incol) {
  143. const uint32_t neocolor = LEDColorWhite() == incol
  144. ? neo2.Color(NEO2_WHITE)
  145. : neo2.Color(incol.r, incol.g, incol.b, incol.w);
  146. neo2.set_brightness(incol.i);
  147. neo2.set_color(neocolor);
  148. }
  149. #endif // NEOPIXEL2_SEPARATE
  150. #endif // HAS_COLOR_LEDS