Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2016-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 "hal/Power.h" |
| 9 | |
| 10 | #include <memory> |
| 11 | |
| 12 | #include "HALInitializer.h" |
| 13 | #include "hal/ChipObject.h" |
| 14 | |
| 15 | using namespace hal; |
| 16 | |
| 17 | namespace hal { |
| 18 | |
| 19 | static std::unique_ptr<tPower> power{nullptr}; |
| 20 | |
| 21 | static void initializePower(int32_t* status) { |
| 22 | hal::init::CheckInit(); |
| 23 | if (power == nullptr) { |
| 24 | power.reset(tPower::create(status)); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | } // namespace hal |
| 29 | |
| 30 | namespace hal { |
| 31 | namespace init { |
| 32 | void InitializePower() {} |
| 33 | } // namespace init |
| 34 | } // namespace hal |
| 35 | |
| 36 | extern "C" { |
| 37 | |
| 38 | double HAL_GetVinVoltage(int32_t* status) { |
| 39 | initializePower(status); |
| 40 | return power->readVinVoltage(status) / 4.096 * 0.025733 - 0.029; |
| 41 | } |
| 42 | |
| 43 | double HAL_GetVinCurrent(int32_t* status) { |
| 44 | initializePower(status); |
| 45 | return power->readVinCurrent(status) / 4.096 * 0.017042 - 0.071; |
| 46 | } |
| 47 | |
| 48 | double HAL_GetUserVoltage6V(int32_t* status) { |
| 49 | initializePower(status); |
| 50 | return power->readUserVoltage6V(status) / 4.096 * 0.007019 - 0.014; |
| 51 | } |
| 52 | |
| 53 | double HAL_GetUserCurrent6V(int32_t* status) { |
| 54 | initializePower(status); |
| 55 | return power->readUserCurrent6V(status) / 4.096 * 0.005566 - 0.009; |
| 56 | } |
| 57 | |
| 58 | HAL_Bool HAL_GetUserActive6V(int32_t* status) { |
| 59 | initializePower(status); |
| 60 | return power->readStatus_User6V(status) == 4; |
| 61 | } |
| 62 | |
| 63 | int32_t HAL_GetUserCurrentFaults6V(int32_t* status) { |
| 64 | initializePower(status); |
| 65 | return static_cast<int32_t>( |
| 66 | power->readFaultCounts_OverCurrentFaultCount6V(status)); |
| 67 | } |
| 68 | |
| 69 | double HAL_GetUserVoltage5V(int32_t* status) { |
| 70 | initializePower(status); |
| 71 | return power->readUserVoltage5V(status) / 4.096 * 0.005962 - 0.013; |
| 72 | } |
| 73 | |
| 74 | double HAL_GetUserCurrent5V(int32_t* status) { |
| 75 | initializePower(status); |
| 76 | return power->readUserCurrent5V(status) / 4.096 * 0.001996 - 0.002; |
| 77 | } |
| 78 | |
| 79 | HAL_Bool HAL_GetUserActive5V(int32_t* status) { |
| 80 | initializePower(status); |
| 81 | return power->readStatus_User5V(status) == 4; |
| 82 | } |
| 83 | |
| 84 | int32_t HAL_GetUserCurrentFaults5V(int32_t* status) { |
| 85 | initializePower(status); |
| 86 | return static_cast<int32_t>( |
| 87 | power->readFaultCounts_OverCurrentFaultCount5V(status)); |
| 88 | } |
| 89 | |
| 90 | double HAL_GetUserVoltage3V3(int32_t* status) { |
| 91 | initializePower(status); |
| 92 | return power->readUserVoltage3V3(status) / 4.096 * 0.004902 - 0.01; |
| 93 | } |
| 94 | |
| 95 | double HAL_GetUserCurrent3V3(int32_t* status) { |
| 96 | initializePower(status); |
| 97 | return power->readUserCurrent3V3(status) / 4.096 * 0.002486 - 0.003; |
| 98 | } |
| 99 | |
| 100 | HAL_Bool HAL_GetUserActive3V3(int32_t* status) { |
| 101 | initializePower(status); |
| 102 | return power->readStatus_User3V3(status) == 4; |
| 103 | } |
| 104 | |
| 105 | int32_t HAL_GetUserCurrentFaults3V3(int32_t* status) { |
| 106 | initializePower(status); |
| 107 | return static_cast<int32_t>( |
| 108 | power->readFaultCounts_OverCurrentFaultCount3V3(status)); |
| 109 | } |
| 110 | |
| 111 | } // extern "C" |