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.

caselight.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include "../inc/MarlinConfig.h"
  23. #if ENABLED(CASE_LIGHT_ENABLE)
  24. #include "caselight.h"
  25. CaseLight caselight;
  26. #if CASELIGHT_USES_BRIGHTNESS && !defined(CASE_LIGHT_DEFAULT_BRIGHTNESS)
  27. #define CASE_LIGHT_DEFAULT_BRIGHTNESS 0 // For use on PWM pin as non-PWM just sets a default
  28. #endif
  29. #if CASELIGHT_USES_BRIGHTNESS
  30. uint8_t CaseLight::brightness = CASE_LIGHT_DEFAULT_BRIGHTNESS;
  31. #endif
  32. bool CaseLight::on = CASE_LIGHT_DEFAULT_ON;
  33. #if CASE_LIGHT_IS_COLOR_LED
  34. constexpr uint8_t init_case_light[] = CASE_LIGHT_DEFAULT_COLOR;
  35. LEDColor CaseLight::color = { init_case_light[0], init_case_light[1], init_case_light[2] OPTARG(HAS_WHITE_LED, init_case_light[3]) };
  36. #endif
  37. void CaseLight::update(const bool sflag) {
  38. #if CASELIGHT_USES_BRIGHTNESS
  39. /**
  40. * The brightness_sav (and sflag) is needed because ARM chips ignore
  41. * a "WRITE(CASE_LIGHT_PIN,x)" command to the pins that are directly
  42. * controlled by the PWM module. In order to turn them off the brightness
  43. * level needs to be set to OFF. Since we can't use the PWM register to
  44. * save the last brightness level we need a variable to save it.
  45. */
  46. static uint8_t brightness_sav; // Save brightness info for restore on "M355 S1"
  47. if (on || !sflag)
  48. brightness_sav = brightness; // Save brightness except for M355 S0
  49. if (sflag && on)
  50. brightness = brightness_sav; // Restore last brightness for M355 S1
  51. const uint8_t i = on ? brightness : 0, n10ct = ENABLED(INVERT_CASE_LIGHT) ? 255 - i : i;
  52. UNUSED(n10ct);
  53. #endif
  54. #if CASE_LIGHT_IS_COLOR_LED
  55. #if ENABLED(CASE_LIGHT_USE_NEOPIXEL)
  56. if (on)
  57. // Use current color of (NeoPixel) leds and new brightness level
  58. leds.set_color(LEDColor(leds.color.r, leds.color.g, leds.color.b OPTARG(HAS_WHITE_LED, leds.color.w) OPTARG(NEOPIXEL_LED, n10ct)));
  59. else
  60. // Switch off leds
  61. leds.set_off();
  62. #else
  63. // Use CaseLight color (CASE_LIGHT_DEFAULT_COLOR) and new brightness level
  64. leds.set_color(LEDColor(color.r, color.g, color.b OPTARG(HAS_WHITE_LED, color.w) OPTARG(NEOPIXEL_LED, n10ct)));
  65. #endif
  66. #else // !CASE_LIGHT_IS_COLOR_LED
  67. #if CASELIGHT_USES_BRIGHTNESS
  68. if (pin_is_pwm())
  69. hal.set_pwm_duty(pin_t(CASE_LIGHT_PIN), (
  70. #if CASE_LIGHT_MAX_PWM == 255
  71. n10ct
  72. #else
  73. map(n10ct, 0, 255, 0, CASE_LIGHT_MAX_PWM)
  74. #endif
  75. ));
  76. else
  77. #endif
  78. {
  79. const bool s = on ? TERN(INVERT_CASE_LIGHT, LOW, HIGH) : TERN(INVERT_CASE_LIGHT, HIGH, LOW);
  80. WRITE(CASE_LIGHT_PIN, s ? HIGH : LOW);
  81. }
  82. #endif // !CASE_LIGHT_IS_COLOR_LED
  83. #if ENABLED(CASE_LIGHT_USE_RGB_LED)
  84. if (leds.lights_on) leds.update(); else leds.set_off();
  85. #endif
  86. }
  87. #endif // CASE_LIGHT_ENABLE