2016-05-26 22:14:25 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* 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/AnalogTrigger.h"
|
|
|
|
|
|
|
|
|
|
#include "AnalogInternal.h"
|
|
|
|
|
#include "HAL/AnalogInput.h"
|
2016-06-05 15:23:58 -07:00
|
|
|
#include "HAL/Errors.h"
|
2016-07-13 20:29:28 -07:00
|
|
|
#include "HAL/handles/HandlesInternal.h"
|
|
|
|
|
#include "HAL/handles/LimitedHandleResource.h"
|
2016-07-02 23:19:14 -07:00
|
|
|
#include "PortsInternal.h"
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
2016-06-27 21:32:30 -07:00
|
|
|
namespace {
|
|
|
|
|
struct AnalogTrigger {
|
2016-07-20 22:05:17 -07:00
|
|
|
std::unique_ptr<tAnalogTrigger> trigger;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_AnalogInputHandle analogHandle;
|
2016-07-12 10:45:14 -07:00
|
|
|
uint8_t index;
|
2016-05-26 22:14:25 -07:00
|
|
|
};
|
2016-06-27 21:32:30 -07:00
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
static LimitedHandleResource<HAL_AnalogTriggerHandle, AnalogTrigger,
|
|
|
|
|
kNumAnalogTriggers, HAL_HandleEnum::AnalogTrigger>
|
2016-06-27 21:32:30 -07:00
|
|
|
analogTriggerHandles;
|
2016-05-26 22:14:25 -07:00
|
|
|
|
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(
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_AnalogInputHandle portHandle, int32_t* index, int32_t* status) {
|
2016-07-25 23:26:34 -07:00
|
|
|
// ensure we are given a valid and active AnalogInput handle
|
2016-08-12 13:45:28 -07: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
|
|
|
}
|
2016-07-25 23:26:34 -07: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
|
|
|
}
|
2016-06-27 21:32:30 -07: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
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
trigger->analogHandle = portHandle;
|
2016-07-12 10:45:14 -07:00
|
|
|
trigger->index = static_cast<uint8_t>(getHandleIndex(handle));
|
|
|
|
|
*index = trigger->index;
|
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
|
|
|
}
|
|
|
|
|
|
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) {
|
2016-08-12 13:45:28 -07:00
|
|
|
analogTriggerHandles.Free(analogTriggerHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
// caller owns the analog 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) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
trigger->trigger->writeLowerLimit(lower, status);
|
|
|
|
|
trigger->trigger->writeUpperLimit(upper, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the upper and lower limits of the analog trigger.
|
|
|
|
|
* The limits are given as floating point voltage values.
|
|
|
|
|
*/
|
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) {
|
2016-08-12 13:45:28 -07: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;
|
|
|
|
|
}
|
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(
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_GetAnalogVoltsToValue(trigger->analogHandle, lower, status), status);
|
2016-05-26 22:14:25 -07:00
|
|
|
trigger->trigger->writeUpperLimit(
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_GetAnalogVoltsToValue(trigger->analogHandle, upper, status), status);
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configure the analog trigger to use the averaged vs. raw values.
|
|
|
|
|
* If the value is true, then the averaged value is selected for the analog
|
|
|
|
|
* trigger, otherwise the immediate value is used.
|
|
|
|
|
*/
|
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) {
|
2016-08-12 13:45:28 -07: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 (trigger->trigger->readSourceSelect_Filter(status) != 0) {
|
|
|
|
|
*status = INCOMPATIBLE_STATE;
|
|
|
|
|
// TODO: wpi_setWPIErrorWithContext(IncompatibleMode, "Hardware does not
|
|
|
|
|
// support average and filtering at the same time.");
|
|
|
|
|
}
|
|
|
|
|
trigger->trigger->writeSourceSelect_Averaged(useAveragedValue, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configure the analog trigger to use a filtered value.
|
|
|
|
|
* The analog trigger will operate with a 3 point average rejection filter. This
|
|
|
|
|
* is designed to help with 360 degree pot applications for the period where the
|
|
|
|
|
* pot crosses through zero.
|
|
|
|
|
*/
|
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) {
|
2016-08-12 13:45:28 -07: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 (trigger->trigger->readSourceSelect_Averaged(status) != 0) {
|
|
|
|
|
*status = INCOMPATIBLE_STATE;
|
|
|
|
|
// TODO: wpi_setWPIErrorWithContext(IncompatibleMode, "Hardware does not "
|
|
|
|
|
// "support average and filtering at the same time.");
|
|
|
|
|
}
|
|
|
|
|
trigger->trigger->writeSourceSelect_Filter(useFilteredValue, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the InWindow output of the analog trigger.
|
|
|
|
|
* True if the analog input is between the upper and lower limits.
|
|
|
|
|
* @return The InWindow output of the analog trigger.
|
|
|
|
|
*/
|
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) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the TriggerState output of the analog trigger.
|
|
|
|
|
* True if above upper limit.
|
|
|
|
|
* False if below lower limit.
|
|
|
|
|
* If in Hysteresis, maintain previous state.
|
|
|
|
|
* @return The TriggerState output of the analog trigger.
|
|
|
|
|
*/
|
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) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the state of the analog trigger output.
|
|
|
|
|
* @return The state of the analog trigger output.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_Bool HAL_GetAnalogTriggerOutput(HAL_AnalogTriggerHandle analogTriggerHandle,
|
|
|
|
|
HAL_AnalogTriggerType type,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
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);
|
|
|
|
|
break; // XXX: Backport
|
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);
|
|
|
|
|
break; // XXX: Backport
|
2016-07-09 00:24:26 -07:00
|
|
|
case HAL_Trigger_kRisingPulse:
|
|
|
|
|
case HAL_Trigger_kFallingPulse:
|
2016-05-26 22:14:25 -07:00
|
|
|
*status = ANALOG_TRIGGER_PULSE_OUTPUT_ERROR;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|