Files
allwpilib/hal/lib/athena/Solenoid.cpp

154 lines
4.1 KiB
C++
Raw Normal View History

/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. All Rights Reserved. */
/* 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. */
/*----------------------------------------------------------------------------*/
#include "HAL/Solenoid.h"
#include "ChipObject.h"
#include "FRC_NetworkCommunication/LoadOut.h"
#include "HAL/Errors.h"
2016-07-02 23:19:14 -07:00
#include "HAL/Ports.h"
#include "PCMInternal.h"
2016-07-02 23:19:14 -07:00
#include "PortsInternal.h"
#include "ctre/PCM.h"
#include "handles/HandlesInternal.h"
2016-07-02 09:24:54 -07:00
#include "handles/IndexedHandleResource.h"
2016-07-02 09:24:54 -07:00
namespace {
struct Solenoid {
uint8_t module;
uint8_t pin;
};
2016-07-02 09:24:54 -07:00
}
using namespace hal;
2016-07-02 09:24:54 -07:00
static IndexedHandleResource<HalSolenoidHandle, Solenoid,
2016-07-02 23:19:14 -07:00
kNumPCMModules * kNumSolenoidPins,
HalHandleEnum::Solenoid>
2016-07-02 09:24:54 -07:00
solenoidHandles;
extern "C" {
2016-07-02 09:24:54 -07:00
HalSolenoidHandle initializeSolenoidPort(HalPortHandle port_handle,
int32_t* status) {
int16_t pin = getPortHandlePin(port_handle);
int16_t module = getPortHandleModule(port_handle);
if (pin == InvalidHandleIndex) {
2016-07-02 09:24:54 -07:00
*status = PARAMETER_OUT_OF_RANGE; // Change to Handle Error
return HAL_INVALID_HANDLE;
}
2016-07-02 23:19:14 -07:00
if (module >= kNumPCMModules || pin >= kNumSolenoidPins) {
*status = PARAMETER_OUT_OF_RANGE;
2016-07-02 09:24:54 -07:00
return HAL_INVALID_HANDLE;
}
initializePCM(module);
2016-07-02 09:24:54 -07:00
auto handle =
2016-07-02 23:19:14 -07:00
solenoidHandles.Allocate(module * kNumSolenoidPins + pin, status);
2016-07-02 09:24:54 -07:00
if (handle == HAL_INVALID_HANDLE) { // out of resources
*status = NO_AVAILABLE_RESOURCES;
return HAL_INVALID_HANDLE;
}
auto solenoid_port = solenoidHandles.Get(handle);
if (solenoid_port == nullptr) { // would only occur on thread issues
*status = HAL_HANDLE_ERROR;
2016-07-02 09:24:54 -07:00
return HAL_INVALID_HANDLE;
}
solenoid_port->module = static_cast<uint8_t>(module);
solenoid_port->pin = static_cast<uint8_t>(pin);
2016-07-02 09:24:54 -07:00
return handle;
}
2016-07-02 09:24:54 -07:00
void freeSolenoidPort(HalSolenoidHandle solenoid_port_handle) {
solenoidHandles.Free(solenoid_port_handle);
}
2016-07-02 23:19:14 -07:00
bool checkSolenoidModule(uint8_t module) { return module < kNumPCMModules; }
2016-07-02 09:24:54 -07:00
bool getSolenoid(HalSolenoidHandle solenoid_port_handle, int32_t* status) {
auto port = solenoidHandles.Get(solenoid_port_handle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
2016-07-02 09:24:54 -07:00
return false;
}
bool value;
2016-07-02 09:24:54 -07:00
*status = PCM_modules[port->module]->GetSolenoid(port->pin, value);
return value;
}
2016-07-02 09:24:54 -07:00
uint8_t getAllSolenoids(uint8_t module, int32_t* status) {
2016-07-02 23:19:14 -07:00
if (module >= kNumPCMModules) {
2016-07-02 09:24:54 -07:00
*status = PARAMETER_OUT_OF_RANGE;
return 0;
}
uint8_t value;
2016-07-02 09:24:54 -07:00
*status = PCM_modules[module]->GetAllSolenoids(value);
return value;
}
2016-07-02 09:24:54 -07:00
void setSolenoid(HalSolenoidHandle solenoid_port_handle, bool value,
int32_t* status) {
auto port = solenoidHandles.Get(solenoid_port_handle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
2016-07-02 09:24:54 -07:00
return;
}
2016-07-02 09:24:54 -07:00
*status = PCM_modules[port->module]->SetSolenoid(port->pin, value);
}
2016-07-02 09:24:54 -07:00
int getPCMSolenoidBlackList(uint8_t module, int32_t* status) {
2016-07-02 23:19:14 -07:00
if (module >= kNumPCMModules) {
2016-07-02 09:24:54 -07:00
*status = PARAMETER_OUT_OF_RANGE;
return 0;
}
UINT8 value;
2016-07-02 09:24:54 -07:00
*status = PCM_modules[module]->GetSolenoidBlackList(value);
return value;
}
2016-07-02 09:24:54 -07:00
bool getPCMSolenoidVoltageStickyFault(uint8_t module, int32_t* status) {
2016-07-02 23:19:14 -07:00
if (module >= kNumPCMModules) {
2016-07-02 09:24:54 -07:00
*status = PARAMETER_OUT_OF_RANGE;
return false;
}
bool value;
2016-07-02 09:24:54 -07:00
*status = PCM_modules[module]->GetSolenoidStickyFault(value);
return value;
}
2016-07-02 09:24:54 -07:00
bool getPCMSolenoidVoltageFault(uint8_t module, int32_t* status) {
2016-07-02 23:19:14 -07:00
if (module >= kNumPCMModules) {
2016-07-02 09:24:54 -07:00
*status = PARAMETER_OUT_OF_RANGE;
return false;
}
bool value;
2016-07-02 09:24:54 -07:00
*status = PCM_modules[module]->GetSolenoidFault(value);
return value;
}
2016-07-02 09:24:54 -07:00
void clearAllPCMStickyFaults_sol(uint8_t module, int32_t* status) {
2016-07-02 23:19:14 -07:00
if (module >= kNumPCMModules) {
2016-07-02 09:24:54 -07:00
*status = PARAMETER_OUT_OF_RANGE;
return;
}
2016-07-02 09:24:54 -07:00
*status = PCM_modules[module]->ClearStickyFaults();
}
} // extern "C"