From 719404803b7b5967d49afda72fe17660bf5388ea Mon Sep 17 00:00:00 2001 From: Ashammaru Date: Sun, 27 Dec 2020 02:01:54 +0100 Subject: [PATCH] Fix SPINDLE_LASER_FREQUENCY (#20509) --- Marlin/src/feature/spindle_laser.cpp | 21 ++++++++++++++------- Marlin/src/feature/spindle_laser.h | 8 +++++++- Marlin/src/lcd/menu/menu_spindle_laser.cpp | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Marlin/src/feature/spindle_laser.cpp b/Marlin/src/feature/spindle_laser.cpp index 3f6523478..7e17f393c 100644 --- a/Marlin/src/feature/spindle_laser.cpp +++ b/Marlin/src/feature/spindle_laser.cpp @@ -43,7 +43,7 @@ cutter_power_t SpindleLaser::menuPower, // Power s #if ENABLED(MARLIN_DEV_MODE) cutter_frequency_t SpindleLaser::frequency; // PWM frequency setting; range: 2K - 50K #endif -#define SPINDLE_LASER_PWM_OFF ((SPINDLE_LASER_PWM_INVERT) ? 255 : 0) +#define SPINDLE_LASER_PWM_OFF TERN(SPINDLE_LASER_PWM_INVERT, 255, 0) // // Init the cutter to a safe OFF state @@ -71,16 +71,23 @@ void SpindleLaser::init() { /** * Set the cutter PWM directly to the given ocr value */ - void SpindleLaser::set_ocr(const uint8_t ocr) { - WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_STATE); // Turn spindle on - analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF); + void SpindleLaser::_set_ocr(const uint8_t ocr) { #if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY + set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), TERN(MARLIN_DEV_MODE, frequency, SPINDLE_LASER_FREQUENCY)); set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF); + #else + analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF); #endif } + + void SpindleLaser::set_ocr(const uint8_t ocr) { + WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_STATE); // Cutter ON + _set_ocr(ocr); + } + void SpindleLaser::ocr_off() { - WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Turn spindle off - analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte + WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Cutter OFF + _set_ocr(0); } #endif @@ -97,7 +104,7 @@ void SpindleLaser::apply_power(const uint8_t opwr) { ocr_off(); isReady = false; } - else if (enabled() || ENABLED(CUTTER_POWER_RELATIVE)) { + else if (ENABLED(CUTTER_POWER_RELATIVE) || enabled()) { set_ocr(power); isReady = true; } diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 4d3c80241..b0b9c01ec 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -117,6 +117,12 @@ public: #if ENABLED(SPINDLE_LASER_PWM) + private: + + static void _set_ocr(const uint8_t ocr); + + public: + static void set_ocr(const uint8_t ocr); static inline void set_ocr_power(const uint8_t ocr) { power = ocr; set_ocr(ocr); } static void ocr_off(); @@ -143,7 +149,7 @@ public: #elif CUTTER_UNIT_IS(RPM) 2 #else - #error "???" + #error "CUTTER_UNIT_IS(???)" #endif )); } diff --git a/Marlin/src/lcd/menu/menu_spindle_laser.cpp b/Marlin/src/lcd/menu/menu_spindle_laser.cpp index d5a291db7..f11b23d99 100644 --- a/Marlin/src/lcd/menu/menu_spindle_laser.cpp +++ b/Marlin/src/lcd/menu/menu_spindle_laser.cpp @@ -59,7 +59,7 @@ #endif #if BOTH(MARLIN_DEV_MODE, HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY) - EDIT_ITEM_FAST(CUTTER_MENU_FREQUENCY_TYPE, MSG_CUTTER_FREQUENCY, &cutter.frequency, 2000, 50000, cutter.refresh_frequency); + EDIT_ITEM_FAST(CUTTER_MENU_FREQUENCY_TYPE, MSG_CUTTER_FREQUENCY, &cutter.frequency, 2000, 80000, cutter.refresh_frequency); #endif END_MENU();