From 02ecd0878692dd71c1fd1871740956d6152c82a9 Mon Sep 17 00:00:00 2001 From: Marek Bel Date: Fri, 11 May 2018 16:31:42 +0200 Subject: [PATCH] Convert Time class to template, instantiate LongTimer for longer and ShortTimer for shorter periods. --- Firmware/Marlin_main.cpp | 6 +++--- Firmware/Timer.cpp | 19 +++++++++++++------ Firmware/Timer.h | 23 ++++++++++++++++++----- Firmware/ultralcd.cpp | 8 ++++---- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index da4bfba1..bed50e8b 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -2532,7 +2532,7 @@ static void gcode_PRUSA_SN() selectedSerialPort = 0; MSerial.write(";S"); int numbersRead = 0; - Timer timeout; + ShortTimer timeout; timeout.start(); while (numbersRead < 19) { @@ -2543,7 +2543,7 @@ static void gcode_PRUSA_SN() numbersRead++; selectedSerialPort = 0; } - if (timeout.expired(100)) break; + if (timeout.expired(100u)) break; } selectedSerialPort = 1; MSerial.write('\n'); @@ -7140,7 +7140,7 @@ static void handleSafetyTimer() #if (EXTRUDERS > 1) #error Implemented only for one extruder. #endif //(EXTRUDERS > 1) - static Timer safetyTimer; + static LongTimer safetyTimer; if (IS_SD_PRINTING || is_usb_printing || isPrintPaused || (custom_message_type == 4) || (lcd_commands_type == LCD_COMMAND_V2_CAL) || (!degTargetBed() && !degTargetHotend(0))) { diff --git a/Firmware/Timer.cpp b/Firmware/Timer.cpp index 29866a67..ecf9b9b9 100644 --- a/Firmware/Timer.cpp +++ b/Firmware/Timer.cpp @@ -12,14 +12,16 @@ * It is guaranteed, that construction is equivalent with zeroing all members. * This property can be exploited in MenuData union. */ -Timer::Timer() : m_isRunning(false), m_started() +template +Timer::Timer() : m_isRunning(false), m_started() { } /** * @brief Start timer */ -void Timer::start() +template +void Timer::start() { m_started = millis(); m_isRunning = true; @@ -29,19 +31,21 @@ void Timer::start() * @brief Timer has expired * * Timer is considered expired after msPeriod has passed from time the timer was started. - * This function must be called at least each (unsigned long maximum value - msPeriod) milliseconds to be sure to + * Timer is stopped after expiration. + * This function must be called at least each (T maximum value - msPeriod) milliseconds to be sure to * catch first expiration. * This function is expected to handle wrap around of time register well. * - * @param msPeriod Time interval in milliseconds. + * @param msPeriod Time interval in milliseconds. Do not omit "ul" when using constant literal with LongTimer. * @retval true Timer has expired * @retval false Timer not expired yet, or is not running, or time window in which is timer considered expired passed. */ -bool Timer::expired(unsigned long msPeriod) +template +bool Timer::expired(T msPeriod) { if (!m_isRunning) return false; bool expired = false; - const unsigned long now = millis(); + const T now = millis(); if (m_started <= m_started + msPeriod) { if ((now >= m_started + msPeriod) || (now < m_started)) @@ -59,3 +63,6 @@ bool Timer::expired(unsigned long msPeriod) if (expired) m_isRunning = false; return expired; } + +template class Timer; +template class Timer; diff --git a/Firmware/Timer.h b/Firmware/Timer.h index 0d3a89dc..35173d39 100644 --- a/Firmware/Timer.h +++ b/Firmware/Timer.h @@ -10,10 +10,10 @@ * @brief simple timer * * Simple and memory saving implementation. Should handle timer register wrap around well. - * Maximum period is at least 49 days. Resolution is one millisecond. To save memory, doesn't store timer period. - * If you wish timer which is storing period, derive from this. If you need time intervals smaller than 65 seconds - * consider implementing timer with smaller underlying type. + * Resolution is one millisecond. To save memory, doesn't store timer period. + * If you wish timer which is storing period, derive from this. */ +template class Timer { public: @@ -21,10 +21,23 @@ public: void start(); void stop(){m_isRunning = false;} bool running(){return m_isRunning;} - bool expired(unsigned long msPeriod); + bool expired(T msPeriod); private: bool m_isRunning; - unsigned long m_started; + T m_started; }; +/** + * @brief Timer unsigned long specialization + * + * Maximum period is at least 49 days. + */ +using LongTimer = Timer; +/** + * @brief Timer unsigned short specialization + * + * Maximum period is at least 65 seconds. + */ +using ShortTimer = Timer; + #endif /* TIMER_H */ diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp index 3a7d521b..f0340fae 100644 --- a/Firmware/ultralcd.cpp +++ b/Firmware/ultralcd.cpp @@ -111,7 +111,7 @@ union MenuData struct AutoLoadFilamentMenu { - //Timer timer; + //ShortTimer timer; char dummy; } autoLoadFilamentMenu; struct _Lcd_moveMenu @@ -2089,13 +2089,13 @@ static void lcd_menu_AutoLoadFilament() } else { - Timer* ptimer = (Timer*)&(menuData.autoLoadFilamentMenu.dummy); + ShortTimer* ptimer = (ShortTimer*)&(menuData.autoLoadFilamentMenu.dummy); if (!ptimer->running()) ptimer->start(); lcd.setCursor(0, 0); lcd_printPGM(MSG_ERROR); lcd.setCursor(0, 2); lcd_printPGM(MSG_PREHEAT_NOZZLE); - if (ptimer->expired(2000ul)) menu_action_back(); + if (ptimer->expired(2000u)) menu_action_back(); } if (lcd_clicked()) menu_action_back(); } @@ -2645,7 +2645,7 @@ bool lcd_wait_for_pinda(float temp) { lcd_set_custom_characters_degree(); setTargetHotend(0, 0); setTargetBed(0); - Timer pinda_timeout; + LongTimer pinda_timeout; pinda_timeout.start(); bool target_temp_reached = true;