2016-05-26 22:14:25 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-11-01 23:41:30 -07:00
|
|
|
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
|
2016-05-26 22:14:25 -07: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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/AnalogTrigger.h"
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
#include "AnalogInternal.h"
|
2019-11-01 23:41:30 -07:00
|
|
|
#include "DutyCycleInternal.h"
|
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/AnalogInput.h"
|
2019-11-01 23:41:30 -07:00
|
|
|
#include "hal/DutyCycle.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/Errors.h"
|
|
|
|
|
#include "hal/handles/HandlesInternal.h"
|
|
|
|
|
#include "hal/handles/LimitedHandleResource.h"
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
2016-06-27 21:32:30 -07:00
|
|
|
namespace {
|
2017-08-23 22:07:46 -07:00
|
|
|
|
2016-06-27 21:32:30 -07:00
|
|
|
struct AnalogTrigger {
|
2016-07-20 22:05:17 -07:00
|
|
|
std::unique_ptr<tAnalogTrigger> trigger;
|
2019-11-01 23:41:30 -07:00
|
|
|
HAL_Handle handle;
|
2016-07-12 10:45:14 -07:00
|
|
|
uint8_t index;
|
2016-05-26 22:14:25 -07:00
|
|
|
};
|
2017-08-23 22:07:46 -07:00
|
|
|
|
|
|
|
|
} // namespace
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
static LimitedHandleResource<HAL_AnalogTriggerHandle, AnalogTrigger,
|
2017-12-10 18:02:07 -08:00
|
|
|
kNumAnalogTriggers, HAL_HandleEnum::AnalogTrigger>*
|
2016-06-27 21:32:30 -07:00
|
|
|
analogTriggerHandles;
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2017-12-10 18:02:07 -08:00
|
|
|
namespace hal {
|
|
|
|
|
namespace init {
|
|
|
|
|
void InitializeAnalogTrigger() {
|
|
|
|
|
static LimitedHandleResource<HAL_AnalogTriggerHandle, AnalogTrigger,
|
|
|
|
|
kNumAnalogTriggers,
|
|
|
|
|
HAL_HandleEnum::AnalogTrigger>
|
|
|
|
|
atH;
|
|
|
|
|
analogTriggerHandles = &atH;
|
|
|
|
|
}
|
|
|
|
|
} // namespace init
|
|
|
|
|
} // namespace hal
|
|
|
|
|
|
2016-06-27 21:32:30 -07:00
|
|
|
extern "C" {
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_AnalogTriggerHandle HAL_InitializeAnalogTrigger(
|
2019-11-01 23:41:30 -07:00
|
|
|
HAL_AnalogInputHandle portHandle, int32_t* status) {
|
2018-05-13 22:02:47 -07:00
|
|
|
hal::init::CheckInit();
|
2016-07-25 23:26:34 -07:00
|
|
|
// ensure we are given a valid and active AnalogInput handle
|
2017-12-10 18:02:07 -08:00
|
|
|
auto analog_port = analogInputHandles->Get(portHandle);
|
2016-07-25 23:26:34 -07:00
|
|
|
if (analog_port == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-06-27 21:32:30 -07:00
|
|
|
}
|
2017-12-10 18:02:07 -08:00
|
|
|
HAL_AnalogTriggerHandle handle = analogTriggerHandles->Allocate();
|
2016-07-09 00:24:26 -07:00
|
|
|
if (handle == HAL_kInvalidHandle) {
|
2016-06-27 21:32:30 -07:00
|
|
|
*status = NO_AVAILABLE_RESOURCES;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-06-05 15:23:58 -07:00
|
|
|
}
|
2017-12-10 18:02:07 -08:00
|
|
|
auto trigger = analogTriggerHandles->Get(handle);
|
2016-07-25 23:26:34 -07:00
|
|
|
if (trigger == nullptr) { // would only occur on thread issue
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-06-27 21:32:30 -07:00
|
|
|
}
|
2019-11-01 23:41:30 -07:00
|
|
|
trigger->handle = portHandle;
|
2016-07-12 10:45:14 -07:00
|
|
|
trigger->index = static_cast<uint8_t>(getHandleIndex(handle));
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2016-07-20 22:05:17 -07:00
|
|
|
trigger->trigger.reset(tAnalogTrigger::create(trigger->index, status));
|
2016-08-12 13:45:28 -07:00
|
|
|
trigger->trigger->writeSourceSelect_Channel(analog_port->channel, status);
|
2016-06-27 21:32:30 -07:00
|
|
|
return handle;
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
2019-11-01 23:41:30 -07:00
|
|
|
HAL_AnalogTriggerHandle HAL_InitializeAnalogTriggerDutyCycle(
|
|
|
|
|
HAL_DutyCycleHandle dutyCycleHandle, int32_t* status) {
|
|
|
|
|
hal::init::CheckInit();
|
|
|
|
|
// ensure we are given a valid and active DutyCycle handle
|
|
|
|
|
auto dutyCycle = dutyCycleHandles->Get(dutyCycleHandle);
|
|
|
|
|
if (dutyCycle == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
HAL_AnalogTriggerHandle handle = analogTriggerHandles->Allocate();
|
|
|
|
|
if (handle == HAL_kInvalidHandle) {
|
|
|
|
|
*status = NO_AVAILABLE_RESOURCES;
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
auto trigger = analogTriggerHandles->Get(handle);
|
|
|
|
|
if (trigger == nullptr) { // would only occur on thread issue
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
trigger->handle = dutyCycleHandle;
|
|
|
|
|
trigger->index = static_cast<uint8_t>(getHandleIndex(handle));
|
|
|
|
|
|
|
|
|
|
trigger->trigger.reset(tAnalogTrigger::create(trigger->index, status));
|
|
|
|
|
trigger->trigger->writeSourceSelect_Channel(dutyCycle->index, status);
|
|
|
|
|
trigger->trigger->writeSourceSelect_DutyCycle(true, status);
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_CleanAnalogTrigger(HAL_AnalogTriggerHandle analogTriggerHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2017-12-10 18:02:07 -08:00
|
|
|
analogTriggerHandles->Free(analogTriggerHandle);
|
2019-11-01 23:41:30 -07:00
|
|
|
// caller owns the input handle.
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_SetAnalogTriggerLimitsRaw(HAL_AnalogTriggerHandle analogTriggerHandle,
|
|
|
|
|
int32_t lower, int32_t upper,
|
|
|
|
|
int32_t* status) {
|
2017-12-10 18:02:07 -08:00
|
|
|
auto trigger = analogTriggerHandles->Get(analogTriggerHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (trigger == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
if (lower > upper) {
|
|
|
|
|
*status = ANALOG_TRIGGER_LIMIT_ORDER_ERROR;
|
2019-11-01 23:41:30 -07:00
|
|
|
return;
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
trigger->trigger->writeLowerLimit(lower, status);
|
|
|
|
|
trigger->trigger->writeUpperLimit(upper, status);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-01 23:41:30 -07:00
|
|
|
void HAL_SetAnalogTriggerLimitsDutyCycle(
|
|
|
|
|
HAL_AnalogTriggerHandle analogTriggerHandle, double lower, double upper,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto trigger = analogTriggerHandles->Get(analogTriggerHandle);
|
|
|
|
|
if (trigger == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (getHandleType(trigger->handle) != HAL_HandleEnum::DutyCycle) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (lower > upper) {
|
|
|
|
|
*status = ANALOG_TRIGGER_LIMIT_ORDER_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lower < 0.0 || upper > 1.0) {
|
|
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t scaleFactor =
|
|
|
|
|
HAL_GetDutyCycleOutputScaleFactor(trigger->handle, status);
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trigger->trigger->writeLowerLimit(static_cast<int32_t>(scaleFactor * lower),
|
|
|
|
|
status);
|
|
|
|
|
trigger->trigger->writeLowerLimit(static_cast<int32_t>(scaleFactor * upper),
|
|
|
|
|
status);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
void HAL_SetAnalogTriggerLimitsVoltage(
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_AnalogTriggerHandle analogTriggerHandle, double lower, double upper,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2017-12-10 18:02:07 -08:00
|
|
|
auto trigger = analogTriggerHandles->Get(analogTriggerHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (trigger == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2019-11-01 23:41:30 -07:00
|
|
|
|
|
|
|
|
if (getHandleType(trigger->handle) != HAL_HandleEnum::AnalogInput) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
if (lower > upper) {
|
|
|
|
|
*status = ANALOG_TRIGGER_LIMIT_ORDER_ERROR;
|
2019-11-01 23:41:30 -07:00
|
|
|
return;
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
2016-06-27 21:32:30 -07:00
|
|
|
|
2016-05-26 22:14:25 -07:00
|
|
|
// TODO: This depends on the averaged setting. Only raw values will work as
|
|
|
|
|
// is.
|
|
|
|
|
trigger->trigger->writeLowerLimit(
|
2019-11-01 23:41:30 -07:00
|
|
|
HAL_GetAnalogVoltsToValue(trigger->handle, lower, status), status);
|
2016-05-26 22:14:25 -07:00
|
|
|
trigger->trigger->writeUpperLimit(
|
2019-11-01 23:41:30 -07:00
|
|
|
HAL_GetAnalogVoltsToValue(trigger->handle, upper, status), status);
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_SetAnalogTriggerAveraged(HAL_AnalogTriggerHandle analogTriggerHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool useAveragedValue, int32_t* status) {
|
2017-12-10 18:02:07 -08:00
|
|
|
auto trigger = analogTriggerHandles->Get(analogTriggerHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (trigger == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2019-11-01 23:41:30 -07:00
|
|
|
if (trigger->trigger->readSourceSelect_Filter(status) != 0 ||
|
|
|
|
|
trigger->trigger->readSourceSelect_DutyCycle(status) != 0) {
|
2016-05-26 22:14:25 -07:00
|
|
|
*status = INCOMPATIBLE_STATE;
|
|
|
|
|
// TODO: wpi_setWPIErrorWithContext(IncompatibleMode, "Hardware does not
|
|
|
|
|
// support average and filtering at the same time.");
|
|
|
|
|
}
|
|
|
|
|
trigger->trigger->writeSourceSelect_Averaged(useAveragedValue, status);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_SetAnalogTriggerFiltered(HAL_AnalogTriggerHandle analogTriggerHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool useFilteredValue, int32_t* status) {
|
2017-12-10 18:02:07 -08:00
|
|
|
auto trigger = analogTriggerHandles->Get(analogTriggerHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (trigger == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2019-11-01 23:41:30 -07:00
|
|
|
if (trigger->trigger->readSourceSelect_Averaged(status) != 0 ||
|
|
|
|
|
trigger->trigger->readSourceSelect_DutyCycle(status) != 0) {
|
2016-05-26 22:14:25 -07:00
|
|
|
*status = INCOMPATIBLE_STATE;
|
|
|
|
|
// TODO: wpi_setWPIErrorWithContext(IncompatibleMode, "Hardware does not "
|
|
|
|
|
// "support average and filtering at the same time.");
|
|
|
|
|
}
|
|
|
|
|
trigger->trigger->writeSourceSelect_Filter(useFilteredValue, status);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool HAL_GetAnalogTriggerInWindow(
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_AnalogTriggerHandle analogTriggerHandle, int32_t* status) {
|
2017-12-10 18:02:07 -08:00
|
|
|
auto trigger = analogTriggerHandles->Get(analogTriggerHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (trigger == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
return trigger->trigger->readOutput_InHysteresis(trigger->index, status) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool HAL_GetAnalogTriggerTriggerState(
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_AnalogTriggerHandle analogTriggerHandle, int32_t* status) {
|
2017-12-10 18:02:07 -08:00
|
|
|
auto trigger = analogTriggerHandles->Get(analogTriggerHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (trigger == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
return trigger->trigger->readOutput_OverLimit(trigger->index, status) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_Bool HAL_GetAnalogTriggerOutput(HAL_AnalogTriggerHandle analogTriggerHandle,
|
|
|
|
|
HAL_AnalogTriggerType type,
|
|
|
|
|
int32_t* status) {
|
2017-12-10 18:02:07 -08:00
|
|
|
auto trigger = analogTriggerHandles->Get(analogTriggerHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (trigger == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
bool result = false;
|
|
|
|
|
switch (type) {
|
2016-07-09 00:24:26 -07:00
|
|
|
case HAL_Trigger_kInWindow:
|
2016-05-26 22:14:25 -07:00
|
|
|
result =
|
|
|
|
|
trigger->trigger->readOutput_InHysteresis(trigger->index, status);
|
2019-11-01 23:41:30 -07:00
|
|
|
break;
|
2016-07-09 00:24:26 -07:00
|
|
|
case HAL_Trigger_kState:
|
2016-05-26 22:14:25 -07:00
|
|
|
result = trigger->trigger->readOutput_OverLimit(trigger->index, status);
|
2019-11-01 23:41:30 -07:00
|
|
|
break;
|
2016-07-09 00:24:26 -07:00
|
|
|
case HAL_Trigger_kRisingPulse:
|
2019-11-01 23:41:30 -07:00
|
|
|
result = trigger->trigger->readOutput_Rising(trigger->index, status);
|
|
|
|
|
break;
|
2016-07-09 00:24:26 -07:00
|
|
|
case HAL_Trigger_kFallingPulse:
|
2019-11-01 23:41:30 -07:00
|
|
|
result = trigger->trigger->readOutput_Falling(trigger->index, status);
|
|
|
|
|
break;
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2017-08-23 22:07:46 -07:00
|
|
|
|
2019-11-01 23:41:30 -07:00
|
|
|
int32_t HAL_GetAnalogTriggerFPGAIndex(
|
|
|
|
|
HAL_AnalogTriggerHandle analogTriggerHandle, int32_t* status) {
|
|
|
|
|
auto trigger = analogTriggerHandles->Get(analogTriggerHandle);
|
|
|
|
|
if (trigger == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return trigger->index;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-23 22:07:46 -07:00
|
|
|
} // extern "C"
|