blob: 59086ae0a10c5625af16e0774f0d481dacc7ebef [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4b81d302019-12-14 20:53:14 -08002/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* 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#include <atomic>
9#include <chrono>
10#include <cstdio>
11#include <thread>
12
13#include <wpi/timestamp.h>
14
15#include "MockHooksInternal.h"
James Kuszmaul4b81d302019-12-14 20:53:14 -080016#include "NotifierInternal.h"
Brian Silverman41cdd3e2019-01-19 19:48:58 -080017
18static std::atomic<bool> programStarted{false};
19
20static std::atomic<uint64_t> programStartTime{0};
James Kuszmaul4b81d302019-12-14 20:53:14 -080021static std::atomic<uint64_t> programPauseTime{0};
Brian Silverman41cdd3e2019-01-19 19:48:58 -080022
23namespace hal {
24namespace init {
25void InitializeMockHooks() {}
26} // namespace init
27} // namespace hal
28
29namespace hal {
James Kuszmaul4b81d302019-12-14 20:53:14 -080030void RestartTiming() {
31 programStartTime = wpi::Now();
32 if (programPauseTime != 0) programPauseTime = programStartTime.load();
33}
34
35void PauseTiming() {
36 if (programPauseTime == 0) programPauseTime = wpi::Now();
37}
38
39void ResumeTiming() {
40 if (programPauseTime != 0) {
41 programStartTime += wpi::Now() - programPauseTime;
42 programPauseTime = 0;
43 }
44}
45
46bool IsTimingPaused() { return programPauseTime != 0; }
47
48void StepTiming(uint64_t delta) {
49 if (programPauseTime != 0) programPauseTime += delta;
50}
Brian Silverman41cdd3e2019-01-19 19:48:58 -080051
52int64_t GetFPGATime() {
James Kuszmaul4b81d302019-12-14 20:53:14 -080053 uint64_t curTime = programPauseTime;
54 if (curTime == 0) curTime = wpi::Now();
55 return curTime - programStartTime;
Brian Silverman41cdd3e2019-01-19 19:48:58 -080056}
57
58double GetFPGATimestamp() { return GetFPGATime() * 1.0e-6; }
59
60void SetProgramStarted() { programStarted = true; }
61bool GetProgramStarted() { return programStarted; }
62} // namespace hal
63
64using namespace hal;
65
66extern "C" {
67void HALSIM_WaitForProgramStart(void) {
68 int count = 0;
69 while (!programStarted) {
70 count++;
71 std::printf("Waiting for program start signal: %d\n", count);
72 std::this_thread::sleep_for(std::chrono::milliseconds(500));
73 }
74}
75
76void HALSIM_SetProgramStarted(void) { SetProgramStarted(); }
77
78HAL_Bool HALSIM_GetProgramStarted(void) { return GetProgramStarted(); }
79
80void HALSIM_RestartTiming(void) { RestartTiming(); }
James Kuszmaul4b81d302019-12-14 20:53:14 -080081
82void HALSIM_PauseTiming(void) {
83 PauseTiming();
84 PauseNotifiers();
85}
86
87void HALSIM_ResumeTiming(void) {
88 ResumeTiming();
89 ResumeNotifiers();
90}
91
92HAL_Bool HALSIM_IsTimingPaused(void) { return IsTimingPaused(); }
93
94void HALSIM_StepTiming(uint64_t delta) {
95 StepTiming(delta);
96 WakeupNotifiers();
97}
Brian Silverman41cdd3e2019-01-19 19:48:58 -080098} // extern "C"