Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2017-2018 FIRST. 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 | #include <atomic> |
| 9 | #include <chrono> |
| 10 | #include <cstdio> |
| 11 | #include <thread> |
| 12 | |
| 13 | #include <wpi/timestamp.h> |
| 14 | |
| 15 | #include "MockHooksInternal.h" |
| 16 | |
| 17 | static std::atomic<bool> programStarted{false}; |
| 18 | |
| 19 | static std::atomic<uint64_t> programStartTime{0}; |
| 20 | |
| 21 | namespace hal { |
| 22 | namespace init { |
| 23 | void InitializeMockHooks() {} |
| 24 | } // namespace init |
| 25 | } // namespace hal |
| 26 | |
| 27 | namespace hal { |
| 28 | void RestartTiming() { programStartTime = wpi::Now(); } |
| 29 | |
| 30 | int64_t GetFPGATime() { |
| 31 | auto now = wpi::Now(); |
| 32 | auto currentTime = now - programStartTime; |
| 33 | return currentTime; |
| 34 | } |
| 35 | |
| 36 | double GetFPGATimestamp() { return GetFPGATime() * 1.0e-6; } |
| 37 | |
| 38 | void SetProgramStarted() { programStarted = true; } |
| 39 | bool GetProgramStarted() { return programStarted; } |
| 40 | } // namespace hal |
| 41 | |
| 42 | using namespace hal; |
| 43 | |
| 44 | extern "C" { |
| 45 | void HALSIM_WaitForProgramStart(void) { |
| 46 | int count = 0; |
| 47 | while (!programStarted) { |
| 48 | count++; |
| 49 | std::printf("Waiting for program start signal: %d\n", count); |
| 50 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void HALSIM_SetProgramStarted(void) { SetProgramStarted(); } |
| 55 | |
| 56 | HAL_Bool HALSIM_GetProgramStarted(void) { return GetProgramStarted(); } |
| 57 | |
| 58 | void HALSIM_RestartTiming(void) { RestartTiming(); } |
| 59 | } // extern "C" |