blob: b3fe77da1a35c9ef0438217f3f3a051748c5ff1d [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05007
Brian Silverman26e4e522015-12-17 01:56:40 -05008#pragma once
9
10#include "Base.h"
11#include "HAL/cpp/priority_mutex.h"
12
13typedef void (*TimerInterruptHandler)(void *param);
14
15void Wait(double seconds);
16double GetClock();
17double GetTime();
18
19/**
20 * Timer objects measure accumulated time in seconds.
21 * The timer object functions like a stopwatch. It can be started, stopped, and
22 * cleared. When the
23 * timer is running its value counts up in seconds. When stopped, the timer
24 * holds the current
25 * value. The implementation simply records the time when started and subtracts
26 * the current time
27 * whenever the value is 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};