Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2016-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 | #include "HAL/PDP.h" |
| 9 | |
| 10 | #include <memory> |
| 11 | |
| 12 | #include "HAL/Errors.h" |
| 13 | #include "HAL/Ports.h" |
| 14 | #include "HAL/cpp/make_unique.h" |
| 15 | #include "PortsInternal.h" |
| 16 | #include "ctre/PDP.h" |
| 17 | |
| 18 | using namespace hal; |
| 19 | |
| 20 | static std::unique_ptr<PDP> pdp[kNumPDPModules]; |
| 21 | |
| 22 | static inline bool checkPDPInit(int32_t module, int32_t* status) { |
| 23 | if (!HAL_CheckPDPModule(module)) { |
| 24 | *status = RESOURCE_OUT_OF_RANGE; |
| 25 | return false; |
| 26 | } |
| 27 | if (!pdp[module]) { |
| 28 | *status = INCOMPATIBLE_STATE; |
| 29 | return false; |
| 30 | } |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | extern "C" { |
| 35 | |
| 36 | void HAL_InitializePDP(int32_t module, int32_t* status) { |
| 37 | if (!HAL_CheckPDPModule(module)) { |
| 38 | *status = RESOURCE_OUT_OF_RANGE; |
| 39 | return; |
| 40 | } |
| 41 | if (!pdp[module]) { |
| 42 | pdp[module] = std::make_unique<PDP>(module); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | HAL_Bool HAL_CheckPDPModule(int32_t module) { |
| 47 | return module < kNumPDPModules && module >= 0; |
| 48 | } |
| 49 | |
| 50 | HAL_Bool HAL_CheckPDPChannel(int32_t channel) { |
| 51 | return channel < kNumPDPChannels && channel >= 0; |
| 52 | } |
| 53 | |
| 54 | double HAL_GetPDPTemperature(int32_t module, int32_t* status) { |
| 55 | if (!checkPDPInit(module, status)) return 0; |
| 56 | |
| 57 | double temperature; |
| 58 | |
| 59 | *status = pdp[module]->GetTemperature(temperature); |
| 60 | |
| 61 | return temperature; |
| 62 | } |
| 63 | |
| 64 | double HAL_GetPDPVoltage(int32_t module, int32_t* status) { |
| 65 | if (!checkPDPInit(module, status)) return 0; |
| 66 | |
| 67 | double voltage; |
| 68 | |
| 69 | *status = pdp[module]->GetVoltage(voltage); |
| 70 | |
| 71 | return voltage; |
| 72 | } |
| 73 | |
| 74 | double HAL_GetPDPChannelCurrent(int32_t module, int32_t channel, |
| 75 | int32_t* status) { |
| 76 | if (!checkPDPInit(module, status)) return 0; |
| 77 | |
| 78 | double current; |
| 79 | |
| 80 | *status = pdp[module]->GetChannelCurrent(channel, current); |
| 81 | |
| 82 | return current; |
| 83 | } |
| 84 | |
| 85 | double HAL_GetPDPTotalCurrent(int32_t module, int32_t* status) { |
| 86 | if (!checkPDPInit(module, status)) return 0; |
| 87 | |
| 88 | double current; |
| 89 | |
| 90 | *status = pdp[module]->GetTotalCurrent(current); |
| 91 | |
| 92 | return current; |
| 93 | } |
| 94 | |
| 95 | double HAL_GetPDPTotalPower(int32_t module, int32_t* status) { |
| 96 | if (!checkPDPInit(module, status)) return 0; |
| 97 | |
| 98 | double power; |
| 99 | |
| 100 | *status = pdp[module]->GetTotalPower(power); |
| 101 | |
| 102 | return power; |
| 103 | } |
| 104 | |
| 105 | double HAL_GetPDPTotalEnergy(int32_t module, int32_t* status) { |
| 106 | if (!checkPDPInit(module, status)) return 0; |
| 107 | |
| 108 | double energy; |
| 109 | |
| 110 | *status = pdp[module]->GetTotalEnergy(energy); |
| 111 | |
| 112 | return energy; |
| 113 | } |
| 114 | |
| 115 | void HAL_ResetPDPTotalEnergy(int32_t module, int32_t* status) { |
| 116 | if (!checkPDPInit(module, status)) return; |
| 117 | |
| 118 | *status = pdp[module]->ResetEnergy(); |
| 119 | } |
| 120 | |
| 121 | void HAL_ClearPDPStickyFaults(int32_t module, int32_t* status) { |
| 122 | if (!checkPDPInit(module, status)) return; |
| 123 | |
| 124 | *status = pdp[module]->ClearStickyFaults(); |
| 125 | } |
| 126 | |
| 127 | } // extern "C" |