2016-01-02 03:02:34 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-10-30 21:29:35 -07:00
|
|
|
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/Solenoid.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-13 22:02:47 -07:00
|
|
|
#include "HALInitializer.h"
|
2016-07-02 23:19:14 -07:00
|
|
|
#include "PortsInternal.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/Errors.h"
|
|
|
|
|
#include "hal/handles/HandlesInternal.h"
|
|
|
|
|
#include "hal/handles/IndexedHandleResource.h"
|
|
|
|
|
#include "mockdata/PCMDataInternal.h"
|
2014-05-29 14:25:09 -04:00
|
|
|
|
2016-07-02 09:24:54 -07:00
|
|
|
namespace {
|
|
|
|
|
struct Solenoid {
|
|
|
|
|
uint8_t module;
|
2016-08-12 13:45:28 -07:00
|
|
|
uint8_t channel;
|
2014-05-29 14:25:09 -04:00
|
|
|
};
|
2017-10-16 19:56:08 -07:00
|
|
|
} // namespace
|
2014-05-29 14:25:09 -04:00
|
|
|
|
2016-06-05 15:23:58 -07:00
|
|
|
using namespace hal;
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
static IndexedHandleResource<HAL_SolenoidHandle, Solenoid,
|
2016-08-12 13:45:28 -07:00
|
|
|
kNumPCMModules * kNumSolenoidChannels,
|
2017-12-10 19:38:53 -08:00
|
|
|
HAL_HandleEnum::Solenoid>* solenoidHandles;
|
|
|
|
|
|
|
|
|
|
namespace hal {
|
|
|
|
|
namespace init {
|
|
|
|
|
void InitializeSolenoid() {
|
|
|
|
|
static IndexedHandleResource<HAL_SolenoidHandle, Solenoid,
|
|
|
|
|
kNumPCMModules * kNumSolenoidChannels,
|
|
|
|
|
HAL_HandleEnum::Solenoid>
|
|
|
|
|
sH;
|
|
|
|
|
solenoidHandles = &sH;
|
|
|
|
|
}
|
|
|
|
|
} // namespace init
|
|
|
|
|
} // namespace hal
|
2016-07-02 09:24:54 -07:00
|
|
|
|
2015-11-26 00:08:32 -08:00
|
|
|
extern "C" {
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_SolenoidHandle HAL_InitializeSolenoidPort(HAL_PortHandle portHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2018-05-13 22:02:47 -07:00
|
|
|
hal::init::CheckInit();
|
2016-08-12 13:45:28 -07:00
|
|
|
int16_t channel = getPortHandleChannel(portHandle);
|
|
|
|
|
int16_t module = getPortHandleModule(portHandle);
|
|
|
|
|
if (channel == InvalidHandleIndex) {
|
2016-07-12 10:45:14 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-07-02 09:24:54 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
if (!HAL_CheckSolenoidChannel(channel)) {
|
2016-07-13 20:29:28 -07:00
|
|
|
*status = RESOURCE_OUT_OF_RANGE;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-06-05 15:23:58 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
if (!HAL_CheckSolenoidModule(module)) {
|
|
|
|
|
*status = RESOURCE_OUT_OF_RANGE;
|
2016-07-12 10:45:14 -07:00
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
2014-06-16 10:24:48 -04:00
|
|
|
|
2017-12-10 19:38:53 -08:00
|
|
|
auto handle = solenoidHandles->Allocate(
|
|
|
|
|
module * kNumSolenoidChannels + channel, status);
|
2017-08-18 21:35:53 -07:00
|
|
|
if (handle == HAL_kInvalidHandle) { // out of resources
|
|
|
|
|
*status = NO_AVAILABLE_RESOURCES;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-07-02 09:24:54 -07:00
|
|
|
}
|
2017-12-10 19:38:53 -08:00
|
|
|
auto solenoidPort = solenoidHandles->Get(handle);
|
2016-08-12 13:45:28 -07:00
|
|
|
if (solenoidPort == nullptr) { // would only occur on thread issues
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-07-02 09:24:54 -07:00
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
solenoidPort->module = static_cast<uint8_t>(module);
|
|
|
|
|
solenoidPort->channel = static_cast<uint8_t>(channel);
|
2016-05-20 17:30:37 -07:00
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
HALSIM_SetPCMSolenoidInitialized(module, channel, true);
|
|
|
|
|
|
2016-07-02 09:24:54 -07:00
|
|
|
return handle;
|
2014-05-29 14:25:09 -04:00
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_FreeSolenoidPort(HAL_SolenoidHandle solenoidPortHandle) {
|
2017-12-10 19:38:53 -08:00
|
|
|
auto port = solenoidHandles->Get(solenoidPortHandle);
|
2017-08-18 21:35:53 -07:00
|
|
|
if (port == nullptr) return;
|
2017-12-10 19:38:53 -08:00
|
|
|
solenoidHandles->Free(solenoidPortHandle);
|
2017-08-18 21:35:53 -07:00
|
|
|
HALSIM_SetPCMSolenoidInitialized(port->module, port->channel, false);
|
2015-10-20 10:37:04 -07:00
|
|
|
}
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool HAL_CheckSolenoidModule(int32_t module) {
|
2016-07-20 22:47:29 -07:00
|
|
|
return module < kNumPCMModules && module >= 0;
|
2016-07-12 10:45:14 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_Bool HAL_CheckSolenoidChannel(int32_t channel) {
|
|
|
|
|
return channel < kNumSolenoidChannels && channel >= 0;
|
2016-07-12 10:45:14 -07:00
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_Bool HAL_GetSolenoid(HAL_SolenoidHandle solenoidPortHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t* status) {
|
2017-12-10 19:38:53 -08:00
|
|
|
auto port = solenoidHandles->Get(solenoidPortHandle);
|
2016-07-02 09:24:54 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-07-02 09:24:54 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2014-06-16 10:24:48 -04:00
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
return HALSIM_GetPCMSolenoidOutput(port->module, port->channel);
|
2014-05-29 14:25:09 -04:00
|
|
|
}
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetAllSolenoids(int32_t module, int32_t* status) {
|
2017-08-18 21:35:53 -07:00
|
|
|
int32_t total = 0;
|
|
|
|
|
for (int i = 0; i < kNumSolenoidChannels; i++) {
|
|
|
|
|
int32_t channel = HALSIM_GetPCMSolenoidOutput(module, i) ? 1 : 0;
|
|
|
|
|
total = total + (channel << i);
|
|
|
|
|
}
|
2015-11-15 10:46:18 -08:00
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
return total;
|
2015-11-15 10:46:18 -08:00
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_SetSolenoid(HAL_SolenoidHandle solenoidPortHandle, HAL_Bool value,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2017-12-10 19:38:53 -08:00
|
|
|
auto port = solenoidHandles->Get(solenoidPortHandle);
|
2016-07-02 09:24:54 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-07-02 09:24:54 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
HALSIM_SetPCMSolenoidOutput(port->module, port->channel, value);
|
2014-06-16 10:24:48 -04:00
|
|
|
}
|
2019-10-30 21:29:35 -07:00
|
|
|
|
|
|
|
|
void HAL_SetAllSolenoids(int32_t module, int32_t state, int32_t* status) {
|
|
|
|
|
for (int i = 0; i < kNumSolenoidChannels; i++) {
|
|
|
|
|
int set = state & 1;
|
|
|
|
|
HALSIM_SetPCMSolenoidOutput(module, i, set);
|
|
|
|
|
state >>= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetPCMSolenoidBlackList(int32_t module, int32_t* status) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0;
|
2014-12-26 19:40:39 -05:00
|
|
|
}
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool HAL_GetPCMSolenoidVoltageStickyFault(int32_t module, int32_t* status) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0;
|
2014-12-26 19:40:39 -05:00
|
|
|
}
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool HAL_GetPCMSolenoidVoltageFault(int32_t module, int32_t* status) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0;
|
2014-12-26 19:40:39 -05:00
|
|
|
}
|
2017-08-18 21:35:53 -07:00
|
|
|
void HAL_ClearAllPCMStickyFaults(int32_t module, int32_t* status) {}
|
2017-11-26 12:55:21 -08:00
|
|
|
void HAL_SetOneShotDuration(HAL_SolenoidHandle solenoidPortHandle,
|
|
|
|
|
int32_t durMS, int32_t* status) {}
|
|
|
|
|
void HAL_FireOneShot(HAL_SolenoidHandle solenoidPortHandle, int32_t* status) {}
|
2017-10-16 19:56:08 -07:00
|
|
|
} // extern "C"
|