🎨 Misc. Spindle/Laser (etc.) cleanup

This commit is contained in:
Scott Lahteine
2021-09-06 15:34:12 -05:00
parent dc6b86065e
commit 845d42ef40
5 changed files with 70 additions and 58 deletions

View File

@@ -132,54 +132,50 @@ public:
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 inline void ocr_set_power(const uint8_t ocr) { power = ocr; set_ocr(ocr); }
static void ocr_off();
// Used to update output for power->OCR translation
/**
* Update output for power->OCR translation
*/
static inline uint8_t upower_to_ocr(const cutter_power_t upwr) {
return (
return uint8_t(
#if CUTTER_UNIT_IS(PWM255)
uint8_t(upwr)
upwr
#elif CUTTER_UNIT_IS(PERCENT)
pct_to_ocr(upwr)
#else
uint8_t(pct_to_ocr(cpwr_to_pct(upwr)))
pct_to_ocr(cpwr_to_pct(upwr))
#endif
);
}
// Correct power to configured range
/**
* Correct power to configured range
*/
static inline cutter_power_t power_to_range(const cutter_power_t pwr) {
return power_to_range(pwr, (
#if CUTTER_UNIT_IS(PWM255)
0
#elif CUTTER_UNIT_IS(PERCENT)
1
#elif CUTTER_UNIT_IS(RPM)
2
#else
#error "CUTTER_UNIT_IS(unknown)"
#endif
));
return power_to_range(pwr, _CUTTER_POWER(CUTTER_POWER_UNIT));
}
static inline cutter_power_t power_to_range(const cutter_power_t pwr, const uint8_t pwrUnit) {
if (pwr <= 0) return 0;
cutter_power_t upwr;
switch (pwrUnit) {
case 0: // PWM
case _CUTTER_POWER_PWM255:
upwr = cutter_power_t(
(pwr < pct_to_ocr(min_pct)) ? pct_to_ocr(min_pct) // Use minimum if set below
: (pwr > pct_to_ocr(max_pct)) ? pct_to_ocr(max_pct) // Use maximum if set above
: pwr
);
break;
case 1: // PERCENT
case _CUTTER_POWER_PERCENT:
upwr = cutter_power_t(
(pwr < min_pct) ? min_pct // Use minimum if set below
: (pwr > max_pct) ? max_pct // Use maximum if set above
: pwr // PCT
);
break;
case 2: // RPM
case _CUTTER_POWER_RPM:
upwr = cutter_power_t(
(pwr < SPEED_POWER_MIN) ? SPEED_POWER_MIN // Use minimum if set below
: (pwr > SPEED_POWER_MAX) ? SPEED_POWER_MAX // Use maximum if set above
@@ -190,14 +186,34 @@ public:
}
return upwr;
}
#endif // SPINDLE_LASER_PWM
/**
* Enable/Disable spindle/laser
* @param enable true = enable; false = disable
*/
static inline void set_enabled(const bool enable) {
set_power(enable ? TERN(SPINDLE_LASER_PWM, (power ?: (unitPower ? upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP)) : 0)), 255) : 0);
uint8_t value = 0;
if (enable) {
#if ENABLED(SPINDLE_LASER_PWM)
if (power)
value = power;
else if (unitPower)
value = upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP));
#else
value = 255;
#endif
}
set_power(value);
}
// Wait for spindle to spin up or spin down
static inline void disable() { isReady = false; set_enabled(false); }
/**
* Wait for spindle to spin up or spin down
*
* @param on true = state to on; false = state to off.
*/
static inline void power_delay(const bool on) {
#if DISABLED(LASER_POWER_INLINE)
safe_delay(on ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY);
@@ -230,8 +246,6 @@ public:
}
#endif
static inline void disable() { isReady = false; set_enabled(false); }
#if HAS_LCD_MENU
static inline void enable_with_dir(const bool reverse) {
isReady = true;
@@ -325,7 +339,7 @@ public:
planner.laser_inline.power = ocrpwr;
}
#endif
#endif // LASER_POWER_INLINE
#endif // LASER_POWER_INLINE
static inline void kill() {
TERN_(LASER_POWER_INLINE, inline_disable());