Skip to content

Commit

Permalink
All set_power is OCR
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Sep 7, 2021
1 parent 05b1f38 commit 6e85b00
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
28 changes: 14 additions & 14 deletions Marlin/src/feature/spindle_laser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#endif

SpindleLaser cutter;
uint8_t SpindleLaser::power;
uint8_t SpindleLaser::ocr_power;
#if ENABLED(LASER_FEATURE)
cutter_test_pulse_t SpindleLaser::testPulse = 50; // Test fire Pulse time ms value.
#endif
Expand Down Expand Up @@ -118,51 +118,51 @@ void SpindleLaser::ena_pin_set(const bool enable) {
SpindleLaserEvent SpindleLaser::get_event(const uint8_t opwr) {
#if ENABLED(SPINDLE_LASER_PWM) && CUTTER_UNIT_IS(RPM)
if (cutter.unitPower == 0)
return power ? SpindleLaserEvent::TO_OFF : SpindleLaserEvent::OFF;
return ocr_power ? SpindleLaserEvent::TO_OFF : SpindleLaserEvent::OFF;
#endif

if (opwr == 0)
return power ? SpindleLaserEvent::TO_OFF : SpindleLaserEvent::OFF;
return ocr_power ? SpindleLaserEvent::TO_OFF : SpindleLaserEvent::OFF;
else
return power ? SpindleLaserEvent::ON : SpindleLaserEvent::TO_ON;
return ocr_power ? SpindleLaserEvent::ON : SpindleLaserEvent::TO_ON;
}

/**
* Set cutter power value for PWM, Servo, and on/off pin.
* Set cutter ocr_power value for PWM, Servo, and on/off pin.
*
* @param opwr Power value. Range 0 to MAX. When 0 disable spindle/laser.
*/
void SpindleLaser::set_power(const uint8_t opwr) {
void SpindleLaser::ocr_set_power(const uint8_t opwr) {
static uint8_t last_power_applied = 0;

switch (get_event(opwr)) {
case SpindleLaserEvent::ON:
if (opwr == last_power_applied) break;
last_power_applied = power = opwr;
last_power_applied = ocr_power = opwr;

#if ENABLED(SPINDLE_LASER_PWM)
ocr_set(power);
ocr_set(ocr_power);
isReady = true;
#endif

#if ENABLED(SPINDLE_SERVO)
MOVE_SERVO(SPINDLE_SERVO_NR, power);
MOVE_SERVO(SPINDLE_SERVO_NR, ocr_power);
#else
ena_pin_set(true);
isReady = true;
#endif
break;

case SpindleLaserEvent::TO_ON:
last_power_applied = power = opwr;
last_power_applied = ocr_power = opwr;

#if ENABLED(SPINDLE_LASER_PWM)
ocr_set(power);
ocr_set(ocr_power);
isReady = true;
#endif

#if ENABLED(SPINDLE_SERVO)
MOVE_SERVO(SPINDLE_SERVO_NR, power);
MOVE_SERVO(SPINDLE_SERVO_NR, ocr_power);
#else
ena_pin_set(true);
isReady = true;
Expand All @@ -174,15 +174,15 @@ void SpindleLaser::set_power(const uint8_t opwr) {
case SpindleLaserEvent::OFF: break;

case SpindleLaserEvent::TO_OFF:
last_power_applied = power = opwr;
last_power_applied = ocr_power = opwr;

#if ENABLED(SPINDLE_LASER_PWM)
isReady = false;
ocr_set(0);
#endif

#if ENABLED(SPINDLE_SERVO)
MOVE_SERVO(SPINDLE_SERVO_NR, power);
MOVE_SERVO(SPINDLE_SERVO_NR, ocr_power);
#else
isReady = false;
ena_pin_set(false);
Expand Down
22 changes: 10 additions & 12 deletions Marlin/src/feature/spindle_laser.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,13 @@ class SpindleLaser {
static inline bool enabled(const cutter_power_t opwr) { return opwr > 0; }
static inline bool enabled() { return enabled(power); }

static void set_power(const uint8_t inpow);
static void ocr_set_power(const uint8_t opwr);

FORCE_INLINE static void refresh() { set_power(power); }
FORCE_INLINE static void refresh() { ocr_set_power(power); }

#if ENABLED(SPINDLE_LASER_PWM)
public:

static inline void ocr_set_power(const uint8_t ocr) { set_power(ocr); }
static void ocr_set(const uint8_t ocr);
static void ocr_off();

Expand Down Expand Up @@ -194,18 +193,18 @@ class SpindleLaser {
* @param enable true = enable; false = disable
*/
static inline void set_enabled(const bool enable) {
uint8_t value = 0;
uint8_t opwr = 0;
if (enable) {
#if ENABLED(SPINDLE_LASER_PWM)
if (power)
value = power;
opwr = power;
else if (unitPower)
value = upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP));
opwr = upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP));
#else
value = 255;
opwr = 255;
#endif
}
set_power(value);
ocr_set_power(opwr);
}

static inline void disable() { isReady = false; set_enabled(false); }
Expand Down Expand Up @@ -334,10 +333,9 @@ class SpindleLaser {
static inline void inline_direction(const bool) { /* never */ }

#if ENABLED(SPINDLE_LASER_PWM)
static inline void inline_ocr_power(const uint8_t ocrpwr) {
isReady = ocrpwr > 0;
planner.laser_inline.status.isEnabled = ocrpwr > 0;
planner.laser_inline.power = ocrpwr;
static inline void inline_ocr_power(const uint8_t opwr) {
planner.laser_inline.status.isEnabled = isReady = opwr > 0;
planner.laser_inline.power = opwr;
}
#endif
#endif // LASER_POWER_INLINE
Expand Down
8 changes: 4 additions & 4 deletions Marlin/src/gcode/control/M3-M5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void GcodeSuite::M3_M4(const bool is_M4) {
}
else
cutter.unitPower = cutter.cpwr_to_upwr(SPEED_POWER_STARTUP);
return cutter.unitPower;
return cutter.upower_to_ocr(cutter.unitPower);
};

#if ENABLED(LASER_POWER_INLINE)
Expand All @@ -92,7 +92,7 @@ void GcodeSuite::M3_M4(const bool is_M4) {
cutter.inline_ocr_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t)
}
else
cutter.inline_power(cutter.upower_to_ocr(get_s_power()));
cutter.inline_power(get_s_power());
#else
cutter.set_inline_enabled(true);
#endif
Expand All @@ -111,9 +111,9 @@ void GcodeSuite::M3_M4(const bool is_M4) {
cutter.ocr_set_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t)
}
else
cutter.set_power(cutter.upower_to_ocr(get_s_power()));
cutter.ocr_set_power(get_s_power());
#elif ENABLED(SPINDLE_SERVO)
cutter.set_power(get_s_power());
cutter.ocr_set_power(get_s_power());
#else
cutter.set_enabled(true);
#endif
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/module/planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2133,7 +2133,7 @@ bool Planner::_populate_block(block_t * const block, bool split_move,

TERN_(MIXING_EXTRUDER, mixer.populate_block(block->b_color));

TERN_(HAS_CUTTER, block->cutter_power = cutter.power);
TERN_(HAS_CUTTER, block->cutter_ocr_power = cutter.ocr_power);

#if HAS_FAN
FANS_LOOP(i) block->fan_speed[i] = thermalManager.fan_speed[i];
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/module/planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ typedef struct block_t {
#endif

#if HAS_CUTTER
cutter_power_t cutter_power; // Power level for Spindle, Laser, etc.
cutter_power_t cutter_ocr_power; // Power level for Spindle, Laser, etc.
#endif

#if HAS_FAN
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/module/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2070,7 +2070,7 @@ uint32_t Stepper::block_phase_isr() {

// For non-inline cutter, grossly apply power
#if ENABLED(LASER_FEATURE) && DISABLED(LASER_POWER_INLINE)
cutter.set_power(current_block->cutter_power);
cutter.ocr_set_power(current_block->cutter_ocr_power);
#endif

TERN_(POWER_LOSS_RECOVERY, recovery.info.sdpos = current_block->sdpos);
Expand Down

0 comments on commit 6e85b00

Please sign in to comment.