2017-09-18 01:56:29 +02:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 15:00:57 +01:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-18 01:56:29 +02:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-09-18 01:56:29 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-07-23 05:20:14 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-09-18 01:56:29 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
2020-08-06 15:14:00 +02:00
|
|
|
#if ENABLED(CASE_LIGHT_ENABLE)
|
2017-09-18 01:56:29 +02:00
|
|
|
|
2020-08-06 15:14:00 +02:00
|
|
|
#include "caselight.h"
|
|
|
|
|
|
|
|
CaseLight caselight;
|
|
|
|
|
2020-10-23 05:31:48 +02:00
|
|
|
#if CASELIGHT_USES_BRIGHTNESS && !defined(CASE_LIGHT_DEFAULT_BRIGHTNESS)
|
|
|
|
#define CASE_LIGHT_DEFAULT_BRIGHTNESS 0 // For use on PWM pin as non-PWM just sets a default
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CASELIGHT_USES_BRIGHTNESS
|
|
|
|
uint8_t CaseLight::brightness = CASE_LIGHT_DEFAULT_BRIGHTNESS;
|
|
|
|
#endif
|
|
|
|
|
2020-08-06 15:14:00 +02:00
|
|
|
bool CaseLight::on = CASE_LIGHT_DEFAULT_ON;
|
2017-09-27 11:57:14 +02:00
|
|
|
|
2021-02-27 23:06:48 +01:00
|
|
|
#if CASE_LIGHT_IS_COLOR_LED
|
2021-05-06 13:10:18 +02:00
|
|
|
#include "leds/leds.h"
|
|
|
|
constexpr uint8_t init_case_light[] = CASE_LIGHT_DEFAULT_COLOR;
|
2021-05-24 04:33:22 +02:00
|
|
|
LEDColor CaseLight::color = { init_case_light[0], init_case_light[1], init_case_light[2] OPTARG(HAS_WHITE_LED, init_case_light[3]) };
|
2018-01-20 23:58:41 +01:00
|
|
|
#endif
|
|
|
|
|
2020-08-06 15:14:00 +02:00
|
|
|
void CaseLight::update(const bool sflag) {
|
2020-10-23 05:31:48 +02:00
|
|
|
#if CASELIGHT_USES_BRIGHTNESS
|
|
|
|
/**
|
|
|
|
* The brightness_sav (and sflag) is needed because ARM chips ignore
|
|
|
|
* a "WRITE(CASE_LIGHT_PIN,x)" command to the pins that are directly
|
|
|
|
* controlled by the PWM module. In order to turn them off the brightness
|
|
|
|
* level needs to be set to OFF. Since we can't use the PWM register to
|
|
|
|
* save the last brightness level we need a variable to save it.
|
|
|
|
*/
|
|
|
|
static uint8_t brightness_sav; // Save brightness info for restore on "M355 S1"
|
|
|
|
|
|
|
|
if (on || !sflag)
|
|
|
|
brightness_sav = brightness; // Save brightness except for M355 S0
|
|
|
|
if (sflag && on)
|
|
|
|
brightness = brightness_sav; // Restore last brightness for M355 S1
|
|
|
|
|
2021-05-23 04:12:53 +02:00
|
|
|
const uint8_t i = on ? brightness : 0, n10ct = ENABLED(INVERT_CASE_LIGHT) ? 255 - i : i;
|
2021-05-06 13:10:18 +02:00
|
|
|
UNUSED(n10ct);
|
2020-01-05 09:52:38 +01:00
|
|
|
#endif
|
2018-01-20 23:58:41 +01:00
|
|
|
|
2021-02-27 23:06:48 +01:00
|
|
|
#if CASE_LIGHT_IS_COLOR_LED
|
2021-05-26 01:38:23 +02:00
|
|
|
leds.set_color(LEDColor(color.r, color.g, color.b OPTARG(HAS_WHITE_LED, color.w), n10ct));
|
2021-02-27 23:06:48 +01:00
|
|
|
#else // !CASE_LIGHT_IS_COLOR_LED
|
2018-01-20 23:58:41 +01:00
|
|
|
|
2020-10-23 05:31:48 +02:00
|
|
|
#if CASELIGHT_USES_BRIGHTNESS
|
2021-02-27 23:06:48 +01:00
|
|
|
if (pin_is_pwm())
|
2021-11-02 06:47:16 +01:00
|
|
|
set_pwm_duty(pin_t(CASE_LIGHT_PIN), (
|
2019-10-06 05:32:50 +02:00
|
|
|
#if CASE_LIGHT_MAX_PWM == 255
|
|
|
|
n10ct
|
|
|
|
#else
|
|
|
|
map(n10ct, 0, 255, 0, CASE_LIGHT_MAX_PWM)
|
|
|
|
#endif
|
2020-01-05 09:52:38 +01:00
|
|
|
));
|
2019-06-07 14:23:42 +02:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2021-05-23 04:12:53 +02:00
|
|
|
const bool s = on ? TERN(INVERT_CASE_LIGHT, LOW, HIGH) : TERN(INVERT_CASE_LIGHT, HIGH, LOW);
|
2019-06-07 14:23:42 +02:00
|
|
|
WRITE(CASE_LIGHT_PIN, s ? HIGH : LOW);
|
|
|
|
}
|
2018-01-20 23:58:41 +01:00
|
|
|
|
2021-02-27 23:06:48 +01:00
|
|
|
#endif // !CASE_LIGHT_IS_COLOR_LED
|
|
|
|
|
|
|
|
#if ENABLED(CASE_LIGHT_USE_RGB_LED)
|
|
|
|
if (leds.lights_on) leds.update(); else leds.set_off();
|
|
|
|
#endif
|
2017-09-18 01:56:29 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 15:14:00 +02:00
|
|
|
#endif // CASE_LIGHT_ENABLE
|