Add millis helper macros

This commit is contained in:
Scott Lahteine 2020-04-03 19:49:45 -05:00
parent 723d4d6f61
commit 0e06aaa2bc
10 changed files with 26 additions and 23 deletions

View File

@ -227,7 +227,7 @@ bool wait_for_heatup = true;
// Inactivity shutdown // Inactivity shutdown
millis_t max_inactive_time, // = 0 millis_t max_inactive_time, // = 0
stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL; stepper_inactive_time = SEC_TO_MS(DEFAULT_STEPPER_DEACTIVE_TIME);
#if PIN_EXISTS(CHDK) #if PIN_EXISTS(CHDK)
extern millis_t chdk_timeout; extern millis_t chdk_timeout;
@ -543,7 +543,7 @@ inline void manage_inactivity(const bool ignore_stepper_queue=false) {
#if ENABLED(EXTRUDER_RUNOUT_PREVENT) #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
&& ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL) && ELAPSED(ms, gcode.previous_move_ms + SEC_TO_MS(EXTRUDER_RUNOUT_SECONDS))
&& !planner.has_blocks_queued() && !planner.has_blocks_queued()
) { ) {
#if ENABLED(SWITCHING_EXTRUDER) #if ENABLED(SWITCHING_EXTRUDER)

View File

@ -25,5 +25,9 @@
typedef uint32_t millis_t; typedef uint32_t millis_t;
#define SEC_TO_MS(N) millis_t((N)*1000UL)
#define MIN_TO_MS(N) SEC_TO_MS((N)*60UL)
#define MS_TO_SEC(N) millis_t((N)/1000UL)
#define PENDING(NOW,SOON) ((int32_t)(NOW-(SOON))<0) #define PENDING(NOW,SOON) ((int32_t)(NOW-(SOON))<0)
#define ELAPSED(NOW,SOON) (!PENDING(NOW,SOON)) #define ELAPSED(NOW,SOON) (!PENDING(NOW,SOON))

View File

@ -91,7 +91,7 @@ void ControllerFan::update() {
// - If AutoMode is on and steppers have been enabled for CONTROLLERFAN_IDLE_TIME seconds. // - If AutoMode is on and steppers have been enabled for CONTROLLERFAN_IDLE_TIME seconds.
// - If System is on idle and idle fan speed settings is activated. // - If System is on idle and idle fan speed settings is activated.
set_fan_speed( set_fan_speed(
settings.auto_mode && lastMotorOn && PENDING(ms, lastMotorOn + settings.duration * 1000UL) settings.auto_mode && lastMotorOn && PENDING(ms, lastMotorOn + SEC_TO_MS(settings.duration))
? settings.active_speed : settings.idle_speed ? settings.active_speed : settings.idle_speed
); );

View File

@ -485,7 +485,7 @@ void wait_for_confirmation(const bool is_reload/*=false*/, const int8_t max_beep
#endif #endif
// Start the heater idle timers // Start the heater idle timers
const millis_t nozzle_timeout = (millis_t)(PAUSE_PARK_NOZZLE_TIMEOUT) * 1000UL; const millis_t nozzle_timeout = SEC_TO_MS(PAUSE_PARK_NOZZLE_TIMEOUT);
HOTEND_LOOP() thermalManager.hotend_idle[e].start(nozzle_timeout); HOTEND_LOOP() thermalManager.hotend_idle[e].start(nozzle_timeout);
@ -549,7 +549,7 @@ void wait_for_confirmation(const bool is_reload/*=false*/, const int8_t max_beep
show_continue_prompt(is_reload); show_continue_prompt(is_reload);
// Start the heater idle timers // Start the heater idle timers
const millis_t nozzle_timeout = (millis_t)(PAUSE_PARK_NOZZLE_TIMEOUT) * 1000UL; const millis_t nozzle_timeout = SEC_TO_MS(PAUSE_PARK_NOZZLE_TIMEOUT);
HOTEND_LOOP() thermalManager.hotend_idle[e].start(nozzle_timeout); HOTEND_LOOP() thermalManager.hotend_idle[e].start(nozzle_timeout);
#if ENABLED(HOST_PROMPT_SUPPORT) #if ENABLED(HOST_PROMPT_SUPPORT)

View File

@ -98,7 +98,7 @@ void Power::check() {
nextPowerCheck = ms + 2500UL; nextPowerCheck = ms + 2500UL;
if (is_power_needed()) if (is_power_needed())
power_on(); power_on();
else if (!lastPowerOn || ELAPSED(ms, lastPowerOn + (POWER_TIMEOUT) * 1000UL)) else if (!lastPowerOn || ELAPSED(ms, lastPowerOn + SEC_TO_MS(POWER_TIMEOUT)))
power_off(); power_off();
} }
} }

View File

@ -182,7 +182,7 @@ void GcodeSuite::G76() {
do_blocking_move_to(parkpos); do_blocking_move_to(parkpos);
// Wait for heatbed to reach target temp and probe to cool below target temp // Wait for heatbed to reach target temp and probe to cool below target temp
if (wait_for_temps(target_bed, target_probe, next_temp_report, millis() + 900UL * 1000UL)) { if (wait_for_temps(target_bed, target_probe, next_temp_report, millis() + MIN_TO_MS(15))) {
SERIAL_ECHOLNPGM("!Bed heating timeout."); SERIAL_ECHOLNPGM("!Bed heating timeout.");
break; break;
} }

View File

@ -988,7 +988,7 @@ void GcodeSuite::process_subcommands_now(char * gcode) {
break; break;
} }
} }
next_busy_signal_ms = ms + host_keepalive_interval * 1000UL; next_busy_signal_ms = ms + SEC_TO_MS(host_keepalive_interval);
} }
#endif // HOST_KEEPALIVE_FEATURE #endif // HOST_KEEPALIVE_FEATURE

View File

@ -106,8 +106,7 @@ void Stopwatch::reset() {
} }
millis_t Stopwatch::duration() { millis_t Stopwatch::duration() {
return ((isRunning() ? millis() : stopTimestamp) return accumulator + MS_TO_SEC((isRunning() ? millis() : stopTimestamp) - startTimestamp);
- startTimestamp) / 1000UL + accumulator;
} }
#if ENABLED(DEBUG_STOPWATCH) #if ENABLED(DEBUG_STOPWATCH)

View File

@ -399,7 +399,7 @@ volatile bool Temperature::raw_temps_ready = false;
const uint16_t watch_temp_period = GTV(WATCH_BED_TEMP_PERIOD, WATCH_TEMP_PERIOD); const uint16_t watch_temp_period = GTV(WATCH_BED_TEMP_PERIOD, WATCH_TEMP_PERIOD);
const uint8_t watch_temp_increase = GTV(WATCH_BED_TEMP_INCREASE, WATCH_TEMP_INCREASE); const uint8_t watch_temp_increase = GTV(WATCH_BED_TEMP_INCREASE, WATCH_TEMP_INCREASE);
const float watch_temp_target = target - float(watch_temp_increase + GTV(TEMP_BED_HYSTERESIS, TEMP_HYSTERESIS) + 1); const float watch_temp_target = target - float(watch_temp_increase + GTV(TEMP_BED_HYSTERESIS, TEMP_HYSTERESIS) + 1);
millis_t temp_change_ms = next_temp_ms + watch_temp_period * 1000UL; millis_t temp_change_ms = next_temp_ms + SEC_TO_MS(watch_temp_period);
float next_watch_temp = 0.0; float next_watch_temp = 0.0;
bool heated = false; bool heated = false;
#endif #endif
@ -546,7 +546,7 @@ volatile bool Temperature::raw_temps_ready = false;
if (!heated) { // If not yet reached target... if (!heated) { // If not yet reached target...
if (current_temp > next_watch_temp) { // Over the watch temp? if (current_temp > next_watch_temp) { // Over the watch temp?
next_watch_temp = current_temp + watch_temp_increase; // - set the next temp to watch for next_watch_temp = current_temp + watch_temp_increase; // - set the next temp to watch for
temp_change_ms = ms + watch_temp_period * 1000UL; // - move the expiration timer up temp_change_ms = ms + SEC_TO_MS(watch_temp_period); // - move the expiration timer up
if (current_temp > watch_temp_target) heated = true; // - Flag if target temperature reached if (current_temp > watch_temp_target) heated = true; // - Flag if target temperature reached
} }
else if (ELAPSED(ms, temp_change_ms)) // Watch timer expired else if (ELAPSED(ms, temp_change_ms)) // Watch timer expired
@ -2051,7 +2051,7 @@ void Temperature::init() {
#endif #endif
if (current >= tr_target_temperature[heater_index] - hysteresis_degc) { if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
sm.timer = millis() + period_seconds * 1000UL; sm.timer = millis() + SEC_TO_MS(period_seconds);
break; break;
} }
else if (PENDING(millis(), sm.timer)) break; else if (PENDING(millis(), sm.timer)) break;
@ -3124,7 +3124,7 @@ void Temperature::tick() {
millis_t residency_start_ms = 0; millis_t residency_start_ms = 0;
bool first_loop = true; bool first_loop = true;
// Loop until the temperature has stabilized // Loop until the temperature has stabilized
#define TEMP_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_RESIDENCY_TIME) * 1000UL)) #define TEMP_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + SEC_TO_MS(TEMP_RESIDENCY_TIME)))
#else #else
// Loop until the temperature is very close target // Loop until the temperature is very close target
#define TEMP_CONDITIONS (wants_to_cool ? isCoolingHotend(target_extruder) : isHeatingHotend(target_extruder)) #define TEMP_CONDITIONS (wants_to_cool ? isCoolingHotend(target_extruder) : isHeatingHotend(target_extruder))
@ -3160,7 +3160,7 @@ void Temperature::tick() {
#if TEMP_RESIDENCY_TIME > 0 #if TEMP_RESIDENCY_TIME > 0
SERIAL_ECHOPGM(" W:"); SERIAL_ECHOPGM(" W:");
if (residency_start_ms) if (residency_start_ms)
SERIAL_ECHO(long((((TEMP_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL)); SERIAL_ECHO(long((SEC_TO_MS(TEMP_RESIDENCY_TIME) - (now - residency_start_ms)) / 1000UL));
else else
SERIAL_CHAR('?'); SERIAL_CHAR('?');
#endif #endif
@ -3185,7 +3185,7 @@ void Temperature::tick() {
// Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time. // Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time.
if (temp_diff < TEMP_WINDOW) { if (temp_diff < TEMP_WINDOW) {
residency_start_ms = now; residency_start_ms = now;
if (first_loop) residency_start_ms += (TEMP_RESIDENCY_TIME) * 1000UL; if (first_loop) residency_start_ms += SEC_TO_MS(TEMP_RESIDENCY_TIME);
} }
} }
else if (temp_diff > TEMP_HYSTERESIS) { else if (temp_diff > TEMP_HYSTERESIS) {
@ -3247,7 +3247,7 @@ void Temperature::tick() {
millis_t residency_start_ms = 0; millis_t residency_start_ms = 0;
bool first_loop = true; bool first_loop = true;
// Loop until the temperature has stabilized // Loop until the temperature has stabilized
#define TEMP_BED_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_BED_RESIDENCY_TIME) * 1000UL)) #define TEMP_BED_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + SEC_TO_MS(TEMP_BED_RESIDENCY_TIME)))
#else #else
// Loop until the temperature is very close target // Loop until the temperature is very close target
#define TEMP_BED_CONDITIONS (wants_to_cool ? isCoolingBed() : isHeatingBed()) #define TEMP_BED_CONDITIONS (wants_to_cool ? isCoolingBed() : isHeatingBed())
@ -3284,7 +3284,7 @@ void Temperature::tick() {
#if TEMP_BED_RESIDENCY_TIME > 0 #if TEMP_BED_RESIDENCY_TIME > 0
SERIAL_ECHOPGM(" W:"); SERIAL_ECHOPGM(" W:");
if (residency_start_ms) if (residency_start_ms)
SERIAL_ECHO(long((((TEMP_BED_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL)); SERIAL_ECHO(long((SEC_TO_MS(TEMP_BED_RESIDENCY_TIME) - (now - residency_start_ms)) / 1000UL));
else else
SERIAL_CHAR('?'); SERIAL_CHAR('?');
#endif #endif
@ -3309,7 +3309,7 @@ void Temperature::tick() {
// Start the TEMP_BED_RESIDENCY_TIME timer when we reach target temp for the first time. // Start the TEMP_BED_RESIDENCY_TIME timer when we reach target temp for the first time.
if (temp_diff < TEMP_BED_WINDOW) { if (temp_diff < TEMP_BED_WINDOW) {
residency_start_ms = now; residency_start_ms = now;
if (first_loop) residency_start_ms += (TEMP_BED_RESIDENCY_TIME) * 1000UL; if (first_loop) residency_start_ms += SEC_TO_MS(TEMP_BED_RESIDENCY_TIME);
} }
} }
else if (temp_diff > TEMP_BED_HYSTERESIS) { else if (temp_diff > TEMP_BED_HYSTERESIS) {
@ -3373,7 +3373,7 @@ void Temperature::tick() {
millis_t residency_start_ms = 0; millis_t residency_start_ms = 0;
bool first_loop = true; bool first_loop = true;
// Loop until the temperature has stabilized // Loop until the temperature has stabilized
#define TEMP_CHAMBER_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL)) #define TEMP_CHAMBER_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + SEC_TO_MS(TEMP_CHAMBER_RESIDENCY_TIME)))
#else #else
// Loop until the temperature is very close target // Loop until the temperature is very close target
#define TEMP_CHAMBER_CONDITIONS (wants_to_cool ? isCoolingChamber() : isHeatingChamber()) #define TEMP_CHAMBER_CONDITIONS (wants_to_cool ? isCoolingChamber() : isHeatingChamber())
@ -3405,7 +3405,7 @@ void Temperature::tick() {
#if TEMP_CHAMBER_RESIDENCY_TIME > 0 #if TEMP_CHAMBER_RESIDENCY_TIME > 0
SERIAL_ECHOPGM(" W:"); SERIAL_ECHOPGM(" W:");
if (residency_start_ms) if (residency_start_ms)
SERIAL_ECHO(long((((TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL)); SERIAL_ECHO(long((SEC_TO_MS(TEMP_CHAMBER_RESIDENCY_TIME) - (now - residency_start_ms)) / 1000UL));
else else
SERIAL_CHAR('?'); SERIAL_CHAR('?');
#endif #endif
@ -3425,7 +3425,7 @@ void Temperature::tick() {
// Start the TEMP_CHAMBER_RESIDENCY_TIME timer when we reach target temp for the first time. // Start the TEMP_CHAMBER_RESIDENCY_TIME timer when we reach target temp for the first time.
if (temp_diff < TEMP_CHAMBER_WINDOW) { if (temp_diff < TEMP_CHAMBER_WINDOW) {
residency_start_ms = now; residency_start_ms = now;
if (first_loop) residency_start_ms += (TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL; if (first_loop) residency_start_ms += SEC_TO_MS(TEMP_CHAMBER_RESIDENCY_TIME);
} }
} }
else if (temp_diff > TEMP_CHAMBER_HYSTERESIS) { else if (temp_diff > TEMP_CHAMBER_HYSTERESIS) {

View File

@ -241,7 +241,7 @@ struct HeaterWatch {
const int16_t newtarget = curr + INCREASE; const int16_t newtarget = curr + INCREASE;
if (newtarget < tgt - HYSTERESIS - 1) { if (newtarget < tgt - HYSTERESIS - 1) {
target = newtarget; target = newtarget;
next_ms = millis() + PERIOD * 1000UL; next_ms = millis() + SEC_TO_MS(PERIOD);
return; return;
} }
} }