Files
allwpilib/hal/src/main/native/sim/Relay.cpp

110 lines
3.2 KiB
C++
Raw Normal View History

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2016-2017 FIRST. 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/Relay.h"
#include "HAL/handles/IndexedHandleResource.h"
#include "MockData/RelayDataInternal.h"
2016-07-02 23:19:14 -07:00
#include "PortsInternal.h"
using namespace hal;
2016-06-29 18:58:14 -07:00
namespace {
struct Relay {
uint8_t channel;
2016-06-29 18:58:14 -07:00
bool fwd;
};
}
static IndexedHandleResource<HAL_RelayHandle, Relay, kNumRelayChannels,
HAL_HandleEnum::Relay>
2016-06-29 18:58:14 -07:00
relayHandles;
extern "C" {
HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
int32_t* status) {
if (*status != 0) return HAL_kInvalidHandle;
2016-06-29 18:58:14 -07:00
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
if (!fwd) channel += kNumRelayHeaders; // add 4 to reverse channels
2016-06-29 18:58:14 -07:00
auto handle = relayHandles.Allocate(channel, status);
2016-06-29 18:58:14 -07:00
if (*status != 0)
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
2016-06-29 18:58:14 -07:00
auto port = relayHandles.Get(handle);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
2016-06-29 18:58:14 -07:00
if (!fwd) {
// Subtract number of headers to put channel in range
channel -= kNumRelayHeaders;
port->fwd = false; // set to reverse
SimRelayData[channel].SetInitializedReverse(true);
2016-06-29 18:58:14 -07:00
} else {
port->fwd = true; // set to forward
SimRelayData[channel].SetInitializedForward(true);
}
2016-06-29 18:58:14 -07:00
port->channel = static_cast<uint8_t>(channel);
2016-06-29 18:58:14 -07:00
return handle;
}
void HAL_FreeRelayPort(HAL_RelayHandle relayPortHandle) {
auto port = relayHandles.Get(relayPortHandle);
relayHandles.Free(relayPortHandle);
if (port == nullptr) return;
if (port->fwd)
SimRelayData[port->channel].SetInitializedForward(false);
else
SimRelayData[port->channel].SetInitializedReverse(false);
2016-06-29 18:58:14 -07:00
}
HAL_Bool HAL_CheckRelayChannel(int32_t channel) {
2016-07-02 23:19:14 -07:00
// roboRIO only has 4 headers, and the FPGA has
// seperate functions for forward and reverse,
// instead of seperate channel IDs
return channel < kNumRelayHeaders && channel >= 0;
}
void HAL_SetRelay(HAL_RelayHandle relayPortHandle, HAL_Bool on,
int32_t* status) {
auto port = relayHandles.Get(relayPortHandle);
2016-06-29 18:58:14 -07:00
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (port->fwd)
SimRelayData[port->channel].SetForward(on);
else
SimRelayData[port->channel].SetReverse(on);
}
HAL_Bool HAL_GetRelay(HAL_RelayHandle relayPortHandle, int32_t* status) {
auto port = relayHandles.Get(relayPortHandle);
2016-06-29 18:58:14 -07:00
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
if (port->fwd)
return SimRelayData[port->channel].GetForward();
else
return SimRelayData[port->channel].GetReverse();
}
}