blob: 163ca9cc3d756a5b60148960caaca37e7014d283 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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/REVPH.h"
6
7#include "HALInitializer.h"
8#include "HALInternal.h"
9#include "PortsInternal.h"
10#include "hal/CANAPI.h"
11#include "hal/Errors.h"
12#include "hal/handles/IndexedHandleResource.h"
13#include "mockdata/REVPHDataInternal.h"
14
15using namespace hal;
16
17namespace {
18struct PCM {
19 int32_t module;
20 wpi::mutex lock;
21 std::string previousAllocation;
22};
23} // namespace
24
25static IndexedHandleResource<HAL_REVPHHandle, PCM, kNumREVPHModules,
26 HAL_HandleEnum::REVPH>* pcmHandles;
27
28namespace hal::init {
29void InitializeREVPH() {
30 static IndexedHandleResource<HAL_REVPHHandle, PCM, kNumREVPHModules,
31 HAL_HandleEnum::REVPH>
32 pH;
33 pcmHandles = &pH;
34}
35} // namespace hal::init
36
37HAL_REVPHHandle HAL_InitializeREVPH(int32_t module,
38 const char* allocationLocation,
39 int32_t* status) {
40 hal::init::CheckInit();
41
42 if (module == 0) {
43 hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PH", 1,
44 kNumREVPHModules, module);
45 return HAL_kInvalidHandle;
46 }
47
48 HAL_REVPHHandle handle;
49 auto pcm = pcmHandles->Allocate(module, &handle, status);
50
51 if (*status != 0) {
52 if (pcm) {
53 hal::SetLastErrorPreviouslyAllocated(status, "REV PH", module,
54 pcm->previousAllocation);
55 } else {
56 hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PH", 1,
57 kNumREVPHModules, module);
58 }
59 return HAL_kInvalidHandle; // failed to allocate. Pass error back.
60 }
61
62 pcm->previousAllocation = allocationLocation ? allocationLocation : "";
63 pcm->module = module;
64
65 SimREVPHData[module].initialized = true;
66 // Enable closed loop
Austin Schuh75263e32022-02-22 18:05:32 -080067 SimREVPHData[module].compressorConfigType =
68 HAL_REVPHCompressorConfigType_kDigital;
Austin Schuh812d0d12021-11-04 20:16:48 -070069
70 return handle;
71}
72
73void HAL_FreeREVPH(HAL_REVPHHandle handle) {
74 auto pcm = pcmHandles->Get(handle);
75 if (pcm == nullptr) {
76 pcmHandles->Free(handle);
77 return;
78 }
79 SimREVPHData[pcm->module].initialized = false;
80 pcmHandles->Free(handle);
81}
82
83HAL_Bool HAL_CheckREVPHModuleNumber(int32_t module) {
84 return module >= 1 && module < kNumREVPDHModules;
85}
86
87HAL_Bool HAL_CheckREVPHSolenoidChannel(int32_t channel) {
88 return channel < kNumREVPHChannels && channel >= 0;
89}
90
91HAL_Bool HAL_GetREVPHCompressor(HAL_REVPHHandle handle, int32_t* status) {
92 auto pcm = pcmHandles->Get(handle);
93 if (pcm == nullptr) {
94 *status = HAL_HANDLE_ERROR;
95 return false;
96 }
97
98 return SimREVPHData[pcm->module].compressorOn;
99}
100
Austin Schuh75263e32022-02-22 18:05:32 -0800101void HAL_SetREVPHCompressorConfig(HAL_REVPHHandle handle,
102 const HAL_REVPHCompressorConfig* config,
103 int32_t* status) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700104 auto pcm = pcmHandles->Get(handle);
105 if (pcm == nullptr) {
106 *status = HAL_HANDLE_ERROR;
107 return;
108 }
Austin Schuh75263e32022-02-22 18:05:32 -0800109 // TODO
110 // SimREVPHData[pcm->module].compressorConfigType = config.
Austin Schuh812d0d12021-11-04 20:16:48 -0700111}
Austin Schuh75263e32022-02-22 18:05:32 -0800112void HAL_SetREVPHClosedLoopControlDisabled(HAL_REVPHHandle handle,
113 int32_t* status) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700114 auto pcm = pcmHandles->Get(handle);
115 if (pcm == nullptr) {
116 *status = HAL_HANDLE_ERROR;
Austin Schuh75263e32022-02-22 18:05:32 -0800117 return;
Austin Schuh812d0d12021-11-04 20:16:48 -0700118 }
Austin Schuh75263e32022-02-22 18:05:32 -0800119 SimREVPHData[pcm->module].compressorConfigType =
120 HAL_REVPHCompressorConfigType_kDisabled;
121}
Austin Schuh812d0d12021-11-04 20:16:48 -0700122
Austin Schuh75263e32022-02-22 18:05:32 -0800123void HAL_SetREVPHClosedLoopControlDigital(HAL_REVPHHandle handle,
124 int32_t* status) {
125 auto pcm = pcmHandles->Get(handle);
126 if (pcm == nullptr) {
127 *status = HAL_HANDLE_ERROR;
128 return;
129 }
130 SimREVPHData[pcm->module].compressorConfigType =
131 HAL_REVPHCompressorConfigType_kDigital;
132}
133
134void HAL_SetREVPHClosedLoopControlAnalog(HAL_REVPHHandle handle,
135 double minAnalogVoltage,
136 double maxAnalogVoltage,
137 int32_t* status) {
138 auto pcm = pcmHandles->Get(handle);
139 if (pcm == nullptr) {
140 *status = HAL_HANDLE_ERROR;
141 return;
142 }
143 SimREVPHData[pcm->module].compressorConfigType =
144 HAL_REVPHCompressorConfigType_kAnalog;
145}
146
147void HAL_SetREVPHClosedLoopControlHybrid(HAL_REVPHHandle handle,
148 double minAnalogVoltage,
149 double maxAnalogVoltage,
150 int32_t* status) {
151 auto pcm = pcmHandles->Get(handle);
152 if (pcm == nullptr) {
153 *status = HAL_HANDLE_ERROR;
154 return;
155 }
156 SimREVPHData[pcm->module].compressorConfigType =
157 HAL_REVPHCompressorConfigType_kHybrid;
158}
159
160HAL_REVPHCompressorConfigType HAL_GetREVPHCompressorConfig(
161 HAL_REVPHHandle handle, int32_t* status) {
162 auto pcm = pcmHandles->Get(handle);
163 if (pcm == nullptr) {
164 *status = HAL_HANDLE_ERROR;
165 return HAL_REVPHCompressorConfigType_kDisabled;
166 }
167 return SimREVPHData[pcm->module].compressorConfigType;
Austin Schuh812d0d12021-11-04 20:16:48 -0700168}
169
170HAL_Bool HAL_GetREVPHPressureSwitch(HAL_REVPHHandle handle, int32_t* status) {
171 auto pcm = pcmHandles->Get(handle);
172 if (pcm == nullptr) {
173 *status = HAL_HANDLE_ERROR;
174 return false;
175 }
176
177 return SimREVPHData[pcm->module].pressureSwitch;
178}
179
Austin Schuh75263e32022-02-22 18:05:32 -0800180double HAL_GetREVPHAnalogVoltage(HAL_REVPHHandle handle, int32_t channel,
181 int32_t* status) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700182 return 0;
183}
184
185double HAL_GetREVPHCompressorCurrent(HAL_REVPHHandle handle, int32_t* status) {
186 auto pcm = pcmHandles->Get(handle);
187 if (pcm == nullptr) {
188 *status = HAL_HANDLE_ERROR;
189 return 0;
190 }
191
192 return SimREVPHData[pcm->module].compressorCurrent;
193}
194
195int32_t HAL_GetREVPHSolenoids(HAL_REVPHHandle handle, int32_t* status) {
196 auto pcm = pcmHandles->Get(handle);
197 if (pcm == nullptr) {
198 *status = HAL_HANDLE_ERROR;
199 return 0;
200 }
201
202 std::scoped_lock lock{pcm->lock};
203 auto& data = SimREVPHData[pcm->module].solenoidOutput;
204 uint8_t ret = 0;
205 for (int i = 0; i < kNumREVPHChannels; i++) {
206 ret |= (data[i] << i);
207 }
208 return ret;
209}
210void HAL_SetREVPHSolenoids(HAL_REVPHHandle handle, int32_t mask, int32_t values,
211 int32_t* status) {
212 auto pcm = pcmHandles->Get(handle);
213 if (pcm == nullptr) {
214 *status = HAL_HANDLE_ERROR;
215 return;
216 }
217
218 auto& data = SimREVPHData[pcm->module].solenoidOutput;
219 std::scoped_lock lock{pcm->lock};
220 for (int i = 0; i < kNumREVPHChannels; i++) {
221 auto indexMask = (1 << i);
222 if ((mask & indexMask) != 0) {
223 data[i] = (values & indexMask) != 0;
224 }
225 }
226}
227
228void HAL_FireREVPHOneShot(HAL_REVPHHandle handle, int32_t index, int32_t durMs,
229 int32_t* status) {}
Austin Schuh75263e32022-02-22 18:05:32 -0800230
231double HAL_GetREVPHVoltage(HAL_REVPHHandle handle, int32_t* status) {
232 return 0;
233}
234
235double HAL_GetREVPH5VVoltage(HAL_REVPHHandle handle, int32_t* status) {
236 return 0;
237}
238
239double HAL_GetREVPHSolenoidCurrent(HAL_REVPHHandle handle, int32_t* status) {
240 return 0;
241}
242
243double HAL_GetREVPHSolenoidVoltage(HAL_REVPHHandle handle, int32_t* status) {
244 return 0;
245}
246
247void HAL_GetREVPHVersion(HAL_REVPHHandle handle, HAL_REVPHVersion* version,
248 int32_t* status) {}
249
250void HAL_GetREVPHFaults(HAL_REVPHHandle handle, HAL_REVPHFaults* faults,
251 int32_t* status) {}
252
253void HAL_GetREVPHStickyFaults(HAL_REVPHHandle handle,
254 HAL_REVPHStickyFaults* stickyFaults,
255 int32_t* status) {}
256
257void HAL_ClearREVPHStickyFaults(HAL_REVPHHandle handle, int32_t* status) {}