Refactor PRINTER_EVENT_LEDS, apply to M303 (#12038)
Co-Authored-By: Giuliano Zaro <gmagician@users.noreply.github.com>
This commit is contained in:
		
				
					committed by
					
						
						Scott Lahteine
					
				
			
			
				
	
			
			
			
						parent
						
							d8d76cd2ba
						
					
				
				
					commit
					d43d4e4219
				
			@@ -19,15 +19,13 @@
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * leds.h - Marlin general RGB LED support
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef __LEDS_H__
 | 
			
		||||
#define __LEDS_H__
 | 
			
		||||
 | 
			
		||||
#include "../../inc/MarlinConfig.h"
 | 
			
		||||
#include "../../inc/MarlinConfigPre.h"
 | 
			
		||||
 | 
			
		||||
#if ENABLED(NEOPIXEL_LED)
 | 
			
		||||
  #include "neopixel.h"
 | 
			
		||||
@@ -180,5 +178,3 @@ public:
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
extern LEDLights leds;
 | 
			
		||||
 | 
			
		||||
#endif // __LEDS_H__
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										81
									
								
								Marlin/src/feature/leds/printer_event_leds.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								Marlin/src/feature/leds/printer_event_leds.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,81 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Marlin 3D Printer Firmware
 | 
			
		||||
 * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
 | 
			
		||||
 *
 | 
			
		||||
 * Based on Sprinter and grbl.
 | 
			
		||||
 * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
 | 
			
		||||
 *
 | 
			
		||||
 * 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
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * printer_event_leds.cpp - LED color changing based on printer status
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "../../inc/MarlinConfigPre.h"
 | 
			
		||||
 | 
			
		||||
#if ENABLED(PRINTER_EVENT_LEDS)
 | 
			
		||||
 | 
			
		||||
#include "printer_event_leds.h"
 | 
			
		||||
 | 
			
		||||
PrinterEventLEDs printerEventLEDs;
 | 
			
		||||
 | 
			
		||||
#if HAS_LEDS_OFF_FLAG
 | 
			
		||||
  bool PrinterEventLEDs::leds_off_after_print; // = false
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if HAS_TEMP_HOTEND || HAS_HEATED_BED
 | 
			
		||||
 | 
			
		||||
  uint8_t PrinterEventLEDs::old_intensity = 0;
 | 
			
		||||
 | 
			
		||||
  inline uint8_t pel_intensity(const float &start, const float ¤t, const float &target) {
 | 
			
		||||
    return (uint8_t)map(constrain(current, start, target), start, target, 0.f, 255.f);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  inline void pel_set_rgb(const uint8_t r, const uint8_t g, const uint8_t b) {
 | 
			
		||||
    leds.set_color(
 | 
			
		||||
      MakeLEDColor(r, g, b, 0, pixels.getBrightness())
 | 
			
		||||
        #if ENABLED(NEOPIXEL_IS_SEQUENTIAL)
 | 
			
		||||
          , true
 | 
			
		||||
        #endif
 | 
			
		||||
      );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if HAS_TEMP_HOTEND
 | 
			
		||||
 | 
			
		||||
  void PrinterEventLEDs::onHotendHeating(const float &start, const float ¤t, const float &target) {
 | 
			
		||||
    const uint8_t blue = pel_intensity(start, current, target);
 | 
			
		||||
    if (blue != old_intensity) {
 | 
			
		||||
      old_intensity = blue;
 | 
			
		||||
      pel_set_rgb(255, 0, 255 - blue);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if HAS_HEATED_BED
 | 
			
		||||
 | 
			
		||||
  void PrinterEventLEDs::onBedHeating(const float &start, const float ¤t, const float &target) {
 | 
			
		||||
    const uint8_t red = pel_intensity(start, current, target);
 | 
			
		||||
    if (red != old_intensity) {
 | 
			
		||||
      old_intensity = red;
 | 
			
		||||
      pel_set_rgb(red, 0, 255);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif // PRINTER_EVENT_LEDS
 | 
			
		||||
							
								
								
									
										79
									
								
								Marlin/src/feature/leds/printer_event_leds.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								Marlin/src/feature/leds/printer_event_leds.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Marlin 3D Printer Firmware
 | 
			
		||||
 * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
 | 
			
		||||
 *
 | 
			
		||||
 * Based on Sprinter and grbl.
 | 
			
		||||
 * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
 | 
			
		||||
 *
 | 
			
		||||
 * 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
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * printer_event_leds.h - LED color changing based on printer status
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "leds.h"
 | 
			
		||||
#include "../../inc/MarlinConfig.h"
 | 
			
		||||
 | 
			
		||||
class PrinterEventLEDs {
 | 
			
		||||
private:
 | 
			
		||||
  static uint8_t old_intensity;
 | 
			
		||||
 | 
			
		||||
  #if HAS_LEDS_OFF_FLAG
 | 
			
		||||
    static bool leds_off_after_print;
 | 
			
		||||
  #endif
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
  #if HAS_TEMP_HOTEND
 | 
			
		||||
    FORCE_INLINE static void onHotendHeatingStart() { old_intensity = 0; }
 | 
			
		||||
    static void onHotendHeating(const float &start, const float ¤t, const float &target);
 | 
			
		||||
  #endif
 | 
			
		||||
 | 
			
		||||
  #if HAS_HEATED_BED
 | 
			
		||||
    FORCE_INLINE static void onBedHeatingStart() { old_intensity = 127; }
 | 
			
		||||
    static void onBedHeating(const float &start, const float ¤t, const float &target);
 | 
			
		||||
  #endif
 | 
			
		||||
 | 
			
		||||
  #if HAS_TEMP_HOTEND || HAS_HEATED_BED
 | 
			
		||||
    FORCE_INLINE static void onHeated()     { leds.set_white(); }
 | 
			
		||||
    FORCE_INLINE static void onHeatersOff() { leds.set_off(); }
 | 
			
		||||
  #endif
 | 
			
		||||
 | 
			
		||||
  #if ENABLED(SDSUPPORT)
 | 
			
		||||
 | 
			
		||||
    FORCE_INLINE static void onPrintCompleted() {
 | 
			
		||||
      leds.set_green();
 | 
			
		||||
      #if HAS_LEDS_OFF_FLAG
 | 
			
		||||
        leds_off_after_print = true;
 | 
			
		||||
      #else
 | 
			
		||||
        safe_delay(2000);
 | 
			
		||||
        leds.set_off();
 | 
			
		||||
      #endif
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    FORCE_INLINE static void onResumeAfterWait() {
 | 
			
		||||
      #if HAS_LEDS_OFF_FLAG
 | 
			
		||||
        if (leds_off_after_print) {
 | 
			
		||||
          leds.set_off();
 | 
			
		||||
          leds_off_after_print = false;
 | 
			
		||||
        }
 | 
			
		||||
      #endif
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  #endif // SDSUPPORT
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
extern PrinterEventLEDs printerEventLEDs;
 | 
			
		||||
@@ -32,7 +32,7 @@
 | 
			
		||||
#include "../../module/temperature.h"
 | 
			
		||||
 | 
			
		||||
void handle_status_leds(void) {
 | 
			
		||||
  static bool red_led = false;
 | 
			
		||||
  static uint8_t red_led = LOW;
 | 
			
		||||
  static millis_t next_status_led_update_ms = 0;
 | 
			
		||||
  if (ELAPSED(millis(), next_status_led_update_ms)) {
 | 
			
		||||
    next_status_led_update_ms += 500; // Update every 0.5s
 | 
			
		||||
@@ -42,16 +42,16 @@ void handle_status_leds(void) {
 | 
			
		||||
    #endif
 | 
			
		||||
    HOTEND_LOOP()
 | 
			
		||||
      max_temp = MAX(max_temp, thermalManager.degHotend(e), thermalManager.degTargetHotend(e));
 | 
			
		||||
    const bool new_led = (max_temp > 55.0) ? true : (max_temp < 54.0) ? false : red_led;
 | 
			
		||||
    const uint8_t new_led = (max_temp > 55.0) ? HIGH : (max_temp < 54.0) ? LOW : red_led;
 | 
			
		||||
    if (new_led != red_led) {
 | 
			
		||||
      red_led = new_led;
 | 
			
		||||
      #if PIN_EXISTS(STAT_LED_RED)
 | 
			
		||||
        WRITE(STAT_LED_RED_PIN, new_led ? HIGH : LOW);
 | 
			
		||||
        WRITE(STAT_LED_RED_PIN, new_led);
 | 
			
		||||
        #if PIN_EXISTS(STAT_LED_BLUE)
 | 
			
		||||
          WRITE(STAT_LED_BLUE_PIN, new_led ? LOW : HIGH);
 | 
			
		||||
          WRITE(STAT_LED_BLUE_PIN, !new_led);
 | 
			
		||||
        #endif
 | 
			
		||||
      #else
 | 
			
		||||
        WRITE(STAT_LED_BLUE_PIN, new_led ? HIGH : LOW);
 | 
			
		||||
        WRITE(STAT_LED_BLUE_PIN, new_led);
 | 
			
		||||
      #endif
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user