Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include "hal/PowerDistribution.h" |
| 6 | |
| 7 | #include "CTREPDP.h" |
| 8 | #include "HALInternal.h" |
| 9 | #include "PortsInternal.h" |
| 10 | #include "REVPDH.h" |
| 11 | #include "hal/Errors.h" |
| 12 | #include "hal/handles/HandlesInternal.h" |
| 13 | |
| 14 | using namespace hal; |
| 15 | |
| 16 | extern "C" { |
| 17 | |
| 18 | HAL_PowerDistributionHandle HAL_InitializePowerDistribution( |
| 19 | int32_t moduleNumber, HAL_PowerDistributionType type, |
| 20 | const char* allocationLocation, int32_t* status) { |
| 21 | if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kAutomatic) { |
| 22 | type = HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE; |
| 23 | } |
| 24 | |
| 25 | if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE) { |
| 26 | if (moduleNumber == HAL_DEFAULT_POWER_DISTRIBUTION_MODULE) { |
| 27 | moduleNumber = 0; |
| 28 | } |
| 29 | return static_cast<HAL_PowerDistributionHandle>( |
| 30 | HAL_InitializePDP(moduleNumber, allocationLocation, status)); // TODO |
| 31 | } else { |
| 32 | if (moduleNumber == HAL_DEFAULT_POWER_DISTRIBUTION_MODULE) { |
| 33 | moduleNumber = 1; |
| 34 | } |
| 35 | return static_cast<HAL_PowerDistributionHandle>( |
| 36 | HAL_REV_InitializePDH(moduleNumber, allocationLocation, status)); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #define IsCtre(handle) ::hal::isHandleType(handle, HAL_HandleEnum::CTREPDP) |
| 41 | |
| 42 | void HAL_CleanPowerDistribution(HAL_PowerDistributionHandle handle) { |
| 43 | if (IsCtre(handle)) { |
| 44 | HAL_CleanPDP(handle); |
| 45 | } else { |
| 46 | HAL_REV_FreePDH(handle); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | int32_t HAL_GetPowerDistributionModuleNumber(HAL_PowerDistributionHandle handle, |
| 51 | int32_t* status) { |
| 52 | if (IsCtre(handle)) { |
| 53 | return HAL_GetPDPModuleNumber(handle, status); |
| 54 | } else { |
| 55 | return HAL_REV_GetPDHModuleNumber(handle, status); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | HAL_Bool HAL_CheckPowerDistributionChannel(HAL_PowerDistributionHandle handle, |
| 60 | int32_t channel) { |
| 61 | if (IsCtre(handle)) { |
| 62 | return HAL_CheckPDPChannel(channel); |
| 63 | } else { |
| 64 | return HAL_REV_CheckPDHChannelNumber(channel); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | HAL_Bool HAL_CheckPowerDistributionModule(int32_t module, |
| 69 | HAL_PowerDistributionType type) { |
| 70 | if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE) { |
| 71 | return HAL_CheckPDPModule(module); |
| 72 | } else { |
| 73 | return HAL_REV_CheckPDHModuleNumber(module); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | HAL_PowerDistributionType HAL_GetPowerDistributionType( |
| 78 | HAL_PowerDistributionHandle handle, int32_t* status) { |
| 79 | return IsCtre(handle) |
| 80 | ? HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE |
| 81 | : HAL_PowerDistributionType::HAL_PowerDistributionType_kRev; |
| 82 | } |
| 83 | |
| 84 | int32_t HAL_GetPowerDistributionNumChannels(HAL_PowerDistributionHandle handle, |
| 85 | int32_t* status) { |
| 86 | if (IsCtre(handle)) { |
| 87 | return kNumCTREPDPChannels; |
| 88 | } else { |
| 89 | return kNumREVPDHChannels; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | double HAL_GetPowerDistributionTemperature(HAL_PowerDistributionHandle handle, |
| 94 | int32_t* status) { |
| 95 | if (IsCtre(handle)) { |
| 96 | return HAL_GetPDPTemperature(handle, status); |
| 97 | } else { |
| 98 | // Not supported |
| 99 | return 0; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | double HAL_GetPowerDistributionVoltage(HAL_PowerDistributionHandle handle, |
| 104 | int32_t* status) { |
| 105 | if (IsCtre(handle)) { |
| 106 | return HAL_GetPDPVoltage(handle, status); |
| 107 | } else { |
| 108 | return HAL_REV_GetPDHSupplyVoltage(handle, status); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | double HAL_GetPowerDistributionChannelCurrent( |
| 113 | HAL_PowerDistributionHandle handle, int32_t channel, int32_t* status) { |
| 114 | if (IsCtre(handle)) { |
| 115 | return HAL_GetPDPChannelCurrent(handle, channel, status); |
| 116 | } else { |
| 117 | return HAL_REV_GetPDHChannelCurrent(handle, channel, status); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void HAL_GetPowerDistributionAllChannelCurrents( |
| 122 | HAL_PowerDistributionHandle handle, double* currents, |
| 123 | int32_t currentsLength, int32_t* status) { |
| 124 | if (IsCtre(handle)) { |
| 125 | if (currentsLength < kNumCTREPDPChannels) { |
| 126 | *status = PARAMETER_OUT_OF_RANGE; |
| 127 | SetLastError(status, "Output array not large enough"); |
| 128 | return; |
| 129 | } |
| 130 | return HAL_GetPDPAllChannelCurrents(handle, currents, status); |
| 131 | } else { |
| 132 | if (currentsLength < kNumREVPDHChannels) { |
| 133 | *status = PARAMETER_OUT_OF_RANGE; |
| 134 | SetLastError(status, "Output array not large enough"); |
| 135 | return; |
| 136 | } |
| 137 | return HAL_REV_GetPDHAllChannelCurrents(handle, currents, status); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | double HAL_GetPowerDistributionTotalCurrent(HAL_PowerDistributionHandle handle, |
| 142 | int32_t* status) { |
| 143 | if (IsCtre(handle)) { |
| 144 | return HAL_GetPDPTotalCurrent(handle, status); |
| 145 | } else { |
| 146 | return HAL_REV_GetPDHTotalCurrent(handle, status); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | double HAL_GetPowerDistributionTotalPower(HAL_PowerDistributionHandle handle, |
| 151 | int32_t* status) { |
| 152 | if (IsCtre(handle)) { |
| 153 | return HAL_GetPDPTotalPower(handle, status); |
| 154 | } else { |
| 155 | // Not currently supported |
| 156 | return 0; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | double HAL_GetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle, |
| 161 | int32_t* status) { |
| 162 | if (IsCtre(handle)) { |
| 163 | return HAL_GetPDPTotalEnergy(handle, status); |
| 164 | } else { |
| 165 | // Not currently supported |
| 166 | return 0; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void HAL_ResetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle, |
| 171 | int32_t* status) { |
| 172 | if (IsCtre(handle)) { |
| 173 | HAL_ResetPDPTotalEnergy(handle, status); |
| 174 | } else { |
| 175 | // Not supported |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void HAL_ClearPowerDistributionStickyFaults(HAL_PowerDistributionHandle handle, |
| 180 | int32_t* status) { |
| 181 | if (IsCtre(handle)) { |
| 182 | HAL_ClearPDPStickyFaults(handle, status); |
| 183 | } else { |
| 184 | HAL_REV_ClearPDHFaults(handle, status); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void HAL_SetPowerDistributionSwitchableChannel( |
| 189 | HAL_PowerDistributionHandle handle, HAL_Bool enabled, int32_t* status) { |
| 190 | if (IsCtre(handle)) { |
| 191 | // No-op on CTRE |
| 192 | return; |
| 193 | } else { |
| 194 | HAL_REV_SetPDHSwitchableChannel(handle, enabled, status); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | HAL_Bool HAL_GetPowerDistributionSwitchableChannel( |
| 199 | HAL_PowerDistributionHandle handle, int32_t* status) { |
| 200 | if (IsCtre(handle)) { |
| 201 | // No-op on CTRE |
| 202 | return false; |
| 203 | } else { |
| 204 | return HAL_REV_GetPDHSwitchableChannelState(handle, status); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | } // extern "C" |