Fix SPINDLE_LASER_FREQUENCY (#20509)

This commit is contained in:
Ashammaru 2020-12-27 02:01:54 +01:00 committed by Scott Lahteine
parent f9dd484381
commit e3d7603d54
3 changed files with 22 additions and 9 deletions

View file

@ -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;
}

View file

@ -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
));
}

View file

@ -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();