Skip to content

Commit

Permalink
Fix SPINDLE_LASER_FREQUENCY (MarlinFirmware#20509)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashammaru authored and zillarob committed Feb 25, 2021
1 parent 2a0dc00 commit 592f768
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
21 changes: 14 additions & 7 deletions Marlin/src/feature/spindle_laser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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
Expand All @@ -63,16 +63,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

Expand All @@ -89,7 +96,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;
}
Expand Down
8 changes: 7 additions & 1 deletion Marlin/src/feature/spindle_laser.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ class SpindleLaser {

#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();
Expand All @@ -137,7 +143,7 @@ class SpindleLaser {
#elif CUTTER_UNIT_IS(RPM)
2
#else
#error "???"
#error "CUTTER_UNIT_IS(???)"
#endif
));
}
Expand Down

0 comments on commit 592f768

Please sign in to comment.