Browse Source

Convert Time class to template, instantiate LongTimer for longer and ShortTimer for shorter periods.

Marek Bel 6 years ago
parent
commit
02ecd08786
4 changed files with 38 additions and 18 deletions
  1. 3 3
      Firmware/Marlin_main.cpp
  2. 13 6
      Firmware/Timer.cpp
  3. 18 5
      Firmware/Timer.h
  4. 4 4
      Firmware/ultralcd.cpp

+ 3 - 3
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)))
     {

+ 13 - 6
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<typename T>
+Timer<T>::Timer() : m_isRunning(false), m_started()
 {
 }
 
 /**
  * @brief Start timer
  */
-void Timer::start()
+template<typename T>
+void Timer<T>::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<typename T>
+bool Timer<T>::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<unsigned long>;
+template class Timer<unsigned short>;

+ 18 - 5
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 T>
 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<unsigned long>;
+/**
+ * @brief Timer unsigned short specialization
+ *
+ * Maximum period is at least 65 seconds.
+ */
+using ShortTimer = Timer<unsigned short>;
+
 #endif /* TIMER_H */

+ 4 - 4
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;