blob: dbe09d4b2daf11f84cdd1aba21c21ab53b4e036a [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2017-2019 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/PDP.h"
9
10#include "CANAPIInternal.h"
11#include "HALInitializer.h"
12#include "PortsInternal.h"
13#include "hal/CANAPI.h"
14#include "hal/handles/IndexedHandleResource.h"
15#include "mockdata/PDPDataInternal.h"
16
17using namespace hal;
18
19static constexpr HAL_CANManufacturer manufacturer =
20 HAL_CANManufacturer::HAL_CAN_Man_kCTRE;
21
22static constexpr HAL_CANDeviceType deviceType =
23 HAL_CANDeviceType::HAL_CAN_Dev_kPowerDistribution;
24
25namespace hal {
26namespace init {
27void InitializePDP() {}
28} // namespace init
29} // namespace hal
30
31extern "C" {
32HAL_PDPHandle HAL_InitializePDP(int32_t module, int32_t* status) {
33 if (!HAL_CheckPDPModule(module)) {
34 *status = PARAMETER_OUT_OF_RANGE;
35 return HAL_kInvalidHandle;
36 }
37 hal::init::CheckInit();
38 SimPDPData[module].initialized = true;
39 auto handle = HAL_InitializeCAN(manufacturer, module, deviceType, status);
40
41 if (*status != 0) {
42 HAL_CleanCAN(handle);
43 return HAL_kInvalidHandle;
44 }
45
46 return handle;
47}
48
49HAL_Bool HAL_CheckPDPModule(int32_t module) {
50 return module < kNumPDPModules && module >= 0;
51}
52
53HAL_Bool HAL_CheckPDPChannel(int32_t channel) {
54 return channel < kNumPDPChannels && channel >= 0;
55}
56
57void HAL_CleanPDP(HAL_PDPHandle handle) { HAL_CleanCAN(handle); }
58
59double HAL_GetPDPTemperature(HAL_PDPHandle handle, int32_t* status) {
60 auto module = hal::can::GetCANModuleFromHandle(handle, status);
61 if (*status != 0) {
62 return 0.0;
63 }
64 return SimPDPData[module].temperature;
65}
66double HAL_GetPDPVoltage(HAL_PDPHandle handle, int32_t* status) {
67 auto module = hal::can::GetCANModuleFromHandle(handle, status);
68 if (*status != 0) {
69 return 0.0;
70 }
71 return SimPDPData[module].voltage;
72}
73double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel,
74 int32_t* status) {
75 auto module = hal::can::GetCANModuleFromHandle(handle, status);
76 if (*status != 0) {
77 return 0.0;
78 }
79 return SimPDPData[module].current[channel];
80}
81void HAL_GetPDPAllChannelCurrents(HAL_PDPHandle handle, double* currents,
82 int32_t* status) {
83 auto module = hal::can::GetCANModuleFromHandle(handle, status);
84 if (*status != 0) {
85 return;
86 }
87
88 auto& data = SimPDPData[module];
89 for (int i = 0; i < kNumPDPChannels; i++) {
90 currents[i] = data.current[i];
91 }
92}
93double HAL_GetPDPTotalCurrent(HAL_PDPHandle handle, int32_t* status) {
94 return 0.0;
95}
96double HAL_GetPDPTotalPower(HAL_PDPHandle handle, int32_t* status) {
97 return 0.0;
98}
99double HAL_GetPDPTotalEnergy(HAL_PDPHandle handle, int32_t* status) {
100 return 0.0;
101}
102void HAL_ResetPDPTotalEnergy(HAL_PDPHandle handle, int32_t* status) {}
103void HAL_ClearPDPStickyFaults(HAL_PDPHandle handle, int32_t* status) {}
104} // extern "C"