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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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 <http://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 "SailfishRGB_LED.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. #if ENABLED(NEOPIXEL_LED)
  61. setup_neopixel();
  62. #endif
  63. #if ENABLED(PCA9533)
  64. RGBinit();
  65. #endif
  66. #if ENABLED(LED_USER_PRESET_STARTUP)
  67. set_default();
  68. #endif
  69. }
  70. void LEDLights::set_color(const LEDColor &incol
  71. #if ENABLED(NEOPIXEL_LED)
  72. , bool isSequence/*=false*/
  73. #endif
  74. ) {
  75. #if ENABLED(NEOPIXEL_LED)
  76. const uint32_t neocolor = LEDColorWhite() == incol
  77. ? pixels.Color(NEO_WHITE)
  78. : pixels.Color(incol.r, incol.g, incol.b, incol.w);
  79. static uint16_t nextLed = 0;
  80. #ifdef NEOPIXEL_BKGD_LED_INDEX
  81. if (NEOPIXEL_BKGD_LED_INDEX == nextLed) { nextLed++; return; }
  82. #endif
  83. pixels.setBrightness(incol.i);
  84. if (!isSequence)
  85. set_neopixel_color(neocolor);
  86. else {
  87. pixels.setPixelColor(nextLed, neocolor);
  88. pixels.show();
  89. if (++nextLed >= pixels.numPixels()) nextLed = 0;
  90. return;
  91. }
  92. #endif
  93. #if ENABLED(BLINKM)
  94. // This variant uses i2c to send the RGB components to the device.
  95. blinkm_set_led_color(incol);
  96. #endif
  97. #if EITHER(RGB_LED, RGBW_LED)
  98. // This variant uses 3-4 separate pins for the RGB(W) components.
  99. // If the pins can do PWM then their intensity will be set.
  100. #define UPDATE_RGBW(C,c) do{ if (PWM_PIN(RGB_LED_##C##_PIN)) analogWrite(RGB_LED_##C##_PIN, incol.c); else WRITE(RGB_LED_##C##_PIN, incol.c ? HIGH : LOW); }while(0)
  101. UPDATE_RGBW(R,r);
  102. UPDATE_RGBW(G,g);
  103. UPDATE_RGBW(B,b);
  104. #if ENABLED(RGBW_LED)
  105. UPDATE_RGBW(W,w);
  106. #endif
  107. #endif
  108. #if ENABLED(PCA9632)
  109. // Update I2C LED driver
  110. pca9632_set_led_color(incol);
  111. #endif
  112. #if ENABLED(PCA9533)
  113. RGBsetColor(incol.r, incol.g, incol.b, true);
  114. #endif
  115. #if EITHER(LED_CONTROL_MENU, PRINTER_EVENT_LEDS)
  116. // Don't update the color when OFF
  117. lights_on = !incol.is_off();
  118. if (lights_on) color = incol;
  119. #endif
  120. }
  121. #if ENABLED(LED_CONTROL_MENU)
  122. void LEDLights::toggle() { if (lights_on) set_off(); else update(); }
  123. #endif
  124. #endif // HAS_COLOR_LEDS