stopwatch.h 3.22 KB
Newer Older
MagoKimbra's avatar
MagoKimbra committed
1
/**
MagoKimbra's avatar
MagoKimbra committed
2
 * MK & MK4due 3D Printer Firmware
MagoKimbra's avatar
MagoKimbra committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Based on Marlin, Sprinter and grbl
 * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
 * Copyright (C) 2013 - 2016 Alberto Cotronei @MagoKimbra
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef STOPWATCH_H
  #define STOPWATCH_H

  // Print debug messages with M111 S2 (Uses 156 bytes of PROGMEM)
  //#define DEBUG_STOPWATCH

MagoKimbra's avatar
MagoKimbra committed
29
  enum StopwatchState {
MagoKimbra's avatar
MagoKimbra committed
30 31 32
    STOPWATCH_STOPPED,
    STOPWATCH_RUNNING,
    STOPWATCH_PAUSED
MagoKimbra's avatar
MagoKimbra committed
33 34 35 36 37 38 39 40 41
  };

  /**
   * @brief Stopwatch class
   * @details This class acts as a timer proving stopwatch functionality including
   * the ability to pause the running time counter.
   */
  class Stopwatch {
    private:
MagoKimbra's avatar
MagoKimbra committed
42
      StopwatchState state;
MagoKimbra's avatar
MagoKimbra committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56
      uint16_t accumulator;
      uint32_t startTimestamp;
      uint32_t stopTimestamp;

    public:
      /**
       * @brief Class constructor
       */
      Stopwatch();

      /**
       * @brief Stops the stopwatch
       * @details Stops the running timer, it will silently ignore the request if
       * no timer is currently running.
MagoKimbra's avatar
MagoKimbra committed
57
       * @return true is method was successful
MagoKimbra's avatar
MagoKimbra committed
58
       */
MagoKimbra's avatar
MagoKimbra committed
59
      bool stop();
MagoKimbra's avatar
MagoKimbra committed
60 61 62 63 64

      /**
       * @brief Pauses the stopwatch
       * @details Pauses the running timer, it will silently ignore the request if
       * no timer is currently running.
MagoKimbra's avatar
MagoKimbra committed
65
       * @return true is method was successful
MagoKimbra's avatar
MagoKimbra committed
66
       */
MagoKimbra's avatar
MagoKimbra committed
67
      bool pause();
MagoKimbra's avatar
MagoKimbra committed
68 69 70 71 72

      /**
       * @brief Starts the stopwatch
       * @details Starts the timer, it will silently ignore the request if the
       * timer is already running.
MagoKimbra's avatar
MagoKimbra committed
73
       * @return true is method was successful
MagoKimbra's avatar
MagoKimbra committed
74
       */
MagoKimbra's avatar
MagoKimbra committed
75
      bool start();
MagoKimbra's avatar
MagoKimbra committed
76 77 78 79 80 81 82 83 84 85

      /**
       * @brief Resets the stopwatch
       * @details Resets all settings to their default values.
       */
      void reset();

      /**
       * @brief Checks if the timer is running
       * @details Returns true if the timer is currently running, false otherwise.
MagoKimbra's avatar
MagoKimbra committed
86
       * @return true if stopwatch is running
MagoKimbra's avatar
MagoKimbra committed
87 88 89 90 91 92
       */
      bool isRunning();

      /**
       * @brief Checks if the timer is paused
       * @details Returns true if the timer is currently paused, false otherwise.
MagoKimbra's avatar
MagoKimbra committed
93
       * @return true if stopwatch is paused
MagoKimbra's avatar
MagoKimbra committed
94 95 96 97 98 99
       */
      bool isPaused();

      /**
       * @brief Gets the running time
       * @details Returns the total number of seconds the timer has been running.
MagoKimbra's avatar
MagoKimbra committed
100
       * @return the delta since starting the stopwatch
MagoKimbra's avatar
MagoKimbra committed
101 102 103 104 105 106 107 108 109 110 111 112 113
       */
      uint16_t duration();

      #if ENABLED(DEBUG_STOPWATCH)

        /**
         * @brief Prints a debug message
         * @details Prints a simple debug message "Stopwatch::function"
         */
        static void debug(const char func[]);

      #endif
  };
MagoKimbra's avatar
MagoKimbra committed
114
#endif // STOPWATCH_H