blob: c71e3c8104be0ad6052b8cd9dbbe12404ad67427 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#pragma once
9
10#include "Base.h"
11#include "HAL/cpp/priority_mutex.h"
12
13namespace frc {
14
15typedef void (*TimerInterruptHandler)(void* param);
16
17void Wait(double seconds);
18double GetClock();
19double GetTime();
20
21/**
22 * Timer objects measure accumulated time in seconds.
23 * The timer object functions like a stopwatch. It can be started, stopped, and
24 * cleared. When the timer is running its value counts up in seconds. When
25 * stopped, the timer holds the current value. The implementation simply records
26 * the time when started and subtracts the current time whenever the value is
27 * requested.
28 */
29class Timer {
30 public:
31 Timer();
32 virtual ~Timer() = default;
33
34 Timer(const Timer&) = delete;
35 Timer& operator=(const Timer&) = delete;
36
37 double Get() const;
38 void Reset();
39 void Start();
40 void Stop();
41 bool HasPeriodPassed(double period);
42
43 static double GetFPGATimestamp();
44 static double GetPPCTimestamp();
45 static double GetMatchTime();
46
47 // The time, in seconds, at which the 32-bit FPGA timestamp rolls over to 0
48 static const double kRolloverTime;
49
50 private:
51 double m_startTime = 0.0;
52 double m_accumulatedTime = 0.0;
53 bool m_running = false;
54 mutable priority_mutex m_mutex;
55};
56
57} // namespace frc