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

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