Update to 2018_v4 image and new build system. (#598)

* Revert "Force OpenCV to 3.1.0 (#602)"

This reverts commit 50ed55e8e2.

* Removes Simulation

* Removes old build system

* Removes old gtest

* Adds new gmock and gtest

* Updates to new ni-libraries

* removes MyRobot (to be replaced)

* moves files to new location

* Adds new sim backend and new test executables

* updates .styleguide and .gitignore

* Changes cpp WPILibVersion to a function

MSVC throws an AV with the old version.

* Disables USBCamera on all systems except for linux

* 2018 NI Libraries

* New build system
This commit is contained in:
Thad House
2017-08-18 21:35:53 -07:00
committed by Peter Johnson
parent 50ed55e8e2
commit e1195e8b9d
1024 changed files with 64481 additions and 61340 deletions

View File

@@ -0,0 +1,25 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/Accelerometer.h"
#include "MockData/AccelerometerDataInternal.h"
using namespace hal;
extern "C" {
void HAL_SetAccelerometerActive(HAL_Bool active) {
SimAccelerometerData[0].SetActive(active);
}
void HAL_SetAccelerometerRange(HAL_AccelerometerRange range) {
SimAccelerometerData[0].SetRange(range);
}
double HAL_GetAccelerometerX(void) { return SimAccelerometerData[0].GetX(); }
double HAL_GetAccelerometerY(void) { return SimAccelerometerData[0].GetY(); }
double HAL_GetAccelerometerZ(void) { return SimAccelerometerData[0].GetZ(); }
}

View File

@@ -0,0 +1,106 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/AnalogAccumulator.h"
#include "AnalogInternal.h"
#include "MockData/AnalogInDataInternal.h"
using namespace hal;
extern "C" {
HAL_Bool HAL_IsAccumulatorChannel(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
for (int32_t i = 0; i < kNumAccumulators; i++) {
if (port->channel == kAccumulatorChannels[i]) return true;
}
return false;
}
void HAL_InitAccumulator(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (!HAL_IsAccumulatorChannel(analogPortHandle, status)) {
*status = HAL_INVALID_ACCUMULATOR_CHANNEL;
return;
}
SimAnalogInData[port->channel].SetAccumulatorInitialized(true);
}
void HAL_ResetAccumulator(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimAnalogInData[port->channel].SetAccumulatorCenter(0);
SimAnalogInData[port->channel].SetAccumulatorCount(0);
SimAnalogInData[port->channel].SetAccumulatorValue(0);
}
void HAL_SetAccumulatorCenter(HAL_AnalogInputHandle analogPortHandle,
int32_t center, int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimAnalogInData[port->channel].SetAccumulatorCenter(center);
}
void HAL_SetAccumulatorDeadband(HAL_AnalogInputHandle analogPortHandle,
int32_t deadband, int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimAnalogInData[port->channel].SetAccumulatorDeadband(deadband);
}
int64_t HAL_GetAccumulatorValue(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimAnalogInData[port->channel].GetAccumulatorValue();
}
int64_t HAL_GetAccumulatorCount(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimAnalogInData[port->channel].GetAccumulatorCount();
}
void HAL_GetAccumulatorOutput(HAL_AnalogInputHandle analogPortHandle,
int64_t* value, int64_t* count, int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
*count = SimAnalogInData[port->channel].GetAccumulatorCount();
*value = SimAnalogInData[port->channel].GetAccumulatorValue();
}
}

View File

@@ -0,0 +1,135 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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/AnalogGyro.h"
#include <chrono>
#include <thread>
#include "AnalogInternal.h"
#include "HAL/AnalogAccumulator.h"
#include "HAL/AnalogInput.h"
#include "HAL/handles/IndexedHandleResource.h"
#include "MockData/AnalogGyroDataInternal.h"
namespace {
struct AnalogGyro {
HAL_AnalogInputHandle handle;
uint8_t index;
};
}
using namespace hal;
static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators,
HAL_HandleEnum::AnalogGyro>
analogGyroHandles;
extern "C" {
HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle,
int32_t* status) {
if (!HAL_IsAccumulatorChannel(analogHandle, status)) {
if (*status == 0) {
*status = HAL_INVALID_ACCUMULATOR_CHANNEL;
}
return HAL_kInvalidHandle;
}
// handle known to be correct, so no need to type check
int16_t channel = getHandleIndex(analogHandle);
auto handle = analogGyroHandles.Allocate(channel, status);
if (*status != 0)
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
// Initialize port structure
auto gyro = analogGyroHandles.Get(handle);
if (gyro == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
gyro->handle = analogHandle;
gyro->index = channel;
SimAnalogGyroData[channel].SetInitialized(true);
return handle;
}
void HAL_SetupAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
// No op
}
void HAL_FreeAnalogGyro(HAL_GyroHandle handle) {
auto gyro = analogGyroHandles.Get(handle);
analogGyroHandles.Free(handle);
if (gyro == nullptr) return;
SimAnalogGyroData[gyro->index].SetInitialized(false);
}
void HAL_SetAnalogGyroParameters(HAL_GyroHandle handle,
double voltsPerDegreePerSecond, double offset,
int32_t center, int32_t* status) {
// No op
}
void HAL_SetAnalogGyroVoltsPerDegreePerSecond(HAL_GyroHandle handle,
double voltsPerDegreePerSecond,
int32_t* status) {
// No op
}
void HAL_ResetAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
auto gyro = analogGyroHandles.Get(handle);
if (gyro == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimAnalogGyroData[gyro->index].SetAngle(0.0);
}
void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
// Just do a reset
HAL_ResetAnalogGyro(handle, status);
}
void HAL_SetAnalogGyroDeadband(HAL_GyroHandle handle, double volts,
int32_t* status) {
// No op
}
double HAL_GetAnalogGyroAngle(HAL_GyroHandle handle, int32_t* status) {
auto gyro = analogGyroHandles.Get(handle);
if (gyro == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimAnalogGyroData[gyro->index].GetAngle();
}
double HAL_GetAnalogGyroRate(HAL_GyroHandle handle, int32_t* status) {
auto gyro = analogGyroHandles.Get(handle);
if (gyro == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimAnalogGyroData[gyro->index].GetRate();
}
double HAL_GetAnalogGyroOffset(HAL_GyroHandle handle, int32_t* status) {
return 0.0;
}
int32_t HAL_GetAnalogGyroCenter(HAL_GyroHandle handle, int32_t* status) {
return 0;
}
}

View File

@@ -0,0 +1,171 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/AnalogInput.h"
#include "AnalogInternal.h"
#include "HAL/handles/HandlesInternal.h"
#include "MockData/AnalogInDataInternal.h"
#include "PortsInternal.h"
using namespace hal;
extern "C" {
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
int32_t* status) {
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
HAL_AnalogInputHandle handle = analogInputHandles.Allocate(channel, status);
if (*status != 0)
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
// Initialize port structure
auto analog_port = analogInputHandles.Get(handle);
if (analog_port == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
analog_port->channel = static_cast<uint8_t>(channel);
if (HAL_IsAccumulatorChannel(handle, status)) {
analog_port->isAccumulator = true;
} else {
analog_port->isAccumulator = false;
}
SimAnalogInData[channel].SetInitialized(true);
SimAnalogInData[channel].SetAccumulatorInitialized(false);
return handle;
}
void HAL_FreeAnalogInputPort(HAL_AnalogInputHandle analogPortHandle) {
auto port = analogInputHandles.Get(analogPortHandle);
// no status, so no need to check for a proper free.
analogInputHandles.Free(analogPortHandle);
if (port == nullptr) return;
SimAnalogInData[port->channel].SetInitialized(false);
SimAnalogInData[port->channel].SetAccumulatorInitialized(false);
}
HAL_Bool HAL_CheckAnalogModule(int32_t module) { return module == 1; }
HAL_Bool HAL_CheckAnalogInputChannel(int32_t channel) {
return channel < kNumAnalogInputs && channel >= 0;
}
void HAL_SetAnalogSampleRate(double samplesPerSecond, int32_t* status) {
// No op
}
double HAL_GetAnalogSampleRate(int32_t* status) { return kDefaultSampleRate; }
void HAL_SetAnalogAverageBits(HAL_AnalogInputHandle analogPortHandle,
int32_t bits, int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimAnalogInData[port->channel].SetAverageBits(bits);
}
int32_t HAL_GetAnalogAverageBits(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimAnalogInData[port->channel].GetAverageBits();
}
void HAL_SetAnalogOversampleBits(HAL_AnalogInputHandle analogPortHandle,
int32_t bits, int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimAnalogInData[port->channel].SetOversampleBits(bits);
}
int32_t HAL_GetAnalogOversampleBits(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimAnalogInData[port->channel].GetOversampleBits();
}
int32_t HAL_GetAnalogValue(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
double voltage = SimAnalogInData[port->channel].GetVoltage();
return HAL_GetAnalogVoltsToValue(analogPortHandle, voltage, status);
}
int32_t HAL_GetAnalogAverageValue(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
// No averaging supported
return HAL_GetAnalogValue(analogPortHandle, status);
}
int32_t HAL_GetAnalogVoltsToValue(HAL_AnalogInputHandle analogPortHandle,
double voltage, int32_t* status) {
if (voltage > 5.0) {
voltage = 5.0;
*status = VOLTAGE_OUT_OF_RANGE;
}
if (voltage < 0.0) {
voltage = 0.0;
*status = VOLTAGE_OUT_OF_RANGE;
}
int32_t LSBWeight = HAL_GetAnalogLSBWeight(analogPortHandle, status);
int32_t offset = HAL_GetAnalogOffset(analogPortHandle, status);
int32_t value =
static_cast<int32_t>((voltage + offset * 1.0e-9) / (LSBWeight * 1.0e-9));
return value;
}
double HAL_GetAnalogVoltage(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0.0;
}
return SimAnalogInData[port->channel].GetVoltage();
}
double HAL_GetAnalogAverageVoltage(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
auto port = analogInputHandles.Get(analogPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0.0;
}
// No averaging supported
double voltage = SimAnalogInData[port->channel].GetVoltage();
return voltage;
}
int32_t HAL_GetAnalogLSBWeight(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
return 1220703;
}
int32_t HAL_GetAnalogOffset(HAL_AnalogInputHandle analogPortHandle,
int32_t* status) {
return 0;
}
}

View File

@@ -0,0 +1,18 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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 "AnalogInternal.h"
#include "HAL/AnalogInput.h"
#include "HAL/cpp/priority_mutex.h"
#include "PortsInternal.h"
namespace hal {
IndexedHandleResource<HAL_AnalogInputHandle, hal::AnalogPort, kNumAnalogInputs,
HAL_HandleEnum::AnalogInput>
analogInputHandles;
}

View File

@@ -0,0 +1,36 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <stdint.h>
#include <memory>
#include "HAL/Ports.h"
#include "HAL/handles/IndexedHandleResource.h"
#include "PortsInternal.h"
namespace hal {
constexpr int32_t kTimebase = 40000000; ///< 40 MHz clock
constexpr int32_t kDefaultOversampleBits = 0;
constexpr int32_t kDefaultAverageBits = 7;
constexpr double kDefaultSampleRate = 50000.0;
static const uint32_t kAccumulatorChannels[] = {0, 1};
struct AnalogPort {
uint8_t channel;
bool isAccumulator;
};
extern IndexedHandleResource<HAL_AnalogInputHandle, hal::AnalogPort,
kNumAnalogInputs, HAL_HandleEnum::AnalogInput>
analogInputHandles;
int32_t GetAnalogTriggerInputIndex(HAL_AnalogTriggerHandle handle,
int32_t* status);
} // namespace hal

View File

@@ -0,0 +1,88 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/AnalogOutput.h"
#include "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/IndexedHandleResource.h"
#include "MockData/AnalogOutDataInternal.h"
#include "PortsInternal.h"
using namespace hal;
namespace {
struct AnalogOutput {
uint8_t channel;
};
}
static IndexedHandleResource<HAL_AnalogOutputHandle, AnalogOutput,
kNumAnalogOutputs, HAL_HandleEnum::AnalogOutput>
analogOutputHandles;
extern "C" {
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
int32_t* status) {
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
HAL_AnalogOutputHandle handle = analogOutputHandles.Allocate(channel, status);
if (*status != 0)
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
auto port = analogOutputHandles.Get(handle);
if (port == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = static_cast<uint8_t>(channel);
// Initialize sim analog input
SimAnalogOutData[channel].SetInitialized(true);
return handle;
}
void HAL_FreeAnalogOutputPort(HAL_AnalogOutputHandle analogOutputHandle) {
// no status, so no need to check for a proper free.
auto port = analogOutputHandles.Get(analogOutputHandle);
if (port == nullptr) return;
analogOutputHandles.Free(analogOutputHandle);
SimAnalogOutData[port->channel].SetInitialized(false);
}
HAL_Bool HAL_CheckAnalogOutputChannel(int32_t channel) {
return channel < kNumAnalogOutputs && channel >= 0;
}
void HAL_SetAnalogOutput(HAL_AnalogOutputHandle analogOutputHandle,
double voltage, int32_t* status) {
auto port = analogOutputHandles.Get(analogOutputHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimAnalogOutData[port->channel].SetVoltage(voltage);
}
double HAL_GetAnalogOutput(HAL_AnalogOutputHandle analogOutputHandle,
int32_t* status) {
auto port = analogOutputHandles.Get(analogOutputHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0.0;
}
return SimAnalogOutData[port->channel].GetVoltage();
}
}

View File

@@ -0,0 +1,244 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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"
#include "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/LimitedHandleResource.h"
#include "MockData/AnalogInDataInternal.h"
#include "MockData/AnalogTriggerDataInternal.h"
#include "PortsInternal.h"
namespace {
struct AnalogTrigger {
HAL_AnalogInputHandle analogHandle;
uint8_t index;
HAL_Bool trigState;
};
}
using namespace hal;
static LimitedHandleResource<HAL_AnalogTriggerHandle, AnalogTrigger,
kNumAnalogTriggers, HAL_HandleEnum::AnalogTrigger>
analogTriggerHandles;
HAL_AnalogTriggerHandle HAL_InitializeAnalogTrigger(
HAL_AnalogInputHandle portHandle, int32_t* index, int32_t* status) {
// ensure we are given a valid and active AnalogInput handle
auto analog_port = analogInputHandles.Get(portHandle);
if (analog_port == 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->analogHandle = portHandle;
trigger->index = static_cast<uint8_t>(getHandleIndex(handle));
*index = trigger->index;
SimAnalogTriggerData[trigger->index].SetInitialized(true);
trigger->trigState = false;
return handle;
}
void HAL_CleanAnalogTrigger(HAL_AnalogTriggerHandle analogTriggerHandle,
int32_t* status) {
auto trigger = analogTriggerHandles.Get(analogTriggerHandle);
analogTriggerHandles.Free(analogTriggerHandle);
if (trigger == nullptr) return;
SimAnalogTriggerData[trigger->index].SetInitialized(false);
// caller owns the analog input handle.
}
static double GetAnalogValueToVoltage(
HAL_AnalogTriggerHandle analogTriggerHandle, int32_t value,
int32_t* status) {
int32_t LSBWeight = HAL_GetAnalogLSBWeight(analogTriggerHandle, status);
int32_t offset = HAL_GetAnalogOffset(analogTriggerHandle, status);
double voltage = LSBWeight * 1.0e-9 * value - offset * 1.0e-9;
return voltage;
}
int32_t hal::GetAnalogTriggerInputIndex(HAL_AnalogTriggerHandle handle,
int32_t* status) {
auto trigger = analogTriggerHandles.Get(handle);
if (trigger == nullptr) {
*status = HAL_HANDLE_ERROR;
return -1;
}
auto analog_port = analogInputHandles.Get(trigger->analogHandle);
if (analog_port == nullptr) {
*status = HAL_HANDLE_ERROR;
return -1;
}
return analog_port->channel;
}
void HAL_SetAnalogTriggerLimitsRaw(HAL_AnalogTriggerHandle analogTriggerHandle,
int32_t lower, int32_t upper,
int32_t* status) {
auto trigger = analogTriggerHandles.Get(analogTriggerHandle);
if (trigger == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (lower > upper) {
*status = ANALOG_TRIGGER_LIMIT_ORDER_ERROR;
}
double trigLower =
GetAnalogValueToVoltage(trigger->analogHandle, lower, status);
if (status != 0) return;
double trigUpper =
GetAnalogValueToVoltage(trigger->analogHandle, upper, status);
if (status != 0) return;
SimAnalogTriggerData[trigger->index].SetTriggerUpperBound(trigUpper);
SimAnalogTriggerData[trigger->index].SetTriggerLowerBound(trigLower);
}
void HAL_SetAnalogTriggerLimitsVoltage(
HAL_AnalogTriggerHandle analogTriggerHandle, double lower, double upper,
int32_t* status) {
auto trigger = analogTriggerHandles.Get(analogTriggerHandle);
if (trigger == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (lower > upper) {
*status = ANALOG_TRIGGER_LIMIT_ORDER_ERROR;
}
SimAnalogTriggerData[trigger->index].SetTriggerUpperBound(upper);
SimAnalogTriggerData[trigger->index].SetTriggerLowerBound(lower);
}
void HAL_SetAnalogTriggerAveraged(HAL_AnalogTriggerHandle analogTriggerHandle,
HAL_Bool useAveragedValue, int32_t* status) {
auto trigger = analogTriggerHandles.Get(analogTriggerHandle);
if (trigger == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
AnalogTriggerData* triggerData = &SimAnalogTriggerData[trigger->index];
if (triggerData->GetTriggerMode() == HALSIM_AnalogTriggerFiltered) {
*status = INCOMPATIBLE_STATE;
return;
}
auto setVal = useAveragedValue ? HALSIM_AnalogTriggerAveraged
: HALSIM_AnalogTriggerUnassigned;
triggerData->SetTriggerMode(setVal);
}
void HAL_SetAnalogTriggerFiltered(HAL_AnalogTriggerHandle analogTriggerHandle,
HAL_Bool useFilteredValue, int32_t* status) {
auto trigger = analogTriggerHandles.Get(analogTriggerHandle);
if (trigger == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
AnalogTriggerData* triggerData = &SimAnalogTriggerData[trigger->index];
if (triggerData->GetTriggerMode() == HALSIM_AnalogTriggerAveraged) {
*status = INCOMPATIBLE_STATE;
return;
}
auto setVal = useFilteredValue ? HALSIM_AnalogTriggerAveraged
: HALSIM_AnalogTriggerUnassigned;
triggerData->SetTriggerMode(setVal);
}
static double GetTriggerValue(AnalogTrigger* trigger, int32_t* status) {
auto analogIn = analogInputHandles.Get(trigger->analogHandle);
if (analogIn == nullptr) {
// Returning HAL Handle Error, but going to ignore lower down
*status = HAL_HANDLE_ERROR;
return 0.0;
}
return SimAnalogInData[analogIn->channel].GetVoltage();
}
HAL_Bool HAL_GetAnalogTriggerInWindow(
HAL_AnalogTriggerHandle analogTriggerHandle, int32_t* status) {
auto trigger = analogTriggerHandles.Get(analogTriggerHandle);
if (trigger == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
double voltage = GetTriggerValue(trigger.get(), status);
if (*status == HAL_HANDLE_ERROR) {
// Don't error if analog has been destroyed
*status = 0;
return false;
}
auto trigUpper = SimAnalogTriggerData[trigger->index].GetTriggerUpperBound();
auto trigLower = SimAnalogTriggerData[trigger->index].GetTriggerLowerBound();
return voltage >= trigLower && voltage <= trigUpper;
}
HAL_Bool HAL_GetAnalogTriggerTriggerState(
HAL_AnalogTriggerHandle analogTriggerHandle, int32_t* status) {
auto trigger = analogTriggerHandles.Get(analogTriggerHandle);
if (trigger == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
double voltage = GetTriggerValue(trigger.get(), status);
if (*status == HAL_HANDLE_ERROR) {
// Don't error if analog has been destroyed
*status = 0;
return false;
}
auto trigUpper = SimAnalogTriggerData[trigger->index].GetTriggerUpperBound();
auto trigLower = SimAnalogTriggerData[trigger->index].GetTriggerLowerBound();
if (voltage < trigLower) {
trigger->trigState = false;
return false;
}
if (voltage > trigUpper) {
trigger->trigState = true;
return true;
}
return trigger->trigState;
}
HAL_Bool HAL_GetAnalogTriggerOutput(HAL_AnalogTriggerHandle analogTriggerHandle,
HAL_AnalogTriggerType type,
int32_t* status) {
if (type == HAL_Trigger_kInWindow) {
return HAL_GetAnalogTriggerInWindow(analogTriggerHandle, status);
} else if (type == HAL_Trigger_kState) {
return HAL_GetAnalogTriggerTriggerState(analogTriggerHandle, status);
} else {
*status = ANALOG_TRIGGER_PULSE_OUTPUT_ERROR;
return false;
}
}

View File

@@ -0,0 +1,115 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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/Compressor.h"
#include "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "MockData/PCMDataInternal.h"
#include "PortsInternal.h"
using namespace hal;
extern "C" {
HAL_CompressorHandle HAL_InitializeCompressor(int32_t module, int32_t* status) {
// As compressors can have unlimited objects, just create a
// handle with the module number as the index.
SimPCMData[module].SetCompressorInitialized(true);
return (HAL_CompressorHandle)createHandle(static_cast<int16_t>(module),
HAL_HandleEnum::Compressor, 0);
}
HAL_Bool HAL_CheckCompressorModule(int32_t module) {
return module < kNumPCMModules && module >= 0;
}
HAL_Bool HAL_GetCompressor(HAL_CompressorHandle compressorHandle,
int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;
}
return SimPCMData[index].GetCompressorOn();
}
void HAL_SetCompressorClosedLoopControl(HAL_CompressorHandle compressorHandle,
HAL_Bool value, int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return;
}
SimPCMData[index].SetClosedLoopEnabled(value);
}
HAL_Bool HAL_GetCompressorClosedLoopControl(
HAL_CompressorHandle compressorHandle, int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;
}
return SimPCMData[index].GetClosedLoopEnabled();
}
HAL_Bool HAL_GetCompressorPressureSwitch(HAL_CompressorHandle compressorHandle,
int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;
}
return SimPCMData[index].GetPressureSwitch();
}
double HAL_GetCompressorCurrent(HAL_CompressorHandle compressorHandle,
int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimPCMData[index].GetCompressorCurrent();
}
HAL_Bool HAL_GetCompressorCurrentTooHighFault(
HAL_CompressorHandle compressorHandle, int32_t* status) {
return false;
}
HAL_Bool HAL_GetCompressorCurrentTooHighStickyFault(
HAL_CompressorHandle compressorHandle, int32_t* status) {
return false;
}
HAL_Bool HAL_GetCompressorShortedStickyFault(
HAL_CompressorHandle compressorHandle, int32_t* status) {
return false;
}
HAL_Bool HAL_GetCompressorShortedFault(HAL_CompressorHandle compressorHandle,
int32_t* status) {
return false;
}
HAL_Bool HAL_GetCompressorNotConnectedStickyFault(
HAL_CompressorHandle compressorHandle, int32_t* status) {
return false;
}
HAL_Bool HAL_GetCompressorNotConnectedFault(
HAL_CompressorHandle compressorHandle, int32_t* status) {
return false;
}
} // extern "C"

View File

@@ -0,0 +1,18 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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/Constants.h"
#include "ConstantsInternal.h"
using namespace hal;
extern "C" {
int32_t HAL_GetSystemClockTicksPerMicrosecond(void) {
return kSystemClockTicksPerMicrosecond;
}
}

View File

@@ -0,0 +1,14 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <stdint.h>
namespace hal {
constexpr int32_t kSystemClockTicksPerMicrosecond = 40;
}

View File

@@ -0,0 +1,86 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/Counter.h"
#include "CounterInternal.h"
#include "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/LimitedHandleResource.h"
#include "PortsInternal.h"
namespace hal {
LimitedHandleResource<HAL_CounterHandle, Counter, kNumCounters,
HAL_HandleEnum::Counter>
counterHandles;
}
extern "C" {
HAL_CounterHandle HAL_InitializeCounter(HAL_Counter_Mode mode, int32_t* index,
int32_t* status) {
return 0;
}
void HAL_FreeCounter(HAL_CounterHandle counterHandle, int32_t* status) {}
void HAL_SetCounterAverageSize(HAL_CounterHandle counterHandle, int32_t size,
int32_t* status) {}
void HAL_SetCounterUpSource(HAL_CounterHandle counterHandle,
HAL_Handle digitalSourceHandle,
HAL_AnalogTriggerType analogTriggerType,
int32_t* status) {}
void HAL_SetCounterUpSourceEdge(HAL_CounterHandle counterHandle,
HAL_Bool risingEdge, HAL_Bool fallingEdge,
int32_t* status) {}
void HAL_ClearCounterUpSource(HAL_CounterHandle counterHandle,
int32_t* status) {}
void HAL_SetCounterDownSource(HAL_CounterHandle counterHandle,
HAL_Handle digitalSourceHandle,
HAL_AnalogTriggerType analogTriggerType,
int32_t* status) {}
void HAL_SetCounterDownSourceEdge(HAL_CounterHandle counterHandle,
HAL_Bool risingEdge, HAL_Bool fallingEdge,
int32_t* status) {}
void HAL_ClearCounterDownSource(HAL_CounterHandle counterHandle,
int32_t* status) {}
void HAL_SetCounterUpDownMode(HAL_CounterHandle counterHandle,
int32_t* status) {}
void HAL_SetCounterExternalDirectionMode(HAL_CounterHandle counterHandle,
int32_t* status) {}
void HAL_SetCounterSemiPeriodMode(HAL_CounterHandle counterHandle,
HAL_Bool highSemiPeriod, int32_t* status) {}
void HAL_SetCounterPulseLengthMode(HAL_CounterHandle counterHandle,
double threshold, int32_t* status) {}
int32_t HAL_GetCounterSamplesToAverage(HAL_CounterHandle counterHandle,
int32_t* status) {
return 0;
}
void HAL_SetCounterSamplesToAverage(HAL_CounterHandle counterHandle,
int32_t samplesToAverage, int32_t* status) {
}
void HAL_ResetCounter(HAL_CounterHandle counterHandle, int32_t* status) {}
int32_t HAL_GetCounter(HAL_CounterHandle counterHandle, int32_t* status) {
return 0;
}
double HAL_GetCounterPeriod(HAL_CounterHandle counterHandle, int32_t* status) {
return 0.0;
}
void HAL_SetCounterMaxPeriod(HAL_CounterHandle counterHandle, double maxPeriod,
int32_t* status) {}
void HAL_SetCounterUpdateWhenEmpty(HAL_CounterHandle counterHandle,
HAL_Bool enabled, int32_t* status) {}
HAL_Bool HAL_GetCounterStopped(HAL_CounterHandle counterHandle,
int32_t* status) {
return false;
}
HAL_Bool HAL_GetCounterDirection(HAL_CounterHandle counterHandle,
int32_t* status) {
return false;
}
void HAL_SetCounterReverseDirection(HAL_CounterHandle counterHandle,
HAL_Bool reverseDirection,
int32_t* status) {}
}

View File

@@ -0,0 +1,24 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/LimitedHandleResource.h"
#include "PortsInternal.h"
namespace hal {
struct Counter {
uint8_t index;
};
extern LimitedHandleResource<HAL_CounterHandle, Counter, kNumCounters,
HAL_HandleEnum::Counter>
counterHandles;
} // namespace hal

View File

@@ -0,0 +1,336 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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/DIO.h"
#include <cmath>
#include "DigitalInternal.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/LimitedHandleResource.h"
#include "MockData/DIODataInternal.h"
#include "MockData/DigitalPWMDataInternal.h"
#include "PortsInternal.h"
using namespace hal;
static LimitedHandleResource<HAL_DigitalPWMHandle, uint8_t,
kNumDigitalPWMOutputs, HAL_HandleEnum::DigitalPWM>
digitalPWMHandles;
extern "C" {
/**
* Create a new instance of a digital port.
*/
HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
HAL_Bool input, int32_t* status) {
if (*status != 0) return HAL_kInvalidHandle;
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
auto handle =
digitalChannelHandles.Allocate(channel, HAL_HandleEnum::DIO, status);
if (*status != 0)
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
auto port = digitalChannelHandles.Get(handle, HAL_HandleEnum::DIO);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = static_cast<uint8_t>(channel);
SimDIOData[channel].SetInitialized(true);
SimDIOData[channel].SetIsInput(input);
return handle;
}
HAL_Bool HAL_CheckDIOChannel(int32_t channel) {
return channel < kNumDigitalChannels && channel >= 0;
}
void HAL_FreeDIOPort(HAL_DigitalHandle dioPortHandle) {
auto port = digitalChannelHandles.Get(dioPortHandle, HAL_HandleEnum::DIO);
// no status, so no need to check for a proper free.
digitalChannelHandles.Free(dioPortHandle, HAL_HandleEnum::DIO);
if (port == nullptr) return;
SimDIOData[port->channel].SetInitialized(true);
}
/**
* Allocate a DO PWM Generator.
* Allocate PWM generators so that they are not accidentally reused.
*
* @return PWM Generator handle
*/
HAL_DigitalPWMHandle HAL_AllocateDigitalPWM(int32_t* status) {
auto handle = digitalPWMHandles.Allocate();
if (handle == HAL_kInvalidHandle) {
*status = NO_AVAILABLE_RESOURCES;
return HAL_kInvalidHandle;
}
auto id = digitalPWMHandles.Get(handle);
if (id == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
*id = static_cast<uint8_t>(getHandleIndex(handle));
SimDigitalPWMData[*id].SetInitialized(true);
return handle;
}
/**
* Free the resource associated with a DO PWM generator.
*
* @param pwmGenerator The pwmGen to free that was allocated with
* allocateDigitalPWM()
*/
void HAL_FreeDigitalPWM(HAL_DigitalPWMHandle pwmGenerator, int32_t* status) {
auto port = digitalPWMHandles.Get(pwmGenerator);
digitalPWMHandles.Free(pwmGenerator);
if (port == nullptr) return;
int32_t id = *port;
SimDigitalPWMData[id].SetInitialized(false);
}
/**
* Change the frequency of the DO PWM generator.
*
* The valid range is from 0.6 Hz to 19 kHz. The frequency resolution is
* logarithmic.
*
* @param rate The frequency to output all digital output PWM signals.
*/
void HAL_SetDigitalPWMRate(double rate, int32_t* status) {
// Currently rounding in the log rate domain... heavy weight toward picking a
// higher freq.
// TODO: Round in the linear rate domain.
// uint8_t pwmPeriodPower = static_cast<uint8_t>(
// std::log(1.0 / (kExpectedLoopTiming * 0.25E-6 * rate)) /
// std::log(2.0) +
// 0.5);
// TODO(THAD) : Add a case to set this in the simulator
// digitalSystem->writePWMPeriodPower(pwmPeriodPower, status);
}
/**
* Configure the duty-cycle of the PWM generator
*
* @param pwmGenerator The generator index reserved by allocateDigitalPWM()
* @param dutyCycle The percent duty cycle to output [0..1].
*/
void HAL_SetDigitalPWMDutyCycle(HAL_DigitalPWMHandle pwmGenerator,
double dutyCycle, int32_t* status) {
auto port = digitalPWMHandles.Get(pwmGenerator);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
int32_t id = *port;
if (dutyCycle > 1.0) dutyCycle = 1.0;
if (dutyCycle < 0.0) dutyCycle = 0.0;
SimDigitalPWMData[id].SetDutyCycle(dutyCycle);
}
/**
* Configure which DO channel the PWM signal is output on
*
* @param pwmGenerator The generator index reserved by allocateDigitalPWM()
* @param channel The Digital Output channel to output on
*/
void HAL_SetDigitalPWMOutputChannel(HAL_DigitalPWMHandle pwmGenerator,
int32_t channel, int32_t* status) {
auto port = digitalPWMHandles.Get(pwmGenerator);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
int32_t id = *port;
SimDigitalPWMData[id].SetPin(channel);
}
/**
* Write a digital I/O bit to the FPGA.
* Set a single value on a digital I/O channel.
*
* @param channel The Digital I/O channel
* @param value The state to set the digital channel (if it is configured as an
* output)
*/
void HAL_SetDIO(HAL_DigitalHandle dioPortHandle, HAL_Bool value,
int32_t* status) {
auto port = digitalChannelHandles.Get(dioPortHandle, HAL_HandleEnum::DIO);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (value != 0 && value != 1) {
if (value != 0) value = 1;
}
SimDIOData[port->channel].SetValue(value);
}
/**
* Read a digital I/O bit from the FPGA.
* Get a single value from a digital I/O channel.
*
* @param channel The digital I/O channel
* @return The state of the specified channel
*/
HAL_Bool HAL_GetDIO(HAL_DigitalHandle dioPortHandle, int32_t* status) {
auto port = digitalChannelHandles.Get(dioPortHandle, HAL_HandleEnum::DIO);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
HAL_Bool value = SimDIOData[port->channel].GetValue();
if (value > 1) value = 1;
if (value < 0) value = 0;
return value;
}
/**
* Read the direction of a the Digital I/O lines
* A 1 bit means output and a 0 bit means input.
*
* @param channel The digital I/O channel
* @return The direction of the specified channel
*/
HAL_Bool HAL_GetDIODirection(HAL_DigitalHandle dioPortHandle, int32_t* status) {
auto port = digitalChannelHandles.Get(dioPortHandle, HAL_HandleEnum::DIO);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
HAL_Bool value = SimDIOData[port->channel].GetIsInput();
if (value > 1) value = 1;
if (value < 0) value = 0;
return value;
}
/**
* Generate a single pulse.
* Write a pulse to the specified digital output channel. There can only be a
* single pulse going at any time.
*
* @param channel The Digital Output channel that the pulse should be output on
* @param pulseLength The active length of the pulse (in seconds)
*/
void HAL_Pulse(HAL_DigitalHandle dioPortHandle, double pulseLength,
int32_t* status) {
auto port = digitalChannelHandles.Get(dioPortHandle, HAL_HandleEnum::DIO);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
// TODO (Thad) Add this
}
/**
* Check a DIO line to see if it is currently generating a pulse.
*
* @return A pulse is in progress
*/
HAL_Bool HAL_IsPulsing(HAL_DigitalHandle dioPortHandle, int32_t* status) {
auto port = digitalChannelHandles.Get(dioPortHandle, HAL_HandleEnum::DIO);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
return false;
// TODO (Thad) Add this
}
/**
* Check if any DIO line is currently generating a pulse.
*
* @return A pulse on some line is in progress
*/
HAL_Bool HAL_IsAnyPulsing(int32_t* status) {
return false; // TODO(Thad) Figure this out
}
/**
* Write the filter index from the FPGA.
* Set the filter index used to filter out short pulses.
*
* @param dioPortHandle Handle to the digital I/O channel
* @param filterIndex The filter index. Must be in the range 0 - 3, where 0
* means "none" and 1 - 3 means filter # filterIndex - 1.
*/
void HAL_SetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t filterIndex,
int32_t* status) {
auto port = digitalChannelHandles.Get(dioPortHandle, HAL_HandleEnum::DIO);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
// TODO(Thad) Figure this out
}
/**
* Read the filter index from the FPGA.
* Get the filter index used to filter out short pulses.
*
* @param dioPortHandle Handle to the digital I/O channel
* @return filterIndex The filter index. Must be in the range 0 - 3,
* where 0 means "none" and 1 - 3 means filter # filterIndex - 1.
*/
int32_t HAL_GetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t* status) {
auto port = digitalChannelHandles.Get(dioPortHandle, HAL_HandleEnum::DIO);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return 0;
// TODO(Thad) Figure this out
}
/**
* Set the filter period for the specified filter index.
*
* Set the filter period in FPGA cycles. Even though there are 2 different
* filter index domains (MXP vs HDR), ignore that distinction for now since it
* compilicates the interface. That can be changed later.
*
* @param filterIndex The filter index, 0 - 2.
* @param value The number of cycles that the signal must not transition to be
* counted as a transition.
*/
void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status) {
// TODO(Thad) figure this out
}
/**
* Get the filter period for the specified filter index.
*
* Get the filter period in FPGA cycles. Even though there are 2 different
* filter index domains (MXP vs HDR), ignore that distinction for now since it
* compilicates the interface. Set status to NiFpga_Status_SoftwareFault if the
* filter values miss-match.
*
* @param filterIndex The filter index, 0 - 2.
* @param value The number of cycles that the signal must not transition to be
* counted as a transition.
*/
int64_t HAL_GetFilterPeriod(int32_t filterIndex, int32_t* status) {
return 0; // TODO(Thad) figure this out
}
}

View File

@@ -0,0 +1,81 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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 "DigitalInternal.h"
#include "ConstantsInternal.h"
#include "HAL/AnalogTrigger.h"
#include "HAL/HAL.h"
#include "HAL/Ports.h"
#include "HAL/cpp/priority_mutex.h"
#include "PortsInternal.h"
namespace hal {
bool digitalSystemsInitialized = false;
DigitalHandleResource<HAL_DigitalHandle, DigitalPort,
kNumDigitalChannels + kNumPWMHeaders>
digitalChannelHandles;
/**
* Map DIO channel numbers from their physical number (10 to 26) to their
* position in the bit field.
*/
int32_t remapMXPChannel(int32_t channel) { return channel - 10; }
int32_t remapMXPPWMChannel(int32_t channel) {
if (channel < 14) {
return channel - 10; // first block of 4 pwms (MXP 0-3)
} else {
return channel - 6; // block of PWMs after SPI
}
}
/**
* remap the digital source channel and set the module.
* If it's an analog trigger, determine the module from the high order routing
* channel else do normal digital input remapping based on channel number
* (MXP)
*/
bool remapDigitalSource(HAL_Handle digitalSourceHandle,
HAL_AnalogTriggerType analogTriggerType,
uint8_t& channel, uint8_t& module,
bool& analogTrigger) {
if (isHandleType(digitalSourceHandle, HAL_HandleEnum::AnalogTrigger)) {
// If handle passed, index is not negative
int32_t index = getHandleIndex(digitalSourceHandle);
channel = (index << 2) + analogTriggerType;
module = channel >> 4;
analogTrigger = true;
return true;
} else if (isHandleType(digitalSourceHandle, HAL_HandleEnum::DIO)) {
int32_t index = getHandleIndex(digitalSourceHandle);
if (index >= kNumDigitalHeaders) {
channel = remapMXPChannel(index);
module = 1;
} else {
channel = index;
module = 0;
}
analogTrigger = false;
return true;
} else {
return false;
}
}
int32_t GetDigitalInputChannel(HAL_DigitalHandle handle, int32_t* status) {
auto digital = digitalChannelHandles.Get(handle, HAL_HandleEnum::DIO);
if (digital == nullptr) {
*status = HAL_HANDLE_ERROR;
return -1;
}
return digital->channel;
}
} // namespace hal

View File

@@ -0,0 +1,80 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <stdint.h>
#include <memory>
#include "HAL/AnalogTrigger.h"
#include "HAL/Ports.h"
#include "HAL/Types.h"
#include "HAL/handles/DigitalHandleResource.h"
#include "HAL/handles/HandlesInternal.h"
#include "PortsInternal.h"
namespace hal {
/**
* MXP channels when used as digital output PWM are offset from actual value
*/
constexpr int32_t kMXPDigitalPWMOffset = 6;
constexpr int32_t kExpectedLoopTiming = 40;
/**
* kDefaultPwmPeriod is in ms
*
* - 20ms periods (50 Hz) are the "safest" setting in that this works for all
* devices
* - 20ms periods seem to be desirable for Vex Motors
* - 20ms periods are the specified period for HS-322HD servos, but work
* reliably down to 10.0 ms; starting at about 8.5ms, the servo sometimes hums
* and get hot; by 5.0ms the hum is nearly continuous
* - 10ms periods work well for Victor 884
* - 5ms periods allows higher update rates for Luminary Micro Jaguar speed
* controllers. Due to the shipping firmware on the Jaguar, we can't run the
* update period less than 5.05 ms.
*
* kDefaultPwmPeriod is the 1x period (5.05 ms). In hardware, the period
* scaling is implemented as an output squelch to get longer periods for old
* devices.
*/
constexpr float kDefaultPwmPeriod = 5.05;
/**
* kDefaultPwmCenter is the PWM range center in ms
*/
constexpr float kDefaultPwmCenter = 1.5;
/**
* kDefaultPWMStepsDown is the number of PWM steps below the centerpoint
*/
constexpr int32_t kDefaultPwmStepsDown = 1000;
constexpr int32_t kPwmDisabled = 0;
struct DigitalPort {
uint8_t channel;
bool configSet = false;
bool eliminateDeadband = false;
int32_t maxPwm = 0;
int32_t deadbandMaxPwm = 0;
int32_t centerPwm = 0;
int32_t deadbandMinPwm = 0;
int32_t minPwm = 0;
};
extern DigitalHandleResource<HAL_DigitalHandle, DigitalPort,
kNumDigitalChannels + kNumPWMHeaders>
digitalChannelHandles;
bool remapDigitalSource(HAL_Handle digitalSourceHandle,
HAL_AnalogTriggerType analogTriggerType,
uint8_t& channel, uint8_t& module, bool& analogTrigger);
int32_t remapMXPPWMChannel(int32_t channel);
int32_t remapMXPChannel(int32_t channel);
int32_t GetDigitalInputChannel(HAL_DigitalHandle handle, int32_t* status);
} // namespace hal

View File

@@ -0,0 +1,252 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/DriverStation.h"
#include "HAL/cpp/priority_condition_variable.h"
#include "HAL/cpp/priority_mutex.h"
#include "MockData/DriverStationDataInternal.h"
static hal::priority_mutex msgMutex;
static hal::priority_condition_variable newDSDataAvailableCond;
static hal::priority_mutex newDSDataAvailableMutex;
static int newDSDataAvailableCounter{0};
using namespace hal;
extern "C" {
int32_t HAL_SetErrorData(const char* errors, int32_t errorsLength,
int32_t waitMs) {
return 0;
}
int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode,
const char* details, const char* location,
const char* callStack, HAL_Bool printMsg) {
// Avoid flooding console by keeping track of previous 5 error
// messages and only printing again if they're longer than 1 second old.
static constexpr int KEEP_MSGS = 5;
std::lock_guard<hal::priority_mutex> lock(msgMutex);
static std::string prevMsg[KEEP_MSGS];
static std::chrono::time_point<std::chrono::steady_clock>
prevMsgTime[KEEP_MSGS];
static bool initialized = false;
if (!initialized) {
for (int i = 0; i < KEEP_MSGS; i++) {
prevMsgTime[i] =
std::chrono::steady_clock::now() - std::chrono::seconds(2);
}
initialized = true;
}
auto curTime = std::chrono::steady_clock::now();
int i;
for (i = 0; i < KEEP_MSGS; ++i) {
if (prevMsg[i] == details) break;
}
int retval = 0;
if (i == KEEP_MSGS || (curTime - prevMsgTime[i]) >= std::chrono::seconds(1)) {
printMsg = true;
if (printMsg) {
if (location && location[0] != '\0') {
std::fprintf(stderr, "%s at %s: ", isError ? "Error" : "Warning",
location);
}
std::fprintf(stderr, "%s\n", details);
if (callStack && callStack[0] != '\0') {
std::fprintf(stderr, "%s\n", callStack);
}
}
if (i == KEEP_MSGS) {
// replace the oldest one
i = 0;
auto first = prevMsgTime[0];
for (int j = 1; j < KEEP_MSGS; ++j) {
if (prevMsgTime[j] < first) {
first = prevMsgTime[j];
i = j;
}
}
prevMsg[i] = details;
}
prevMsgTime[i] = curTime;
}
return retval;
}
int32_t HAL_GetControlWord(HAL_ControlWord* controlWord) {
controlWord->enabled = SimDriverStationData.GetEnabled();
controlWord->autonomous = SimDriverStationData.GetAutonomous();
controlWord->test = SimDriverStationData.GetTest();
controlWord->eStop = SimDriverStationData.GetEStop();
controlWord->fmsAttached = SimDriverStationData.GetFmsAttached();
controlWord->dsAttached = SimDriverStationData.GetDsAttached();
return 0;
}
HAL_AllianceStationID HAL_GetAllianceStation(int32_t* status) {
*status = 0;
return SimDriverStationData.GetAllianceStationId();
}
int32_t HAL_GetJoystickAxes(int32_t joystickNum, HAL_JoystickAxes* axes) {
return 0;
}
int32_t HAL_GetJoystickPOVs(int32_t joystickNum, HAL_JoystickPOVs* povs) {
return 0;
}
int32_t HAL_GetJoystickButtons(int32_t joystickNum,
HAL_JoystickButtons* buttons) {
return 0;
}
/**
* Retrieve the Joystick Descriptor for particular slot
* @param desc [out] descriptor (data transfer object) to fill in. desc is
* filled in regardless of success. In other words, if descriptor is not
* available, desc is filled in with default values matching the init-values in
* Java and C++ Driverstation for when caller requests a too-large joystick
* index.
*
* @return error code reported from Network Comm back-end. Zero is good,
* nonzero is bad.
*/
int32_t HAL_GetJoystickDescriptor(int32_t joystickNum,
HAL_JoystickDescriptor* desc) {
return 0;
}
HAL_Bool HAL_GetJoystickIsXbox(int32_t joystickNum) { return false; }
int32_t HAL_GetJoystickType(int32_t joystickNum) { return 0; }
char* HAL_GetJoystickName(int32_t joystickNum) {
char* name = static_cast<char*>(std::malloc(1));
name[0] = '\0';
return name;
}
void HAL_FreeJoystickName(char* name) { std::free(name); }
int32_t HAL_GetJoystickAxisType(int32_t joystickNum, int32_t axis) { return 0; }
int32_t HAL_SetJoystickOutputs(int32_t joystickNum, int64_t outputs,
int32_t leftRumble, int32_t rightRumble) {
return 0;
}
double HAL_GetMatchTime(int32_t* status) {
return SimDriverStationData.GetMatchTime();
}
void HAL_ObserveUserProgramStarting(void) {
// TODO
}
void HAL_ObserveUserProgramDisabled(void) {
// TODO
}
void HAL_ObserveUserProgramAutonomous(void) {
// TODO
}
void HAL_ObserveUserProgramTeleop(void) {
// TODO
}
void HAL_ObserveUserProgramTest(void) {
// TODO
}
bool HAL_IsNewControlData(void) {
// There is a rollover error condition here. At Packet# = n * (uintmax), this
// will return false when instead it should return true. However, this at a
// 20ms rate occurs once every 2.7 years of DS connected runtime, so not
// worth the cycles to check.
thread_local int lastCount{-1};
int currentCount = 0;
{
std::unique_lock<hal::priority_mutex> lock(newDSDataAvailableMutex);
currentCount = newDSDataAvailableCounter;
}
if (lastCount == currentCount) return false;
lastCount = currentCount;
return true;
}
/**
* Waits for the newest DS packet to arrive. Note that this is a blocking call.
*/
void HAL_WaitForDSData(void) { HAL_WaitForDSDataTimeout(0); }
/**
* Waits for the newest DS packet to arrive. If timeout is <= 0, this will wait
* forever. Otherwise, it will wait until either a new packet, or the timeout
* time has passed. Returns true on new data, false on timeout.
*/
HAL_Bool HAL_WaitForDSDataTimeout(double timeout) {
auto timeoutTime =
std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
std::unique_lock<hal::priority_mutex> lock(newDSDataAvailableMutex);
int currentCount = newDSDataAvailableCounter;
while (newDSDataAvailableCounter == currentCount) {
if (timeout > 0) {
auto timedOut = newDSDataAvailableCond.wait_until(lock, timeoutTime);
if (timedOut == std::cv_status::timeout) {
return false;
}
} else {
newDSDataAvailableCond.wait(lock);
}
}
return true;
}
// Constant number to be used for our occur handle
constexpr int32_t refNumber = 42;
static int32_t newDataOccur(uint32_t refNum) {
// Since we could get other values, require our specific handle
// to signal our threads
if (refNum != refNumber) return 0;
std::lock_guard<hal::priority_mutex> lock(newDSDataAvailableMutex);
// Nofify all threads
newDSDataAvailableCounter++;
newDSDataAvailableCond.notify_all();
return 0;
}
/*
* Call this to initialize the driver station communication. This will properly
* handle multiple calls. However note that this CANNOT be called from a library
* that interfaces with LabVIEW.
*/
void HAL_InitializeDriverStation(void) {
static std::atomic_bool initialized{false};
static hal::priority_mutex initializeMutex;
// Initial check, as if it's true initialization has finished
if (initialized) return;
std::lock_guard<hal::priority_mutex> lock(initializeMutex);
// Second check in case another thread was waiting
if (initialized) return;
SimDriverStationData.ResetData();
initialized = true;
}
/*
* Releases the DS Mutex to allow proper shutdown of any threads that are
* waiting on it.
*/
void HAL_ReleaseDSMutex(void) { newDataOccur(refNumber); }
} // extern "C"

View File

@@ -0,0 +1,326 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/Encoder.h"
#include "CounterInternal.h"
#include "HAL/Counter.h"
#include "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/LimitedHandleResource.h"
#include "MockData/EncoderDataInternal.h"
#include "PortsInternal.h"
using namespace hal;
namespace {
struct Encoder {
HAL_Handle nativeHandle;
HAL_EncoderEncodingType encodingType;
double distancePerPulse;
uint8_t index;
};
struct Empty {};
}
static LimitedHandleResource<HAL_EncoderHandle, Encoder,
kNumEncoders + kNumCounters,
HAL_HandleEnum::Encoder>
encoderHandles;
static LimitedHandleResource<HAL_FPGAEncoderHandle, Empty, kNumEncoders,
HAL_HandleEnum::FPGAEncoder>
fpgaEncoderHandles;
extern "C" {
HAL_EncoderHandle HAL_InitializeEncoder(
HAL_Handle digitalSourceHandleA, HAL_AnalogTriggerType analogTriggerTypeA,
HAL_Handle digitalSourceHandleB, HAL_AnalogTriggerType analogTriggerTypeB,
HAL_Bool reverseDirection, HAL_EncoderEncodingType encodingType,
int32_t* status) {
HAL_Handle nativeHandle = HAL_kInvalidHandle;
if (encodingType == HAL_EncoderEncodingType::HAL_Encoder_k4X) {
// k4x, allocate encoder
nativeHandle = fpgaEncoderHandles.Allocate();
} else {
// k2x or k1x, allocate counter
nativeHandle = counterHandles.Allocate();
}
if (nativeHandle == HAL_kInvalidHandle) {
*status = NO_AVAILABLE_RESOURCES;
return HAL_kInvalidHandle;
}
auto handle = encoderHandles.Allocate();
if (handle == HAL_kInvalidHandle) {
*status = NO_AVAILABLE_RESOURCES;
return HAL_kInvalidHandle;
}
auto encoder = encoderHandles.Get(handle);
if (encoder == nullptr) { // would only occur on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
int16_t index = getHandleIndex(handle);
SimEncoderData[index].SetInitialized(true);
// TODO: Add encoding type to Sim data
encoder->index = index;
encoder->nativeHandle = nativeHandle;
encoder->encodingType = encodingType;
encoder->distancePerPulse = 1.0;
return handle;
}
void HAL_FreeEncoder(HAL_EncoderHandle encoderHandle, int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
encoderHandles.Free(encoderHandle);
if (encoder == nullptr) return;
if (isHandleType(encoder->nativeHandle, HAL_HandleEnum::FPGAEncoder)) {
fpgaEncoderHandles.Free(encoder->nativeHandle);
} else if (isHandleType(encoder->nativeHandle, HAL_HandleEnum::Counter)) {
counterHandles.Free(encoder->nativeHandle);
}
SimEncoderData[encoder->index].SetInitialized(false);
}
static inline int EncodingScaleFactor(Encoder* encoder) {
switch (encoder->encodingType) {
case HAL_Encoder_k1X:
return 1;
case HAL_Encoder_k2X:
return 2;
case HAL_Encoder_k4X:
return 4;
default:
return 0;
}
}
static inline double DecodingScaleFactor(Encoder* encoder) {
switch (encoder->encodingType) {
case HAL_Encoder_k1X:
return 1.0;
case HAL_Encoder_k2X:
return 0.5;
case HAL_Encoder_k4X:
return 0.25;
default:
return 0.0;
}
}
int32_t HAL_GetEncoder(HAL_EncoderHandle encoderHandle, int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimEncoderData[encoder->index].GetCount();
}
int32_t HAL_GetEncoderRaw(HAL_EncoderHandle encoderHandle, int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimEncoderData[encoder->index].GetCount() /
DecodingScaleFactor(encoder.get());
}
int32_t HAL_GetEncoderEncodingScale(HAL_EncoderHandle encoderHandle,
int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return EncodingScaleFactor(encoder.get());
}
void HAL_ResetEncoder(HAL_EncoderHandle encoderHandle, int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimEncoderData[encoder->index].SetCount(0);
SimEncoderData[encoder->index].SetPeriod(std::numeric_limits<double>::max());
SimEncoderData[encoder->index].SetReset(true);
}
double HAL_GetEncoderPeriod(HAL_EncoderHandle encoderHandle, int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimEncoderData[encoder->index].GetPeriod();
}
void HAL_SetEncoderMaxPeriod(HAL_EncoderHandle encoderHandle, double maxPeriod,
int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimEncoderData[encoder->index].SetMaxPeriod(maxPeriod);
}
HAL_Bool HAL_GetEncoderStopped(HAL_EncoderHandle encoderHandle,
int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimEncoderData[encoder->index].GetPeriod() >
SimEncoderData[encoder->index].GetMaxPeriod();
}
HAL_Bool HAL_GetEncoderDirection(HAL_EncoderHandle encoderHandle,
int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimEncoderData[encoder->index].GetDirection();
}
double HAL_GetEncoderDistance(HAL_EncoderHandle encoderHandle,
int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimEncoderData[encoder->index].GetCount() * encoder->distancePerPulse;
}
double HAL_GetEncoderRate(HAL_EncoderHandle encoderHandle, int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return encoder->distancePerPulse / SimEncoderData[encoder->index].GetPeriod();
}
void HAL_SetEncoderMinRate(HAL_EncoderHandle encoderHandle, double minRate,
int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (minRate == 0.0) {
*status = PARAMETER_OUT_OF_RANGE;
return;
}
SimEncoderData[encoder->index].SetMaxPeriod(encoder->distancePerPulse /
minRate);
}
void HAL_SetEncoderDistancePerPulse(HAL_EncoderHandle encoderHandle,
double distancePerPulse, int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (distancePerPulse == 0.0) {
*status = PARAMETER_OUT_OF_RANGE;
return;
}
encoder->distancePerPulse = distancePerPulse;
}
void HAL_SetEncoderReverseDirection(HAL_EncoderHandle encoderHandle,
HAL_Bool reverseDirection,
int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimEncoderData[encoder->index].SetReverseDirection(reverseDirection);
}
void HAL_SetEncoderSamplesToAverage(HAL_EncoderHandle encoderHandle,
int32_t samplesToAverage, int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimEncoderData[encoder->index].SetSamplesToAverage(samplesToAverage);
}
int32_t HAL_GetEncoderSamplesToAverage(HAL_EncoderHandle encoderHandle,
int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimEncoderData[encoder->index].GetSamplesToAverage();
}
void HAL_SetEncoderIndexSource(HAL_EncoderHandle encoderHandle,
HAL_Handle digitalSourceHandle,
HAL_AnalogTriggerType analogTriggerType,
HAL_EncoderIndexingType type, int32_t* status) {
// Not implemented yet
}
int32_t HAL_GetEncoderFPGAIndex(HAL_EncoderHandle encoderHandle,
int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return encoder->index;
}
double HAL_GetEncoderDecodingScaleFactor(HAL_EncoderHandle encoderHandle,
int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0.0;
}
return DecodingScaleFactor(encoder.get());
}
double HAL_GetEncoderDistancePerPulse(HAL_EncoderHandle encoderHandle,
int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0.0;
}
return encoder->distancePerPulse;
}
HAL_EncoderEncodingType HAL_GetEncoderEncodingType(
HAL_EncoderHandle encoderHandle, int32_t* status) {
auto encoder = encoderHandles.Get(encoderHandle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return HAL_Encoder_k4X; // default to k4x
}
return encoder->encodingType;
}
}

View File

@@ -0,0 +1,457 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
typedef enum {
CTR_OKAY, // No Error - Function executed as expected
CTR_RxTimeout, // CAN frame has not been received within specified period of
// time.
CTR_TxTimeout, // Not used.
CTR_InvalidParamValue, // Caller passed an invalid param
CTR_UnexpectedArbId, // Specified CAN Id is invalid.
CTR_TxFailed, // Could not transmit the CAN frame.
CTR_SigNotUpdated, // Have not received an value response for signal.
CTR_BufferFull, // Caller attempted to insert data into a buffer that is
// full.
} CTR_Code;
// CAN Errors
#define ERR_CANSessionMux_InvalidBuffer -44086
#define ERR_CANSessionMux_MessageNotFound -44087
#define WARN_CANSessionMux_NoToken 44087
#define ERR_CANSessionMux_NotAllowed -44088
#define ERR_CANSessionMux_NotInitialized -44089
#define ERR_CANSessionMux_SessionOverrun 44050
// VISA Error
#define _VI_ERROR (-2147483647L - 1)
#define VI_ERROR_SYSTEM_ERROR (_VI_ERROR + 0x3FFF0000L)
#define VI_ERROR_INV_OBJECT (_VI_ERROR + 0x3FFF000EL)
#define VI_ERROR_RSRC_LOCKED (_VI_ERROR + 0x3FFF000FL)
#define VI_ERROR_INV_EXPR (_VI_ERROR + 0x3FFF0010L)
#define VI_ERROR_RSRC_NFOUND (_VI_ERROR + 0x3FFF0011L)
#define VI_ERROR_INV_RSRC_NAME (_VI_ERROR + 0x3FFF0012L)
#define VI_ERROR_INV_ACC_MODE (_VI_ERROR + 0x3FFF0013L)
#define VI_ERROR_TMO (_VI_ERROR + 0x3FFF0015L)
#define VI_ERROR_CLOSING_FAILED (_VI_ERROR + 0x3FFF0016L)
#define VI_ERROR_INV_DEGREE (_VI_ERROR + 0x3FFF001BL)
#define VI_ERROR_INV_JOB_ID (_VI_ERROR + 0x3FFF001CL)
#define VI_ERROR_NSUP_ATTR (_VI_ERROR + 0x3FFF001DL)
#define VI_ERROR_NSUP_ATTR_STATE (_VI_ERROR + 0x3FFF001EL)
#define VI_ERROR_ATTR_READONLY (_VI_ERROR + 0x3FFF001FL)
#define VI_ERROR_INV_LOCK_TYPE (_VI_ERROR + 0x3FFF0020L)
#define VI_ERROR_INV_ACCESS_KEY (_VI_ERROR + 0x3FFF0021L)
#define VI_ERROR_INV_EVENT (_VI_ERROR + 0x3FFF0026L)
#define VI_ERROR_INV_MECH (_VI_ERROR + 0x3FFF0027L)
#define VI_ERROR_HNDLR_NINSTALLED (_VI_ERROR + 0x3FFF0028L)
#define VI_ERROR_INV_HNDLR_REF (_VI_ERROR + 0x3FFF0029L)
#define VI_ERROR_INV_CONTEXT (_VI_ERROR + 0x3FFF002AL)
#define VI_ERROR_QUEUE_OVERFLOW (_VI_ERROR + 0x3FFF002DL)
#define VI_ERROR_NENABLED (_VI_ERROR + 0x3FFF002FL)
#define VI_ERROR_ABORT (_VI_ERROR + 0x3FFF0030L)
#define VI_ERROR_RAW_WR_PROT_VIOL (_VI_ERROR + 0x3FFF0034L)
#define VI_ERROR_RAW_RD_PROT_VIOL (_VI_ERROR + 0x3FFF0035L)
#define VI_ERROR_OUTP_PROT_VIOL (_VI_ERROR + 0x3FFF0036L)
#define VI_ERROR_INP_PROT_VIOL (_VI_ERROR + 0x3FFF0037L)
#define VI_ERROR_BERR (_VI_ERROR + 0x3FFF0038L)
#define VI_ERROR_IN_PROGRESS (_VI_ERROR + 0x3FFF0039L)
#define VI_ERROR_INV_SETUP (_VI_ERROR + 0x3FFF003AL)
#define VI_ERROR_QUEUE_ERROR (_VI_ERROR + 0x3FFF003BL)
#define VI_ERROR_ALLOC (_VI_ERROR + 0x3FFF003CL)
#define VI_ERROR_INV_MASK (_VI_ERROR + 0x3FFF003DL)
#define VI_ERROR_IO (_VI_ERROR + 0x3FFF003EL)
#define VI_ERROR_INV_FMT (_VI_ERROR + 0x3FFF003FL)
#define VI_ERROR_NSUP_FMT (_VI_ERROR + 0x3FFF0041L)
#define VI_ERROR_LINE_IN_USE (_VI_ERROR + 0x3FFF0042L)
#define VI_ERROR_NSUP_MODE (_VI_ERROR + 0x3FFF0046L)
#define VI_ERROR_SRQ_NOCCURRED (_VI_ERROR + 0x3FFF004AL)
#define VI_ERROR_INV_SPACE (_VI_ERROR + 0x3FFF004EL)
#define VI_ERROR_INV_OFFSET (_VI_ERROR + 0x3FFF0051L)
#define VI_ERROR_INV_WIDTH (_VI_ERROR + 0x3FFF0052L)
#define VI_ERROR_NSUP_OFFSET (_VI_ERROR + 0x3FFF0054L)
#define VI_ERROR_NSUP_VAR_WIDTH (_VI_ERROR + 0x3FFF0055L)
#define VI_ERROR_WINDOW_NMAPPED (_VI_ERROR + 0x3FFF0057L)
#define VI_ERROR_RESP_PENDING (_VI_ERROR + 0x3FFF0059L)
#define VI_ERROR_NLISTENERS (_VI_ERROR + 0x3FFF005FL)
#define VI_ERROR_NCIC (_VI_ERROR + 0x3FFF0060L)
#define VI_ERROR_NSYS_CNTLR (_VI_ERROR + 0x3FFF0061L)
#define VI_ERROR_NSUP_OPER (_VI_ERROR + 0x3FFF0067L)
#define VI_ERROR_INTR_PENDING (_VI_ERROR + 0x3FFF0068L)
#define VI_ERROR_ASRL_PARITY (_VI_ERROR + 0x3FFF006AL)
#define VI_ERROR_ASRL_FRAMING (_VI_ERROR + 0x3FFF006BL)
#define VI_ERROR_ASRL_OVERRUN (_VI_ERROR + 0x3FFF006CL)
#define VI_ERROR_TRIG_NMAPPED (_VI_ERROR + 0x3FFF006EL)
#define VI_ERROR_NSUP_ALIGN_OFFSET (_VI_ERROR + 0x3FFF0070L)
#define VI_ERROR_USER_BUF (_VI_ERROR + 0x3FFF0071L)
#define VI_ERROR_RSRC_BUSY (_VI_ERROR + 0x3FFF0072L)
#define VI_ERROR_NSUP_WIDTH (_VI_ERROR + 0x3FFF0076L)
#define VI_ERROR_INV_PARAMETER (_VI_ERROR + 0x3FFF0078L)
#define VI_ERROR_INV_PROT (_VI_ERROR + 0x3FFF0079L)
#define VI_ERROR_INV_SIZE (_VI_ERROR + 0x3FFF007BL)
#define VI_ERROR_WINDOW_MAPPED (_VI_ERROR + 0x3FFF0080L)
#define VI_ERROR_NIMPL_OPER (_VI_ERROR + 0x3FFF0081L)
#define VI_ERROR_INV_LENGTH (_VI_ERROR + 0x3FFF0083L)
#define VI_ERROR_INV_MODE (_VI_ERROR + 0x3FFF0091L)
#define VI_ERROR_SESN_NLOCKED (_VI_ERROR + 0x3FFF009CL)
#define VI_ERROR_MEM_NSHARED (_VI_ERROR + 0x3FFF009DL)
#define VI_ERROR_LIBRARY_NFOUND (_VI_ERROR + 0x3FFF009EL)
#define VI_ERROR_NSUP_INTR (_VI_ERROR + 0x3FFF009FL)
#define VI_ERROR_INV_LINE (_VI_ERROR + 0x3FFF00A0L)
#define VI_ERROR_FILE_ACCESS (_VI_ERROR + 0x3FFF00A1L)
#define VI_ERROR_FILE_IO (_VI_ERROR + 0x3FFF00A2L)
#define VI_ERROR_NSUP_LINE (_VI_ERROR + 0x3FFF00A3L)
#define VI_ERROR_NSUP_MECH (_VI_ERROR + 0x3FFF00A4L)
#define VI_ERROR_INTF_NUM_NCONFIG (_VI_ERROR + 0x3FFF00A5L)
#define VI_ERROR_CONN_LOST (_VI_ERROR + 0x3FFF00A6L)
#define VI_ERROR_MACHINE_NAVAIL (_VI_ERROR + 0x3FFF00A7L)
#define VI_ERROR_NPERMISSION (_VI_ERROR + 0x3FFF00A8L)
// FPGA Errors
/**
* Represents the resulting status of a function call through its return value.
* 0 is success, negative values are errors, and positive values are warnings.
*/
typedef int32_t NiFpga_Status;
/**
* No errors or warnings.
*/
static const NiFpga_Status NiFpga_Status_Success = 0;
/**
* The timeout expired before the FIFO operation could complete.
*/
static const NiFpga_Status NiFpga_Status_FifoTimeout = -50400;
/**
* No transfer is in progress because the transfer was aborted by the client.
* The operation could not be completed as specified.
*/
static const NiFpga_Status NiFpga_Status_TransferAborted = -50405;
/**
* A memory allocation failed. Try again after rebooting.
*/
static const NiFpga_Status NiFpga_Status_MemoryFull = -52000;
/**
* An unexpected software error occurred.
*/
static const NiFpga_Status NiFpga_Status_SoftwareFault = -52003;
/**
* A parameter to a function was not valid. This could be a NULL pointer, a bad
* value, etc.
*/
static const NiFpga_Status NiFpga_Status_InvalidParameter = -52005;
/**
* A required resource was not found. The NiFpga.* library, the RIO resource, or
* some other resource may be missing.
*/
static const NiFpga_Status NiFpga_Status_ResourceNotFound = -52006;
/**
* A required resource was not properly initialized. This could occur if
* NiFpga_Initialize was not called or a required NiFpga_IrqContext was not
* reserved.
*/
static const NiFpga_Status NiFpga_Status_ResourceNotInitialized = -52010;
/**
* A hardware failure has occurred. The operation could not be completed as
* specified.
*/
static const NiFpga_Status NiFpga_Status_HardwareFault = -52018;
/**
* The FPGA is already running.
*/
static const NiFpga_Status NiFpga_Status_FpgaAlreadyRunning = -61003;
/**
* An error occurred downloading the VI to the FPGA device. Verify that
* the target is connected and powered and that the resource of the target
* is properly configured.
*/
static const NiFpga_Status NiFpga_Status_DownloadError = -61018;
/**
* The bitfile was not compiled for the specified resource's device type.
*/
static const NiFpga_Status NiFpga_Status_DeviceTypeMismatch = -61024;
/**
* An error was detected in the communication between the host computer and the
* FPGA target.
*/
static const NiFpga_Status NiFpga_Status_CommunicationTimeout = -61046;
/**
* The timeout expired before any of the IRQs were asserted.
*/
static const NiFpga_Status NiFpga_Status_IrqTimeout = -61060;
/**
* The specified bitfile is invalid or corrupt.
*/
static const NiFpga_Status NiFpga_Status_CorruptBitfile = -61070;
/**
* The requested FIFO depth is invalid. It is either 0 or an amount not
* supported by the hardware.
*/
static const NiFpga_Status NiFpga_Status_BadDepth = -61072;
/**
* The number of FIFO elements is invalid. Either the number is greater than the
* depth of the host memory DMA FIFO, or more elements were requested for
* release than had been acquired.
*/
static const NiFpga_Status NiFpga_Status_BadReadWriteCount = -61073;
/**
* A hardware clocking error occurred. A derived clock lost lock with its base
* clock during the execution of the LabVIEW FPGA VI. If any base clocks with
* derived clocks are referencing an external source, make sure that the
* external source is connected and within the supported frequency, jitter,
* accuracy, duty cycle, and voltage specifications. Also verify that the
* characteristics of the base clock match the configuration specified in the
* FPGA Base Clock Properties. If all base clocks with derived clocks are
* generated from free-running, on-board sources, please contact National
* Instruments technical support at ni.com/support.
*/
static const NiFpga_Status NiFpga_Status_ClockLostLock = -61083;
/**
* The operation could not be performed because the FPGA is busy. Stop all
* activities on the FPGA before requesting this operation. If the target is in
* Scan Interface programming mode, put it in FPGA Interface programming mode.
*/
static const NiFpga_Status NiFpga_Status_FpgaBusy = -61141;
/**
* The operation could not be performed because the FPGA is busy operating in
* FPGA Interface C API mode. Stop all activities on the FPGA before requesting
* this operation.
*/
static const NiFpga_Status NiFpga_Status_FpgaBusyFpgaInterfaceCApi = -61200;
/**
* The chassis is in Scan Interface programming mode. In order to run FPGA VIs,
* you must go to the chassis properties page, select FPGA programming mode, and
* deploy settings.
*/
static const NiFpga_Status NiFpga_Status_FpgaBusyScanInterface = -61201;
/**
* The operation could not be performed because the FPGA is busy operating in
* FPGA Interface mode. Stop all activities on the FPGA before requesting this
* operation.
*/
static const NiFpga_Status NiFpga_Status_FpgaBusyFpgaInterface = -61202;
/**
* The operation could not be performed because the FPGA is busy operating in
* Interactive mode. Stop all activities on the FPGA before requesting this
* operation.
*/
static const NiFpga_Status NiFpga_Status_FpgaBusyInteractive = -61203;
/**
* The operation could not be performed because the FPGA is busy operating in
* Emulation mode. Stop all activities on the FPGA before requesting this
* operation.
*/
static const NiFpga_Status NiFpga_Status_FpgaBusyEmulation = -61204;
/**
* LabVIEW FPGA does not support the Reset method for bitfiles that allow
* removal of implicit enable signals in single-cycle Timed Loops.
*/
static const NiFpga_Status NiFpga_Status_ResetCalledWithImplicitEnableRemoval =
-61211;
/**
* LabVIEW FPGA does not support the Abort method for bitfiles that allow
* removal of implicit enable signals in single-cycle Timed Loops.
*/
static const NiFpga_Status NiFpga_Status_AbortCalledWithImplicitEnableRemoval =
-61212;
/**
* LabVIEW FPGA does not support Close and Reset if Last Reference for bitfiles
* that allow removal of implicit enable signals in single-cycle Timed Loops.
* Pass the NiFpga_CloseAttribute_NoResetIfLastSession attribute to NiFpga_Close
* instead of 0.
*/
static const NiFpga_Status
NiFpga_Status_CloseAndResetCalledWithImplicitEnableRemoval = -61213;
/**
* For bitfiles that allow removal of implicit enable signals in single-cycle
* Timed Loops, LabVIEW FPGA does not support this method prior to running the
* bitfile.
*/
static const NiFpga_Status NiFpga_Status_ImplicitEnableRemovalButNotYetRun =
-61214;
/**
* Bitfiles that allow removal of implicit enable signals in single-cycle Timed
* Loops can run only once. Download the bitfile again before re-running the VI.
*/
static const NiFpga_Status
NiFpga_Status_RunAfterStoppedCalledWithImplicitEnableRemoval = -61215;
/**
* A gated clock has violated the handshaking protocol. If you are using
* external gated clocks, ensure that they follow the required clock gating
* protocol. If you are generating your clocks internally, please contact
* National Instruments Technical Support.
*/
static const NiFpga_Status NiFpga_Status_GatedClockHandshakingViolation =
-61216;
/**
* The number of elements requested must be less than or equal to the number of
* unacquired elements left in the host memory DMA FIFO. There are currently
* fewer unacquired elements left in the FIFO than are being requested. Release
* some acquired elements before acquiring more elements.
*/
static const NiFpga_Status NiFpga_Status_ElementsNotPermissibleToBeAcquired =
-61219;
/**
* The operation could not be performed because the FPGA is in configuration or
* discovery mode. Wait for configuration or discovery to complete and retry
* your operation.
*/
static const NiFpga_Status NiFpga_Status_FpgaBusyConfiguration = -61252;
/**
* An unexpected internal error occurred.
*/
static const NiFpga_Status NiFpga_Status_InternalError = -61499;
/**
* The NI-RIO driver was unable to allocate memory for a FIFO. This can happen
* when the combined depth of all DMA FIFOs exceeds the maximum depth for the
* controller, or when the controller runs out of system memory. You may be able
* to reconfigure the controller with a greater maximum FIFO depth. For more
* information, refer to the NI KnowledgeBase article 65OF2ERQ.
*/
static const NiFpga_Status NiFpga_Status_TotalDmaFifoDepthExceeded = -63003;
/**
* Access to the remote system was denied. Use MAX to check the Remote Device
* Access settings under Software>>NI-RIO>>NI-RIO Settings on the remote system.
*/
static const NiFpga_Status NiFpga_Status_AccessDenied = -63033;
/**
* The NI-RIO software on the host is not compatible with the software on the
* target. Upgrade the NI-RIO software on the host in order to connect to this
* target.
*/
static const NiFpga_Status NiFpga_Status_HostVersionMismatch = -63038;
/**
* A connection could not be established to the specified remote device. Ensure
* that the device is on and accessible over the network, that NI-RIO software
* is installed, and that the RIO server is running and properly configured.
*/
static const NiFpga_Status NiFpga_Status_RpcConnectionError = -63040;
/**
* The RPC session is invalid. The target may have reset or been rebooted. Check
* the network connection and retry the operation.
*/
static const NiFpga_Status NiFpga_Status_RpcSessionError = -63043;
/**
* The operation could not complete because another session is accessing the
* FIFO. Close the other session and retry.
*/
static const NiFpga_Status NiFpga_Status_FifoReserved = -63082;
/**
* A Configure FIFO, Stop FIFO, Read FIFO, or Write FIFO function was called
* while the host had acquired elements of the FIFO. Release all acquired
* elements before configuring, stopping, reading, or writing.
*/
static const NiFpga_Status NiFpga_Status_FifoElementsCurrentlyAcquired = -63083;
/**
* A function was called using a misaligned address. The address must be a
* multiple of the size of the datatype.
*/
static const NiFpga_Status NiFpga_Status_MisalignedAccess = -63084;
/**
* The FPGA Read/Write Control Function is accessing a control or indicator
* with data that exceeds the maximum size supported on the current target.
* Refer to the hardware documentation for the limitations on data types for
* this target.
*/
static const NiFpga_Status NiFpga_Status_ControlOrIndicatorTooLarge = -63085;
/**
* A valid .lvbitx bitfile is required. If you are using a valid .lvbitx
* bitfile, the bitfile may not be compatible with the software you are using.
* Determine which version of LabVIEW was used to make the bitfile, update your
* software to that version or later, and try again.
*/
static const NiFpga_Status NiFpga_Status_BitfileReadError = -63101;
/**
* The specified signature does not match the signature of the bitfile. If the
* bitfile has been recompiled, regenerate the C API and rebuild the
* application.
*/
static const NiFpga_Status NiFpga_Status_SignatureMismatch = -63106;
/**
* The bitfile you are trying to use is incompatible with the version
* of NI-RIO installed on the target and/or host. Update the version
* of NI-RIO on the target and/or host to the same version (or later)
* used to compile the bitfile. Alternatively, recompile the bitfile
* with the same version of NI-RIO that is currently installed on the
* target and/or host.
*/
static const NiFpga_Status NiFpga_Status_IncompatibleBitfile = -63107;
/**
* Either the supplied resource name is invalid as a RIO resource name, or the
* device was not found. Use MAX to find the proper resource name for the
* intended device.
*/
static const NiFpga_Status NiFpga_Status_InvalidResourceName = -63192;
/**
* The requested feature is not supported.
*/
static const NiFpga_Status NiFpga_Status_FeatureNotSupported = -63193;
/**
* The NI-RIO software on the target system is not compatible with this
* software. Upgrade the NI-RIO software on the target system.
*/
static const NiFpga_Status NiFpga_Status_VersionMismatch = -63194;
/**
* The session is invalid or has been closed.
*/
static const NiFpga_Status NiFpga_Status_InvalidSession = -63195;
/**
* The maximum number of open FPGA sessions has been reached. Close some open
* sessions.
*/
static const NiFpga_Status NiFpga_Status_OutOfHandles = -63198;

View File

@@ -0,0 +1,207 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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/HAL.h"
#include "ErrorsInternal.h"
#include "HAL/DriverStation.h"
#include "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "MockData/RoboRioDataInternal.h"
#include "MockHooksInternal.h"
using namespace hal;
extern "C" {
HAL_PortHandle HAL_GetPort(int32_t channel) {
// Dont allow a number that wouldn't fit in a uint8_t
if (channel < 0 || channel >= 255) return HAL_kInvalidHandle;
return createPortHandle(channel, 1);
}
/**
* @deprecated Uses module numbers
*/
HAL_PortHandle HAL_GetPortWithModule(int32_t module, int32_t channel) {
// Dont allow a number that wouldn't fit in a uint8_t
if (channel < 0 || channel >= 255) return HAL_kInvalidHandle;
if (module < 0 || module >= 255) return HAL_kInvalidHandle;
return createPortHandle(channel, module);
}
const char* HAL_GetErrorMessage(int32_t code) {
switch (code) {
case 0:
return "";
case CTR_RxTimeout:
return CTR_RxTimeout_MESSAGE;
case CTR_TxTimeout:
return CTR_TxTimeout_MESSAGE;
case CTR_InvalidParamValue:
return CTR_InvalidParamValue_MESSAGE;
case CTR_UnexpectedArbId:
return CTR_UnexpectedArbId_MESSAGE;
case CTR_TxFailed:
return CTR_TxFailed_MESSAGE;
case CTR_SigNotUpdated:
return CTR_SigNotUpdated_MESSAGE;
case NiFpga_Status_FifoTimeout:
return NiFpga_Status_FifoTimeout_MESSAGE;
case NiFpga_Status_TransferAborted:
return NiFpga_Status_TransferAborted_MESSAGE;
case NiFpga_Status_MemoryFull:
return NiFpga_Status_MemoryFull_MESSAGE;
case NiFpga_Status_SoftwareFault:
return NiFpga_Status_SoftwareFault_MESSAGE;
case NiFpga_Status_InvalidParameter:
return NiFpga_Status_InvalidParameter_MESSAGE;
case NiFpga_Status_ResourceNotFound:
return NiFpga_Status_ResourceNotFound_MESSAGE;
case NiFpga_Status_ResourceNotInitialized:
return NiFpga_Status_ResourceNotInitialized_MESSAGE;
case NiFpga_Status_HardwareFault:
return NiFpga_Status_HardwareFault_MESSAGE;
case NiFpga_Status_IrqTimeout:
return NiFpga_Status_IrqTimeout_MESSAGE;
case SAMPLE_RATE_TOO_HIGH:
return SAMPLE_RATE_TOO_HIGH_MESSAGE;
case VOLTAGE_OUT_OF_RANGE:
return VOLTAGE_OUT_OF_RANGE_MESSAGE;
case LOOP_TIMING_ERROR:
return LOOP_TIMING_ERROR_MESSAGE;
case SPI_WRITE_NO_MOSI:
return SPI_WRITE_NO_MOSI_MESSAGE;
case SPI_READ_NO_MISO:
return SPI_READ_NO_MISO_MESSAGE;
case SPI_READ_NO_DATA:
return SPI_READ_NO_DATA_MESSAGE;
case INCOMPATIBLE_STATE:
return INCOMPATIBLE_STATE_MESSAGE;
case NO_AVAILABLE_RESOURCES:
return NO_AVAILABLE_RESOURCES_MESSAGE;
case RESOURCE_IS_ALLOCATED:
return RESOURCE_IS_ALLOCATED_MESSAGE;
case RESOURCE_OUT_OF_RANGE:
return RESOURCE_OUT_OF_RANGE_MESSAGE;
case HAL_INVALID_ACCUMULATOR_CHANNEL:
return HAL_INVALID_ACCUMULATOR_CHANNEL_MESSAGE;
case HAL_HANDLE_ERROR:
return HAL_HANDLE_ERROR_MESSAGE;
case NULL_PARAMETER:
return NULL_PARAMETER_MESSAGE;
case ANALOG_TRIGGER_LIMIT_ORDER_ERROR:
return ANALOG_TRIGGER_LIMIT_ORDER_ERROR_MESSAGE;
case ANALOG_TRIGGER_PULSE_OUTPUT_ERROR:
return ANALOG_TRIGGER_PULSE_OUTPUT_ERROR_MESSAGE;
case PARAMETER_OUT_OF_RANGE:
return PARAMETER_OUT_OF_RANGE_MESSAGE;
case HAL_COUNTER_NOT_SUPPORTED:
return HAL_COUNTER_NOT_SUPPORTED_MESSAGE;
case ERR_CANSessionMux_InvalidBuffer:
return ERR_CANSessionMux_InvalidBuffer_MESSAGE;
case ERR_CANSessionMux_MessageNotFound:
return ERR_CANSessionMux_MessageNotFound_MESSAGE;
case WARN_CANSessionMux_NoToken:
return WARN_CANSessionMux_NoToken_MESSAGE;
case ERR_CANSessionMux_NotAllowed:
return ERR_CANSessionMux_NotAllowed_MESSAGE;
case ERR_CANSessionMux_NotInitialized:
return ERR_CANSessionMux_NotInitialized_MESSAGE;
case VI_ERROR_SYSTEM_ERROR:
return VI_ERROR_SYSTEM_ERROR_MESSAGE;
case VI_ERROR_INV_OBJECT:
return VI_ERROR_INV_OBJECT_MESSAGE;
case VI_ERROR_RSRC_LOCKED:
return VI_ERROR_RSRC_LOCKED_MESSAGE;
case VI_ERROR_RSRC_NFOUND:
return VI_ERROR_RSRC_NFOUND_MESSAGE;
case VI_ERROR_INV_RSRC_NAME:
return VI_ERROR_INV_RSRC_NAME_MESSAGE;
case VI_ERROR_QUEUE_OVERFLOW:
return VI_ERROR_QUEUE_OVERFLOW_MESSAGE;
case VI_ERROR_IO:
return VI_ERROR_IO_MESSAGE;
case VI_ERROR_ASRL_PARITY:
return VI_ERROR_ASRL_PARITY_MESSAGE;
case VI_ERROR_ASRL_FRAMING:
return VI_ERROR_ASRL_FRAMING_MESSAGE;
case VI_ERROR_ASRL_OVERRUN:
return VI_ERROR_ASRL_OVERRUN_MESSAGE;
case VI_ERROR_RSRC_BUSY:
return VI_ERROR_RSRC_BUSY_MESSAGE;
case VI_ERROR_INV_PARAMETER:
return VI_ERROR_INV_PARAMETER_MESSAGE;
case HAL_PWM_SCALE_ERROR:
return HAL_PWM_SCALE_ERROR_MESSAGE;
default:
return "Unknown error status";
}
}
/**
* Returns the runtime type of this HAL
*/
HAL_RuntimeType HAL_GetRuntimeType() { return HAL_Mock; }
/**
* Return the FPGA Version number.
* For now, expect this to be competition year.
* @return FPGA Version number.
*/
int32_t HAL_GetFPGAVersion(int32_t* status) {
return 2018; // Automatically script this at some point
}
/**
* Return the FPGA Revision number.
* The format of the revision is 3 numbers.
* The 12 most significant bits are the Major Revision.
* the next 8 bits are the Minor Revision.
* The 12 least significant bits are the Build Number.
* @return FPGA Revision number.
*/
int64_t HAL_GetFPGARevision(int32_t* status) {
return 0; // TODO: Find a better number to return;
}
/**
* Read the microsecond-resolution timer on the FPGA.
*
* @return The current time in microseconds according to the FPGA (since FPGA
* reset).
*/
uint64_t HAL_GetFPGATime(int32_t* status) { return hal::GetFPGATime(); }
/**
* Get the state of the "USER" button on the roboRIO
* @return true if the button is currently pressed down
*/
HAL_Bool HAL_GetFPGAButton(int32_t* status) {
return SimRoboRioData[0].GetFPGAButton();
}
HAL_Bool HAL_GetSystemActive(int32_t* status) {
return true; // Figure out if we need to handle this
}
HAL_Bool HAL_GetBrownedOut(int32_t* status) {
return false; // Figure out if we need to detect a brownout condition
}
HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
hal::RestartTiming();
HAL_InitializeDriverStation();
return true; // Add initialization if we need to at a later point
}
int64_t HAL_Report(int32_t resource, int32_t instanceNumber, int32_t context,
const char* feature) {
return 0; // Do nothing for now
}
} // extern "C"

View File

@@ -0,0 +1,26 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/I2C.h"
extern "C" {
void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {}
int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
uint8_t* dataToSend, int32_t sendSize,
uint8_t* dataReceived, int32_t receiveSize) {
return 0;
}
int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress,
uint8_t* dataToSend, int32_t sendSize) {
return 0;
}
int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t* buffer,
int32_t count) {
return 0;
}
void HAL_CloseI2C(HAL_I2CPort port) {}
}

View File

@@ -0,0 +1,555 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/Interrupts.h"
#include <condition_variable>
#include <memory>
#include "AnalogInternal.h"
#include "DigitalInternal.h"
#include "ErrorsInternal.h"
#include "HAL/AnalogTrigger.h"
#include "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/LimitedHandleResource.h"
#include "HAL/handles/UnlimitedHandleResource.h"
#include "MockData/AnalogInDataInternal.h"
#include "MockData/DIODataInternal.h"
#include "MockData/HAL_Value.h"
#include "MockHooksInternal.h"
#include "PortsInternal.h"
using namespace hal;
enum WaitResult {
Timeout = 0x0,
RisingEdge = 0x1,
FallingEdge = 0x100,
Both = 0x101,
};
namespace {
struct Interrupt {
bool isAnalog;
HAL_Handle portHandle;
uint8_t index;
HAL_AnalogTriggerType trigType;
bool watcher;
double risingTimestamp;
double fallingTimestamp;
bool previousState;
bool fireOnUp;
bool fireOnDown;
int32_t callbackId;
void* callbackParam;
HAL_InterruptHandlerFunction callbackFunction;
};
struct SynchronousWaitData {
HAL_InterruptHandle interruptHandle;
std::condition_variable waitCond;
HAL_Bool waitPredicate;
};
} // namespace
static LimitedHandleResource<HAL_InterruptHandle, Interrupt, kNumInterrupts,
HAL_HandleEnum::Interrupt>
interruptHandles;
typedef HAL_Handle SynchronousWaitDataHandle;
static UnlimitedHandleResource<SynchronousWaitDataHandle, SynchronousWaitData,
HAL_HandleEnum::Vendor>
synchronousInterruptHandles;
extern "C" {
HAL_InterruptHandle HAL_InitializeInterrupts(HAL_Bool watcher,
int32_t* status) {
HAL_InterruptHandle handle = interruptHandles.Allocate();
if (handle == HAL_kInvalidHandle) {
*status = NO_AVAILABLE_RESOURCES;
return HAL_kInvalidHandle;
}
auto anInterrupt = interruptHandles.Get(handle);
if (anInterrupt == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
anInterrupt->index = getHandleIndex(handle);
anInterrupt->callbackId = -1;
anInterrupt->watcher = watcher;
return handle;
}
void HAL_CleanInterrupts(HAL_InterruptHandle interruptHandle, int32_t* status) {
HAL_DisableInterrupts(interruptHandle, status);
auto interrupt = interruptHandles.Get(interruptHandle);
interruptHandles.Free(interruptHandle);
}
static void ProcessInterruptDigitalSynchronous(const char* name, void* param,
const struct HAL_Value* value) {
// void* is a SynchronousWaitDataHandle.
// convert to intptr_t first, then to handle
intptr_t handleTmp = reinterpret_cast<intptr_t>(param);
SynchronousWaitDataHandle handle =
static_cast<SynchronousWaitDataHandle>(handleTmp);
auto interruptData = synchronousInterruptHandles.Get(handle);
if (interruptData == nullptr) return;
auto interrupt = interruptHandles.Get(interruptData->interruptHandle);
if (interrupt == nullptr) return;
// Have a valid interrupt
if (value->type != HAL_Type::HAL_BOOLEAN) return;
bool retVal = value->data.v_boolean;
// If no change in interrupt, return;
if (retVal == interrupt->previousState) return;
// If its a falling change, and we dont fire on falling return
if (interrupt->previousState && !interrupt->fireOnDown) return;
// If its a rising change, and we dont fire on rising return.
if (!interrupt->previousState && !interrupt->fireOnUp) return;
interruptData->waitPredicate = true;
// Pulse interrupt
interruptData->waitCond.notify_all();
}
static double GetAnalogTriggerValue(HAL_Handle triggerHandle,
HAL_AnalogTriggerType type,
int32_t* status) {
return HAL_GetAnalogTriggerOutput(triggerHandle, type, status);
}
static void ProcessInterruptAnalogSynchronous(const char* name, void* param,
const struct HAL_Value* value) {
// void* is a SynchronousWaitDataHandle.
// convert to intptr_t first, then to handle
intptr_t handleTmp = reinterpret_cast<intptr_t>(param);
SynchronousWaitDataHandle handle =
static_cast<SynchronousWaitDataHandle>(handleTmp);
auto interruptData = synchronousInterruptHandles.Get(handle);
if (interruptData == nullptr) return;
auto interrupt = interruptHandles.Get(interruptData->interruptHandle);
if (interrupt == nullptr) return;
// Have a valid interrupt
if (value->type != HAL_Type::HAL_DOUBLE) return;
int32_t status = 0;
bool retVal = GetAnalogTriggerValue(interrupt->portHandle,
interrupt->trigType, &status);
if (status != 0) {
// Interrupt and Cancel
interruptData->waitPredicate = true;
// Pulse interrupt
interruptData->waitCond.notify_all();
}
// If no change in interrupt, return;
if (retVal == interrupt->previousState) return;
// If its a falling change, and we dont fire on falling return
if (interrupt->previousState && !interrupt->fireOnDown) return;
// If its a rising change, and we dont fire on rising return.
if (!interrupt->previousState && !interrupt->fireOnUp) return;
interruptData->waitPredicate = true;
// Pulse interrupt
interruptData->waitCond.notify_all();
}
static int64_t WaitForInterruptDigital(HAL_InterruptHandle handle,
Interrupt* interrupt, double timeout,
bool ignorePrevious) {
auto data = std::make_shared<SynchronousWaitData>();
auto dataHandle = synchronousInterruptHandles.Allocate(data);
if (dataHandle == HAL_kInvalidHandle) {
// Error allocating data
return WaitResult::Timeout;
}
// auto data = synchronousInterruptHandles.Get(dataHandle);
data->waitPredicate = false;
data->interruptHandle = handle;
int32_t status = 0;
int32_t digitalIndex = GetDigitalInputChannel(interrupt->portHandle, &status);
if (status != 0) return WaitResult::Timeout;
interrupt->previousState = SimDIOData[digitalIndex].GetValue();
int32_t uid = SimDIOData[digitalIndex].RegisterValueCallback(
&ProcessInterruptDigitalSynchronous, reinterpret_cast<void*>(dataHandle),
false);
bool timedOut = false;
std::mutex waitMutex;
#if defined(_MSC_VER) && _MSC_VER < 1900
auto timeoutTime = std::chrono::steady_clock::now() +
std::chrono::duration<int64_t, std::nano>(
static_cast<int64_t>(timeout * 1e9));
#else
auto timeoutTime =
std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
#endif
{
std::unique_lock<std::mutex> lock(waitMutex);
while (!data->waitPredicate) {
if (data->waitCond.wait_until(lock, timeoutTime) ==
std::cv_status::timeout) {
timedOut = true;
break;
}
}
}
// Cancel our callback
SimDIOData[digitalIndex].CancelValueCallback(uid);
synchronousInterruptHandles.Free(dataHandle);
// Check for what to return
if (timedOut) return WaitResult::Timeout;
// True => false, Falling
if (interrupt->previousState) {
// Set our return value and our timestamps
interrupt->fallingTimestamp = hal::GetFPGATimestamp();
return 1 << (8 + interrupt->index);
} else {
interrupt->risingTimestamp = hal::GetFPGATimestamp();
return 1 << (interrupt->index);
}
}
static int64_t WaitForInterruptAnalog(HAL_InterruptHandle handle,
Interrupt* interrupt, double timeout,
bool ignorePrevious) {
auto data = std::make_shared<SynchronousWaitData>();
auto dataHandle = synchronousInterruptHandles.Allocate(data);
if (dataHandle == HAL_kInvalidHandle) {
// Error allocating data
return WaitResult::Timeout;
}
data->waitPredicate = false;
data->interruptHandle = handle;
int32_t status = 0;
interrupt->previousState = GetAnalogTriggerValue(
interrupt->portHandle, interrupt->trigType, &status);
if (status != 0) return WaitResult::Timeout;
int32_t analogIndex =
GetAnalogTriggerInputIndex(interrupt->portHandle, &status);
if (status != 0) return WaitResult::Timeout;
int32_t uid = SimAnalogInData[analogIndex].RegisterVoltageCallback(
&ProcessInterruptAnalogSynchronous, reinterpret_cast<void*>(dataHandle),
false);
bool timedOut = false;
std::mutex waitMutex;
#if defined(_MSC_VER) && _MSC_VER < 1900
auto timeoutTime = std::chrono::steady_clock::now() +
std::chrono::duration<int64_t, std::nano>(
static_cast<int64_t>(timeout * 1e9));
#else
auto timeoutTime =
std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
#endif
{
std::unique_lock<std::mutex> lock(waitMutex);
while (!data->waitPredicate) {
if (data->waitCond.wait_until(lock, timeoutTime) ==
std::cv_status::timeout) {
timedOut = true;
break;
}
}
}
// Cancel our callback
SimAnalogInData[analogIndex].CancelVoltageCallback(uid);
synchronousInterruptHandles.Free(dataHandle);
// Check for what to return
if (timedOut) return WaitResult::Timeout;
// True => false, Falling
if (interrupt->previousState) {
// Set our return value and our timestamps
interrupt->fallingTimestamp = hal::GetFPGATimestamp();
return 1 << (8 + interrupt->index);
} else {
interrupt->risingTimestamp = hal::GetFPGATimestamp();
return 1 << (interrupt->index);
}
}
int64_t HAL_WaitForInterrupt(HAL_InterruptHandle interruptHandle,
double timeout, HAL_Bool ignorePrevious,
int32_t* status) {
auto interrupt = interruptHandles.Get(interruptHandle);
if (interrupt == nullptr) {
*status = HAL_HANDLE_ERROR;
return WaitResult::Timeout;
}
// Check to make sure we are actually an interrupt in synchronous mode
if (!interrupt->watcher) {
*status = NiFpga_Status_InvalidParameter;
return WaitResult::Timeout;
}
if (interrupt->isAnalog) {
return WaitForInterruptAnalog(interruptHandle, interrupt.get(), timeout,
ignorePrevious);
} else {
return WaitForInterruptDigital(interruptHandle, interrupt.get(), timeout,
ignorePrevious);
}
}
static void ProcessInterruptDigitalAsynchronous(const char* name, void* param,
const struct HAL_Value* value) {
// void* is a HAL handle
// convert to intptr_t first, then to handle
intptr_t handleTmp = reinterpret_cast<intptr_t>(param);
HAL_InterruptHandle handle = static_cast<HAL_InterruptHandle>(handleTmp);
auto interrupt = interruptHandles.Get(handle);
if (interrupt == nullptr) return;
// Have a valid interrupt
if (value->type != HAL_Type::HAL_BOOLEAN) return;
bool retVal = value->data.v_boolean;
// If no change in interrupt, return;
if (retVal == interrupt->previousState) return;
int32_t mask = 0;
if (interrupt->previousState) {
interrupt->previousState = retVal;
interrupt->fallingTimestamp = hal::GetFPGATimestamp();
mask = 1 << (8 + interrupt->index);
if (!interrupt->fireOnDown) return;
} else {
interrupt->previousState = retVal;
interrupt->risingTimestamp = hal::GetFPGATimestamp();
mask = 1 << (interrupt->index);
if (!interrupt->fireOnUp) return;
}
// run callback
auto callback = interrupt->callbackFunction;
if (callback == nullptr) return;
callback(mask, interrupt->callbackParam);
}
static void ProcessInterruptAnalogAsynchronous(const char* name, void* param,
const struct HAL_Value* value) {
// void* is a HAL handle
// convert to intptr_t first, then to handle
intptr_t handleTmp = reinterpret_cast<intptr_t>(param);
HAL_InterruptHandle handle = static_cast<HAL_InterruptHandle>(handleTmp);
auto interrupt = interruptHandles.Get(handle);
if (interrupt == nullptr) return;
// Have a valid interrupt
if (value->type != HAL_Type::HAL_DOUBLE) return;
int32_t status = 0;
bool retVal = GetAnalogTriggerValue(interrupt->portHandle,
interrupt->trigType, &status);
if (status != 0) return;
// If no change in interrupt, return;
if (retVal == interrupt->previousState) return;
int mask = 0;
if (interrupt->previousState) {
interrupt->previousState = retVal;
interrupt->fallingTimestamp = hal::GetFPGATimestamp();
if (!interrupt->fireOnDown) return;
mask = 1 << (8 + interrupt->index);
} else {
interrupt->previousState = retVal;
interrupt->risingTimestamp = hal::GetFPGATimestamp();
if (!interrupt->fireOnUp) return;
mask = 1 << (interrupt->index);
}
// run callback
auto callback = interrupt->callbackFunction;
if (callback == nullptr) return;
callback(mask, interrupt->callbackParam);
}
static void EnableInterruptsDigital(HAL_InterruptHandle handle,
Interrupt* interrupt) {
int32_t status = 0;
int32_t digitalIndex = GetDigitalInputChannel(interrupt->portHandle, &status);
if (status != 0) return;
interrupt->previousState = SimDIOData[digitalIndex].GetValue();
int32_t uid = SimDIOData[digitalIndex].RegisterValueCallback(
&ProcessInterruptDigitalAsynchronous, reinterpret_cast<void*>(handle),
false);
interrupt->callbackId = uid;
}
static void EnableInterruptsAnalog(HAL_InterruptHandle handle,
Interrupt* interrupt) {
int32_t status = 0;
int32_t analogIndex =
GetAnalogTriggerInputIndex(interrupt->portHandle, &status);
if (status != 0) return;
status = 0;
interrupt->previousState = GetAnalogTriggerValue(
interrupt->portHandle, interrupt->trigType, &status);
if (status != 0) return;
int32_t uid = SimAnalogInData[analogIndex].RegisterVoltageCallback(
&ProcessInterruptAnalogAsynchronous, reinterpret_cast<void*>(handle),
false);
interrupt->callbackId = uid;
}
void HAL_EnableInterrupts(HAL_InterruptHandle interruptHandle,
int32_t* status) {
auto interrupt = interruptHandles.Get(interruptHandle);
if (interrupt == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
// If we have not had a callback set, error out
if (interrupt->callbackFunction == nullptr) {
*status = INCOMPATIBLE_STATE;
return;
}
// EnableInterrupts has already been called
if (interrupt->callbackId >= 0) {
// We can double enable safely.
return;
}
if (interrupt->isAnalog) {
EnableInterruptsAnalog(interruptHandle, interrupt.get());
} else {
EnableInterruptsDigital(interruptHandle, interrupt.get());
}
}
void HAL_DisableInterrupts(HAL_InterruptHandle interruptHandle,
int32_t* status) {
auto interrupt = interruptHandles.Get(interruptHandle);
if (interrupt == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
// No need to disable if we are already disabled
if (interrupt->callbackId < 0) return;
if (interrupt->isAnalog) {
// Do analog
int32_t status = 0;
int32_t analogIndex =
GetAnalogTriggerInputIndex(interrupt->portHandle, &status);
if (status != 0) return;
SimAnalogInData[analogIndex].CancelVoltageCallback(interrupt->callbackId);
} else {
int32_t status = 0;
int32_t digitalIndex =
GetDigitalInputChannel(interrupt->portHandle, &status);
if (status != 0) return;
SimDIOData[digitalIndex].CancelValueCallback(interrupt->callbackId);
}
interrupt->callbackId = -1;
}
double HAL_ReadInterruptRisingTimestamp(HAL_InterruptHandle interruptHandle,
int32_t* status) {
auto interrupt = interruptHandles.Get(interruptHandle);
if (interrupt == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return interrupt->risingTimestamp;
}
double HAL_ReadInterruptFallingTimestamp(HAL_InterruptHandle interruptHandle,
int32_t* status) {
auto interrupt = interruptHandles.Get(interruptHandle);
if (interrupt == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return interrupt->fallingTimestamp;
}
void HAL_RequestInterrupts(HAL_InterruptHandle interruptHandle,
HAL_Handle digitalSourceHandle,
HAL_AnalogTriggerType analogTriggerType,
int32_t* status) {
auto interrupt = interruptHandles.Get(interruptHandle);
if (interrupt == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
bool routingAnalogTrigger = false;
uint8_t routingChannel = 0;
uint8_t routingModule = 0;
bool success =
remapDigitalSource(digitalSourceHandle, analogTriggerType, routingChannel,
routingModule, routingAnalogTrigger);
if (!success) {
*status = HAL_HANDLE_ERROR;
return;
}
interrupt->isAnalog = routingAnalogTrigger;
interrupt->trigType = analogTriggerType;
interrupt->portHandle = digitalSourceHandle;
}
void HAL_AttachInterruptHandler(HAL_InterruptHandle interruptHandle,
HAL_InterruptHandlerFunction handler,
void* param, int32_t* status) {
auto interrupt = interruptHandles.Get(interruptHandle);
if (interrupt == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
interrupt->callbackFunction = handler;
interrupt->callbackParam = param;
}
void HAL_AttachInterruptHandlerThreaded(HAL_InterruptHandle interruptHandle,
HAL_InterruptHandlerFunction handler,
void* param, int32_t* status) {
HAL_AttachInterruptHandler(interruptHandle, handler, param, status);
}
void HAL_SetInterruptUpSourceEdge(HAL_InterruptHandle interruptHandle,
HAL_Bool risingEdge, HAL_Bool fallingEdge,
int32_t* status) {
auto interrupt = interruptHandles.Get(interruptHandle);
if (interrupt == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
interrupt->fireOnDown = fallingEdge;
interrupt->fireOnUp = risingEdge;
}
}

View File

@@ -0,0 +1,312 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "AccelerometerDataInternal.h"
#include "NotifyCallbackHelpers.h"
using namespace hal;
AccelerometerData hal::SimAccelerometerData[1];
void AccelerometerData::ResetData() {
m_active = false;
m_activeCallbacks = nullptr;
m_range = static_cast<HAL_AccelerometerRange>(0);
m_rangeCallbacks = nullptr;
m_x = 0.0;
m_xCallbacks = nullptr;
m_y = 0.0;
m_yCallbacks = nullptr;
m_z = 0.0;
m_zCallbacks = nullptr;
}
int32_t AccelerometerData::RegisterActiveCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_activeCallbacks =
RegisterCallback(m_activeCallbacks, "Active", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetActive());
callback("Active", param, &value);
}
return newUid;
}
void AccelerometerData::CancelActiveCallback(int32_t uid) {
m_activeCallbacks = CancelCallback(m_activeCallbacks, uid);
}
void AccelerometerData::InvokeActiveCallback(HAL_Value value) {
InvokeCallback(m_activeCallbacks, "Active", &value);
}
HAL_Bool AccelerometerData::GetActive() { return m_active; }
void AccelerometerData::SetActive(HAL_Bool active) {
HAL_Bool oldValue = m_active.exchange(active);
if (oldValue != active) {
InvokeActiveCallback(MakeBoolean(active));
}
}
int32_t AccelerometerData::RegisterRangeCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_rangeCallbacks =
RegisterCallback(m_rangeCallbacks, "Range", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeEnum(GetRange());
callback("Range", param, &value);
}
return newUid;
}
void AccelerometerData::CancelRangeCallback(int32_t uid) {
m_rangeCallbacks = CancelCallback(m_rangeCallbacks, uid);
}
void AccelerometerData::InvokeRangeCallback(HAL_Value value) {
InvokeCallback(m_rangeCallbacks, "Range", &value);
}
HAL_AccelerometerRange AccelerometerData::GetRange() { return m_range; }
void AccelerometerData::SetRange(HAL_AccelerometerRange range) {
HAL_AccelerometerRange oldValue = m_range.exchange(range);
if (oldValue != range) {
InvokeRangeCallback(MakeEnum(range));
}
}
int32_t AccelerometerData::RegisterXCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_xCallbacks =
RegisterCallback(m_xCallbacks, "X", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetX());
callback("X", param, &value);
}
return newUid;
}
void AccelerometerData::CancelXCallback(int32_t uid) {
m_xCallbacks = CancelCallback(m_xCallbacks, uid);
}
void AccelerometerData::InvokeXCallback(HAL_Value value) {
InvokeCallback(m_xCallbacks, "X", &value);
}
double AccelerometerData::GetX() { return m_x; }
void AccelerometerData::SetX(double x) {
double oldValue = m_x.exchange(x);
if (oldValue != x) {
InvokeXCallback(MakeDouble(x));
}
}
int32_t AccelerometerData::RegisterYCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_yCallbacks =
RegisterCallback(m_yCallbacks, "Y", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetY());
callback("Y", param, &value);
}
return newUid;
}
void AccelerometerData::CancelYCallback(int32_t uid) {
m_yCallbacks = CancelCallback(m_yCallbacks, uid);
}
void AccelerometerData::InvokeYCallback(HAL_Value value) {
InvokeCallback(m_yCallbacks, "Y", &value);
}
double AccelerometerData::GetY() { return m_y; }
void AccelerometerData::SetY(double y) {
double oldValue = m_y.exchange(y);
if (oldValue != y) {
InvokeYCallback(MakeDouble(y));
}
}
int32_t AccelerometerData::RegisterZCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_zCallbacks =
RegisterCallback(m_zCallbacks, "Z", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetZ());
callback("Z", param, &value);
}
return newUid;
}
void AccelerometerData::CancelZCallback(int32_t uid) {
m_zCallbacks = CancelCallback(m_zCallbacks, uid);
}
void AccelerometerData::InvokeZCallback(HAL_Value value) {
InvokeCallback(m_zCallbacks, "Z", &value);
}
double AccelerometerData::GetZ() { return m_z; }
void AccelerometerData::SetZ(double z) {
double oldValue = m_z.exchange(z);
if (oldValue != z) {
InvokeZCallback(MakeDouble(z));
}
}
extern "C" {
void HALSIM_ResetAccelerometerData(int32_t index) {
SimAccelerometerData[index].ResetData();
}
int32_t HALSIM_RegisterAccelerometerActiveCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAccelerometerData[index].RegisterActiveCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAccelerometerActiveCallback(int32_t index, int32_t uid) {
SimAccelerometerData[index].CancelActiveCallback(uid);
}
HAL_Bool HALSIM_GetAccelerometerActive(int32_t index) {
return SimAccelerometerData[index].GetActive();
}
void HALSIM_SetAccelerometerActive(int32_t index, HAL_Bool active) {
SimAccelerometerData[index].SetActive(active);
}
int32_t HALSIM_RegisterAccelerometerRangeCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAccelerometerData[index].RegisterRangeCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAccelerometerRangeCallback(int32_t index, int32_t uid) {
SimAccelerometerData[index].CancelRangeCallback(uid);
}
HAL_AccelerometerRange HALSIM_GetAccelerometerRange(int32_t index) {
return SimAccelerometerData[index].GetRange();
}
void HALSIM_SetAccelerometerRange(int32_t index, HAL_AccelerometerRange range) {
SimAccelerometerData[index].SetRange(range);
}
int32_t HALSIM_RegisterAccelerometerXCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAccelerometerData[index].RegisterXCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAccelerometerXCallback(int32_t index, int32_t uid) {
SimAccelerometerData[index].CancelXCallback(uid);
}
double HALSIM_GetAccelerometerX(int32_t index) {
return SimAccelerometerData[index].GetX();
}
void HALSIM_SetAccelerometerX(int32_t index, double x) {
SimAccelerometerData[index].SetX(x);
}
int32_t HALSIM_RegisterAccelerometerYCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAccelerometerData[index].RegisterYCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAccelerometerYCallback(int32_t index, int32_t uid) {
SimAccelerometerData[index].CancelYCallback(uid);
}
double HALSIM_GetAccelerometerY(int32_t index) {
return SimAccelerometerData[index].GetY();
}
void HALSIM_SetAccelerometerY(int32_t index, double y) {
SimAccelerometerData[index].SetY(y);
}
int32_t HALSIM_RegisterAccelerometerZCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAccelerometerData[index].RegisterZCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAccelerometerZCallback(int32_t index, int32_t uid) {
SimAccelerometerData[index].CancelZCallback(uid);
}
double HALSIM_GetAccelerometerZ(int32_t index) {
return SimAccelerometerData[index].GetZ();
}
void HALSIM_SetAccelerometerZ(int32_t index, double z) {
SimAccelerometerData[index].SetZ(z);
}
}

View File

@@ -0,0 +1,71 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/AccelerometerData.h"
#include "MockData/NotifyListenerVector.h"
namespace hal {
class AccelerometerData {
public:
int32_t RegisterActiveCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelActiveCallback(int32_t uid);
void InvokeActiveCallback(HAL_Value value);
HAL_Bool GetActive();
void SetActive(HAL_Bool active);
int32_t RegisterRangeCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelRangeCallback(int32_t uid);
void InvokeRangeCallback(HAL_Value value);
HAL_AccelerometerRange GetRange();
void SetRange(HAL_AccelerometerRange range);
int32_t RegisterXCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelXCallback(int32_t uid);
void InvokeXCallback(HAL_Value value);
double GetX();
void SetX(double x);
int32_t RegisterYCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelYCallback(int32_t uid);
void InvokeYCallback(HAL_Value value);
double GetY();
void SetY(double y);
int32_t RegisterZCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelZCallback(int32_t uid);
void InvokeZCallback(HAL_Value value);
double GetZ();
void SetZ(double z);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_active{false};
std::shared_ptr<NotifyListenerVector> m_activeCallbacks = nullptr;
std::atomic<HAL_AccelerometerRange> m_range{
static_cast<HAL_AccelerometerRange>(0)};
std::shared_ptr<NotifyListenerVector> m_rangeCallbacks = nullptr;
std::atomic<double> m_x{0.0};
std::shared_ptr<NotifyListenerVector> m_xCallbacks = nullptr;
std::atomic<double> m_y{0.0};
std::shared_ptr<NotifyListenerVector> m_yCallbacks = nullptr;
std::atomic<double> m_z{0.0};
std::shared_ptr<NotifyListenerVector> m_zCallbacks = nullptr;
};
extern AccelerometerData SimAccelerometerData[];
} // namespace hal

View File

@@ -0,0 +1,195 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "AnalogGyroDataInternal.h"
#include "NotifyCallbackHelpers.h"
using namespace hal;
AnalogGyroData hal::SimAnalogGyroData[kNumAccumulators];
void AnalogGyroData::ResetData() {
m_angle = 0.0;
m_angleCallbacks = nullptr;
m_rate = 0.0;
m_rateCallbacks = nullptr;
m_initialized = false;
m_initializedCallbacks = nullptr;
}
int32_t AnalogGyroData::RegisterAngleCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_angleCallbacks =
RegisterCallback(m_angleCallbacks, "Angle", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetAngle());
callback("Angle", param, &value);
}
return newUid;
}
void AnalogGyroData::CancelAngleCallback(int32_t uid) {
m_angleCallbacks = CancelCallback(m_angleCallbacks, uid);
}
void AnalogGyroData::InvokeAngleCallback(HAL_Value value) {
InvokeCallback(m_angleCallbacks, "Angle", &value);
}
double AnalogGyroData::GetAngle() { return m_angle; }
void AnalogGyroData::SetAngle(double angle) {
double oldValue = m_angle.exchange(angle);
if (oldValue != angle) {
InvokeAngleCallback(MakeDouble(angle));
}
}
int32_t AnalogGyroData::RegisterRateCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_rateCallbacks =
RegisterCallback(m_rateCallbacks, "Rate", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetRate());
callback("Rate", param, &value);
}
return newUid;
}
void AnalogGyroData::CancelRateCallback(int32_t uid) {
m_rateCallbacks = CancelCallback(m_rateCallbacks, uid);
}
void AnalogGyroData::InvokeRateCallback(HAL_Value value) {
InvokeCallback(m_rateCallbacks, "Rate", &value);
}
double AnalogGyroData::GetRate() { return m_rate; }
void AnalogGyroData::SetRate(double rate) {
double oldValue = m_rate.exchange(rate);
if (oldValue != rate) {
InvokeRateCallback(MakeDouble(rate));
}
}
int32_t AnalogGyroData::RegisterInitializedCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_initializedCallbacks = RegisterCallback(
m_initializedCallbacks, "Initialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetInitialized());
callback("Initialized", param, &value);
}
return newUid;
}
void AnalogGyroData::CancelInitializedCallback(int32_t uid) {
m_initializedCallbacks = CancelCallback(m_initializedCallbacks, uid);
}
void AnalogGyroData::InvokeInitializedCallback(HAL_Value value) {
InvokeCallback(m_initializedCallbacks, "Initialized", &value);
}
HAL_Bool AnalogGyroData::GetInitialized() { return m_initialized; }
void AnalogGyroData::SetInitialized(HAL_Bool initialized) {
HAL_Bool oldValue = m_initialized.exchange(initialized);
if (oldValue != initialized) {
InvokeInitializedCallback(MakeBoolean(initialized));
}
}
extern "C" {
void HALSIM_ResetAnalogGyroData(int32_t index) {
SimAnalogGyroData[index].ResetData();
}
int32_t HALSIM_RegisterAnalogGyroAngleCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAnalogGyroData[index].RegisterAngleCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAnalogGyroAngleCallback(int32_t index, int32_t uid) {
SimAnalogGyroData[index].CancelAngleCallback(uid);
}
double HALSIM_GetAnalogGyroAngle(int32_t index) {
return SimAnalogGyroData[index].GetAngle();
}
void HALSIM_SetAnalogGyroAngle(int32_t index, double angle) {
SimAnalogGyroData[index].SetAngle(angle);
}
int32_t HALSIM_RegisterAnalogGyroRateCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAnalogGyroData[index].RegisterRateCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAnalogGyroRateCallback(int32_t index, int32_t uid) {
SimAnalogGyroData[index].CancelRateCallback(uid);
}
double HALSIM_GetAnalogGyroRate(int32_t index) {
return SimAnalogGyroData[index].GetRate();
}
void HALSIM_SetAnalogGyroRate(int32_t index, double rate) {
SimAnalogGyroData[index].SetRate(rate);
}
int32_t HALSIM_RegisterAnalogGyroInitializedCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimAnalogGyroData[index].RegisterInitializedCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAnalogGyroInitializedCallback(int32_t index, int32_t uid) {
SimAnalogGyroData[index].CancelInitializedCallback(uid);
}
HAL_Bool HALSIM_GetAnalogGyroInitialized(int32_t index) {
return SimAnalogGyroData[index].GetInitialized();
}
void HALSIM_SetAnalogGyroInitialized(int32_t index, HAL_Bool initialized) {
SimAnalogGyroData[index].SetInitialized(initialized);
}
}

View File

@@ -0,0 +1,52 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/AnalogGyroData.h"
#include "MockData/NotifyListenerVector.h"
namespace hal {
class AnalogGyroData {
public:
int32_t RegisterAngleCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelAngleCallback(int32_t uid);
void InvokeAngleCallback(HAL_Value value);
double GetAngle();
void SetAngle(double angle);
int32_t RegisterRateCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelRateCallback(int32_t uid);
void InvokeRateCallback(HAL_Value value);
double GetRate();
void SetRate(double rate);
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelInitializedCallback(int32_t uid);
void InvokeInitializedCallback(HAL_Value value);
HAL_Bool GetInitialized();
void SetInitialized(HAL_Bool initialized);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<double> m_angle{0.0};
std::shared_ptr<NotifyListenerVector> m_angleCallbacks = nullptr;
std::atomic<double> m_rate{0.0};
std::shared_ptr<NotifyListenerVector> m_rateCallbacks = nullptr;
std::atomic<HAL_Bool> m_initialized{false};
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
};
extern AnalogGyroData SimAnalogGyroData[];
} // namespace hal

View File

@@ -0,0 +1,553 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "AnalogInDataInternal.h"
#include "NotifyCallbackHelpers.h"
using namespace hal;
AnalogInData hal::SimAnalogInData[kNumAnalogInputs];
void AnalogInData::ResetData() {
m_initialized = false;
m_initializedCallbacks = nullptr;
m_averageBits = 7;
m_averageBitsCallbacks = nullptr;
m_oversampleBits = 0;
m_oversampleBitsCallbacks = nullptr;
m_voltage = 0.0;
m_voltageCallbacks = nullptr;
m_accumulatorInitialized = false;
m_accumulatorInitializedCallbacks = nullptr;
m_accumulatorValue = 0;
m_accumulatorValueCallbacks = nullptr;
m_accumulatorCount = 0;
m_accumulatorCountCallbacks = nullptr;
m_accumulatorCenter = 0;
m_accumulatorCenterCallbacks = nullptr;
m_accumulatorDeadband = 0;
m_accumulatorDeadbandCallbacks = nullptr;
}
int32_t AnalogInData::RegisterInitializedCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_initializedCallbacks = RegisterCallback(
m_initializedCallbacks, "Initialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetInitialized());
callback("Initialized", param, &value);
}
return newUid;
}
void AnalogInData::CancelInitializedCallback(int32_t uid) {
m_initializedCallbacks = CancelCallback(m_initializedCallbacks, uid);
}
void AnalogInData::InvokeInitializedCallback(HAL_Value value) {
InvokeCallback(m_initializedCallbacks, "Initialized", &value);
}
HAL_Bool AnalogInData::GetInitialized() { return m_initialized; }
void AnalogInData::SetInitialized(HAL_Bool initialized) {
HAL_Bool oldValue = m_initialized.exchange(initialized);
if (oldValue != initialized) {
InvokeInitializedCallback(MakeBoolean(initialized));
}
}
int32_t AnalogInData::RegisterAverageBitsCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_averageBitsCallbacks = RegisterCallback(
m_averageBitsCallbacks, "AverageBits", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetAverageBits());
callback("AverageBits", param, &value);
}
return newUid;
}
void AnalogInData::CancelAverageBitsCallback(int32_t uid) {
m_averageBitsCallbacks = CancelCallback(m_averageBitsCallbacks, uid);
}
void AnalogInData::InvokeAverageBitsCallback(HAL_Value value) {
InvokeCallback(m_averageBitsCallbacks, "AverageBits", &value);
}
int32_t AnalogInData::GetAverageBits() { return m_averageBits; }
void AnalogInData::SetAverageBits(int32_t averageBits) {
int32_t oldValue = m_averageBits.exchange(averageBits);
if (oldValue != averageBits) {
InvokeAverageBitsCallback(MakeInt(averageBits));
}
}
int32_t AnalogInData::RegisterOversampleBitsCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_oversampleBitsCallbacks = RegisterCallback(
m_oversampleBitsCallbacks, "OversampleBits", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetOversampleBits());
callback("OversampleBits", param, &value);
}
return newUid;
}
void AnalogInData::CancelOversampleBitsCallback(int32_t uid) {
m_oversampleBitsCallbacks = CancelCallback(m_oversampleBitsCallbacks, uid);
}
void AnalogInData::InvokeOversampleBitsCallback(HAL_Value value) {
InvokeCallback(m_oversampleBitsCallbacks, "OversampleBits", &value);
}
int32_t AnalogInData::GetOversampleBits() { return m_oversampleBits; }
void AnalogInData::SetOversampleBits(int32_t oversampleBits) {
int32_t oldValue = m_oversampleBits.exchange(oversampleBits);
if (oldValue != oversampleBits) {
InvokeOversampleBitsCallback(MakeInt(oversampleBits));
}
}
int32_t AnalogInData::RegisterVoltageCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_voltageCallbacks = RegisterCallback(m_voltageCallbacks, "Voltage",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetVoltage());
callback("Voltage", param, &value);
}
return newUid;
}
void AnalogInData::CancelVoltageCallback(int32_t uid) {
m_voltageCallbacks = CancelCallback(m_voltageCallbacks, uid);
}
void AnalogInData::InvokeVoltageCallback(HAL_Value value) {
InvokeCallback(m_voltageCallbacks, "Voltage", &value);
}
double AnalogInData::GetVoltage() { return m_voltage; }
void AnalogInData::SetVoltage(double voltage) {
double oldValue = m_voltage.exchange(voltage);
if (oldValue != voltage) {
InvokeVoltageCallback(MakeDouble(voltage));
}
}
int32_t AnalogInData::RegisterAccumulatorInitializedCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_accumulatorInitializedCallbacks =
RegisterCallback(m_accumulatorInitializedCallbacks,
"AccumulatorInitialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetAccumulatorInitialized());
callback("AccumulatorInitialized", param, &value);
}
return newUid;
}
void AnalogInData::CancelAccumulatorInitializedCallback(int32_t uid) {
m_accumulatorInitializedCallbacks =
CancelCallback(m_accumulatorInitializedCallbacks, uid);
}
void AnalogInData::InvokeAccumulatorInitializedCallback(HAL_Value value) {
InvokeCallback(m_accumulatorInitializedCallbacks, "AccumulatorInitialized",
&value);
}
HAL_Bool AnalogInData::GetAccumulatorInitialized() {
return m_accumulatorInitialized;
}
void AnalogInData::SetAccumulatorInitialized(HAL_Bool accumulatorInitialized) {
HAL_Bool oldValue = m_accumulatorInitialized.exchange(accumulatorInitialized);
if (oldValue != accumulatorInitialized) {
InvokeAccumulatorInitializedCallback(MakeBoolean(accumulatorInitialized));
}
}
int32_t AnalogInData::RegisterAccumulatorValueCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_accumulatorValueCallbacks =
RegisterCallback(m_accumulatorValueCallbacks, "AccumulatorValue",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeLong(GetAccumulatorValue());
callback("AccumulatorValue", param, &value);
}
return newUid;
}
void AnalogInData::CancelAccumulatorValueCallback(int32_t uid) {
m_accumulatorValueCallbacks =
CancelCallback(m_accumulatorValueCallbacks, uid);
}
void AnalogInData::InvokeAccumulatorValueCallback(HAL_Value value) {
InvokeCallback(m_accumulatorValueCallbacks, "AccumulatorValue", &value);
}
int64_t AnalogInData::GetAccumulatorValue() { return m_accumulatorValue; }
void AnalogInData::SetAccumulatorValue(int64_t accumulatorValue) {
int64_t oldValue = m_accumulatorValue.exchange(accumulatorValue);
if (oldValue != accumulatorValue) {
InvokeAccumulatorValueCallback(MakeLong(accumulatorValue));
}
}
int32_t AnalogInData::RegisterAccumulatorCountCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_accumulatorCountCallbacks =
RegisterCallback(m_accumulatorCountCallbacks, "AccumulatorCount",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeLong(GetAccumulatorCount());
callback("AccumulatorCount", param, &value);
}
return newUid;
}
void AnalogInData::CancelAccumulatorCountCallback(int32_t uid) {
m_accumulatorCountCallbacks =
CancelCallback(m_accumulatorCountCallbacks, uid);
}
void AnalogInData::InvokeAccumulatorCountCallback(HAL_Value value) {
InvokeCallback(m_accumulatorCountCallbacks, "AccumulatorCount", &value);
}
int64_t AnalogInData::GetAccumulatorCount() { return m_accumulatorCount; }
void AnalogInData::SetAccumulatorCount(int64_t accumulatorCount) {
int64_t oldValue = m_accumulatorCount.exchange(accumulatorCount);
if (oldValue != accumulatorCount) {
InvokeAccumulatorCountCallback(MakeLong(accumulatorCount));
}
}
int32_t AnalogInData::RegisterAccumulatorCenterCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_accumulatorCenterCallbacks =
RegisterCallback(m_accumulatorCenterCallbacks, "AccumulatorCenter",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetAccumulatorCenter());
callback("AccumulatorCenter", param, &value);
}
return newUid;
}
void AnalogInData::CancelAccumulatorCenterCallback(int32_t uid) {
m_accumulatorCenterCallbacks =
CancelCallback(m_accumulatorCenterCallbacks, uid);
}
void AnalogInData::InvokeAccumulatorCenterCallback(HAL_Value value) {
InvokeCallback(m_accumulatorCenterCallbacks, "AccumulatorCenter", &value);
}
int32_t AnalogInData::GetAccumulatorCenter() { return m_accumulatorCenter; }
void AnalogInData::SetAccumulatorCenter(int32_t accumulatorCenter) {
int32_t oldValue = m_accumulatorCenter.exchange(accumulatorCenter);
if (oldValue != accumulatorCenter) {
InvokeAccumulatorCenterCallback(MakeInt(accumulatorCenter));
}
}
int32_t AnalogInData::RegisterAccumulatorDeadbandCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_accumulatorDeadbandCallbacks =
RegisterCallback(m_accumulatorDeadbandCallbacks, "AccumulatorDeadband",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetAccumulatorDeadband());
callback("AccumulatorDeadband", param, &value);
}
return newUid;
}
void AnalogInData::CancelAccumulatorDeadbandCallback(int32_t uid) {
m_accumulatorDeadbandCallbacks =
CancelCallback(m_accumulatorDeadbandCallbacks, uid);
}
void AnalogInData::InvokeAccumulatorDeadbandCallback(HAL_Value value) {
InvokeCallback(m_accumulatorDeadbandCallbacks, "AccumulatorDeadband", &value);
}
int32_t AnalogInData::GetAccumulatorDeadband() { return m_accumulatorDeadband; }
void AnalogInData::SetAccumulatorDeadband(int32_t accumulatorDeadband) {
int32_t oldValue = m_accumulatorDeadband.exchange(accumulatorDeadband);
if (oldValue != accumulatorDeadband) {
InvokeAccumulatorDeadbandCallback(MakeInt(accumulatorDeadband));
}
}
extern "C" {
void HALSIM_ResetAnalogInData(int32_t index) {
SimAnalogInData[index].ResetData();
}
int32_t HALSIM_RegisterAnalogInInitializedCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAnalogInData[index].RegisterInitializedCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAnalogInInitializedCallback(int32_t index, int32_t uid) {
SimAnalogInData[index].CancelInitializedCallback(uid);
}
HAL_Bool HALSIM_GetAnalogInInitialized(int32_t index) {
return SimAnalogInData[index].GetInitialized();
}
void HALSIM_SetAnalogInInitialized(int32_t index, HAL_Bool initialized) {
SimAnalogInData[index].SetInitialized(initialized);
}
int32_t HALSIM_RegisterAnalogInAverageBitsCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAnalogInData[index].RegisterAverageBitsCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAnalogInAverageBitsCallback(int32_t index, int32_t uid) {
SimAnalogInData[index].CancelAverageBitsCallback(uid);
}
int32_t HALSIM_GetAnalogInAverageBits(int32_t index) {
return SimAnalogInData[index].GetAverageBits();
}
void HALSIM_SetAnalogInAverageBits(int32_t index, int32_t averageBits) {
SimAnalogInData[index].SetAverageBits(averageBits);
}
int32_t HALSIM_RegisterAnalogInOversampleBitsCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimAnalogInData[index].RegisterOversampleBitsCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAnalogInOversampleBitsCallback(int32_t index, int32_t uid) {
SimAnalogInData[index].CancelOversampleBitsCallback(uid);
}
int32_t HALSIM_GetAnalogInOversampleBits(int32_t index) {
return SimAnalogInData[index].GetOversampleBits();
}
void HALSIM_SetAnalogInOversampleBits(int32_t index, int32_t oversampleBits) {
SimAnalogInData[index].SetOversampleBits(oversampleBits);
}
int32_t HALSIM_RegisterAnalogInVoltageCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAnalogInData[index].RegisterVoltageCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAnalogInVoltageCallback(int32_t index, int32_t uid) {
SimAnalogInData[index].CancelVoltageCallback(uid);
}
double HALSIM_GetAnalogInVoltage(int32_t index) {
return SimAnalogInData[index].GetVoltage();
}
void HALSIM_SetAnalogInVoltage(int32_t index, double voltage) {
SimAnalogInData[index].SetVoltage(voltage);
}
int32_t HALSIM_RegisterAnalogInAccumulatorInitializedCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimAnalogInData[index].RegisterAccumulatorInitializedCallback(
callback, param, initialNotify);
}
void HALSIM_CancelAnalogInAccumulatorInitializedCallback(int32_t index,
int32_t uid) {
SimAnalogInData[index].CancelAccumulatorInitializedCallback(uid);
}
HAL_Bool HALSIM_GetAnalogInAccumulatorInitialized(int32_t index) {
return SimAnalogInData[index].GetAccumulatorInitialized();
}
void HALSIM_SetAnalogInAccumulatorInitialized(int32_t index,
HAL_Bool accumulatorInitialized) {
SimAnalogInData[index].SetAccumulatorInitialized(accumulatorInitialized);
}
int32_t HALSIM_RegisterAnalogInAccumulatorValueCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimAnalogInData[index].RegisterAccumulatorValueCallback(
callback, param, initialNotify);
}
void HALSIM_CancelAnalogInAccumulatorValueCallback(int32_t index, int32_t uid) {
SimAnalogInData[index].CancelAccumulatorValueCallback(uid);
}
int64_t HALSIM_GetAnalogInAccumulatorValue(int32_t index) {
return SimAnalogInData[index].GetAccumulatorValue();
}
void HALSIM_SetAnalogInAccumulatorValue(int32_t index,
int64_t accumulatorValue) {
SimAnalogInData[index].SetAccumulatorValue(accumulatorValue);
}
int32_t HALSIM_RegisterAnalogInAccumulatorCountCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimAnalogInData[index].RegisterAccumulatorCountCallback(
callback, param, initialNotify);
}
void HALSIM_CancelAnalogInAccumulatorCountCallback(int32_t index, int32_t uid) {
SimAnalogInData[index].CancelAccumulatorCountCallback(uid);
}
int64_t HALSIM_GetAnalogInAccumulatorCount(int32_t index) {
return SimAnalogInData[index].GetAccumulatorCount();
}
void HALSIM_SetAnalogInAccumulatorCount(int32_t index,
int64_t accumulatorCount) {
SimAnalogInData[index].SetAccumulatorCount(accumulatorCount);
}
int32_t HALSIM_RegisterAnalogInAccumulatorCenterCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimAnalogInData[index].RegisterAccumulatorCenterCallback(
callback, param, initialNotify);
}
void HALSIM_CancelAnalogInAccumulatorCenterCallback(int32_t index,
int32_t uid) {
SimAnalogInData[index].CancelAccumulatorCenterCallback(uid);
}
int32_t HALSIM_GetAnalogInAccumulatorCenter(int32_t index) {
return SimAnalogInData[index].GetAccumulatorCenter();
}
void HALSIM_SetAnalogInAccumulatorCenter(int32_t index,
int32_t accumulatorCenter) {
SimAnalogInData[index].SetAccumulatorCenter(accumulatorCenter);
}
int32_t HALSIM_RegisterAnalogInAccumulatorDeadbandCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimAnalogInData[index].RegisterAccumulatorDeadbandCallback(
callback, param, initialNotify);
}
void HALSIM_CancelAnalogInAccumulatorDeadbandCallback(int32_t index,
int32_t uid) {
SimAnalogInData[index].CancelAccumulatorDeadbandCallback(uid);
}
int32_t HALSIM_GetAnalogInAccumulatorDeadband(int32_t index) {
return SimAnalogInData[index].GetAccumulatorDeadband();
}
void HALSIM_SetAnalogInAccumulatorDeadband(int32_t index,
int32_t accumulatorDeadband) {
SimAnalogInData[index].SetAccumulatorDeadband(accumulatorDeadband);
}
}

View File

@@ -0,0 +1,111 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/AnalogInData.h"
#include "MockData/NotifyListenerVector.h"
namespace hal {
class AnalogInData {
public:
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelInitializedCallback(int32_t uid);
void InvokeInitializedCallback(HAL_Value value);
HAL_Bool GetInitialized();
void SetInitialized(HAL_Bool initialized);
int32_t RegisterAverageBitsCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelAverageBitsCallback(int32_t uid);
void InvokeAverageBitsCallback(HAL_Value value);
int32_t GetAverageBits();
void SetAverageBits(int32_t averageBits);
int32_t RegisterOversampleBitsCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelOversampleBitsCallback(int32_t uid);
void InvokeOversampleBitsCallback(HAL_Value value);
int32_t GetOversampleBits();
void SetOversampleBits(int32_t oversampleBits);
int32_t RegisterVoltageCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelVoltageCallback(int32_t uid);
void InvokeVoltageCallback(HAL_Value value);
double GetVoltage();
void SetVoltage(double voltage);
int32_t RegisterAccumulatorInitializedCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelAccumulatorInitializedCallback(int32_t uid);
void InvokeAccumulatorInitializedCallback(HAL_Value value);
HAL_Bool GetAccumulatorInitialized();
void SetAccumulatorInitialized(HAL_Bool accumulatorInitialized);
int32_t RegisterAccumulatorValueCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelAccumulatorValueCallback(int32_t uid);
void InvokeAccumulatorValueCallback(HAL_Value value);
int64_t GetAccumulatorValue();
void SetAccumulatorValue(int64_t accumulatorValue);
int32_t RegisterAccumulatorCountCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelAccumulatorCountCallback(int32_t uid);
void InvokeAccumulatorCountCallback(HAL_Value value);
int64_t GetAccumulatorCount();
void SetAccumulatorCount(int64_t accumulatorCount);
int32_t RegisterAccumulatorCenterCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelAccumulatorCenterCallback(int32_t uid);
void InvokeAccumulatorCenterCallback(HAL_Value value);
int32_t GetAccumulatorCenter();
void SetAccumulatorCenter(int32_t accumulatorCenter);
int32_t RegisterAccumulatorDeadbandCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelAccumulatorDeadbandCallback(int32_t uid);
void InvokeAccumulatorDeadbandCallback(HAL_Value value);
int32_t GetAccumulatorDeadband();
void SetAccumulatorDeadband(int32_t accumulatorDeadband);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_initialized{false};
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
std::atomic<int32_t> m_averageBits{7};
std::shared_ptr<NotifyListenerVector> m_averageBitsCallbacks = nullptr;
std::atomic<int32_t> m_oversampleBits{0};
std::shared_ptr<NotifyListenerVector> m_oversampleBitsCallbacks = nullptr;
std::atomic<double> m_voltage{0.0};
std::shared_ptr<NotifyListenerVector> m_voltageCallbacks = nullptr;
std::atomic<HAL_Bool> m_accumulatorInitialized{false};
std::shared_ptr<NotifyListenerVector> m_accumulatorInitializedCallbacks =
nullptr;
std::atomic<int64_t> m_accumulatorValue{0};
std::shared_ptr<NotifyListenerVector> m_accumulatorValueCallbacks = nullptr;
std::atomic<int64_t> m_accumulatorCount{0};
std::shared_ptr<NotifyListenerVector> m_accumulatorCountCallbacks = nullptr;
std::atomic<int32_t> m_accumulatorCenter{0};
std::shared_ptr<NotifyListenerVector> m_accumulatorCenterCallbacks = nullptr;
std::atomic<int32_t> m_accumulatorDeadband{0};
std::shared_ptr<NotifyListenerVector> m_accumulatorDeadbandCallbacks =
nullptr;
};
extern AnalogInData SimAnalogInData[];
} // namespace hal

View File

@@ -0,0 +1,138 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "AnalogOutDataInternal.h"
#include "NotifyCallbackHelpers.h"
using namespace hal;
AnalogOutData hal::SimAnalogOutData[kNumAnalogOutputs];
void AnalogOutData::ResetData() {
m_voltage = 0.0;
m_voltageCallbacks = nullptr;
m_initialized = 0;
m_initializedCallbacks = nullptr;
}
int32_t AnalogOutData::RegisterVoltageCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_voltageCallbacks = RegisterCallback(m_voltageCallbacks, "Voltage",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetVoltage());
callback("Voltage", param, &value);
}
return newUid;
}
void AnalogOutData::CancelVoltageCallback(int32_t uid) {
m_voltageCallbacks = CancelCallback(m_voltageCallbacks, uid);
}
void AnalogOutData::InvokeVoltageCallback(HAL_Value value) {
InvokeCallback(m_voltageCallbacks, "Voltage", &value);
}
double AnalogOutData::GetVoltage() { return m_voltage; }
void AnalogOutData::SetVoltage(double voltage) {
double oldValue = m_voltage.exchange(voltage);
if (oldValue != voltage) {
InvokeVoltageCallback(MakeDouble(voltage));
}
}
int32_t AnalogOutData::RegisterInitializedCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_initializedCallbacks = RegisterCallback(
m_initializedCallbacks, "Initialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetInitialized());
callback("Initialized", param, &value);
}
return newUid;
}
void AnalogOutData::CancelInitializedCallback(int32_t uid) {
m_initializedCallbacks = CancelCallback(m_initializedCallbacks, uid);
}
void AnalogOutData::InvokeInitializedCallback(HAL_Value value) {
InvokeCallback(m_initializedCallbacks, "Initialized", &value);
}
HAL_Bool AnalogOutData::GetInitialized() { return m_initialized; }
void AnalogOutData::SetInitialized(HAL_Bool initialized) {
HAL_Bool oldValue = m_initialized.exchange(initialized);
if (oldValue != initialized) {
InvokeInitializedCallback(MakeBoolean(initialized));
}
}
extern "C" {
void HALSIM_ResetAnalogOutData(int32_t index) {
SimAnalogOutData[index].ResetData();
}
int32_t HALSIM_RegisterAnalogOutVoltageCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAnalogOutData[index].RegisterVoltageCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAnalogOutVoltageCallback(int32_t index, int32_t uid) {
SimAnalogOutData[index].CancelVoltageCallback(uid);
}
double HALSIM_GetAnalogOutVoltage(int32_t index) {
return SimAnalogOutData[index].GetVoltage();
}
void HALSIM_SetAnalogOutVoltage(int32_t index, double voltage) {
SimAnalogOutData[index].SetVoltage(voltage);
}
int32_t HALSIM_RegisterAnalogOutInitializedCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimAnalogOutData[index].RegisterInitializedCallback(callback, param,
initialNotify);
}
void HALSIM_CancelAnalogOutInitializedCallback(int32_t index, int32_t uid) {
SimAnalogOutData[index].CancelInitializedCallback(uid);
}
HAL_Bool HALSIM_GetAnalogOutInitialized(int32_t index) {
return SimAnalogOutData[index].GetInitialized();
}
void HALSIM_SetAnalogOutInitialized(int32_t index, HAL_Bool initialized) {
SimAnalogOutData[index].SetInitialized(initialized);
}
}

View File

@@ -0,0 +1,43 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/AnalogOutData.h"
#include "MockData/NotifyListenerVector.h"
namespace hal {
class AnalogOutData {
public:
int32_t RegisterVoltageCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelVoltageCallback(int32_t uid);
void InvokeVoltageCallback(HAL_Value value);
double GetVoltage();
void SetVoltage(double voltage);
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelInitializedCallback(int32_t uid);
void InvokeInitializedCallback(HAL_Value value);
HAL_Bool GetInitialized();
void SetInitialized(HAL_Bool initialized);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<double> m_voltage{0.0};
std::shared_ptr<NotifyListenerVector> m_voltageCallbacks = nullptr;
std::atomic<HAL_Bool> m_initialized{0};
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
};
extern AnalogOutData SimAnalogOutData[];
} // namespace hal

View File

@@ -0,0 +1,257 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "AnalogTriggerDataInternal.h"
#include "NotifyCallbackHelpers.h"
using namespace hal;
AnalogTriggerData hal::SimAnalogTriggerData[kNumAnalogTriggers];
void AnalogTriggerData::ResetData() {
m_initialized = 0;
m_initializedCallbacks = nullptr;
m_triggerLowerBound = 0;
m_triggerLowerBoundCallbacks = nullptr;
m_triggerUpperBound = 0;
m_triggerUpperBoundCallbacks = nullptr;
m_triggerMode = static_cast<HALSIM_AnalogTriggerMode>(0);
m_triggerModeCallbacks = nullptr;
}
int32_t AnalogTriggerData::RegisterInitializedCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_initializedCallbacks = RegisterCallback(
m_initializedCallbacks, "Initialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetInitialized());
callback("Initialized", param, &value);
}
return newUid;
}
void AnalogTriggerData::CancelInitializedCallback(int32_t uid) {
m_initializedCallbacks = CancelCallback(m_initializedCallbacks, uid);
}
void AnalogTriggerData::InvokeInitializedCallback(HAL_Value value) {
InvokeCallback(m_initializedCallbacks, "Initialized", &value);
}
HAL_Bool AnalogTriggerData::GetInitialized() { return m_initialized; }
void AnalogTriggerData::SetInitialized(HAL_Bool initialized) {
HAL_Bool oldValue = m_initialized.exchange(initialized);
if (oldValue != initialized) {
InvokeInitializedCallback(MakeBoolean(initialized));
}
}
int32_t AnalogTriggerData::RegisterTriggerLowerBoundCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_triggerLowerBoundCallbacks =
RegisterCallback(m_triggerLowerBoundCallbacks, "TriggerLowerBound",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetTriggerLowerBound());
callback("TriggerLowerBound", param, &value);
}
return newUid;
}
void AnalogTriggerData::CancelTriggerLowerBoundCallback(int32_t uid) {
m_triggerLowerBoundCallbacks =
CancelCallback(m_triggerLowerBoundCallbacks, uid);
}
void AnalogTriggerData::InvokeTriggerLowerBoundCallback(HAL_Value value) {
InvokeCallback(m_triggerLowerBoundCallbacks, "TriggerLowerBound", &value);
}
double AnalogTriggerData::GetTriggerLowerBound() { return m_triggerLowerBound; }
void AnalogTriggerData::SetTriggerLowerBound(double triggerLowerBound) {
double oldValue = m_triggerLowerBound.exchange(triggerLowerBound);
if (oldValue != triggerLowerBound) {
InvokeTriggerLowerBoundCallback(MakeDouble(triggerLowerBound));
}
}
int32_t AnalogTriggerData::RegisterTriggerUpperBoundCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_triggerUpperBoundCallbacks =
RegisterCallback(m_triggerUpperBoundCallbacks, "TriggerUpperBound",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetTriggerUpperBound());
callback("TriggerUpperBound", param, &value);
}
return newUid;
}
void AnalogTriggerData::CancelTriggerUpperBoundCallback(int32_t uid) {
m_triggerUpperBoundCallbacks =
CancelCallback(m_triggerUpperBoundCallbacks, uid);
}
void AnalogTriggerData::InvokeTriggerUpperBoundCallback(HAL_Value value) {
InvokeCallback(m_triggerUpperBoundCallbacks, "TriggerUpperBound", &value);
}
double AnalogTriggerData::GetTriggerUpperBound() { return m_triggerUpperBound; }
void AnalogTriggerData::SetTriggerUpperBound(double triggerUpperBound) {
double oldValue = m_triggerUpperBound.exchange(triggerUpperBound);
if (oldValue != triggerUpperBound) {
InvokeTriggerUpperBoundCallback(MakeDouble(triggerUpperBound));
}
}
int32_t AnalogTriggerData::RegisterTriggerModeCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_triggerModeCallbacks = RegisterCallback(
m_triggerModeCallbacks, "TriggerMode", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeEnum(GetTriggerMode());
callback("TriggerMode", param, &value);
}
return newUid;
}
void AnalogTriggerData::CancelTriggerModeCallback(int32_t uid) {
m_triggerModeCallbacks = CancelCallback(m_triggerModeCallbacks, uid);
}
void AnalogTriggerData::InvokeTriggerModeCallback(HAL_Value value) {
InvokeCallback(m_triggerModeCallbacks, "TriggerMode", &value);
}
HALSIM_AnalogTriggerMode AnalogTriggerData::GetTriggerMode() {
return m_triggerMode;
}
void AnalogTriggerData::SetTriggerMode(HALSIM_AnalogTriggerMode triggerMode) {
HALSIM_AnalogTriggerMode oldValue = m_triggerMode.exchange(triggerMode);
if (oldValue != triggerMode) {
InvokeTriggerModeCallback(MakeEnum(triggerMode));
}
}
extern "C" {
void HALSIM_ResetAnalogTriggerData(int32_t index) {
SimAnalogTriggerData[index].ResetData();
}
int32_t HALSIM_RegisterAnalogTriggerInitializedCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimAnalogTriggerData[index].RegisterInitializedCallback(
callback, param, initialNotify);
}
void HALSIM_CancelAnalogTriggerInitializedCallback(int32_t index, int32_t uid) {
SimAnalogTriggerData[index].CancelInitializedCallback(uid);
}
HAL_Bool HALSIM_GetAnalogTriggerInitialized(int32_t index) {
return SimAnalogTriggerData[index].GetInitialized();
}
void HALSIM_SetAnalogTriggerInitialized(int32_t index, HAL_Bool initialized) {
SimAnalogTriggerData[index].SetInitialized(initialized);
}
int32_t HALSIM_RegisterAnalogTriggerTriggerLowerBoundCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimAnalogTriggerData[index].RegisterTriggerLowerBoundCallback(
callback, param, initialNotify);
}
void HALSIM_CancelAnalogTriggerTriggerLowerBoundCallback(int32_t index,
int32_t uid) {
SimAnalogTriggerData[index].CancelTriggerLowerBoundCallback(uid);
}
double HALSIM_GetAnalogTriggerTriggerLowerBound(int32_t index) {
return SimAnalogTriggerData[index].GetTriggerLowerBound();
}
void HALSIM_SetAnalogTriggerTriggerLowerBound(int32_t index,
double triggerLowerBound) {
SimAnalogTriggerData[index].SetTriggerLowerBound(triggerLowerBound);
}
int32_t HALSIM_RegisterAnalogTriggerTriggerUpperBoundCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimAnalogTriggerData[index].RegisterTriggerUpperBoundCallback(
callback, param, initialNotify);
}
void HALSIM_CancelAnalogTriggerTriggerUpperBoundCallback(int32_t index,
int32_t uid) {
SimAnalogTriggerData[index].CancelTriggerUpperBoundCallback(uid);
}
double HALSIM_GetAnalogTriggerTriggerUpperBound(int32_t index) {
return SimAnalogTriggerData[index].GetTriggerUpperBound();
}
void HALSIM_SetAnalogTriggerTriggerUpperBound(int32_t index,
double triggerUpperBound) {
SimAnalogTriggerData[index].SetTriggerUpperBound(triggerUpperBound);
}
int32_t HALSIM_RegisterAnalogTriggerTriggerModeCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimAnalogTriggerData[index].RegisterTriggerModeCallback(
callback, param, initialNotify);
}
void HALSIM_CancelAnalogTriggerTriggerModeCallback(int32_t index, int32_t uid) {
SimAnalogTriggerData[index].CancelTriggerModeCallback(uid);
}
HALSIM_AnalogTriggerMode HALSIM_GetAnalogTriggerTriggerMode(int32_t index) {
return SimAnalogTriggerData[index].GetTriggerMode();
}
void HALSIM_SetAnalogTriggerTriggerMode(int32_t index,
HALSIM_AnalogTriggerMode triggerMode) {
SimAnalogTriggerData[index].SetTriggerMode(triggerMode);
}
}

View File

@@ -0,0 +1,64 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/AnalogTriggerData.h"
#include "MockData/NotifyListenerVector.h"
namespace hal {
class AnalogTriggerData {
public:
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelInitializedCallback(int32_t uid);
void InvokeInitializedCallback(HAL_Value value);
HAL_Bool GetInitialized();
void SetInitialized(HAL_Bool initialized);
int32_t RegisterTriggerLowerBoundCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelTriggerLowerBoundCallback(int32_t uid);
void InvokeTriggerLowerBoundCallback(HAL_Value value);
double GetTriggerLowerBound();
void SetTriggerLowerBound(double triggerLowerBound);
int32_t RegisterTriggerUpperBoundCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelTriggerUpperBoundCallback(int32_t uid);
void InvokeTriggerUpperBoundCallback(HAL_Value value);
double GetTriggerUpperBound();
void SetTriggerUpperBound(double triggerUpperBound);
int32_t RegisterTriggerModeCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelTriggerModeCallback(int32_t uid);
void InvokeTriggerModeCallback(HAL_Value value);
HALSIM_AnalogTriggerMode GetTriggerMode();
void SetTriggerMode(HALSIM_AnalogTriggerMode triggerMode);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_initialized{0};
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
std::atomic<double> m_triggerLowerBound{0};
std::shared_ptr<NotifyListenerVector> m_triggerLowerBoundCallbacks = nullptr;
std::atomic<double> m_triggerUpperBound{0};
std::shared_ptr<NotifyListenerVector> m_triggerUpperBoundCallbacks = nullptr;
std::atomic<HALSIM_AnalogTriggerMode> m_triggerMode{
static_cast<HALSIM_AnalogTriggerMode>(0)};
std::shared_ptr<NotifyListenerVector> m_triggerModeCallbacks = nullptr;
};
extern AnalogTriggerData SimAnalogTriggerData[];
} // namespace hal

View File

@@ -0,0 +1,306 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "DIODataInternal.h"
#include "NotifyCallbackHelpers.h"
using namespace hal;
DIOData hal::SimDIOData[kNumDigitalChannels];
void DIOData::ResetData() {
m_initialized = false;
m_initializedCallbacks = nullptr;
m_value = true;
m_valueCallbacks = nullptr;
m_pulseLength = 0.0;
m_pulseLengthCallbacks = nullptr;
m_isInput = true;
m_isInputCallbacks = nullptr;
m_filterIndex = -1;
m_filterIndexCallbacks = nullptr;
}
int32_t DIOData::RegisterInitializedCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_initializedCallbacks = RegisterCallback(
m_initializedCallbacks, "Initialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetInitialized());
callback("Initialized", param, &value);
}
return newUid;
}
void DIOData::CancelInitializedCallback(int32_t uid) {
m_initializedCallbacks = CancelCallback(m_initializedCallbacks, uid);
}
void DIOData::InvokeInitializedCallback(HAL_Value value) {
InvokeCallback(m_initializedCallbacks, "Initialized", &value);
}
HAL_Bool DIOData::GetInitialized() { return m_initialized; }
void DIOData::SetInitialized(HAL_Bool initialized) {
HAL_Bool oldValue = m_initialized.exchange(initialized);
if (oldValue != initialized) {
InvokeInitializedCallback(MakeBoolean(initialized));
}
}
int32_t DIOData::RegisterValueCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_valueCallbacks =
RegisterCallback(m_valueCallbacks, "Value", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetValue());
callback("Value", param, &value);
}
return newUid;
}
void DIOData::CancelValueCallback(int32_t uid) {
m_valueCallbacks = CancelCallback(m_valueCallbacks, uid);
}
void DIOData::InvokeValueCallback(HAL_Value value) {
InvokeCallback(m_valueCallbacks, "Value", &value);
}
HAL_Bool DIOData::GetValue() { return m_value; }
void DIOData::SetValue(HAL_Bool value) {
HAL_Bool oldValue = m_value.exchange(value);
if (oldValue != value) {
InvokeValueCallback(MakeBoolean(value));
}
}
int32_t DIOData::RegisterPulseLengthCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_pulseLengthCallbacks = RegisterCallback(
m_pulseLengthCallbacks, "PulseLength", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetPulseLength());
callback("PulseLength", param, &value);
}
return newUid;
}
void DIOData::CancelPulseLengthCallback(int32_t uid) {
m_pulseLengthCallbacks = CancelCallback(m_pulseLengthCallbacks, uid);
}
void DIOData::InvokePulseLengthCallback(HAL_Value value) {
InvokeCallback(m_pulseLengthCallbacks, "PulseLength", &value);
}
double DIOData::GetPulseLength() { return m_pulseLength; }
void DIOData::SetPulseLength(double pulseLength) {
double oldValue = m_pulseLength.exchange(pulseLength);
if (oldValue != pulseLength) {
InvokePulseLengthCallback(MakeDouble(pulseLength));
}
}
int32_t DIOData::RegisterIsInputCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_isInputCallbacks = RegisterCallback(m_isInputCallbacks, "IsInput",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetIsInput());
callback("IsInput", param, &value);
}
return newUid;
}
void DIOData::CancelIsInputCallback(int32_t uid) {
m_isInputCallbacks = CancelCallback(m_isInputCallbacks, uid);
}
void DIOData::InvokeIsInputCallback(HAL_Value value) {
InvokeCallback(m_isInputCallbacks, "IsInput", &value);
}
HAL_Bool DIOData::GetIsInput() { return m_isInput; }
void DIOData::SetIsInput(HAL_Bool isInput) {
HAL_Bool oldValue = m_isInput.exchange(isInput);
if (oldValue != isInput) {
InvokeIsInputCallback(MakeBoolean(isInput));
}
}
int32_t DIOData::RegisterFilterIndexCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_filterIndexCallbacks = RegisterCallback(
m_filterIndexCallbacks, "FilterIndex", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetFilterIndex());
callback("FilterIndex", param, &value);
}
return newUid;
}
void DIOData::CancelFilterIndexCallback(int32_t uid) {
m_filterIndexCallbacks = CancelCallback(m_filterIndexCallbacks, uid);
}
void DIOData::InvokeFilterIndexCallback(HAL_Value value) {
InvokeCallback(m_filterIndexCallbacks, "FilterIndex", &value);
}
int32_t DIOData::GetFilterIndex() { return m_filterIndex; }
void DIOData::SetFilterIndex(int32_t filterIndex) {
int32_t oldValue = m_filterIndex.exchange(filterIndex);
if (oldValue != filterIndex) {
InvokeFilterIndexCallback(MakeInt(filterIndex));
}
}
extern "C" {
void HALSIM_ResetDIOData(int32_t index) { SimDIOData[index].ResetData(); }
int32_t HALSIM_RegisterDIOInitializedCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimDIOData[index].RegisterInitializedCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDIOInitializedCallback(int32_t index, int32_t uid) {
SimDIOData[index].CancelInitializedCallback(uid);
}
HAL_Bool HALSIM_GetDIOInitialized(int32_t index) {
return SimDIOData[index].GetInitialized();
}
void HALSIM_SetDIOInitialized(int32_t index, HAL_Bool initialized) {
SimDIOData[index].SetInitialized(initialized);
}
int32_t HALSIM_RegisterDIOValueCallback(int32_t index,
HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {
return SimDIOData[index].RegisterValueCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDIOValueCallback(int32_t index, int32_t uid) {
SimDIOData[index].CancelValueCallback(uid);
}
HAL_Bool HALSIM_GetDIOValue(int32_t index) {
return SimDIOData[index].GetValue();
}
void HALSIM_SetDIOValue(int32_t index, HAL_Bool value) {
SimDIOData[index].SetValue(value);
}
int32_t HALSIM_RegisterDIOPulseLengthCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimDIOData[index].RegisterPulseLengthCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDIOPulseLengthCallback(int32_t index, int32_t uid) {
SimDIOData[index].CancelPulseLengthCallback(uid);
}
double HALSIM_GetDIOPulseLength(int32_t index) {
return SimDIOData[index].GetPulseLength();
}
void HALSIM_SetDIOPulseLength(int32_t index, double pulseLength) {
SimDIOData[index].SetPulseLength(pulseLength);
}
int32_t HALSIM_RegisterDIOIsInputCallback(int32_t index,
HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {
return SimDIOData[index].RegisterIsInputCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDIOIsInputCallback(int32_t index, int32_t uid) {
SimDIOData[index].CancelIsInputCallback(uid);
}
HAL_Bool HALSIM_GetDIOIsInput(int32_t index) {
return SimDIOData[index].GetIsInput();
}
void HALSIM_SetDIOIsInput(int32_t index, HAL_Bool isInput) {
SimDIOData[index].SetIsInput(isInput);
}
int32_t HALSIM_RegisterDIOFilterIndexCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimDIOData[index].RegisterFilterIndexCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDIOFilterIndexCallback(int32_t index, int32_t uid) {
SimDIOData[index].CancelFilterIndexCallback(uid);
}
int32_t HALSIM_GetDIOFilterIndex(int32_t index) {
return SimDIOData[index].GetFilterIndex();
}
void HALSIM_SetDIOFilterIndex(int32_t index, int32_t filterIndex) {
SimDIOData[index].SetFilterIndex(filterIndex);
}
}

View File

@@ -0,0 +1,70 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/DIOData.h"
#include "MockData/NotifyListenerVector.h"
namespace hal {
class DIOData {
public:
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelInitializedCallback(int32_t uid);
void InvokeInitializedCallback(HAL_Value value);
HAL_Bool GetInitialized();
void SetInitialized(HAL_Bool initialized);
int32_t RegisterValueCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelValueCallback(int32_t uid);
void InvokeValueCallback(HAL_Value value);
HAL_Bool GetValue();
void SetValue(HAL_Bool value);
int32_t RegisterPulseLengthCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelPulseLengthCallback(int32_t uid);
void InvokePulseLengthCallback(HAL_Value value);
double GetPulseLength();
void SetPulseLength(double pulseLength);
int32_t RegisterIsInputCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelIsInputCallback(int32_t uid);
void InvokeIsInputCallback(HAL_Value value);
HAL_Bool GetIsInput();
void SetIsInput(HAL_Bool isInput);
int32_t RegisterFilterIndexCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelFilterIndexCallback(int32_t uid);
void InvokeFilterIndexCallback(HAL_Value value);
int32_t GetFilterIndex();
void SetFilterIndex(int32_t filterIndex);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_initialized{false};
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
std::atomic<HAL_Bool> m_value{true};
std::shared_ptr<NotifyListenerVector> m_valueCallbacks = nullptr;
std::atomic<double> m_pulseLength{0.0};
std::shared_ptr<NotifyListenerVector> m_pulseLengthCallbacks = nullptr;
std::atomic<HAL_Bool> m_isInput{true};
std::shared_ptr<NotifyListenerVector> m_isInputCallbacks = nullptr;
std::atomic<int32_t> m_filterIndex{-1};
std::shared_ptr<NotifyListenerVector> m_filterIndexCallbacks = nullptr;
};
extern DIOData SimDIOData[];
} // namespace hal

View File

@@ -0,0 +1,195 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "DigitalPWMDataInternal.h"
#include "NotifyCallbackHelpers.h"
using namespace hal;
DigitalPWMData hal::SimDigitalPWMData[kNumDigitalPWMOutputs];
void DigitalPWMData::ResetData() {
m_initialized = false;
m_initializedCallbacks = nullptr;
m_dutyCycle = false;
m_dutyCycleCallbacks = nullptr;
m_pin = 0;
m_pinCallbacks = nullptr;
}
int32_t DigitalPWMData::RegisterInitializedCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_initializedCallbacks = RegisterCallback(
m_initializedCallbacks, "Initialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetInitialized());
callback("Initialized", param, &value);
}
return newUid;
}
void DigitalPWMData::CancelInitializedCallback(int32_t uid) {
m_initializedCallbacks = CancelCallback(m_initializedCallbacks, uid);
}
void DigitalPWMData::InvokeInitializedCallback(HAL_Value value) {
InvokeCallback(m_initializedCallbacks, "Initialized", &value);
}
HAL_Bool DigitalPWMData::GetInitialized() { return m_initialized; }
void DigitalPWMData::SetInitialized(HAL_Bool initialized) {
HAL_Bool oldValue = m_initialized.exchange(initialized);
if (oldValue != initialized) {
InvokeInitializedCallback(MakeBoolean(initialized));
}
}
int32_t DigitalPWMData::RegisterDutyCycleCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_dutyCycleCallbacks = RegisterCallback(m_dutyCycleCallbacks, "DutyCycle",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetDutyCycle());
callback("DutyCycle", param, &value);
}
return newUid;
}
void DigitalPWMData::CancelDutyCycleCallback(int32_t uid) {
m_dutyCycleCallbacks = CancelCallback(m_dutyCycleCallbacks, uid);
}
void DigitalPWMData::InvokeDutyCycleCallback(HAL_Value value) {
InvokeCallback(m_dutyCycleCallbacks, "DutyCycle", &value);
}
double DigitalPWMData::GetDutyCycle() { return m_dutyCycle; }
void DigitalPWMData::SetDutyCycle(double dutyCycle) {
double oldValue = m_dutyCycle.exchange(dutyCycle);
if (oldValue != dutyCycle) {
InvokeDutyCycleCallback(MakeDouble(dutyCycle));
}
}
int32_t DigitalPWMData::RegisterPinCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_pinCallbacks =
RegisterCallback(m_pinCallbacks, "Pin", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetPin());
callback("Pin", param, &value);
}
return newUid;
}
void DigitalPWMData::CancelPinCallback(int32_t uid) {
m_pinCallbacks = CancelCallback(m_pinCallbacks, uid);
}
void DigitalPWMData::InvokePinCallback(HAL_Value value) {
InvokeCallback(m_pinCallbacks, "Pin", &value);
}
int32_t DigitalPWMData::GetPin() { return m_pin; }
void DigitalPWMData::SetPin(int32_t pin) {
int32_t oldValue = m_pin.exchange(pin);
if (oldValue != pin) {
InvokePinCallback(MakeInt(pin));
}
}
extern "C" {
void HALSIM_ResetDigitalPWMData(int32_t index) {
SimDigitalPWMData[index].ResetData();
}
int32_t HALSIM_RegisterDigitalPWMInitializedCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimDigitalPWMData[index].RegisterInitializedCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDigitalPWMInitializedCallback(int32_t index, int32_t uid) {
SimDigitalPWMData[index].CancelInitializedCallback(uid);
}
HAL_Bool HALSIM_GetDigitalPWMInitialized(int32_t index) {
return SimDigitalPWMData[index].GetInitialized();
}
void HALSIM_SetDigitalPWMInitialized(int32_t index, HAL_Bool initialized) {
SimDigitalPWMData[index].SetInitialized(initialized);
}
int32_t HALSIM_RegisterDigitalPWMDutyCycleCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimDigitalPWMData[index].RegisterDutyCycleCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDigitalPWMDutyCycleCallback(int32_t index, int32_t uid) {
SimDigitalPWMData[index].CancelDutyCycleCallback(uid);
}
double HALSIM_GetDigitalPWMDutyCycle(int32_t index) {
return SimDigitalPWMData[index].GetDutyCycle();
}
void HALSIM_SetDigitalPWMDutyCycle(int32_t index, double dutyCycle) {
SimDigitalPWMData[index].SetDutyCycle(dutyCycle);
}
int32_t HALSIM_RegisterDigitalPWMPinCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimDigitalPWMData[index].RegisterPinCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDigitalPWMPinCallback(int32_t index, int32_t uid) {
SimDigitalPWMData[index].CancelPinCallback(uid);
}
int32_t HALSIM_GetDigitalPWMPin(int32_t index) {
return SimDigitalPWMData[index].GetPin();
}
void HALSIM_SetDigitalPWMPin(int32_t index, int32_t pin) {
SimDigitalPWMData[index].SetPin(pin);
}
}

View File

@@ -0,0 +1,52 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/DigitalPWMData.h"
#include "MockData/NotifyListenerVector.h"
namespace hal {
class DigitalPWMData {
public:
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelInitializedCallback(int32_t uid);
void InvokeInitializedCallback(HAL_Value value);
HAL_Bool GetInitialized();
void SetInitialized(HAL_Bool initialized);
int32_t RegisterDutyCycleCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelDutyCycleCallback(int32_t uid);
void InvokeDutyCycleCallback(HAL_Value value);
double GetDutyCycle();
void SetDutyCycle(double dutyCycle);
int32_t RegisterPinCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelPinCallback(int32_t uid);
void InvokePinCallback(HAL_Value value);
int32_t GetPin();
void SetPin(int32_t pin);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_initialized{false};
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
std::atomic<double> m_dutyCycle{false};
std::shared_ptr<NotifyListenerVector> m_dutyCycleCallbacks = nullptr;
std::atomic<int32_t> m_pin{0};
std::shared_ptr<NotifyListenerVector> m_pinCallbacks = nullptr;
};
extern DigitalPWMData SimDigitalPWMData[];
} // namespace hal

View File

@@ -0,0 +1,451 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "DriverStationDataInternal.h"
#include "NotifyCallbackHelpers.h"
using namespace hal;
DriverStationData hal::SimDriverStationData;
void DriverStationData::ResetData() {
m_enabled = false;
m_enabledCallbacks = nullptr;
m_autonomous = false;
m_autonomousCallbacks = nullptr;
m_test = false;
m_testCallbacks = nullptr;
m_eStop = false;
m_eStopCallbacks = nullptr;
m_fmsAttached = false;
m_fmsAttachedCallbacks = nullptr;
m_dsAttached = false;
m_dsAttachedCallbacks = nullptr;
}
int32_t DriverStationData::RegisterEnabledCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_enabledCallbacks = RegisterCallback(m_enabledCallbacks, "Enabled",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetEnabled());
callback("Enabled", param, &value);
}
return newUid;
}
void DriverStationData::CancelEnabledCallback(int32_t uid) {
m_enabledCallbacks = CancelCallback(m_enabledCallbacks, uid);
}
void DriverStationData::InvokeEnabledCallback(HAL_Value value) {
InvokeCallback(m_enabledCallbacks, "Enabled", &value);
}
HAL_Bool DriverStationData::GetEnabled() { return m_enabled; }
void DriverStationData::SetEnabled(HAL_Bool enabled) {
HAL_Bool oldValue = m_enabled.exchange(enabled);
if (oldValue != enabled) {
InvokeEnabledCallback(MakeBoolean(enabled));
}
}
int32_t DriverStationData::RegisterAutonomousCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_autonomousCallbacks = RegisterCallback(
m_autonomousCallbacks, "Autonomous", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetAutonomous());
callback("Autonomous", param, &value);
}
return newUid;
}
void DriverStationData::CancelAutonomousCallback(int32_t uid) {
m_autonomousCallbacks = CancelCallback(m_autonomousCallbacks, uid);
}
void DriverStationData::InvokeAutonomousCallback(HAL_Value value) {
InvokeCallback(m_autonomousCallbacks, "Autonomous", &value);
}
HAL_Bool DriverStationData::GetAutonomous() { return m_autonomous; }
void DriverStationData::SetAutonomous(HAL_Bool autonomous) {
HAL_Bool oldValue = m_autonomous.exchange(autonomous);
if (oldValue != autonomous) {
InvokeAutonomousCallback(MakeBoolean(autonomous));
}
}
int32_t DriverStationData::RegisterTestCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_testCallbacks =
RegisterCallback(m_testCallbacks, "Test", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetTest());
callback("Test", param, &value);
}
return newUid;
}
void DriverStationData::CancelTestCallback(int32_t uid) {
m_testCallbacks = CancelCallback(m_testCallbacks, uid);
}
void DriverStationData::InvokeTestCallback(HAL_Value value) {
InvokeCallback(m_testCallbacks, "Test", &value);
}
HAL_Bool DriverStationData::GetTest() { return m_test; }
void DriverStationData::SetTest(HAL_Bool test) {
HAL_Bool oldValue = m_test.exchange(test);
if (oldValue != test) {
InvokeTestCallback(MakeBoolean(test));
}
}
int32_t DriverStationData::RegisterEStopCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_eStopCallbacks =
RegisterCallback(m_eStopCallbacks, "EStop", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetEStop());
callback("EStop", param, &value);
}
return newUid;
}
void DriverStationData::CancelEStopCallback(int32_t uid) {
m_eStopCallbacks = CancelCallback(m_eStopCallbacks, uid);
}
void DriverStationData::InvokeEStopCallback(HAL_Value value) {
InvokeCallback(m_eStopCallbacks, "EStop", &value);
}
HAL_Bool DriverStationData::GetEStop() { return m_eStop; }
void DriverStationData::SetEStop(HAL_Bool eStop) {
HAL_Bool oldValue = m_eStop.exchange(eStop);
if (oldValue != eStop) {
InvokeEStopCallback(MakeBoolean(eStop));
}
}
int32_t DriverStationData::RegisterFmsAttachedCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_fmsAttachedCallbacks = RegisterCallback(
m_fmsAttachedCallbacks, "FmsAttached", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetFmsAttached());
callback("FmsAttached", param, &value);
}
return newUid;
}
void DriverStationData::CancelFmsAttachedCallback(int32_t uid) {
m_fmsAttachedCallbacks = CancelCallback(m_fmsAttachedCallbacks, uid);
}
void DriverStationData::InvokeFmsAttachedCallback(HAL_Value value) {
InvokeCallback(m_fmsAttachedCallbacks, "FmsAttached", &value);
}
HAL_Bool DriverStationData::GetFmsAttached() { return m_fmsAttached; }
void DriverStationData::SetFmsAttached(HAL_Bool fmsAttached) {
HAL_Bool oldValue = m_fmsAttached.exchange(fmsAttached);
if (oldValue != fmsAttached) {
InvokeFmsAttachedCallback(MakeBoolean(fmsAttached));
}
}
int32_t DriverStationData::RegisterDsAttachedCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_dsAttachedCallbacks = RegisterCallback(
m_dsAttachedCallbacks, "DsAttached", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetDsAttached());
callback("DsAttached", param, &value);
}
return newUid;
}
void DriverStationData::CancelDsAttachedCallback(int32_t uid) {
m_dsAttachedCallbacks = CancelCallback(m_dsAttachedCallbacks, uid);
}
void DriverStationData::InvokeDsAttachedCallback(HAL_Value value) {
InvokeCallback(m_dsAttachedCallbacks, "DsAttached", &value);
}
HAL_Bool DriverStationData::GetDsAttached() { return m_dsAttached; }
void DriverStationData::SetDsAttached(HAL_Bool dsAttached) {
HAL_Bool oldValue = m_dsAttached.exchange(dsAttached);
if (oldValue != dsAttached) {
InvokeDsAttachedCallback(MakeBoolean(dsAttached));
}
}
int32_t DriverStationData::RegisterAllianceStationIdCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_allianceStationIdCallbacks =
RegisterCallback(m_allianceStationIdCallbacks, "AllianceStationId",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeEnum(GetAllianceStationId());
callback("AllianceStationId", param, &value);
}
return newUid;
}
void DriverStationData::CancelAllianceStationIdCallback(int32_t uid) {
m_allianceStationIdCallbacks =
CancelCallback(m_allianceStationIdCallbacks, uid);
}
void DriverStationData::InvokeAllianceStationIdCallback(HAL_Value value) {
InvokeCallback(m_allianceStationIdCallbacks, "AllianceStationId", &value);
}
HAL_AllianceStationID DriverStationData::GetAllianceStationId() {
return m_allianceStationId;
}
void DriverStationData::SetAllianceStationId(
HAL_AllianceStationID allianceStationId) {
HAL_AllianceStationID oldValue =
m_allianceStationId.exchange(allianceStationId);
if (oldValue != allianceStationId) {
InvokeAllianceStationIdCallback(MakeEnum(allianceStationId));
}
}
int32_t DriverStationData::RegisterMatchTimeCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_matchTimeCallbacks = RegisterCallback(m_matchTimeCallbacks, "MatchTime",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetMatchTime());
callback("MatchTime", param, &value);
}
return newUid;
}
void DriverStationData::CancelMatchTimeCallback(int32_t uid) {
m_matchTimeCallbacks = CancelCallback(m_matchTimeCallbacks, uid);
}
void DriverStationData::InvokeMatchTimeCallback(HAL_Value value) {
InvokeCallback(m_matchTimeCallbacks, "MatchTime", &value);
}
double DriverStationData::GetMatchTime() { return m_matchTime; }
void DriverStationData::SetMatchTime(double matchTime) {
double oldValue = m_matchTime.exchange(matchTime);
if (oldValue != matchTime) {
InvokeMatchTimeCallback(MakeDouble(matchTime));
}
}
void DriverStationData::NotifyNewData() { HAL_ReleaseDSMutex(); }
extern "C" {
void HALSIM_ResetDriverStationData(void) { SimDriverStationData.ResetData(); }
int32_t HALSIM_RegisterDriverStationEnabledCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimDriverStationData.RegisterEnabledCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDriverStationEnabledCallback(int32_t uid) {
SimDriverStationData.CancelEnabledCallback(uid);
}
HAL_Bool HALSIM_GetDriverStationEnabled() {
return SimDriverStationData.GetEnabled();
}
void HALSIM_SetDriverStationEnabled(HAL_Bool enabled) {
SimDriverStationData.SetEnabled(enabled);
}
int32_t HALSIM_RegisterDriverStationAutonomousCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
return SimDriverStationData.RegisterAutonomousCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDriverStationAutonomousCallback(int32_t uid) {
SimDriverStationData.CancelAutonomousCallback(uid);
}
HAL_Bool HALSIM_GetDriverStationAutonomous() {
return SimDriverStationData.GetAutonomous();
}
void HALSIM_SetDriverStationAutonomous(HAL_Bool autonomous) {
SimDriverStationData.SetAutonomous(autonomous);
}
int32_t HALSIM_RegisterDriverStationTestCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimDriverStationData.RegisterTestCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDriverStationTestCallback(int32_t uid) {
SimDriverStationData.CancelTestCallback(uid);
}
HAL_Bool HALSIM_GetDriverStationTest() {
return SimDriverStationData.GetTest();
}
void HALSIM_SetDriverStationTest(HAL_Bool test) {
SimDriverStationData.SetTest(test);
}
int32_t HALSIM_RegisterDriverStationEStopCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimDriverStationData.RegisterEStopCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDriverStationEStopCallback(int32_t uid) {
SimDriverStationData.CancelEStopCallback(uid);
}
HAL_Bool HALSIM_GetDriverStationEStop() {
return SimDriverStationData.GetEStop();
}
void HALSIM_SetDriverStationEStop(HAL_Bool eStop) {
SimDriverStationData.SetEStop(eStop);
}
int32_t HALSIM_RegisterDriverStationFmsAttachedCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
return SimDriverStationData.RegisterFmsAttachedCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDriverStationFmsAttachedCallback(int32_t uid) {
SimDriverStationData.CancelFmsAttachedCallback(uid);
}
HAL_Bool HALSIM_GetDriverStationFmsAttached() {
return SimDriverStationData.GetFmsAttached();
}
void HALSIM_SetDriverStationFmsAttached(HAL_Bool fmsAttached) {
SimDriverStationData.SetFmsAttached(fmsAttached);
}
int32_t HALSIM_RegisterDriverStationDsAttachedCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
return SimDriverStationData.RegisterDsAttachedCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDriverStationDsAttachedCallback(int32_t uid) {
SimDriverStationData.CancelDsAttachedCallback(uid);
}
HAL_Bool HALSIM_GetDriverStationDsAttached() {
return SimDriverStationData.GetDsAttached();
}
void HALSIM_SetDriverStationDsAttached(HAL_Bool dsAttached) {
SimDriverStationData.SetDsAttached(dsAttached);
}
int32_t HALSIM_RegisterDriverStationAllianceStationIdCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
return SimDriverStationData.RegisterAllianceStationIdCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDriverStationAllianceStationIdCallback(int32_t uid) {
SimDriverStationData.CancelAllianceStationIdCallback(uid);
}
HAL_AllianceStationID HALSIM_GetDriverStationAllianceStationId() {
return SimDriverStationData.GetAllianceStationId();
}
void HALSIM_SetDriverStationAllianceStationId(
HAL_AllianceStationID allianceStationId) {
SimDriverStationData.SetAllianceStationId(allianceStationId);
}
int32_t HALSIM_RegisterDriverStationMatchTimeCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
return SimDriverStationData.RegisterMatchTimeCallback(callback, param,
initialNotify);
}
void HALSIM_CancelDriverStationMatchTimeCallback(int32_t uid) {
SimDriverStationData.CancelMatchTimeCallback(uid);
}
double HALSIM_GetDriverStationMatchTime() {
return SimDriverStationData.GetMatchTime();
}
void HALSIM_SetDriverStationMatchTime(double matchTime) {
SimDriverStationData.SetMatchTime(matchTime);
}
void HALSIM_NotifyDriverStationNewData(void) {
SimDriverStationData.NotifyNewData();
}
} // extern "C"

View File

@@ -0,0 +1,101 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/DriverStationData.h"
#include "MockData/NotifyListenerVector.h"
namespace hal {
class DriverStationData {
public:
void ResetData();
int32_t RegisterEnabledCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelEnabledCallback(int32_t uid);
void InvokeEnabledCallback(HAL_Value value);
HAL_Bool GetEnabled();
void SetEnabled(HAL_Bool enabled);
int32_t RegisterAutonomousCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelAutonomousCallback(int32_t uid);
void InvokeAutonomousCallback(HAL_Value value);
HAL_Bool GetAutonomous();
void SetAutonomous(HAL_Bool autonomous);
int32_t RegisterTestCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelTestCallback(int32_t uid);
void InvokeTestCallback(HAL_Value value);
HAL_Bool GetTest();
void SetTest(HAL_Bool test);
int32_t RegisterEStopCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelEStopCallback(int32_t uid);
void InvokeEStopCallback(HAL_Value value);
HAL_Bool GetEStop();
void SetEStop(HAL_Bool eStop);
int32_t RegisterFmsAttachedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelFmsAttachedCallback(int32_t uid);
void InvokeFmsAttachedCallback(HAL_Value value);
HAL_Bool GetFmsAttached();
void SetFmsAttached(HAL_Bool fmsAttached);
int32_t RegisterDsAttachedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelDsAttachedCallback(int32_t uid);
void InvokeDsAttachedCallback(HAL_Value value);
HAL_Bool GetDsAttached();
void SetDsAttached(HAL_Bool dsAttached);
int32_t RegisterAllianceStationIdCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelAllianceStationIdCallback(int32_t uid);
void InvokeAllianceStationIdCallback(HAL_Value value);
HAL_AllianceStationID GetAllianceStationId();
void SetAllianceStationId(HAL_AllianceStationID allianceStationId);
int32_t RegisterMatchTimeCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelMatchTimeCallback(int32_t uid);
void InvokeMatchTimeCallback(HAL_Value value);
double GetMatchTime();
void SetMatchTime(double matchTime);
void NotifyNewData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_enabled{false};
std::shared_ptr<NotifyListenerVector> m_enabledCallbacks = nullptr;
std::atomic<HAL_Bool> m_autonomous{false};
std::shared_ptr<NotifyListenerVector> m_autonomousCallbacks = nullptr;
std::atomic<HAL_Bool> m_test{false};
std::shared_ptr<NotifyListenerVector> m_testCallbacks = nullptr;
std::atomic<HAL_Bool> m_eStop{false};
std::shared_ptr<NotifyListenerVector> m_eStopCallbacks = nullptr;
std::atomic<HAL_Bool> m_fmsAttached{false};
std::shared_ptr<NotifyListenerVector> m_fmsAttachedCallbacks = nullptr;
std::atomic<HAL_Bool> m_dsAttached{false};
std::shared_ptr<NotifyListenerVector> m_dsAttachedCallbacks = nullptr;
std::atomic<HAL_AllianceStationID> m_allianceStationId{
static_cast<HAL_AllianceStationID>(0)};
std::shared_ptr<NotifyListenerVector> m_allianceStationIdCallbacks = nullptr;
std::atomic<double> m_matchTime{0.0};
std::shared_ptr<NotifyListenerVector> m_matchTimeCallbacks = nullptr;
};
extern DriverStationData SimDriverStationData;
} // namespace hal

View File

@@ -0,0 +1,488 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "EncoderDataInternal.h"
#include "NotifyCallbackHelpers.h"
using namespace hal;
EncoderData hal::SimEncoderData[kNumEncoders];
void EncoderData::ResetData() {
m_initialized = false;
m_initializedCallbacks = nullptr;
m_count = 0;
m_countCallbacks = nullptr;
m_period = std::numeric_limits<double>::max();
m_periodCallbacks = nullptr;
m_reset = false;
m_resetCallbacks = nullptr;
m_maxPeriod = 0;
m_maxPeriodCallbacks = nullptr;
m_direction = false;
m_directionCallbacks = nullptr;
m_reverseDirection = false;
m_reverseDirectionCallbacks = nullptr;
m_samplesToAverage = 0;
m_samplesToAverageCallbacks = nullptr;
}
int32_t EncoderData::RegisterInitializedCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_initializedCallbacks = RegisterCallback(
m_initializedCallbacks, "Initialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetInitialized());
callback("Initialized", param, &value);
}
return newUid;
}
void EncoderData::CancelInitializedCallback(int32_t uid) {
m_initializedCallbacks = CancelCallback(m_initializedCallbacks, uid);
}
void EncoderData::InvokeInitializedCallback(HAL_Value value) {
InvokeCallback(m_initializedCallbacks, "Initialized", &value);
}
HAL_Bool EncoderData::GetInitialized() { return m_initialized; }
void EncoderData::SetInitialized(HAL_Bool initialized) {
HAL_Bool oldValue = m_initialized.exchange(initialized);
if (oldValue != initialized) {
InvokeInitializedCallback(MakeBoolean(initialized));
}
}
int32_t EncoderData::RegisterCountCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_countCallbacks =
RegisterCallback(m_countCallbacks, "Count", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetCount());
callback("Count", param, &value);
}
return newUid;
}
void EncoderData::CancelCountCallback(int32_t uid) {
m_countCallbacks = CancelCallback(m_countCallbacks, uid);
}
void EncoderData::InvokeCountCallback(HAL_Value value) {
InvokeCallback(m_countCallbacks, "Count", &value);
}
int32_t EncoderData::GetCount() { return m_count; }
void EncoderData::SetCount(int32_t count) {
int32_t oldValue = m_count.exchange(count);
if (oldValue != count) {
InvokeCountCallback(MakeInt(count));
}
}
int32_t EncoderData::RegisterPeriodCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_periodCallbacks =
RegisterCallback(m_periodCallbacks, "Period", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetPeriod());
callback("Period", param, &value);
}
return newUid;
}
void EncoderData::CancelPeriodCallback(int32_t uid) {
m_periodCallbacks = CancelCallback(m_periodCallbacks, uid);
}
void EncoderData::InvokePeriodCallback(HAL_Value value) {
InvokeCallback(m_periodCallbacks, "Period", &value);
}
double EncoderData::GetPeriod() { return m_period; }
void EncoderData::SetPeriod(double period) {
double oldValue = m_period.exchange(period);
if (oldValue != period) {
InvokePeriodCallback(MakeDouble(period));
}
}
int32_t EncoderData::RegisterResetCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_resetCallbacks =
RegisterCallback(m_resetCallbacks, "Reset", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetReset());
callback("Reset", param, &value);
}
return newUid;
}
void EncoderData::CancelResetCallback(int32_t uid) {
m_resetCallbacks = CancelCallback(m_resetCallbacks, uid);
}
void EncoderData::InvokeResetCallback(HAL_Value value) {
InvokeCallback(m_resetCallbacks, "Reset", &value);
}
HAL_Bool EncoderData::GetReset() { return m_reset; }
void EncoderData::SetReset(HAL_Bool reset) {
HAL_Bool oldValue = m_reset.exchange(reset);
if (oldValue != reset) {
InvokeResetCallback(MakeBoolean(reset));
}
}
int32_t EncoderData::RegisterMaxPeriodCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_maxPeriodCallbacks = RegisterCallback(m_maxPeriodCallbacks, "MaxPeriod",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetMaxPeriod());
callback("MaxPeriod", param, &value);
}
return newUid;
}
void EncoderData::CancelMaxPeriodCallback(int32_t uid) {
m_maxPeriodCallbacks = CancelCallback(m_maxPeriodCallbacks, uid);
}
void EncoderData::InvokeMaxPeriodCallback(HAL_Value value) {
InvokeCallback(m_maxPeriodCallbacks, "MaxPeriod", &value);
}
double EncoderData::GetMaxPeriod() { return m_maxPeriod; }
void EncoderData::SetMaxPeriod(double maxPeriod) {
double oldValue = m_maxPeriod.exchange(maxPeriod);
if (oldValue != maxPeriod) {
InvokeMaxPeriodCallback(MakeDouble(maxPeriod));
}
}
int32_t EncoderData::RegisterDirectionCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_directionCallbacks = RegisterCallback(m_directionCallbacks, "Direction",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetDirection());
callback("Direction", param, &value);
}
return newUid;
}
void EncoderData::CancelDirectionCallback(int32_t uid) {
m_directionCallbacks = CancelCallback(m_directionCallbacks, uid);
}
void EncoderData::InvokeDirectionCallback(HAL_Value value) {
InvokeCallback(m_directionCallbacks, "Direction", &value);
}
HAL_Bool EncoderData::GetDirection() { return m_direction; }
void EncoderData::SetDirection(HAL_Bool direction) {
HAL_Bool oldValue = m_direction.exchange(direction);
if (oldValue != direction) {
InvokeDirectionCallback(MakeBoolean(direction));
}
}
int32_t EncoderData::RegisterReverseDirectionCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_reverseDirectionCallbacks =
RegisterCallback(m_reverseDirectionCallbacks, "ReverseDirection",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetReverseDirection());
callback("ReverseDirection", param, &value);
}
return newUid;
}
void EncoderData::CancelReverseDirectionCallback(int32_t uid) {
m_reverseDirectionCallbacks =
CancelCallback(m_reverseDirectionCallbacks, uid);
}
void EncoderData::InvokeReverseDirectionCallback(HAL_Value value) {
InvokeCallback(m_reverseDirectionCallbacks, "ReverseDirection", &value);
}
HAL_Bool EncoderData::GetReverseDirection() { return m_reverseDirection; }
void EncoderData::SetReverseDirection(HAL_Bool reverseDirection) {
HAL_Bool oldValue = m_reverseDirection.exchange(reverseDirection);
if (oldValue != reverseDirection) {
InvokeReverseDirectionCallback(MakeBoolean(reverseDirection));
}
}
int32_t EncoderData::RegisterSamplesToAverageCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_samplesToAverageCallbacks =
RegisterCallback(m_samplesToAverageCallbacks, "SamplesToAverage",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetSamplesToAverage());
callback("SamplesToAverage", param, &value);
}
return newUid;
}
void EncoderData::CancelSamplesToAverageCallback(int32_t uid) {
m_samplesToAverageCallbacks =
CancelCallback(m_samplesToAverageCallbacks, uid);
}
void EncoderData::InvokeSamplesToAverageCallback(HAL_Value value) {
InvokeCallback(m_samplesToAverageCallbacks, "SamplesToAverage", &value);
}
int32_t EncoderData::GetSamplesToAverage() { return m_samplesToAverage; }
void EncoderData::SetSamplesToAverage(int32_t samplesToAverage) {
int32_t oldValue = m_samplesToAverage.exchange(samplesToAverage);
if (oldValue != samplesToAverage) {
InvokeSamplesToAverageCallback(MakeInt(samplesToAverage));
}
}
extern "C" {
void HALSIM_ResetEncoderData(int32_t index) {
SimEncoderData[index].ResetData();
}
int32_t HALSIM_RegisterEncoderInitializedCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimEncoderData[index].RegisterInitializedCallback(callback, param,
initialNotify);
}
void HALSIM_CancelEncoderInitializedCallback(int32_t index, int32_t uid) {
SimEncoderData[index].CancelInitializedCallback(uid);
}
HAL_Bool HALSIM_GetEncoderInitialized(int32_t index) {
return SimEncoderData[index].GetInitialized();
}
void HALSIM_SetEncoderInitialized(int32_t index, HAL_Bool initialized) {
SimEncoderData[index].SetInitialized(initialized);
}
int32_t HALSIM_RegisterEncoderCountCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimEncoderData[index].RegisterCountCallback(callback, param,
initialNotify);
}
void HALSIM_CancelEncoderCountCallback(int32_t index, int32_t uid) {
SimEncoderData[index].CancelCountCallback(uid);
}
int32_t HALSIM_GetEncoderCount(int32_t index) {
return SimEncoderData[index].GetCount();
}
void HALSIM_SetEncoderCount(int32_t index, int32_t count) {
SimEncoderData[index].SetCount(count);
}
int32_t HALSIM_RegisterEncoderPeriodCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimEncoderData[index].RegisterPeriodCallback(callback, param,
initialNotify);
}
void HALSIM_CancelEncoderPeriodCallback(int32_t index, int32_t uid) {
SimEncoderData[index].CancelPeriodCallback(uid);
}
double HALSIM_GetEncoderPeriod(int32_t index) {
return SimEncoderData[index].GetPeriod();
}
void HALSIM_SetEncoderPeriod(int32_t index, double period) {
SimEncoderData[index].SetPeriod(period);
}
int32_t HALSIM_RegisterEncoderResetCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimEncoderData[index].RegisterResetCallback(callback, param,
initialNotify);
}
void HALSIM_CancelEncoderResetCallback(int32_t index, int32_t uid) {
SimEncoderData[index].CancelResetCallback(uid);
}
HAL_Bool HALSIM_GetEncoderReset(int32_t index) {
return SimEncoderData[index].GetReset();
}
void HALSIM_SetEncoderReset(int32_t index, HAL_Bool reset) {
SimEncoderData[index].SetReset(reset);
}
int32_t HALSIM_RegisterEncoderMaxPeriodCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimEncoderData[index].RegisterMaxPeriodCallback(callback, param,
initialNotify);
}
void HALSIM_CancelEncoderMaxPeriodCallback(int32_t index, int32_t uid) {
SimEncoderData[index].CancelMaxPeriodCallback(uid);
}
double HALSIM_GetEncoderMaxPeriod(int32_t index) {
return SimEncoderData[index].GetMaxPeriod();
}
void HALSIM_SetEncoderMaxPeriod(int32_t index, double maxPeriod) {
SimEncoderData[index].SetMaxPeriod(maxPeriod);
}
int32_t HALSIM_RegisterEncoderDirectionCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimEncoderData[index].RegisterDirectionCallback(callback, param,
initialNotify);
}
void HALSIM_CancelEncoderDirectionCallback(int32_t index, int32_t uid) {
SimEncoderData[index].CancelDirectionCallback(uid);
}
HAL_Bool HALSIM_GetEncoderDirection(int32_t index) {
return SimEncoderData[index].GetDirection();
}
void HALSIM_SetEncoderDirection(int32_t index, HAL_Bool direction) {
SimEncoderData[index].SetDirection(direction);
}
int32_t HALSIM_RegisterEncoderReverseDirectionCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimEncoderData[index].RegisterReverseDirectionCallback(callback, param,
initialNotify);
}
void HALSIM_CancelEncoderReverseDirectionCallback(int32_t index, int32_t uid) {
SimEncoderData[index].CancelReverseDirectionCallback(uid);
}
HAL_Bool HALSIM_GetEncoderReverseDirection(int32_t index) {
return SimEncoderData[index].GetReverseDirection();
}
void HALSIM_SetEncoderReverseDirection(int32_t index,
HAL_Bool reverseDirection) {
SimEncoderData[index].SetReverseDirection(reverseDirection);
}
int32_t HALSIM_RegisterEncoderSamplesToAverageCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimEncoderData[index].RegisterSamplesToAverageCallback(callback, param,
initialNotify);
}
void HALSIM_CancelEncoderSamplesToAverageCallback(int32_t index, int32_t uid) {
SimEncoderData[index].CancelSamplesToAverageCallback(uid);
}
int32_t HALSIM_GetEncoderSamplesToAverage(int32_t index) {
return SimEncoderData[index].GetSamplesToAverage();
}
void HALSIM_SetEncoderSamplesToAverage(int32_t index,
int32_t samplesToAverage) {
SimEncoderData[index].SetSamplesToAverage(samplesToAverage);
}
}

View File

@@ -0,0 +1,98 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <limits>
#include <memory>
#include "MockData/EncoderData.h"
#include "MockData/NotifyListenerVector.h"
namespace hal {
class EncoderData {
public:
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelInitializedCallback(int32_t uid);
void InvokeInitializedCallback(HAL_Value value);
HAL_Bool GetInitialized();
void SetInitialized(HAL_Bool initialized);
int32_t RegisterCountCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelCountCallback(int32_t uid);
void InvokeCountCallback(HAL_Value value);
int32_t GetCount();
void SetCount(int32_t count);
int32_t RegisterPeriodCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelPeriodCallback(int32_t uid);
void InvokePeriodCallback(HAL_Value value);
double GetPeriod();
void SetPeriod(double period);
int32_t RegisterResetCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelResetCallback(int32_t uid);
void InvokeResetCallback(HAL_Value value);
HAL_Bool GetReset();
void SetReset(HAL_Bool reset);
int32_t RegisterMaxPeriodCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelMaxPeriodCallback(int32_t uid);
void InvokeMaxPeriodCallback(HAL_Value value);
double GetMaxPeriod();
void SetMaxPeriod(double maxPeriod);
int32_t RegisterDirectionCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelDirectionCallback(int32_t uid);
void InvokeDirectionCallback(HAL_Value value);
HAL_Bool GetDirection();
void SetDirection(HAL_Bool direction);
int32_t RegisterReverseDirectionCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelReverseDirectionCallback(int32_t uid);
void InvokeReverseDirectionCallback(HAL_Value value);
HAL_Bool GetReverseDirection();
void SetReverseDirection(HAL_Bool reverseDirection);
int32_t RegisterSamplesToAverageCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelSamplesToAverageCallback(int32_t uid);
void InvokeSamplesToAverageCallback(HAL_Value value);
int32_t GetSamplesToAverage();
void SetSamplesToAverage(int32_t samplesToAverage);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_initialized{false};
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
std::atomic<int32_t> m_count{0};
std::shared_ptr<NotifyListenerVector> m_countCallbacks = nullptr;
std::atomic<double> m_period{std::numeric_limits<double>::max()};
std::shared_ptr<NotifyListenerVector> m_periodCallbacks = nullptr;
std::atomic<HAL_Bool> m_reset{false};
std::shared_ptr<NotifyListenerVector> m_resetCallbacks = nullptr;
std::atomic<double> m_maxPeriod{0};
std::shared_ptr<NotifyListenerVector> m_maxPeriodCallbacks = nullptr;
std::atomic<HAL_Bool> m_direction{false};
std::shared_ptr<NotifyListenerVector> m_directionCallbacks = nullptr;
std::atomic<HAL_Bool> m_reverseDirection{false};
std::shared_ptr<NotifyListenerVector> m_reverseDirectionCallbacks = nullptr;
std::atomic<int32_t> m_samplesToAverage{0};
std::shared_ptr<NotifyListenerVector> m_samplesToAverageCallbacks = nullptr;
};
extern EncoderData SimEncoderData[];
} // namespace hal

View File

@@ -0,0 +1,27 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <memory>
#include <string>
#include "MockData/HALValue.h"
#include "MockData/llvm/StringRef.h"
namespace hal {
class Value;
void ConvertToC(const Value& in, HAL_Value* out);
std::shared_ptr<Value> ConvertFromC(const HAL_Value& value);
void ConvertToC(llvm::StringRef in, HALString* out);
inline llvm::StringRef ConvertFromC(const HALString& str) {
return llvm::StringRef(str.str, str.len);
}
} // namespace hal

View File

@@ -0,0 +1,44 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "NotifyCallbackHelpers.h"
using namespace hal;
std::shared_ptr<NotifyListenerVector> RegisterCallback(
std::shared_ptr<NotifyListenerVector> currentVector, const char* name,
HAL_NotifyCallback callback, void* param, int32_t* newUid) {
std::shared_ptr<NotifyListenerVector> newCallbacks;
if (currentVector == nullptr) {
newCallbacks = std::make_shared<NotifyListenerVector>(
param, callback, reinterpret_cast<unsigned int*>(newUid));
} else {
newCallbacks = currentVector->emplace_back(
param, callback, reinterpret_cast<unsigned int*>(newUid));
}
return newCallbacks;
}
std::shared_ptr<NotifyListenerVector> CancelCallback(
std::shared_ptr<NotifyListenerVector> currentVector, int32_t uid) {
// Create a copy of the callbacks to erase from
auto newCallbacks = currentVector->erase(uid);
return newCallbacks;
}
void InvokeCallback(std::shared_ptr<NotifyListenerVector> currentVector,
const char* name, const HAL_Value* value) {
// Return if no callbacks are assigned
if (currentVector == nullptr) return;
// Get a copy of the shared_ptr, then iterate and callback listeners
auto newCallbacks = currentVector;
for (size_t i = 0; i < newCallbacks->size(); ++i) {
if (!(*newCallbacks)[i]) continue; // removed
auto listener = (*newCallbacks)[i];
listener.callback(name, listener.param, value);
}
}

View File

@@ -0,0 +1,22 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <memory>
#include "MockData/NotifyListenerVector.h"
std::shared_ptr<hal::NotifyListenerVector> RegisterCallback(
std::shared_ptr<hal::NotifyListenerVector> currentVector, const char* name,
HAL_NotifyCallback callback, void* param, int32_t* newUid);
std::shared_ptr<hal::NotifyListenerVector> CancelCallback(
std::shared_ptr<hal::NotifyListenerVector> currentVector, int32_t uid);
void InvokeCallback(std::shared_ptr<hal::NotifyListenerVector> currentVector,
const char* name, const HAL_Value* value);

View File

@@ -0,0 +1,59 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "MockData/NotifyListenerVector.h"
using namespace hal;
NotifyListenerVector::NotifyListenerVector(const NotifyListenerVector* copyFrom,
const private_init&)
: m_vector(copyFrom->m_vector), m_free(copyFrom->m_free) {}
NotifyListenerVector::NotifyListenerVector(void* param,
HAL_NotifyCallback callback,
unsigned int* newUid) {
*newUid = emplace_back_impl(param, callback);
}
std::shared_ptr<NotifyListenerVector> NotifyListenerVector::emplace_back(
void* param, HAL_NotifyCallback callback, unsigned int* newUid) {
auto newVector = std::make_shared<NotifyListenerVector>(this, private_init());
newVector->m_vector = m_vector;
newVector->m_free = m_free;
*newUid = newVector->emplace_back_impl(param, callback);
return newVector;
}
std::shared_ptr<NotifyListenerVector> NotifyListenerVector::erase(
unsigned int uid) {
auto newVector = std::make_shared<NotifyListenerVector>(this, private_init());
newVector->m_vector = m_vector;
newVector->m_free = m_free;
newVector->erase_impl(uid);
return newVector;
}
unsigned int NotifyListenerVector::emplace_back_impl(
void* param, HAL_NotifyCallback callback) {
unsigned int uid;
if (m_free.empty()) {
uid = m_vector.size();
m_vector.emplace_back(param, callback);
} else {
uid = m_free.back();
m_free.pop_back();
m_vector[uid] = NotifyListener(param, callback);
}
return uid + 1;
}
void NotifyListenerVector::erase_impl(unsigned int uid) {
--uid;
if (uid >= m_vector.size() || !m_vector[uid]) return;
m_free.push_back(uid);
m_vector[uid] = NotifyListener();
}

View File

@@ -0,0 +1,451 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "NotifyCallbackHelpers.h"
#include "PCMDataInternal.h"
using namespace hal;
PCMData hal::SimPCMData[kNumPCMModules];
void PCMData::ResetData() {
for (int i = 0; i < kNumSolenoidChannels; i++) {
m_solenoidInitialized[i] = false;
m_solenoidInitializedCallbacks[i] = nullptr;
m_solenoidOutput[i] = false;
m_solenoidOutputCallbacks[i] = nullptr;
}
m_compressorInitialized = false;
m_compressorInitializedCallbacks = nullptr;
m_compressorOn = false;
m_compressorOnCallbacks = nullptr;
m_closedLoopEnabled = true;
m_closedLoopEnabledCallbacks = nullptr;
m_pressureSwitch = false;
m_pressureSwitchCallbacks = nullptr;
m_compressorCurrent = 0.0;
m_compressorCurrentCallbacks = nullptr;
}
int32_t PCMData::RegisterSolenoidInitializedCallback(
int32_t channel, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_solenoidInitializedCallbacks[channel] =
RegisterCallback(m_solenoidInitializedCallbacks[channel],
"SolenoidInitialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetSolenoidInitialized(channel));
callback("SolenoidInitialized", param, &value);
}
return newUid;
}
void PCMData::CancelSolenoidInitializedCallback(int32_t channel, int32_t uid) {
m_solenoidInitializedCallbacks[channel] =
CancelCallback(m_solenoidInitializedCallbacks[channel], uid);
}
void PCMData::InvokeSolenoidInitializedCallback(int32_t channel,
HAL_Value value) {
InvokeCallback(m_solenoidInitializedCallbacks[channel], "SolenoidInitialized",
&value);
}
HAL_Bool PCMData::GetSolenoidInitialized(int32_t channel) {
return m_solenoidInitialized[channel];
}
void PCMData::SetSolenoidInitialized(int32_t channel,
HAL_Bool solenoidInitialized) {
HAL_Bool oldValue =
m_solenoidInitialized[channel].exchange(solenoidInitialized);
if (oldValue != solenoidInitialized) {
InvokeSolenoidInitializedCallback(channel,
MakeBoolean(solenoidInitialized));
}
}
int32_t PCMData::RegisterSolenoidOutputCallback(int32_t channel,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_solenoidOutputCallbacks[channel] =
RegisterCallback(m_solenoidOutputCallbacks[channel], "SolenoidOutput",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetSolenoidOutput(channel));
callback("SolenoidOutput", param, &value);
}
return newUid;
}
void PCMData::CancelSolenoidOutputCallback(int32_t channel, int32_t uid) {
m_solenoidOutputCallbacks[channel] =
CancelCallback(m_solenoidOutputCallbacks[channel], uid);
}
void PCMData::InvokeSolenoidOutputCallback(int32_t channel, HAL_Value value) {
InvokeCallback(m_solenoidOutputCallbacks[channel], "SolenoidOutput", &value);
}
HAL_Bool PCMData::GetSolenoidOutput(int32_t channel) {
return m_solenoidOutput[channel];
}
void PCMData::SetSolenoidOutput(int32_t channel, HAL_Bool solenoidOutput) {
HAL_Bool oldValue = m_solenoidOutput[channel].exchange(solenoidOutput);
if (oldValue != solenoidOutput) {
InvokeSolenoidOutputCallback(channel, MakeBoolean(solenoidOutput));
}
}
int32_t PCMData::RegisterCompressorInitializedCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_compressorInitializedCallbacks =
RegisterCallback(m_compressorInitializedCallbacks,
"CompressorInitialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetCompressorInitialized());
callback("CompressorInitialized", param, &value);
}
return newUid;
}
void PCMData::CancelCompressorInitializedCallback(int32_t uid) {
m_compressorInitializedCallbacks =
CancelCallback(m_compressorInitializedCallbacks, uid);
}
void PCMData::InvokeCompressorInitializedCallback(HAL_Value value) {
InvokeCallback(m_compressorInitializedCallbacks, "CompressorInitialized",
&value);
}
HAL_Bool PCMData::GetCompressorInitialized() { return m_compressorInitialized; }
void PCMData::SetCompressorInitialized(HAL_Bool compressorInitialized) {
HAL_Bool oldValue = m_compressorInitialized.exchange(compressorInitialized);
if (oldValue != compressorInitialized) {
InvokeCompressorInitializedCallback(MakeBoolean(compressorInitialized));
}
}
int32_t PCMData::RegisterCompressorOnCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_compressorOnCallbacks = RegisterCallback(
m_compressorOnCallbacks, "CompressorOn", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetCompressorOn());
callback("CompressorOn", param, &value);
}
return newUid;
}
void PCMData::CancelCompressorOnCallback(int32_t uid) {
m_compressorOnCallbacks = CancelCallback(m_compressorOnCallbacks, uid);
}
void PCMData::InvokeCompressorOnCallback(HAL_Value value) {
InvokeCallback(m_compressorOnCallbacks, "CompressorOn", &value);
}
HAL_Bool PCMData::GetCompressorOn() { return m_compressorOn; }
void PCMData::SetCompressorOn(HAL_Bool compressorOn) {
HAL_Bool oldValue = m_compressorOn.exchange(compressorOn);
if (oldValue != compressorOn) {
InvokeCompressorOnCallback(MakeBoolean(compressorOn));
}
}
int32_t PCMData::RegisterClosedLoopEnabledCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_closedLoopEnabledCallbacks =
RegisterCallback(m_closedLoopEnabledCallbacks, "ClosedLoopEnabled",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetClosedLoopEnabled());
callback("ClosedLoopEnabled", param, &value);
}
return newUid;
}
void PCMData::CancelClosedLoopEnabledCallback(int32_t uid) {
m_closedLoopEnabledCallbacks =
CancelCallback(m_closedLoopEnabledCallbacks, uid);
}
void PCMData::InvokeClosedLoopEnabledCallback(HAL_Value value) {
InvokeCallback(m_closedLoopEnabledCallbacks, "ClosedLoopEnabled", &value);
}
HAL_Bool PCMData::GetClosedLoopEnabled() { return m_closedLoopEnabled; }
void PCMData::SetClosedLoopEnabled(HAL_Bool closedLoopEnabled) {
HAL_Bool oldValue = m_closedLoopEnabled.exchange(closedLoopEnabled);
if (oldValue != closedLoopEnabled) {
InvokeClosedLoopEnabledCallback(MakeBoolean(closedLoopEnabled));
}
}
int32_t PCMData::RegisterPressureSwitchCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_pressureSwitchCallbacks = RegisterCallback(
m_pressureSwitchCallbacks, "PressureSwitch", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetPressureSwitch());
callback("PressureSwitch", param, &value);
}
return newUid;
}
void PCMData::CancelPressureSwitchCallback(int32_t uid) {
m_pressureSwitchCallbacks = CancelCallback(m_pressureSwitchCallbacks, uid);
}
void PCMData::InvokePressureSwitchCallback(HAL_Value value) {
InvokeCallback(m_pressureSwitchCallbacks, "PressureSwitch", &value);
}
HAL_Bool PCMData::GetPressureSwitch() { return m_pressureSwitch; }
void PCMData::SetPressureSwitch(HAL_Bool pressureSwitch) {
HAL_Bool oldValue = m_pressureSwitch.exchange(pressureSwitch);
if (oldValue != pressureSwitch) {
InvokePressureSwitchCallback(MakeBoolean(pressureSwitch));
}
}
int32_t PCMData::RegisterCompressorCurrentCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_compressorCurrentCallbacks =
RegisterCallback(m_compressorCurrentCallbacks, "CompressorCurrent",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetCompressorCurrent());
callback("CompressorCurrent", param, &value);
}
return newUid;
}
void PCMData::CancelCompressorCurrentCallback(int32_t uid) {
m_compressorCurrentCallbacks =
CancelCallback(m_compressorCurrentCallbacks, uid);
}
void PCMData::InvokeCompressorCurrentCallback(HAL_Value value) {
InvokeCallback(m_compressorCurrentCallbacks, "CompressorCurrent", &value);
}
double PCMData::GetCompressorCurrent() { return m_compressorCurrent; }
void PCMData::SetCompressorCurrent(double compressorCurrent) {
double oldValue = m_compressorCurrent.exchange(compressorCurrent);
if (oldValue != compressorCurrent) {
InvokeCompressorCurrentCallback(MakeDouble(compressorCurrent));
}
}
extern "C" {
void HALSIM_ResetPCMData(int32_t index) { SimPCMData[index].ResetData(); }
int32_t HALSIM_RegisterPCMSolenoidInitializedCallback(
int32_t index, int32_t channel, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimPCMData[index].RegisterSolenoidInitializedCallback(
channel, callback, param, initialNotify);
}
void HALSIM_CancelPCMSolenoidInitializedCallback(int32_t index, int32_t channel,
int32_t uid) {
SimPCMData[index].CancelSolenoidInitializedCallback(channel, uid);
}
HAL_Bool HALSIM_GetPCMSolenoidInitialized(int32_t index, int32_t channel) {
return SimPCMData[index].GetSolenoidInitialized(channel);
}
void HALSIM_SetPCMSolenoidInitialized(int32_t index, int32_t channel,
HAL_Bool solenoidInitialized) {
SimPCMData[index].SetSolenoidInitialized(channel, solenoidInitialized);
}
int32_t HALSIM_RegisterPCMSolenoidOutputCallback(int32_t index, int32_t channel,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPCMData[index].RegisterSolenoidOutputCallback(channel, callback,
param, initialNotify);
}
void HALSIM_CancelPCMSolenoidOutputCallback(int32_t index, int32_t channel,
int32_t uid) {
SimPCMData[index].CancelSolenoidOutputCallback(channel, uid);
}
HAL_Bool HALSIM_GetPCMSolenoidOutput(int32_t index, int32_t channel) {
return SimPCMData[index].GetSolenoidOutput(channel);
}
void HALSIM_SetPCMSolenoidOutput(int32_t index, int32_t channel,
HAL_Bool solenoidOutput) {
SimPCMData[index].SetSolenoidOutput(channel, solenoidOutput);
}
int32_t HALSIM_RegisterPCMCompressorInitializedCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimPCMData[index].RegisterCompressorInitializedCallback(
callback, param, initialNotify);
}
void HALSIM_CancelPCMCompressorInitializedCallback(int32_t index, int32_t uid) {
SimPCMData[index].CancelCompressorInitializedCallback(uid);
}
HAL_Bool HALSIM_GetPCMCompressorInitialized(int32_t index) {
return SimPCMData[index].GetCompressorInitialized();
}
void HALSIM_SetPCMCompressorInitialized(int32_t index,
HAL_Bool compressorInitialized) {
SimPCMData[index].SetCompressorInitialized(compressorInitialized);
}
int32_t HALSIM_RegisterPCMCompressorOnCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPCMData[index].RegisterCompressorOnCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPCMCompressorOnCallback(int32_t index, int32_t uid) {
SimPCMData[index].CancelCompressorOnCallback(uid);
}
HAL_Bool HALSIM_GetPCMCompressorOn(int32_t index) {
return SimPCMData[index].GetCompressorOn();
}
void HALSIM_SetPCMCompressorOn(int32_t index, HAL_Bool compressorOn) {
SimPCMData[index].SetCompressorOn(compressorOn);
}
int32_t HALSIM_RegisterPCMClosedLoopEnabledCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPCMData[index].RegisterClosedLoopEnabledCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPCMClosedLoopEnabledCallback(int32_t index, int32_t uid) {
SimPCMData[index].CancelClosedLoopEnabledCallback(uid);
}
HAL_Bool HALSIM_GetPCMClosedLoopEnabled(int32_t index) {
return SimPCMData[index].GetClosedLoopEnabled();
}
void HALSIM_SetPCMClosedLoopEnabled(int32_t index, HAL_Bool closedLoopEnabled) {
SimPCMData[index].SetClosedLoopEnabled(closedLoopEnabled);
}
int32_t HALSIM_RegisterPCMPressureSwitchCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPCMData[index].RegisterPressureSwitchCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPCMPressureSwitchCallback(int32_t index, int32_t uid) {
SimPCMData[index].CancelPressureSwitchCallback(uid);
}
HAL_Bool HALSIM_GetPCMPressureSwitch(int32_t index) {
return SimPCMData[index].GetPressureSwitch();
}
void HALSIM_SetPCMPressureSwitch(int32_t index, HAL_Bool pressureSwitch) {
SimPCMData[index].SetPressureSwitch(pressureSwitch);
}
int32_t HALSIM_RegisterPCMCompressorCurrentCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPCMData[index].RegisterCompressorCurrentCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPCMCompressorCurrentCallback(int32_t index, int32_t uid) {
SimPCMData[index].CancelCompressorCurrentCallback(uid);
}
double HALSIM_GetPCMCompressorCurrent(int32_t index) {
return SimPCMData[index].GetCompressorCurrent();
}
void HALSIM_SetPCMCompressorCurrent(int32_t index, double compressorCurrent) {
SimPCMData[index].SetCompressorCurrent(compressorCurrent);
}
}

View File

@@ -0,0 +1,98 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "../PortsInternal.h"
#include "MockData/NotifyListenerVector.h"
#include "MockData/PCMData.h"
namespace hal {
class PCMData {
public:
int32_t RegisterSolenoidInitializedCallback(int32_t channel,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelSolenoidInitializedCallback(int32_t channel, int32_t uid);
void InvokeSolenoidInitializedCallback(int32_t channel, HAL_Value value);
HAL_Bool GetSolenoidInitialized(int32_t channel);
void SetSolenoidInitialized(int32_t channel, HAL_Bool solenoidInitialized);
int32_t RegisterSolenoidOutputCallback(int32_t channel,
HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelSolenoidOutputCallback(int32_t channel, int32_t uid);
void InvokeSolenoidOutputCallback(int32_t channel, HAL_Value value);
HAL_Bool GetSolenoidOutput(int32_t channel);
void SetSolenoidOutput(int32_t channel, HAL_Bool solenoidOutput);
int32_t RegisterCompressorInitializedCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelCompressorInitializedCallback(int32_t uid);
void InvokeCompressorInitializedCallback(HAL_Value value);
HAL_Bool GetCompressorInitialized();
void SetCompressorInitialized(HAL_Bool compressorInitialized);
int32_t RegisterCompressorOnCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelCompressorOnCallback(int32_t uid);
void InvokeCompressorOnCallback(HAL_Value value);
HAL_Bool GetCompressorOn();
void SetCompressorOn(HAL_Bool compressorOn);
int32_t RegisterClosedLoopEnabledCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelClosedLoopEnabledCallback(int32_t uid);
void InvokeClosedLoopEnabledCallback(HAL_Value value);
HAL_Bool GetClosedLoopEnabled();
void SetClosedLoopEnabled(HAL_Bool closedLoopEnabled);
int32_t RegisterPressureSwitchCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelPressureSwitchCallback(int32_t uid);
void InvokePressureSwitchCallback(HAL_Value value);
HAL_Bool GetPressureSwitch();
void SetPressureSwitch(HAL_Bool pressureSwitch);
int32_t RegisterCompressorCurrentCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelCompressorCurrentCallback(int32_t uid);
void InvokeCompressorCurrentCallback(HAL_Value value);
double GetCompressorCurrent();
void SetCompressorCurrent(double compressorCurrent);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_solenoidInitialized[kNumSolenoidChannels];
std::shared_ptr<NotifyListenerVector>
m_solenoidInitializedCallbacks[kNumSolenoidChannels];
std::atomic<HAL_Bool> m_solenoidOutput[kNumSolenoidChannels];
std::shared_ptr<NotifyListenerVector>
m_solenoidOutputCallbacks[kNumSolenoidChannels];
std::atomic<HAL_Bool> m_compressorInitialized{false};
std::shared_ptr<NotifyListenerVector> m_compressorInitializedCallbacks =
nullptr;
std::atomic<HAL_Bool> m_compressorOn{false};
std::shared_ptr<NotifyListenerVector> m_compressorOnCallbacks = nullptr;
std::atomic<HAL_Bool> m_closedLoopEnabled{true};
std::shared_ptr<NotifyListenerVector> m_closedLoopEnabledCallbacks = nullptr;
std::atomic<HAL_Bool> m_pressureSwitch{false};
std::shared_ptr<NotifyListenerVector> m_pressureSwitchCallbacks = nullptr;
std::atomic<double> m_compressorCurrent{0.0};
std::shared_ptr<NotifyListenerVector> m_compressorCurrentCallbacks = nullptr;
};
extern PCMData SimPCMData[];
} // namespace hal

View File

@@ -0,0 +1,253 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "NotifyCallbackHelpers.h"
#include "PDPDataInternal.h"
using namespace hal;
PDPData hal::SimPDPData[kNumPDPModules];
void PDPData::ResetData() {
m_initialized = false;
m_initializedCallbacks = nullptr;
m_temperature = 0.0;
m_temperatureCallbacks = nullptr;
m_voltage = 12.0;
m_voltageCallbacks = nullptr;
for (int i = 0; i < kNumPDPChannels; i++) {
m_current[i] = 0;
m_currentCallbacks[i] = nullptr;
}
}
int32_t PDPData::RegisterInitializedCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_initializedCallbacks = RegisterCallback(
m_initializedCallbacks, "Initialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetInitialized());
callback("Initialized", param, &value);
}
return newUid;
}
void PDPData::CancelInitializedCallback(int32_t uid) {
m_initializedCallbacks = CancelCallback(m_initializedCallbacks, uid);
}
void PDPData::InvokeInitializedCallback(HAL_Value value) {
InvokeCallback(m_initializedCallbacks, "Initialized", &value);
}
HAL_Bool PDPData::GetInitialized() { return m_initialized; }
void PDPData::SetInitialized(HAL_Bool initialized) {
HAL_Bool oldValue = m_initialized.exchange(initialized);
if (oldValue != initialized) {
InvokeInitializedCallback(MakeBoolean(initialized));
}
}
int32_t PDPData::RegisterTemperatureCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_temperatureCallbacks = RegisterCallback(
m_temperatureCallbacks, "Temperature", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetTemperature());
callback("Temperature", param, &value);
}
return newUid;
}
void PDPData::CancelTemperatureCallback(int32_t uid) {
m_temperatureCallbacks = CancelCallback(m_temperatureCallbacks, uid);
}
void PDPData::InvokeTemperatureCallback(HAL_Value value) {
InvokeCallback(m_temperatureCallbacks, "Temperature", &value);
}
double PDPData::GetTemperature() { return m_temperature; }
void PDPData::SetTemperature(double temperature) {
double oldValue = m_temperature.exchange(temperature);
if (oldValue != temperature) {
InvokeTemperatureCallback(MakeDouble(temperature));
}
}
int32_t PDPData::RegisterVoltageCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_voltageCallbacks = RegisterCallback(m_voltageCallbacks, "Voltage",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetVoltage());
callback("Voltage", param, &value);
}
return newUid;
}
void PDPData::CancelVoltageCallback(int32_t uid) {
m_voltageCallbacks = CancelCallback(m_voltageCallbacks, uid);
}
void PDPData::InvokeVoltageCallback(HAL_Value value) {
InvokeCallback(m_voltageCallbacks, "Voltage", &value);
}
double PDPData::GetVoltage() { return m_voltage; }
void PDPData::SetVoltage(double voltage) {
double oldValue = m_voltage.exchange(voltage);
if (oldValue != voltage) {
InvokeVoltageCallback(MakeDouble(voltage));
}
}
int32_t PDPData::RegisterCurrentCallback(int32_t channel,
HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_currentCallbacks[channel] = RegisterCallback(
m_currentCallbacks[channel], "Current", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetCurrent(channel));
callback("Current", param, &value);
}
return newUid;
}
void PDPData::CancelCurrentCallback(int32_t channel, int32_t uid) {
m_currentCallbacks[channel] =
CancelCallback(m_currentCallbacks[channel], uid);
}
void PDPData::InvokeCurrentCallback(int32_t channel, HAL_Value value) {
InvokeCallback(m_currentCallbacks[channel], "Current", &value);
}
double PDPData::GetCurrent(int32_t channel) { return m_current[channel]; }
void PDPData::SetCurrent(int32_t channel, double current) {
double oldValue = m_current[channel].exchange(current);
if (oldValue != current) {
InvokeCurrentCallback(channel, MakeDouble(current));
}
}
extern "C" {
void HALSIM_ResetPDPData(int32_t index) { SimPDPData[index].ResetData(); }
int32_t HALSIM_RegisterPDPInitializedCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPDPData[index].RegisterInitializedCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPDPInitializedCallback(int32_t index, int32_t uid) {
SimPDPData[index].CancelInitializedCallback(uid);
}
HAL_Bool HALSIM_GetPDPInitialized(int32_t index) {
return SimPDPData[index].GetInitialized();
}
void HALSIM_SetPDPInitialized(int32_t index, HAL_Bool initialized) {
SimPDPData[index].SetInitialized(initialized);
}
int32_t HALSIM_RegisterPDPTemperatureCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPDPData[index].RegisterTemperatureCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPDPTemperatureCallback(int32_t index, int32_t uid) {
SimPDPData[index].CancelTemperatureCallback(uid);
}
double HALSIM_GetPDPTemperature(int32_t index) {
return SimPDPData[index].GetTemperature();
}
void HALSIM_SetPDPTemperature(int32_t index, double temperature) {
SimPDPData[index].SetTemperature(temperature);
}
int32_t HALSIM_RegisterPDPVoltageCallback(int32_t index,
HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {
return SimPDPData[index].RegisterVoltageCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPDPVoltageCallback(int32_t index, int32_t uid) {
SimPDPData[index].CancelVoltageCallback(uid);
}
double HALSIM_GetPDPVoltage(int32_t index) {
return SimPDPData[index].GetVoltage();
}
void HALSIM_SetPDPVoltage(int32_t index, double voltage) {
SimPDPData[index].SetVoltage(voltage);
}
int32_t HALSIM_RegisterPDPCurrentCallback(int32_t index, int32_t channel,
HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {
return SimPDPData[index].RegisterCurrentCallback(channel, callback, param,
initialNotify);
}
void HALSIM_CancelPDPCurrentCallback(int32_t index, int32_t channel,
int32_t uid) {
SimPDPData[index].CancelCurrentCallback(channel, uid);
}
double HALSIM_GetPDPCurrent(int32_t index, int32_t channel) {
return SimPDPData[index].GetCurrent(channel);
}
void HALSIM_SetPDPCurrent(int32_t index, int32_t channel, double current) {
SimPDPData[index].SetCurrent(channel, current);
}
}

View File

@@ -0,0 +1,62 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "../PortsInternal.h"
#include "MockData/NotifyListenerVector.h"
#include "MockData/PDPData.h"
namespace hal {
class PDPData {
public:
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelInitializedCallback(int32_t uid);
void InvokeInitializedCallback(HAL_Value value);
HAL_Bool GetInitialized();
void SetInitialized(HAL_Bool initialized);
int32_t RegisterTemperatureCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelTemperatureCallback(int32_t uid);
void InvokeTemperatureCallback(HAL_Value value);
double GetTemperature();
void SetTemperature(double temperature);
int32_t RegisterVoltageCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelVoltageCallback(int32_t uid);
void InvokeVoltageCallback(HAL_Value value);
double GetVoltage();
void SetVoltage(double voltage);
int32_t RegisterCurrentCallback(int32_t channel, HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelCurrentCallback(int32_t channel, int32_t uid);
void InvokeCurrentCallback(int32_t channel, HAL_Value value);
double GetCurrent(int32_t channel);
void SetCurrent(int32_t channel, double current);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_initialized{false};
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
std::atomic<double> m_temperature{0.0};
std::shared_ptr<NotifyListenerVector> m_temperatureCallbacks = nullptr;
std::atomic<double> m_voltage{12.0};
std::shared_ptr<NotifyListenerVector> m_voltageCallbacks = nullptr;
std::atomic<double> m_current[kNumPDPChannels];
std::shared_ptr<NotifyListenerVector> m_currentCallbacks[kNumPDPChannels];
};
extern PDPData SimPDPData[];
} // namespace hal

View File

@@ -0,0 +1,364 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "NotifyCallbackHelpers.h"
#include "PWMDataInternal.h"
using namespace hal;
PWMData hal::SimPWMData[kNumPWMChannels];
void PWMData::ResetData() {
m_initialized = false;
m_initializedCallbacks = nullptr;
m_rawValue = 0;
m_rawValueCallbacks = nullptr;
m_speed = 0;
m_speedCallbacks = nullptr;
m_position = 0;
m_positionCallbacks = nullptr;
m_periodScale = 0;
m_periodScaleCallbacks = nullptr;
m_zeroLatch = false;
m_zeroLatchCallbacks = nullptr;
}
int32_t PWMData::RegisterInitializedCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_initializedCallbacks = RegisterCallback(
m_initializedCallbacks, "Initialized", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetInitialized());
callback("Initialized", param, &value);
}
return newUid;
}
void PWMData::CancelInitializedCallback(int32_t uid) {
m_initializedCallbacks = CancelCallback(m_initializedCallbacks, uid);
}
void PWMData::InvokeInitializedCallback(HAL_Value value) {
InvokeCallback(m_initializedCallbacks, "Initialized", &value);
}
HAL_Bool PWMData::GetInitialized() { return m_initialized; }
void PWMData::SetInitialized(HAL_Bool initialized) {
HAL_Bool oldValue = m_initialized.exchange(initialized);
if (oldValue != initialized) {
InvokeInitializedCallback(MakeBoolean(initialized));
}
}
int32_t PWMData::RegisterRawValueCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_rawValueCallbacks = RegisterCallback(m_rawValueCallbacks, "RawValue",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetRawValue());
callback("RawValue", param, &value);
}
return newUid;
}
void PWMData::CancelRawValueCallback(int32_t uid) {
m_rawValueCallbacks = CancelCallback(m_rawValueCallbacks, uid);
}
void PWMData::InvokeRawValueCallback(HAL_Value value) {
InvokeCallback(m_rawValueCallbacks, "RawValue", &value);
}
int32_t PWMData::GetRawValue() { return m_rawValue; }
void PWMData::SetRawValue(int32_t rawValue) {
int32_t oldValue = m_rawValue.exchange(rawValue);
if (oldValue != rawValue) {
InvokeRawValueCallback(MakeInt(rawValue));
}
}
int32_t PWMData::RegisterSpeedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_speedCallbacks =
RegisterCallback(m_speedCallbacks, "Speed", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetSpeed());
callback("Speed", param, &value);
}
return newUid;
}
void PWMData::CancelSpeedCallback(int32_t uid) {
m_speedCallbacks = CancelCallback(m_speedCallbacks, uid);
}
void PWMData::InvokeSpeedCallback(HAL_Value value) {
InvokeCallback(m_speedCallbacks, "Speed", &value);
}
double PWMData::GetSpeed() { return m_speed; }
void PWMData::SetSpeed(double speed) {
double oldValue = m_speed.exchange(speed);
if (oldValue != speed) {
InvokeSpeedCallback(MakeDouble(speed));
}
}
int32_t PWMData::RegisterPositionCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_positionCallbacks = RegisterCallback(m_positionCallbacks, "Position",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetPosition());
callback("Position", param, &value);
}
return newUid;
}
void PWMData::CancelPositionCallback(int32_t uid) {
m_positionCallbacks = CancelCallback(m_positionCallbacks, uid);
}
void PWMData::InvokePositionCallback(HAL_Value value) {
InvokeCallback(m_positionCallbacks, "Position", &value);
}
double PWMData::GetPosition() { return m_position; }
void PWMData::SetPosition(double position) {
double oldValue = m_position.exchange(position);
if (oldValue != position) {
InvokePositionCallback(MakeDouble(position));
}
}
int32_t PWMData::RegisterPeriodScaleCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_periodScaleCallbacks = RegisterCallback(
m_periodScaleCallbacks, "PeriodScale", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetPeriodScale());
callback("PeriodScale", param, &value);
}
return newUid;
}
void PWMData::CancelPeriodScaleCallback(int32_t uid) {
m_periodScaleCallbacks = CancelCallback(m_periodScaleCallbacks, uid);
}
void PWMData::InvokePeriodScaleCallback(HAL_Value value) {
InvokeCallback(m_periodScaleCallbacks, "PeriodScale", &value);
}
int32_t PWMData::GetPeriodScale() { return m_periodScale; }
void PWMData::SetPeriodScale(int32_t periodScale) {
int32_t oldValue = m_periodScale.exchange(periodScale);
if (oldValue != periodScale) {
InvokePeriodScaleCallback(MakeInt(periodScale));
}
}
int32_t PWMData::RegisterZeroLatchCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_zeroLatchCallbacks = RegisterCallback(m_zeroLatchCallbacks, "ZeroLatch",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetZeroLatch());
callback("ZeroLatch", param, &value);
}
return newUid;
}
void PWMData::CancelZeroLatchCallback(int32_t uid) {
m_zeroLatchCallbacks = CancelCallback(m_zeroLatchCallbacks, uid);
}
void PWMData::InvokeZeroLatchCallback(HAL_Value value) {
InvokeCallback(m_zeroLatchCallbacks, "ZeroLatch", &value);
}
HAL_Bool PWMData::GetZeroLatch() { return m_zeroLatch; }
void PWMData::SetZeroLatch(HAL_Bool zeroLatch) {
HAL_Bool oldValue = m_zeroLatch.exchange(zeroLatch);
if (oldValue != zeroLatch) {
InvokeZeroLatchCallback(MakeBoolean(zeroLatch));
}
}
extern "C" {
void HALSIM_ResetPWMData(int32_t index) { SimPWMData[index].ResetData(); }
int32_t HALSIM_RegisterPWMInitializedCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPWMData[index].RegisterInitializedCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPWMInitializedCallback(int32_t index, int32_t uid) {
SimPWMData[index].CancelInitializedCallback(uid);
}
HAL_Bool HALSIM_GetPWMInitialized(int32_t index) {
return SimPWMData[index].GetInitialized();
}
void HALSIM_SetPWMInitialized(int32_t index, HAL_Bool initialized) {
SimPWMData[index].SetInitialized(initialized);
}
int32_t HALSIM_RegisterPWMRawValueCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPWMData[index].RegisterRawValueCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPWMRawValueCallback(int32_t index, int32_t uid) {
SimPWMData[index].CancelRawValueCallback(uid);
}
int32_t HALSIM_GetPWMRawValue(int32_t index) {
return SimPWMData[index].GetRawValue();
}
void HALSIM_SetPWMRawValue(int32_t index, int32_t rawValue) {
SimPWMData[index].SetRawValue(rawValue);
}
int32_t HALSIM_RegisterPWMSpeedCallback(int32_t index,
HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {
return SimPWMData[index].RegisterSpeedCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPWMSpeedCallback(int32_t index, int32_t uid) {
SimPWMData[index].CancelSpeedCallback(uid);
}
double HALSIM_GetPWMSpeed(int32_t index) {
return SimPWMData[index].GetSpeed();
}
void HALSIM_SetPWMSpeed(int32_t index, double speed) {
SimPWMData[index].SetSpeed(speed);
}
int32_t HALSIM_RegisterPWMPositionCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPWMData[index].RegisterPositionCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPWMPositionCallback(int32_t index, int32_t uid) {
SimPWMData[index].CancelPositionCallback(uid);
}
double HALSIM_GetPWMPosition(int32_t index) {
return SimPWMData[index].GetPosition();
}
void HALSIM_SetPWMPosition(int32_t index, double position) {
SimPWMData[index].SetPosition(position);
}
int32_t HALSIM_RegisterPWMPeriodScaleCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPWMData[index].RegisterPeriodScaleCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPWMPeriodScaleCallback(int32_t index, int32_t uid) {
SimPWMData[index].CancelPeriodScaleCallback(uid);
}
int32_t HALSIM_GetPWMPeriodScale(int32_t index) {
return SimPWMData[index].GetPeriodScale();
}
void HALSIM_SetPWMPeriodScale(int32_t index, int32_t periodScale) {
SimPWMData[index].SetPeriodScale(periodScale);
}
int32_t HALSIM_RegisterPWMZeroLatchCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimPWMData[index].RegisterZeroLatchCallback(callback, param,
initialNotify);
}
void HALSIM_CancelPWMZeroLatchCallback(int32_t index, int32_t uid) {
SimPWMData[index].CancelZeroLatchCallback(uid);
}
HAL_Bool HALSIM_GetPWMZeroLatch(int32_t index) {
return SimPWMData[index].GetZeroLatch();
}
void HALSIM_SetPWMZeroLatch(int32_t index, HAL_Bool zeroLatch) {
SimPWMData[index].SetZeroLatch(zeroLatch);
}
}

View File

@@ -0,0 +1,79 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/NotifyListenerVector.h"
#include "MockData/PWMData.h"
namespace hal {
class PWMData {
public:
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelInitializedCallback(int32_t uid);
void InvokeInitializedCallback(HAL_Value value);
HAL_Bool GetInitialized();
void SetInitialized(HAL_Bool initialized);
int32_t RegisterRawValueCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelRawValueCallback(int32_t uid);
void InvokeRawValueCallback(HAL_Value value);
int32_t GetRawValue();
void SetRawValue(int32_t rawValue);
int32_t RegisterSpeedCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelSpeedCallback(int32_t uid);
void InvokeSpeedCallback(HAL_Value value);
double GetSpeed();
void SetSpeed(double speed);
int32_t RegisterPositionCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelPositionCallback(int32_t uid);
void InvokePositionCallback(HAL_Value value);
double GetPosition();
void SetPosition(double position);
int32_t RegisterPeriodScaleCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelPeriodScaleCallback(int32_t uid);
void InvokePeriodScaleCallback(HAL_Value value);
int32_t GetPeriodScale();
void SetPeriodScale(int32_t periodScale);
int32_t RegisterZeroLatchCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelZeroLatchCallback(int32_t uid);
void InvokeZeroLatchCallback(HAL_Value value);
HAL_Bool GetZeroLatch();
void SetZeroLatch(HAL_Bool zeroLatch);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_initialized{false};
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
std::atomic<int32_t> m_rawValue{0};
std::shared_ptr<NotifyListenerVector> m_rawValueCallbacks = nullptr;
std::atomic<double> m_speed{0};
std::shared_ptr<NotifyListenerVector> m_speedCallbacks = nullptr;
std::atomic<double> m_position{0};
std::shared_ptr<NotifyListenerVector> m_positionCallbacks = nullptr;
std::atomic<int32_t> m_periodScale{0};
std::shared_ptr<NotifyListenerVector> m_periodScaleCallbacks = nullptr;
std::atomic<HAL_Bool> m_zeroLatch{false};
std::shared_ptr<NotifyListenerVector> m_zeroLatchCallbacks = nullptr;
};
extern PWMData SimPWMData[];
} // namespace hal

View File

@@ -0,0 +1,254 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "NotifyCallbackHelpers.h"
#include "RelayDataInternal.h"
using namespace hal;
RelayData hal::SimRelayData[kNumRelayHeaders];
void RelayData::ResetData() {
m_initializedForward = false;
m_initializedForwardCallbacks = nullptr;
m_initializedReverse = false;
m_initializedReverseCallbacks = nullptr;
m_forward = false;
m_forwardCallbacks = nullptr;
m_reverse = false;
m_reverseCallbacks = nullptr;
}
int32_t RelayData::RegisterInitializedForwardCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_initializedForwardCallbacks =
RegisterCallback(m_initializedForwardCallbacks, "InitializedForward",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetInitializedForward());
callback("InitializedForward", param, &value);
}
return newUid;
}
void RelayData::CancelInitializedForwardCallback(int32_t uid) {
m_initializedForwardCallbacks =
CancelCallback(m_initializedForwardCallbacks, uid);
}
void RelayData::InvokeInitializedForwardCallback(HAL_Value value) {
InvokeCallback(m_initializedForwardCallbacks, "InitializedForward", &value);
}
HAL_Bool RelayData::GetInitializedForward() { return m_initializedForward; }
void RelayData::SetInitializedForward(HAL_Bool initializedForward) {
HAL_Bool oldValue = m_initializedForward.exchange(initializedForward);
if (oldValue != initializedForward) {
InvokeInitializedForwardCallback(MakeBoolean(initializedForward));
}
}
int32_t RelayData::RegisterInitializedReverseCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_initializedReverseCallbacks =
RegisterCallback(m_initializedReverseCallbacks, "InitializedReverse",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetInitializedReverse());
callback("InitializedReverse", param, &value);
}
return newUid;
}
void RelayData::CancelInitializedReverseCallback(int32_t uid) {
m_initializedReverseCallbacks =
CancelCallback(m_initializedReverseCallbacks, uid);
}
void RelayData::InvokeInitializedReverseCallback(HAL_Value value) {
InvokeCallback(m_initializedReverseCallbacks, "InitializedReverse", &value);
}
HAL_Bool RelayData::GetInitializedReverse() { return m_initializedReverse; }
void RelayData::SetInitializedReverse(HAL_Bool initializedReverse) {
HAL_Bool oldValue = m_initializedReverse.exchange(initializedReverse);
if (oldValue != initializedReverse) {
InvokeInitializedReverseCallback(MakeBoolean(initializedReverse));
}
}
int32_t RelayData::RegisterForwardCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_forwardCallbacks = RegisterCallback(m_forwardCallbacks, "Forward",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetForward());
callback("Forward", param, &value);
}
return newUid;
}
void RelayData::CancelForwardCallback(int32_t uid) {
m_forwardCallbacks = CancelCallback(m_forwardCallbacks, uid);
}
void RelayData::InvokeForwardCallback(HAL_Value value) {
InvokeCallback(m_forwardCallbacks, "Forward", &value);
}
HAL_Bool RelayData::GetForward() { return m_forward; }
void RelayData::SetForward(HAL_Bool forward) {
HAL_Bool oldValue = m_forward.exchange(forward);
if (oldValue != forward) {
InvokeForwardCallback(MakeBoolean(forward));
}
}
int32_t RelayData::RegisterReverseCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_reverseCallbacks = RegisterCallback(m_reverseCallbacks, "Reverse",
callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetReverse());
callback("Reverse", param, &value);
}
return newUid;
}
void RelayData::CancelReverseCallback(int32_t uid) {
m_reverseCallbacks = CancelCallback(m_reverseCallbacks, uid);
}
void RelayData::InvokeReverseCallback(HAL_Value value) {
InvokeCallback(m_reverseCallbacks, "Reverse", &value);
}
HAL_Bool RelayData::GetReverse() { return m_reverse; }
void RelayData::SetReverse(HAL_Bool reverse) {
HAL_Bool oldValue = m_reverse.exchange(reverse);
if (oldValue != reverse) {
InvokeReverseCallback(MakeBoolean(reverse));
}
}
extern "C" {
void HALSIM_ResetRelayData(int32_t index) { SimRelayData[index].ResetData(); }
int32_t HALSIM_RegisterRelayInitializedForwardCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimRelayData[index].RegisterInitializedForwardCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRelayInitializedForwardCallback(int32_t index, int32_t uid) {
SimRelayData[index].CancelInitializedForwardCallback(uid);
}
HAL_Bool HALSIM_GetRelayInitializedForward(int32_t index) {
return SimRelayData[index].GetInitializedForward();
}
void HALSIM_SetRelayInitializedForward(int32_t index,
HAL_Bool initializedForward) {
SimRelayData[index].SetInitializedForward(initializedForward);
}
int32_t HALSIM_RegisterRelayInitializedReverseCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimRelayData[index].RegisterInitializedReverseCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRelayInitializedReverseCallback(int32_t index, int32_t uid) {
SimRelayData[index].CancelInitializedReverseCallback(uid);
}
HAL_Bool HALSIM_GetRelayInitializedReverse(int32_t index) {
return SimRelayData[index].GetInitializedReverse();
}
void HALSIM_SetRelayInitializedReverse(int32_t index,
HAL_Bool initializedReverse) {
SimRelayData[index].SetInitializedReverse(initializedReverse);
}
int32_t HALSIM_RegisterRelayForwardCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRelayData[index].RegisterForwardCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRelayForwardCallback(int32_t index, int32_t uid) {
SimRelayData[index].CancelForwardCallback(uid);
}
HAL_Bool HALSIM_GetRelayForward(int32_t index) {
return SimRelayData[index].GetForward();
}
void HALSIM_SetRelayForward(int32_t index, HAL_Bool forward) {
SimRelayData[index].SetForward(forward);
}
int32_t HALSIM_RegisterRelayReverseCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRelayData[index].RegisterReverseCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRelayReverseCallback(int32_t index, int32_t uid) {
SimRelayData[index].CancelReverseCallback(uid);
}
HAL_Bool HALSIM_GetRelayReverse(int32_t index) {
return SimRelayData[index].GetReverse();
}
void HALSIM_SetRelayReverse(int32_t index, HAL_Bool reverse) {
SimRelayData[index].SetReverse(reverse);
}
}

View File

@@ -0,0 +1,63 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/NotifyListenerVector.h"
#include "MockData/RelayData.h"
namespace hal {
class RelayData {
public:
int32_t RegisterInitializedForwardCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelInitializedForwardCallback(int32_t uid);
void InvokeInitializedForwardCallback(HAL_Value value);
HAL_Bool GetInitializedForward();
void SetInitializedForward(HAL_Bool initializedForward);
int32_t RegisterInitializedReverseCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void CancelInitializedReverseCallback(int32_t uid);
void InvokeInitializedReverseCallback(HAL_Value value);
HAL_Bool GetInitializedReverse();
void SetInitializedReverse(HAL_Bool initializedReverse);
int32_t RegisterForwardCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelForwardCallback(int32_t uid);
void InvokeForwardCallback(HAL_Value value);
HAL_Bool GetForward();
void SetForward(HAL_Bool forward);
int32_t RegisterReverseCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelReverseCallback(int32_t uid);
void InvokeReverseCallback(HAL_Value value);
HAL_Bool GetReverse();
void SetReverse(HAL_Bool reverse);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_initializedForward{false};
std::shared_ptr<NotifyListenerVector> m_initializedForwardCallbacks = nullptr;
std::atomic<HAL_Bool> m_initializedReverse{false};
std::shared_ptr<NotifyListenerVector> m_initializedReverseCallbacks = nullptr;
std::atomic<HAL_Bool> m_forward{false};
std::shared_ptr<NotifyListenerVector> m_forwardCallbacks = nullptr;
std::atomic<HAL_Bool> m_reverse{false};
std::shared_ptr<NotifyListenerVector> m_reverseCallbacks = nullptr;
};
extern RelayData SimRelayData[];
} // namespace hal

View File

@@ -0,0 +1,890 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "NotifyCallbackHelpers.h"
#include "RoboRioDataInternal.h"
using namespace hal;
RoboRioData hal::SimRoboRioData[1];
void RoboRioData::ResetData() {
m_fPGAButton = false;
m_fPGAButtonCallbacks = nullptr;
m_vInVoltage = 0.0;
m_vInVoltageCallbacks = nullptr;
m_vInCurrent = 0.0;
m_vInCurrentCallbacks = nullptr;
m_userVoltage6V = 6.0;
m_userVoltage6VCallbacks = nullptr;
m_userCurrent6V = 0.0;
m_userCurrent6VCallbacks = nullptr;
m_userActive6V = false;
m_userActive6VCallbacks = nullptr;
m_userVoltage5V = 5.0;
m_userVoltage5VCallbacks = nullptr;
m_userCurrent5V = 0.0;
m_userCurrent5VCallbacks = nullptr;
m_userActive5V = false;
m_userActive5VCallbacks = nullptr;
m_userVoltage3V3 = 3.3;
m_userVoltage3V3Callbacks = nullptr;
m_userCurrent3V3 = 0.0;
m_userCurrent3V3Callbacks = nullptr;
m_userActive3V3 = false;
m_userActive3V3Callbacks = nullptr;
m_userFaults6V = 0;
m_userFaults6VCallbacks = nullptr;
m_userFaults5V = 0;
m_userFaults5VCallbacks = nullptr;
m_userFaults3V3 = 0;
m_userFaults3V3Callbacks = nullptr;
}
int32_t RoboRioData::RegisterFPGAButtonCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_fPGAButtonCallbacks = RegisterCallback(
m_fPGAButtonCallbacks, "FPGAButton", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetFPGAButton());
callback("FPGAButton", param, &value);
}
return newUid;
}
void RoboRioData::CancelFPGAButtonCallback(int32_t uid) {
m_fPGAButtonCallbacks = CancelCallback(m_fPGAButtonCallbacks, uid);
}
void RoboRioData::InvokeFPGAButtonCallback(HAL_Value value) {
InvokeCallback(m_fPGAButtonCallbacks, "FPGAButton", &value);
}
HAL_Bool RoboRioData::GetFPGAButton() { return m_fPGAButton; }
void RoboRioData::SetFPGAButton(HAL_Bool fPGAButton) {
HAL_Bool oldValue = m_fPGAButton.exchange(fPGAButton);
if (oldValue != fPGAButton) {
InvokeFPGAButtonCallback(MakeBoolean(fPGAButton));
}
}
int32_t RoboRioData::RegisterVInVoltageCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_vInVoltageCallbacks = RegisterCallback(
m_vInVoltageCallbacks, "VInVoltage", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetVInVoltage());
callback("VInVoltage", param, &value);
}
return newUid;
}
void RoboRioData::CancelVInVoltageCallback(int32_t uid) {
m_vInVoltageCallbacks = CancelCallback(m_vInVoltageCallbacks, uid);
}
void RoboRioData::InvokeVInVoltageCallback(HAL_Value value) {
InvokeCallback(m_vInVoltageCallbacks, "VInVoltage", &value);
}
double RoboRioData::GetVInVoltage() { return m_vInVoltage; }
void RoboRioData::SetVInVoltage(double vInVoltage) {
double oldValue = m_vInVoltage.exchange(vInVoltage);
if (oldValue != vInVoltage) {
InvokeVInVoltageCallback(MakeDouble(vInVoltage));
}
}
int32_t RoboRioData::RegisterVInCurrentCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_vInCurrentCallbacks = RegisterCallback(
m_vInCurrentCallbacks, "VInCurrent", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetVInCurrent());
callback("VInCurrent", param, &value);
}
return newUid;
}
void RoboRioData::CancelVInCurrentCallback(int32_t uid) {
m_vInCurrentCallbacks = CancelCallback(m_vInCurrentCallbacks, uid);
}
void RoboRioData::InvokeVInCurrentCallback(HAL_Value value) {
InvokeCallback(m_vInCurrentCallbacks, "VInCurrent", &value);
}
double RoboRioData::GetVInCurrent() { return m_vInCurrent; }
void RoboRioData::SetVInCurrent(double vInCurrent) {
double oldValue = m_vInCurrent.exchange(vInCurrent);
if (oldValue != vInCurrent) {
InvokeVInCurrentCallback(MakeDouble(vInCurrent));
}
}
int32_t RoboRioData::RegisterUserVoltage6VCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userVoltage6VCallbacks = RegisterCallback(
m_userVoltage6VCallbacks, "UserVoltage6V", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetUserVoltage6V());
callback("UserVoltage6V", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserVoltage6VCallback(int32_t uid) {
m_userVoltage6VCallbacks = CancelCallback(m_userVoltage6VCallbacks, uid);
}
void RoboRioData::InvokeUserVoltage6VCallback(HAL_Value value) {
InvokeCallback(m_userVoltage6VCallbacks, "UserVoltage6V", &value);
}
double RoboRioData::GetUserVoltage6V() { return m_userVoltage6V; }
void RoboRioData::SetUserVoltage6V(double userVoltage6V) {
double oldValue = m_userVoltage6V.exchange(userVoltage6V);
if (oldValue != userVoltage6V) {
InvokeUserVoltage6VCallback(MakeDouble(userVoltage6V));
}
}
int32_t RoboRioData::RegisterUserCurrent6VCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userCurrent6VCallbacks = RegisterCallback(
m_userCurrent6VCallbacks, "UserCurrent6V", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetUserCurrent6V());
callback("UserCurrent6V", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserCurrent6VCallback(int32_t uid) {
m_userCurrent6VCallbacks = CancelCallback(m_userCurrent6VCallbacks, uid);
}
void RoboRioData::InvokeUserCurrent6VCallback(HAL_Value value) {
InvokeCallback(m_userCurrent6VCallbacks, "UserCurrent6V", &value);
}
double RoboRioData::GetUserCurrent6V() { return m_userCurrent6V; }
void RoboRioData::SetUserCurrent6V(double userCurrent6V) {
double oldValue = m_userCurrent6V.exchange(userCurrent6V);
if (oldValue != userCurrent6V) {
InvokeUserCurrent6VCallback(MakeDouble(userCurrent6V));
}
}
int32_t RoboRioData::RegisterUserActive6VCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userActive6VCallbacks = RegisterCallback(
m_userActive6VCallbacks, "UserActive6V", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetUserActive6V());
callback("UserActive6V", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserActive6VCallback(int32_t uid) {
m_userActive6VCallbacks = CancelCallback(m_userActive6VCallbacks, uid);
}
void RoboRioData::InvokeUserActive6VCallback(HAL_Value value) {
InvokeCallback(m_userActive6VCallbacks, "UserActive6V", &value);
}
HAL_Bool RoboRioData::GetUserActive6V() { return m_userActive6V; }
void RoboRioData::SetUserActive6V(HAL_Bool userActive6V) {
HAL_Bool oldValue = m_userActive6V.exchange(userActive6V);
if (oldValue != userActive6V) {
InvokeUserActive6VCallback(MakeBoolean(userActive6V));
}
}
int32_t RoboRioData::RegisterUserVoltage5VCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userVoltage5VCallbacks = RegisterCallback(
m_userVoltage5VCallbacks, "UserVoltage5V", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetUserVoltage5V());
callback("UserVoltage5V", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserVoltage5VCallback(int32_t uid) {
m_userVoltage5VCallbacks = CancelCallback(m_userVoltage5VCallbacks, uid);
}
void RoboRioData::InvokeUserVoltage5VCallback(HAL_Value value) {
InvokeCallback(m_userVoltage5VCallbacks, "UserVoltage5V", &value);
}
double RoboRioData::GetUserVoltage5V() { return m_userVoltage5V; }
void RoboRioData::SetUserVoltage5V(double userVoltage5V) {
double oldValue = m_userVoltage5V.exchange(userVoltage5V);
if (oldValue != userVoltage5V) {
InvokeUserVoltage5VCallback(MakeDouble(userVoltage5V));
}
}
int32_t RoboRioData::RegisterUserCurrent5VCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userCurrent5VCallbacks = RegisterCallback(
m_userCurrent5VCallbacks, "UserCurrent5V", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetUserCurrent5V());
callback("UserCurrent5V", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserCurrent5VCallback(int32_t uid) {
m_userCurrent5VCallbacks = CancelCallback(m_userCurrent5VCallbacks, uid);
}
void RoboRioData::InvokeUserCurrent5VCallback(HAL_Value value) {
InvokeCallback(m_userCurrent5VCallbacks, "UserCurrent5V", &value);
}
double RoboRioData::GetUserCurrent5V() { return m_userCurrent5V; }
void RoboRioData::SetUserCurrent5V(double userCurrent5V) {
double oldValue = m_userCurrent5V.exchange(userCurrent5V);
if (oldValue != userCurrent5V) {
InvokeUserCurrent5VCallback(MakeDouble(userCurrent5V));
}
}
int32_t RoboRioData::RegisterUserActive5VCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userActive5VCallbacks = RegisterCallback(
m_userActive5VCallbacks, "UserActive5V", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetUserActive5V());
callback("UserActive5V", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserActive5VCallback(int32_t uid) {
m_userActive5VCallbacks = CancelCallback(m_userActive5VCallbacks, uid);
}
void RoboRioData::InvokeUserActive5VCallback(HAL_Value value) {
InvokeCallback(m_userActive5VCallbacks, "UserActive5V", &value);
}
HAL_Bool RoboRioData::GetUserActive5V() { return m_userActive5V; }
void RoboRioData::SetUserActive5V(HAL_Bool userActive5V) {
HAL_Bool oldValue = m_userActive5V.exchange(userActive5V);
if (oldValue != userActive5V) {
InvokeUserActive5VCallback(MakeBoolean(userActive5V));
}
}
int32_t RoboRioData::RegisterUserVoltage3V3Callback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userVoltage3V3Callbacks = RegisterCallback(
m_userVoltage3V3Callbacks, "UserVoltage3V3", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetUserVoltage3V3());
callback("UserVoltage3V3", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserVoltage3V3Callback(int32_t uid) {
m_userVoltage3V3Callbacks = CancelCallback(m_userVoltage3V3Callbacks, uid);
}
void RoboRioData::InvokeUserVoltage3V3Callback(HAL_Value value) {
InvokeCallback(m_userVoltage3V3Callbacks, "UserVoltage3V3", &value);
}
double RoboRioData::GetUserVoltage3V3() { return m_userVoltage3V3; }
void RoboRioData::SetUserVoltage3V3(double userVoltage3V3) {
double oldValue = m_userVoltage3V3.exchange(userVoltage3V3);
if (oldValue != userVoltage3V3) {
InvokeUserVoltage3V3Callback(MakeDouble(userVoltage3V3));
}
}
int32_t RoboRioData::RegisterUserCurrent3V3Callback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userCurrent3V3Callbacks = RegisterCallback(
m_userCurrent3V3Callbacks, "UserCurrent3V3", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetUserCurrent3V3());
callback("UserCurrent3V3", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserCurrent3V3Callback(int32_t uid) {
m_userCurrent3V3Callbacks = CancelCallback(m_userCurrent3V3Callbacks, uid);
}
void RoboRioData::InvokeUserCurrent3V3Callback(HAL_Value value) {
InvokeCallback(m_userCurrent3V3Callbacks, "UserCurrent3V3", &value);
}
double RoboRioData::GetUserCurrent3V3() { return m_userCurrent3V3; }
void RoboRioData::SetUserCurrent3V3(double userCurrent3V3) {
double oldValue = m_userCurrent3V3.exchange(userCurrent3V3);
if (oldValue != userCurrent3V3) {
InvokeUserCurrent3V3Callback(MakeDouble(userCurrent3V3));
}
}
int32_t RoboRioData::RegisterUserActive3V3Callback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userActive3V3Callbacks = RegisterCallback(
m_userActive3V3Callbacks, "UserActive3V3", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetUserActive3V3());
callback("UserActive3V3", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserActive3V3Callback(int32_t uid) {
m_userActive3V3Callbacks = CancelCallback(m_userActive3V3Callbacks, uid);
}
void RoboRioData::InvokeUserActive3V3Callback(HAL_Value value) {
InvokeCallback(m_userActive3V3Callbacks, "UserActive3V3", &value);
}
HAL_Bool RoboRioData::GetUserActive3V3() { return m_userActive3V3; }
void RoboRioData::SetUserActive3V3(HAL_Bool userActive3V3) {
HAL_Bool oldValue = m_userActive3V3.exchange(userActive3V3);
if (oldValue != userActive3V3) {
InvokeUserActive3V3Callback(MakeBoolean(userActive3V3));
}
}
int32_t RoboRioData::RegisterUserFaults6VCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userFaults6VCallbacks = RegisterCallback(
m_userFaults6VCallbacks, "UserFaults6V", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetUserFaults6V());
callback("UserFaults6V", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserFaults6VCallback(int32_t uid) {
m_userFaults6VCallbacks = CancelCallback(m_userFaults6VCallbacks, uid);
}
void RoboRioData::InvokeUserFaults6VCallback(HAL_Value value) {
InvokeCallback(m_userFaults6VCallbacks, "UserFaults6V", &value);
}
int32_t RoboRioData::GetUserFaults6V() { return m_userFaults6V; }
void RoboRioData::SetUserFaults6V(int32_t userFaults6V) {
int32_t oldValue = m_userFaults6V.exchange(userFaults6V);
if (oldValue != userFaults6V) {
InvokeUserFaults6VCallback(MakeInt(userFaults6V));
}
}
int32_t RoboRioData::RegisterUserFaults5VCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userFaults5VCallbacks = RegisterCallback(
m_userFaults5VCallbacks, "UserFaults5V", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetUserFaults5V());
callback("UserFaults5V", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserFaults5VCallback(int32_t uid) {
m_userFaults5VCallbacks = CancelCallback(m_userFaults5VCallbacks, uid);
}
void RoboRioData::InvokeUserFaults5VCallback(HAL_Value value) {
InvokeCallback(m_userFaults5VCallbacks, "UserFaults5V", &value);
}
int32_t RoboRioData::GetUserFaults5V() { return m_userFaults5V; }
void RoboRioData::SetUserFaults5V(int32_t userFaults5V) {
int32_t oldValue = m_userFaults5V.exchange(userFaults5V);
if (oldValue != userFaults5V) {
InvokeUserFaults5VCallback(MakeInt(userFaults5V));
}
}
int32_t RoboRioData::RegisterUserFaults3V3Callback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_userFaults3V3Callbacks = RegisterCallback(
m_userFaults3V3Callbacks, "UserFaults3V3", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetUserFaults3V3());
callback("UserFaults3V3", param, &value);
}
return newUid;
}
void RoboRioData::CancelUserFaults3V3Callback(int32_t uid) {
m_userFaults3V3Callbacks = CancelCallback(m_userFaults3V3Callbacks, uid);
}
void RoboRioData::InvokeUserFaults3V3Callback(HAL_Value value) {
InvokeCallback(m_userFaults3V3Callbacks, "UserFaults3V3", &value);
}
int32_t RoboRioData::GetUserFaults3V3() { return m_userFaults3V3; }
void RoboRioData::SetUserFaults3V3(int32_t userFaults3V3) {
int32_t oldValue = m_userFaults3V3.exchange(userFaults3V3);
if (oldValue != userFaults3V3) {
InvokeUserFaults3V3Callback(MakeInt(userFaults3V3));
}
}
extern "C" {
void HALSIM_ResetRoboRioData(int32_t index) {
SimRoboRioData[index].ResetData();
}
int32_t HALSIM_RegisterRoboRioFPGAButtonCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterFPGAButtonCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioFPGAButtonCallback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelFPGAButtonCallback(uid);
}
HAL_Bool HALSIM_GetRoboRioFPGAButton(int32_t index) {
return SimRoboRioData[index].GetFPGAButton();
}
void HALSIM_SetRoboRioFPGAButton(int32_t index, HAL_Bool fPGAButton) {
SimRoboRioData[index].SetFPGAButton(fPGAButton);
}
int32_t HALSIM_RegisterRoboRioVInVoltageCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterVInVoltageCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioVInVoltageCallback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelVInVoltageCallback(uid);
}
double HALSIM_GetRoboRioVInVoltage(int32_t index) {
return SimRoboRioData[index].GetVInVoltage();
}
void HALSIM_SetRoboRioVInVoltage(int32_t index, double vInVoltage) {
SimRoboRioData[index].SetVInVoltage(vInVoltage);
}
int32_t HALSIM_RegisterRoboRioVInCurrentCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterVInCurrentCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioVInCurrentCallback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelVInCurrentCallback(uid);
}
double HALSIM_GetRoboRioVInCurrent(int32_t index) {
return SimRoboRioData[index].GetVInCurrent();
}
void HALSIM_SetRoboRioVInCurrent(int32_t index, double vInCurrent) {
SimRoboRioData[index].SetVInCurrent(vInCurrent);
}
int32_t HALSIM_RegisterRoboRioUserVoltage6VCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserVoltage6VCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserVoltage6VCallback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserVoltage6VCallback(uid);
}
double HALSIM_GetRoboRioUserVoltage6V(int32_t index) {
return SimRoboRioData[index].GetUserVoltage6V();
}
void HALSIM_SetRoboRioUserVoltage6V(int32_t index, double userVoltage6V) {
SimRoboRioData[index].SetUserVoltage6V(userVoltage6V);
}
int32_t HALSIM_RegisterRoboRioUserCurrent6VCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserCurrent6VCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserCurrent6VCallback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserCurrent6VCallback(uid);
}
double HALSIM_GetRoboRioUserCurrent6V(int32_t index) {
return SimRoboRioData[index].GetUserCurrent6V();
}
void HALSIM_SetRoboRioUserCurrent6V(int32_t index, double userCurrent6V) {
SimRoboRioData[index].SetUserCurrent6V(userCurrent6V);
}
int32_t HALSIM_RegisterRoboRioUserActive6VCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserActive6VCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserActive6VCallback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserActive6VCallback(uid);
}
HAL_Bool HALSIM_GetRoboRioUserActive6V(int32_t index) {
return SimRoboRioData[index].GetUserActive6V();
}
void HALSIM_SetRoboRioUserActive6V(int32_t index, HAL_Bool userActive6V) {
SimRoboRioData[index].SetUserActive6V(userActive6V);
}
int32_t HALSIM_RegisterRoboRioUserVoltage5VCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserVoltage5VCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserVoltage5VCallback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserVoltage5VCallback(uid);
}
double HALSIM_GetRoboRioUserVoltage5V(int32_t index) {
return SimRoboRioData[index].GetUserVoltage5V();
}
void HALSIM_SetRoboRioUserVoltage5V(int32_t index, double userVoltage5V) {
SimRoboRioData[index].SetUserVoltage5V(userVoltage5V);
}
int32_t HALSIM_RegisterRoboRioUserCurrent5VCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserCurrent5VCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserCurrent5VCallback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserCurrent5VCallback(uid);
}
double HALSIM_GetRoboRioUserCurrent5V(int32_t index) {
return SimRoboRioData[index].GetUserCurrent5V();
}
void HALSIM_SetRoboRioUserCurrent5V(int32_t index, double userCurrent5V) {
SimRoboRioData[index].SetUserCurrent5V(userCurrent5V);
}
int32_t HALSIM_RegisterRoboRioUserActive5VCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserActive5VCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserActive5VCallback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserActive5VCallback(uid);
}
HAL_Bool HALSIM_GetRoboRioUserActive5V(int32_t index) {
return SimRoboRioData[index].GetUserActive5V();
}
void HALSIM_SetRoboRioUserActive5V(int32_t index, HAL_Bool userActive5V) {
SimRoboRioData[index].SetUserActive5V(userActive5V);
}
int32_t HALSIM_RegisterRoboRioUserVoltage3V3Callback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserVoltage3V3Callback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserVoltage3V3Callback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserVoltage3V3Callback(uid);
}
double HALSIM_GetRoboRioUserVoltage3V3(int32_t index) {
return SimRoboRioData[index].GetUserVoltage3V3();
}
void HALSIM_SetRoboRioUserVoltage3V3(int32_t index, double userVoltage3V3) {
SimRoboRioData[index].SetUserVoltage3V3(userVoltage3V3);
}
int32_t HALSIM_RegisterRoboRioUserCurrent3V3Callback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserCurrent3V3Callback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserCurrent3V3Callback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserCurrent3V3Callback(uid);
}
double HALSIM_GetRoboRioUserCurrent3V3(int32_t index) {
return SimRoboRioData[index].GetUserCurrent3V3();
}
void HALSIM_SetRoboRioUserCurrent3V3(int32_t index, double userCurrent3V3) {
SimRoboRioData[index].SetUserCurrent3V3(userCurrent3V3);
}
int32_t HALSIM_RegisterRoboRioUserActive3V3Callback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserActive3V3Callback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserActive3V3Callback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserActive3V3Callback(uid);
}
HAL_Bool HALSIM_GetRoboRioUserActive3V3(int32_t index) {
return SimRoboRioData[index].GetUserActive3V3();
}
void HALSIM_SetRoboRioUserActive3V3(int32_t index, HAL_Bool userActive3V3) {
SimRoboRioData[index].SetUserActive3V3(userActive3V3);
}
int32_t HALSIM_RegisterRoboRioUserFaults6VCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserFaults6VCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserFaults6VCallback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserFaults6VCallback(uid);
}
int32_t HALSIM_GetRoboRioUserFaults6V(int32_t index) {
return SimRoboRioData[index].GetUserFaults6V();
}
void HALSIM_SetRoboRioUserFaults6V(int32_t index, int32_t userFaults6V) {
SimRoboRioData[index].SetUserFaults6V(userFaults6V);
}
int32_t HALSIM_RegisterRoboRioUserFaults5VCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserFaults5VCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserFaults5VCallback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserFaults5VCallback(uid);
}
int32_t HALSIM_GetRoboRioUserFaults5V(int32_t index) {
return SimRoboRioData[index].GetUserFaults5V();
}
void HALSIM_SetRoboRioUserFaults5V(int32_t index, int32_t userFaults5V) {
SimRoboRioData[index].SetUserFaults5V(userFaults5V);
}
int32_t HALSIM_RegisterRoboRioUserFaults3V3Callback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimRoboRioData[index].RegisterUserFaults3V3Callback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioUserFaults3V3Callback(int32_t index, int32_t uid) {
SimRoboRioData[index].CancelUserFaults3V3Callback(uid);
}
int32_t HALSIM_GetRoboRioUserFaults3V3(int32_t index) {
return SimRoboRioData[index].GetUserFaults3V3();
}
void HALSIM_SetRoboRioUserFaults3V3(int32_t index, int32_t userFaults3V3) {
SimRoboRioData[index].SetUserFaults3V3(userFaults3V3);
}
}

View File

@@ -0,0 +1,160 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/NotifyListenerVector.h"
#include "MockData/RoboRioData.h"
namespace hal {
class RoboRioData {
public:
int32_t RegisterFPGAButtonCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelFPGAButtonCallback(int32_t uid);
void InvokeFPGAButtonCallback(HAL_Value value);
HAL_Bool GetFPGAButton();
void SetFPGAButton(HAL_Bool fPGAButton);
int32_t RegisterVInVoltageCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelVInVoltageCallback(int32_t uid);
void InvokeVInVoltageCallback(HAL_Value value);
double GetVInVoltage();
void SetVInVoltage(double vInVoltage);
int32_t RegisterVInCurrentCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelVInCurrentCallback(int32_t uid);
void InvokeVInCurrentCallback(HAL_Value value);
double GetVInCurrent();
void SetVInCurrent(double vInCurrent);
int32_t RegisterUserVoltage6VCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelUserVoltage6VCallback(int32_t uid);
void InvokeUserVoltage6VCallback(HAL_Value value);
double GetUserVoltage6V();
void SetUserVoltage6V(double userVoltage6V);
int32_t RegisterUserCurrent6VCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelUserCurrent6VCallback(int32_t uid);
void InvokeUserCurrent6VCallback(HAL_Value value);
double GetUserCurrent6V();
void SetUserCurrent6V(double userCurrent6V);
int32_t RegisterUserActive6VCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelUserActive6VCallback(int32_t uid);
void InvokeUserActive6VCallback(HAL_Value value);
HAL_Bool GetUserActive6V();
void SetUserActive6V(HAL_Bool userActive6V);
int32_t RegisterUserVoltage5VCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelUserVoltage5VCallback(int32_t uid);
void InvokeUserVoltage5VCallback(HAL_Value value);
double GetUserVoltage5V();
void SetUserVoltage5V(double userVoltage5V);
int32_t RegisterUserCurrent5VCallback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelUserCurrent5VCallback(int32_t uid);
void InvokeUserCurrent5VCallback(HAL_Value value);
double GetUserCurrent5V();
void SetUserCurrent5V(double userCurrent5V);
int32_t RegisterUserActive5VCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelUserActive5VCallback(int32_t uid);
void InvokeUserActive5VCallback(HAL_Value value);
HAL_Bool GetUserActive5V();
void SetUserActive5V(HAL_Bool userActive5V);
int32_t RegisterUserVoltage3V3Callback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelUserVoltage3V3Callback(int32_t uid);
void InvokeUserVoltage3V3Callback(HAL_Value value);
double GetUserVoltage3V3();
void SetUserVoltage3V3(double userVoltage3V3);
int32_t RegisterUserCurrent3V3Callback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelUserCurrent3V3Callback(int32_t uid);
void InvokeUserCurrent3V3Callback(HAL_Value value);
double GetUserCurrent3V3();
void SetUserCurrent3V3(double userCurrent3V3);
int32_t RegisterUserActive3V3Callback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelUserActive3V3Callback(int32_t uid);
void InvokeUserActive3V3Callback(HAL_Value value);
HAL_Bool GetUserActive3V3();
void SetUserActive3V3(HAL_Bool userActive3V3);
int32_t RegisterUserFaults6VCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelUserFaults6VCallback(int32_t uid);
void InvokeUserFaults6VCallback(HAL_Value value);
int32_t GetUserFaults6V();
void SetUserFaults6V(int32_t userFaults6V);
int32_t RegisterUserFaults5VCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelUserFaults5VCallback(int32_t uid);
void InvokeUserFaults5VCallback(HAL_Value value);
int32_t GetUserFaults5V();
void SetUserFaults5V(int32_t userFaults5V);
int32_t RegisterUserFaults3V3Callback(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void CancelUserFaults3V3Callback(int32_t uid);
void InvokeUserFaults3V3Callback(HAL_Value value);
int32_t GetUserFaults3V3();
void SetUserFaults3V3(int32_t userFaults3V3);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_fPGAButton{false};
std::shared_ptr<NotifyListenerVector> m_fPGAButtonCallbacks = nullptr;
std::atomic<double> m_vInVoltage{0.0};
std::shared_ptr<NotifyListenerVector> m_vInVoltageCallbacks = nullptr;
std::atomic<double> m_vInCurrent{0.0};
std::shared_ptr<NotifyListenerVector> m_vInCurrentCallbacks = nullptr;
std::atomic<double> m_userVoltage6V{6.0};
std::shared_ptr<NotifyListenerVector> m_userVoltage6VCallbacks = nullptr;
std::atomic<double> m_userCurrent6V{0.0};
std::shared_ptr<NotifyListenerVector> m_userCurrent6VCallbacks = nullptr;
std::atomic<HAL_Bool> m_userActive6V{false};
std::shared_ptr<NotifyListenerVector> m_userActive6VCallbacks = nullptr;
std::atomic<double> m_userVoltage5V{5.0};
std::shared_ptr<NotifyListenerVector> m_userVoltage5VCallbacks = nullptr;
std::atomic<double> m_userCurrent5V{0.0};
std::shared_ptr<NotifyListenerVector> m_userCurrent5VCallbacks = nullptr;
std::atomic<HAL_Bool> m_userActive5V{false};
std::shared_ptr<NotifyListenerVector> m_userActive5VCallbacks = nullptr;
std::atomic<double> m_userVoltage3V3{3.3};
std::shared_ptr<NotifyListenerVector> m_userVoltage3V3Callbacks = nullptr;
std::atomic<double> m_userCurrent3V3{0.0};
std::shared_ptr<NotifyListenerVector> m_userCurrent3V3Callbacks = nullptr;
std::atomic<HAL_Bool> m_userActive3V3{false};
std::shared_ptr<NotifyListenerVector> m_userActive3V3Callbacks = nullptr;
std::atomic<int32_t> m_userFaults6V{0};
std::shared_ptr<NotifyListenerVector> m_userFaults6VCallbacks = nullptr;
std::atomic<int32_t> m_userFaults5V{0};
std::shared_ptr<NotifyListenerVector> m_userFaults5VCallbacks = nullptr;
std::atomic<int32_t> m_userFaults3V3{0};
std::shared_ptr<NotifyListenerVector> m_userFaults3V3Callbacks = nullptr;
};
extern RoboRioData SimRoboRioData[];
} // namespace hal

View File

@@ -0,0 +1,309 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "../PortsInternal.h"
#include "NotifyCallbackHelpers.h"
#include "SPIAccelerometerDataInternal.h"
using namespace hal;
SPIAccelerometerData hal::SimSPIAccelerometerData[5];
void SPIAccelerometerData::ResetData() {
m_active = false;
m_activeCallbacks = nullptr;
m_range = 0;
m_rangeCallbacks = nullptr;
m_x = 0.0;
m_xCallbacks = nullptr;
m_y = 0.0;
m_yCallbacks = nullptr;
m_z = 0.0;
m_zCallbacks = nullptr;
}
int32_t SPIAccelerometerData::RegisterActiveCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_activeCallbacks =
RegisterCallback(m_activeCallbacks, "Active", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeBoolean(GetActive());
callback("Active", param, &value);
}
return newUid;
}
void SPIAccelerometerData::CancelActiveCallback(int32_t uid) {
m_activeCallbacks = CancelCallback(m_activeCallbacks, uid);
}
void SPIAccelerometerData::InvokeActiveCallback(HAL_Value value) {
InvokeCallback(m_activeCallbacks, "Active", &value);
}
HAL_Bool SPIAccelerometerData::GetActive() { return m_active; }
void SPIAccelerometerData::SetActive(HAL_Bool active) {
HAL_Bool oldValue = m_active.exchange(active);
if (oldValue != active) {
InvokeActiveCallback(MakeBoolean(active));
}
}
int32_t SPIAccelerometerData::RegisterRangeCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_rangeCallbacks =
RegisterCallback(m_rangeCallbacks, "Range", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeInt(GetRange());
callback("Range", param, &value);
}
return newUid;
}
void SPIAccelerometerData::CancelRangeCallback(int32_t uid) {
m_rangeCallbacks = CancelCallback(m_rangeCallbacks, uid);
}
void SPIAccelerometerData::InvokeRangeCallback(HAL_Value value) {
InvokeCallback(m_rangeCallbacks, "Range", &value);
}
int32_t SPIAccelerometerData::GetRange() { return m_range; }
void SPIAccelerometerData::SetRange(int32_t range) {
int32_t oldValue = m_range.exchange(range);
if (oldValue != range) {
InvokeRangeCallback(MakeInt(range));
}
}
int32_t SPIAccelerometerData::RegisterXCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_xCallbacks =
RegisterCallback(m_xCallbacks, "X", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetX());
callback("X", param, &value);
}
return newUid;
}
void SPIAccelerometerData::CancelXCallback(int32_t uid) {
m_xCallbacks = CancelCallback(m_xCallbacks, uid);
}
void SPIAccelerometerData::InvokeXCallback(HAL_Value value) {
InvokeCallback(m_xCallbacks, "X", &value);
}
double SPIAccelerometerData::GetX() { return m_x; }
void SPIAccelerometerData::SetX(double x) {
double oldValue = m_x.exchange(x);
if (oldValue != x) {
InvokeXCallback(MakeDouble(x));
}
}
int32_t SPIAccelerometerData::RegisterYCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_yCallbacks =
RegisterCallback(m_yCallbacks, "Y", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetY());
callback("Y", param, &value);
}
return newUid;
}
void SPIAccelerometerData::CancelYCallback(int32_t uid) {
m_yCallbacks = CancelCallback(m_yCallbacks, uid);
}
void SPIAccelerometerData::InvokeYCallback(HAL_Value value) {
InvokeCallback(m_yCallbacks, "Y", &value);
}
double SPIAccelerometerData::GetY() { return m_y; }
void SPIAccelerometerData::SetY(double y) {
double oldValue = m_y.exchange(y);
if (oldValue != y) {
InvokeYCallback(MakeDouble(y));
}
}
int32_t SPIAccelerometerData::RegisterZCallback(HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
int32_t newUid = 0;
{
std::lock_guard<std::mutex> lock(m_registerMutex);
m_zCallbacks =
RegisterCallback(m_zCallbacks, "Z", callback, param, &newUid);
}
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeDouble(GetZ());
callback("Z", param, &value);
}
return newUid;
}
void SPIAccelerometerData::CancelZCallback(int32_t uid) {
m_zCallbacks = CancelCallback(m_zCallbacks, uid);
}
void SPIAccelerometerData::InvokeZCallback(HAL_Value value) {
InvokeCallback(m_zCallbacks, "Z", &value);
}
double SPIAccelerometerData::GetZ() { return m_z; }
void SPIAccelerometerData::SetZ(double z) {
double oldValue = m_z.exchange(z);
if (oldValue != z) {
InvokeZCallback(MakeDouble(z));
}
}
extern "C" {
void HALSIM_ResetSPIAccelerometerData(int32_t index) {
SimSPIAccelerometerData[index].ResetData();
}
int32_t HALSIM_RegisterSPIAccelerometerActiveCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimSPIAccelerometerData[index].RegisterActiveCallback(callback, param,
initialNotify);
}
void HALSIM_CancelSPIAccelerometerActiveCallback(int32_t index, int32_t uid) {
SimSPIAccelerometerData[index].CancelActiveCallback(uid);
}
HAL_Bool HALSIM_GetSPIAccelerometerActive(int32_t index) {
return SimSPIAccelerometerData[index].GetActive();
}
void HALSIM_SetSPIAccelerometerActive(int32_t index, HAL_Bool active) {
SimSPIAccelerometerData[index].SetActive(active);
}
int32_t HALSIM_RegisterSPIAccelerometerRangeCallback(
int32_t index, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {
return SimSPIAccelerometerData[index].RegisterRangeCallback(callback, param,
initialNotify);
}
void HALSIM_CancelSPIAccelerometerRangeCallback(int32_t index, int32_t uid) {
SimSPIAccelerometerData[index].CancelRangeCallback(uid);
}
int32_t HALSIM_GetSPIAccelerometerRange(int32_t index) {
return SimSPIAccelerometerData[index].GetRange();
}
void HALSIM_SetSPIAccelerometerRange(int32_t index, int32_t range) {
SimSPIAccelerometerData[index].SetRange(range);
}
int32_t HALSIM_RegisterSPIAccelerometerXCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimSPIAccelerometerData[index].RegisterXCallback(callback, param,
initialNotify);
}
void HALSIM_CancelSPIAccelerometerXCallback(int32_t index, int32_t uid) {
SimSPIAccelerometerData[index].CancelXCallback(uid);
}
double HALSIM_GetSPIAccelerometerX(int32_t index) {
return SimSPIAccelerometerData[index].GetX();
}
void HALSIM_SetSPIAccelerometerX(int32_t index, double x) {
SimSPIAccelerometerData[index].SetX(x);
}
int32_t HALSIM_RegisterSPIAccelerometerYCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimSPIAccelerometerData[index].RegisterYCallback(callback, param,
initialNotify);
}
void HALSIM_CancelSPIAccelerometerYCallback(int32_t index, int32_t uid) {
SimSPIAccelerometerData[index].CancelYCallback(uid);
}
double HALSIM_GetSPIAccelerometerY(int32_t index) {
return SimSPIAccelerometerData[index].GetY();
}
void HALSIM_SetSPIAccelerometerY(int32_t index, double y) {
SimSPIAccelerometerData[index].SetY(y);
}
int32_t HALSIM_RegisterSPIAccelerometerZCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
return SimSPIAccelerometerData[index].RegisterZCallback(callback, param,
initialNotify);
}
void HALSIM_CancelSPIAccelerometerZCallback(int32_t index, int32_t uid) {
SimSPIAccelerometerData[index].CancelZCallback(uid);
}
double HALSIM_GetSPIAccelerometerZ(int32_t index) {
return SimSPIAccelerometerData[index].GetZ();
}
void HALSIM_SetSPIAccelerometerZ(int32_t index, double z) {
SimSPIAccelerometerData[index].SetZ(z);
}
}

View File

@@ -0,0 +1,70 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <atomic>
#include <memory>
#include "MockData/NotifyListenerVector.h"
#include "MockData/SPIAccelerometerData.h"
namespace hal {
class SPIAccelerometerData {
public:
int32_t RegisterActiveCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelActiveCallback(int32_t uid);
void InvokeActiveCallback(HAL_Value value);
HAL_Bool GetActive();
void SetActive(HAL_Bool active);
int32_t RegisterRangeCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelRangeCallback(int32_t uid);
void InvokeRangeCallback(HAL_Value value);
int32_t GetRange();
void SetRange(int32_t range);
int32_t RegisterXCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelXCallback(int32_t uid);
void InvokeXCallback(HAL_Value value);
double GetX();
void SetX(double x);
int32_t RegisterYCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelYCallback(int32_t uid);
void InvokeYCallback(HAL_Value value);
double GetY();
void SetY(double y);
int32_t RegisterZCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelZCallback(int32_t uid);
void InvokeZCallback(HAL_Value value);
double GetZ();
void SetZ(double z);
virtual void ResetData();
private:
std::mutex m_registerMutex;
std::atomic<HAL_Bool> m_active{false};
std::shared_ptr<NotifyListenerVector> m_activeCallbacks = nullptr;
std::atomic<int32_t> m_range{0};
std::shared_ptr<NotifyListenerVector> m_rangeCallbacks = nullptr;
std::atomic<double> m_x{0.0};
std::shared_ptr<NotifyListenerVector> m_xCallbacks = nullptr;
std::atomic<double> m_y{0.0};
std::shared_ptr<NotifyListenerVector> m_yCallbacks = nullptr;
std::atomic<double> m_z{0.0};
std::shared_ptr<NotifyListenerVector> m_zCallbacks = nullptr;
};
extern SPIAccelerometerData SimSPIAccelerometerData[];
} // namespace hal

View File

@@ -0,0 +1,53 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 <atomic>
#include <chrono>
#include <cstdio>
#include <thread>
#include "MockHooksInternal.h"
#include "support/timestamp.h"
static std::atomic<bool> programStarted{false};
static std::atomic<uint64_t> programStartTime{0};
namespace hal {
void RestartTiming() { programStartTime = wpi::Now() / 10; }
int64_t GetFPGATime() {
auto now = wpi::Now() / 10;
auto currentTime = now - programStartTime;
return currentTime;
}
double GetFPGATimestamp() {
auto now = wpi::Now() / 10;
auto currentTime = now - programStartTime;
return currentTime * 1.0e-6;
}
void SetProgramStarted() { programStarted = true; }
} // namespace hal
using namespace hal;
extern "C" {
void HALSIM_WaitForProgramStart(void) {
int count = 0;
while (!programStarted) {
count++;
std::printf("Waiting for program start signal: %d\n", count);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
void HALSIM_SetProgramStarted(void) { SetProgramStarted(); }
void HALSIM_RestartTiming(void) { RestartTiming(); }
}

View File

@@ -0,0 +1,22 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <stdint.h>
#include "MockData/MockHooks.h"
namespace hal {
void RestartTiming();
int64_t GetFPGATime();
double GetFPGATimestamp();
void SetProgramStarted();
}

View File

@@ -0,0 +1,155 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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/Notifier.h"
#include <chrono>
#include <iostream>
#include "HAL/HAL.h"
#include "HAL/cpp/fpga_clock.h"
#include "HAL/cpp/make_unique.h"
#include "HAL/handles/UnlimitedHandleResource.h"
#include "support/SafeThread.h"
#include "support/timestamp.h"
namespace {
class NotifierThread : public wpi::SafeThread {
public:
void Main() {
int32_t status = 0;
std::unique_lock<std::mutex> lock(m_mutex);
while (m_active) {
startNotifierLoop:
double waitTime = m_waitTime * 1e-6;
if (!m_running) {
status = 0;
waitTime = (HAL_GetFPGATime(&status) * 1e-6) + 1000.0;
// If not running, wait 1000 seconds
}
auto timeoutTime =
hal::fpga_clock::epoch() + std::chrono::duration<double>(waitTime);
// auto timeoutTime = std::chrono::steady_clock::now() +
// std::chrono::duration<double>(1.0);
m_cond.wait_until(lock, timeoutTime);
if (m_updatedAlarm) {
m_updatedAlarm = false;
goto startNotifierLoop;
}
if (!m_running) continue;
if (!m_active) break;
m_running = false;
int32_t status = 0;
uint64_t currentTime = HAL_GetFPGATime(&status);
HAL_NotifierHandle handle = m_handle;
HAL_NotifierProcessFunction process = m_process;
lock.unlock(); // don't hold mutex during callback execution
process(currentTime, handle);
m_updatedAlarm = false;
lock.lock();
}
}
HAL_NotifierHandle m_handle = HAL_kInvalidHandle;
HAL_NotifierProcessFunction m_process;
uint64_t m_waitTime;
bool m_updatedAlarm = false;
bool m_running = false;
};
class NotifierThreadOwner : public wpi::SafeThreadOwner<NotifierThread> {
public:
void SetFunc(HAL_NotifierProcessFunction process, HAL_NotifierHandle handle) {
auto thr = GetThread();
if (!thr) return;
thr->m_process = process;
thr->m_handle = handle;
}
void UpdateAlarm(uint64_t triggerTime) {
auto thr = GetThread();
if (!thr) return;
thr->m_waitTime = triggerTime;
thr->m_running = true;
thr->m_updatedAlarm = true;
thr->m_cond.notify_one();
}
void StopAlarm() {
auto thr = GetThread();
if (!thr) return;
thr->m_running = false;
}
};
struct Notifier {
std::unique_ptr<NotifierThreadOwner> thread;
void* param;
};
} // namespace
using namespace hal;
static UnlimitedHandleResource<HAL_NotifierHandle, Notifier,
HAL_HandleEnum::Notifier>
notifierHandles;
extern "C" {
HAL_NotifierHandle HAL_InitializeNotifierNonThreadedUnsafe(
HAL_NotifierProcessFunction process, void* param, int32_t* status) {
return HAL_InitializeNotifier(process, param, status);
}
HAL_NotifierHandle HAL_InitializeNotifier(HAL_NotifierProcessFunction process,
void* param, int32_t* status) {
if (!process) {
*status = NULL_PARAMETER;
return 0;
}
std::shared_ptr<Notifier> notifier = std::make_shared<Notifier>();
HAL_NotifierHandle handle = notifierHandles.Allocate(notifier);
if (handle == HAL_kInvalidHandle) {
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
notifier->thread = std::make_unique<NotifierThreadOwner>();
notifier->thread->Start();
notifier->thread->SetFunc(process, handle);
notifier->param = param;
return handle;
}
void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
auto notifier = notifierHandles.Get(notifierHandle);
if (!notifier) return;
notifier->thread->StopAlarm();
notifierHandles.Free(notifierHandle);
}
void* HAL_GetNotifierParam(HAL_NotifierHandle notifierHandle, int32_t* status) {
auto notifier = notifierHandles.Get(notifierHandle);
if (!notifier) return nullptr;
return notifier->param;
}
void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle,
uint64_t triggerTime, int32_t* status) {
auto notifier = notifierHandles.Get(notifierHandle);
if (!notifier) return;
notifier->thread->UpdateAlarm(triggerTime);
}
void HAL_StopNotifierAlarm(HAL_NotifierHandle notifierHandle, int32_t* status) {
auto notifier = notifierHandles.Get(notifierHandle);
if (!notifier) return;
notifier->thread->StopAlarm();
}
} // extern "C"

View File

@@ -0,0 +1,47 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/SerialPort.h"
extern "C" {
void HAL_InitializeOSSerialPort(HAL_SerialPort port, int32_t* status) {}
void HAL_SetOSSerialBaudRate(HAL_SerialPort port, int32_t baud,
int32_t* status) {}
void HAL_SetOSSerialDataBits(HAL_SerialPort port, int32_t bits,
int32_t* status) {}
void HAL_SetOSSerialParity(HAL_SerialPort port, int32_t parity,
int32_t* status) {}
void HAL_SetOSSerialStopBits(HAL_SerialPort port, int32_t stopBits,
int32_t* status) {}
void HAL_SetOSSerialWriteMode(HAL_SerialPort port, int32_t mode,
int32_t* status) {}
void HAL_SetOSSerialFlowControl(HAL_SerialPort port, int32_t flow,
int32_t* status) {}
void HAL_SetOSSerialTimeout(HAL_SerialPort port, double timeout,
int32_t* status) {}
void HAL_EnableOSSerialTermination(HAL_SerialPort port, char terminator,
int32_t* status) {}
void HAL_DisableOSSerialTermination(HAL_SerialPort port, int32_t* status) {}
void HAL_SetOSSerialReadBufferSize(HAL_SerialPort port, int32_t size,
int32_t* status) {}
void HAL_SetOSSerialWriteBufferSize(HAL_SerialPort port, int32_t size,
int32_t* status) {}
int32_t HAL_GetOSSerialBytesReceived(HAL_SerialPort port, int32_t* status) {
return 0;
}
int32_t HAL_ReadOSSerial(HAL_SerialPort port, char* buffer, int32_t count,
int32_t* status) {
return 0;
}
int32_t HAL_WriteOSSerial(HAL_SerialPort port, const char* buffer,
int32_t count, int32_t* status) {
return 0;
}
void HAL_FlushOSSerial(HAL_SerialPort port, int32_t* status) {}
void HAL_ClearOSSerial(HAL_SerialPort port, int32_t* status) {}
void HAL_CloseOSSerial(HAL_SerialPort port, int32_t* status) {}
}

View File

@@ -0,0 +1,41 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/PDP.h"
#include "MockData/PDPDataInternal.h"
#include "PortsInternal.h"
using namespace hal;
extern "C" {
void HAL_InitializePDP(int32_t module, int32_t* status) {
SimPDPData[module].SetInitialized(true);
}
HAL_Bool HAL_CheckPDPModule(int32_t module) {
return module < kNumPDPModules && module >= 0;
}
HAL_Bool HAL_CheckPDPChannel(int32_t channel) {
return channel < kNumPDPChannels && channel >= 0;
}
double HAL_GetPDPTemperature(int32_t module, int32_t* status) {
return SimPDPData[module].GetTemperature();
}
double HAL_GetPDPVoltage(int32_t module, int32_t* status) {
return SimPDPData[module].GetVoltage();
}
double HAL_GetPDPChannelCurrent(int32_t module, int32_t channel,
int32_t* status) {
return SimPDPData[module].GetCurrent(channel);
}
double HAL_GetPDPTotalCurrent(int32_t module, int32_t* status) { return 0.0; }
double HAL_GetPDPTotalPower(int32_t module, int32_t* status) { return 0.0; }
double HAL_GetPDPTotalEnergy(int32_t module, int32_t* status) { return 0.0; }
void HAL_ResetPDPTotalEnergy(int32_t module, int32_t* status) {}
void HAL_ClearPDPStickyFaults(int32_t module, int32_t* status) {}
}

View File

@@ -0,0 +1,344 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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/PWM.h"
#include "ConstantsInternal.h"
#include "DigitalInternal.h"
#include "HAL/handles/HandlesInternal.h"
#include "MockData/PWMDataInternal.h"
#include "PortsInternal.h"
using namespace hal;
extern "C" {
HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
int32_t* status) {
if (*status != 0) return HAL_kInvalidHandle;
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
uint8_t origChannel = static_cast<uint8_t>(channel);
if (origChannel < kNumPWMHeaders) {
channel += kNumDigitalChannels; // remap Headers to end of allocations
} else {
channel = remapMXPPWMChannel(channel) + 10; // remap MXP to proper channel
}
auto handle =
digitalChannelHandles.Allocate(channel, HAL_HandleEnum::PWM, status);
if (*status != 0)
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
auto port = digitalChannelHandles.Get(handle, HAL_HandleEnum::PWM);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = origChannel;
SimPWMData[origChannel].SetInitialized(true);
return handle;
}
void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimPWMData[port->channel].SetInitialized(false);
digitalChannelHandles.Free(pwmPortHandle, HAL_HandleEnum::PWM);
}
HAL_Bool HAL_CheckPWMChannel(int32_t channel) {
return channel < kNumPWMChannels && channel >= 0;
}
void HAL_SetPWMConfig(HAL_DigitalHandle pwmPortHandle, double max,
double deadbandMax, double center, double deadbandMin,
double min, int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
// calculate the loop time in milliseconds
double loopTime =
HAL_GetLoopTiming(status) / (kSystemClockTicksPerMicrosecond * 1e3);
if (*status != 0) return;
int32_t maxPwm = static_cast<int32_t>((max - kDefaultPwmCenter) / loopTime +
kDefaultPwmStepsDown - 1);
int32_t deadbandMaxPwm = static_cast<int32_t>(
(deadbandMax - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1);
int32_t centerPwm = static_cast<int32_t>(
(center - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1);
int32_t deadbandMinPwm = static_cast<int32_t>(
(deadbandMin - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1);
int32_t minPwm = static_cast<int32_t>((min - kDefaultPwmCenter) / loopTime +
kDefaultPwmStepsDown - 1);
port->maxPwm = maxPwm;
port->deadbandMaxPwm = deadbandMaxPwm;
port->deadbandMinPwm = deadbandMinPwm;
port->centerPwm = centerPwm;
port->minPwm = minPwm;
port->configSet = true;
}
void HAL_SetPWMConfigRaw(HAL_DigitalHandle pwmPortHandle, int32_t maxPwm,
int32_t deadbandMaxPwm, int32_t centerPwm,
int32_t deadbandMinPwm, int32_t minPwm,
int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
port->maxPwm = maxPwm;
port->deadbandMaxPwm = deadbandMaxPwm;
port->deadbandMinPwm = deadbandMinPwm;
port->centerPwm = centerPwm;
port->minPwm = minPwm;
}
void HAL_GetPWMConfigRaw(HAL_DigitalHandle pwmPortHandle, int32_t* maxPwm,
int32_t* deadbandMaxPwm, int32_t* centerPwm,
int32_t* deadbandMinPwm, int32_t* minPwm,
int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
*maxPwm = port->maxPwm;
*deadbandMaxPwm = port->deadbandMaxPwm;
*deadbandMinPwm = port->deadbandMinPwm;
*centerPwm = port->centerPwm;
*minPwm = port->minPwm;
}
void HAL_SetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle,
HAL_Bool eliminateDeadband, int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
port->eliminateDeadband = eliminateDeadband;
}
HAL_Bool HAL_GetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle,
int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
return port->eliminateDeadband;
}
/**
* Set a PWM channel to the desired value. The values range from 0 to 255 and
* the period is controlled
* by the PWM Period and MinHigh registers.
*
* @param channel The PWM channel to set.
* @param value The PWM value to set.
*/
void HAL_SetPWMRaw(HAL_DigitalHandle pwmPortHandle, int32_t value,
int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimPWMData[port->channel].SetRawValue(value);
}
/**
* Set a PWM channel to the desired scaled value. The values range from -1 to 1
* and
* the period is controlled
* by the PWM Period and MinHigh registers.
*
* @param channel The PWM channel to set.
* @param value The scaled PWM value to set.
*/
void HAL_SetPWMSpeed(HAL_DigitalHandle pwmPortHandle, double speed,
int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return;
}
if (speed < -1.0) {
speed = -1.0;
} else if (speed > 1.0) {
speed = 1.0;
}
SimPWMData[port->channel].SetSpeed(speed);
}
/**
* Set a PWM channel to the desired position value. The values range from 0 to 1
* and
* the period is controlled
* by the PWM Period and MinHigh registers.
*
* @param channel The PWM channel to set.
* @param value The scaled PWM value to set.
*/
void HAL_SetPWMPosition(HAL_DigitalHandle pwmPortHandle, double pos,
int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return;
}
if (pos < 0.0) {
pos = 0.0;
} else if (pos > 1.0) {
pos = 1.0;
}
SimPWMData[port->channel].SetPosition(pos);
}
void HAL_SetPWMDisabled(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimPWMData[port->channel].SetRawValue(0);
SimPWMData[port->channel].SetPosition(0);
SimPWMData[port->channel].SetSpeed(0);
}
/**
* Get a value from a PWM channel. The values range from 0 to 255.
*
* @param channel The PWM channel to read from.
* @return The raw PWM value.
*/
int32_t HAL_GetPWMRaw(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimPWMData[port->channel].GetRawValue();
}
/**
* Get a scaled value from a PWM channel. The values range from -1 to 1.
*
* @param channel The PWM channel to read from.
* @return The scaled PWM value.
*/
double HAL_GetPWMSpeed(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return 0;
}
double speed = SimPWMData[port->channel].GetSpeed();
if (speed > 1) speed = 1;
if (speed < -1) speed = -1;
return speed;
}
/**
* Get a position value from a PWM channel. The values range from 0 to 1.
*
* @param channel The PWM channel to read from.
* @return The scaled PWM value.
*/
double HAL_GetPWMPosition(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return 0;
}
double position = SimPWMData[port->channel].GetPosition();
if (position > 1) position = 1;
if (position < 0) position = 0;
return position;
}
void HAL_LatchPWMZero(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimPWMData[port->channel].SetZeroLatch(true);
SimPWMData[port->channel].SetZeroLatch(false);
}
/**
* Set how how often the PWM signal is squelched, thus scaling the period.
*
* @param channel The PWM channel to configure.
* @param squelchMask The 2-bit mask of outputs to squelch.
*/
void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
int32_t* status) {
auto port = digitalChannelHandles.Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimPWMData[port->channel].SetPeriodScale(squelchMask);
}
/**
* Get the loop timing of the PWM system
*
* @return The loop time
*/
int32_t HAL_GetLoopTiming(int32_t* status) { return kExpectedLoopTiming; }
}

View File

@@ -0,0 +1,34 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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/Ports.h"
#include "PortsInternal.h"
using namespace hal;
extern "C" {
int32_t HAL_GetNumAccumulators(void) { return kNumAccumulators; }
int32_t HAL_GetNumAnalogTriggers(void) { return kNumAnalogTriggers; }
int32_t HAL_GetNumAnalogInputs(void) { return kNumAnalogInputs; }
int32_t HAL_GetNumAnalogOutputs(void) { return kNumAnalogOutputs; }
int32_t HAL_GetNumCounters(void) { return kNumCounters; }
int32_t HAL_GetNumDigitalHeaders(void) { return kNumDigitalHeaders; }
int32_t HAL_GetNumPWMHeaders(void) { return kNumPWMHeaders; }
int32_t HAL_GetNumDigitalChannels(void) { return kNumDigitalChannels; }
int32_t HAL_GetNumPWMChannels(void) { return kNumPWMChannels; }
int32_t HAL_GetNumDigitalPWMOutputs(void) { return kNumDigitalPWMOutputs; }
int32_t HAL_GetNumEncoders(void) { return kNumEncoders; }
int32_t HAL_GetNumInterrupts(void) { return kNumInterrupts; }
int32_t HAL_GetNumRelayChannels(void) { return kNumRelayChannels; }
int32_t HAL_GetNumRelayHeaders(void) { return kNumRelayHeaders; }
int32_t HAL_GetNumPCMModules(void) { return kNumPCMModules; }
int32_t HAL_GetNumSolenoidChannels(void) { return kNumSolenoidChannels; }
int32_t HAL_GetNumPDPModules(void) { return kNumPDPModules; }
int32_t HAL_GetNumPDPChannels(void) { return kNumPDPChannels; }
int32_t HAL_GetNumCanTalons(void) { return kNumCanTalons; }
}

View File

@@ -0,0 +1,32 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <stdint.h>
namespace hal {
constexpr int32_t kNumAccumulators = 2;
constexpr int32_t kNumAnalogTriggers = 8;
constexpr int32_t kNumAnalogInputs = 8;
constexpr int32_t kNumAnalogOutputs = 2;
constexpr int32_t kNumCounters = 8;
constexpr int32_t kNumDigitalHeaders = 10;
constexpr int32_t kNumPWMHeaders = 10;
constexpr int32_t kNumDigitalChannels = 26;
constexpr int32_t kNumPWMChannels = 20;
constexpr int32_t kNumDigitalPWMOutputs = 6;
constexpr int32_t kNumEncoders = 8;
constexpr int32_t kNumInterrupts = 8;
constexpr int32_t kNumRelayChannels = 8;
constexpr int32_t kNumRelayHeaders = kNumRelayChannels / 2;
constexpr int32_t kNumPCMModules = 63;
constexpr int32_t kNumSolenoidChannels = 8;
constexpr int32_t kNumPDPModules = 63;
constexpr int32_t kNumPDPChannels = 16;
constexpr int32_t kNumCanTalons = 63;
} // namespace hal

View File

@@ -0,0 +1,58 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/Power.h"
#include "MockData/RoboRioDataInternal.h"
using namespace hal;
// TODO: Fix the naming in here
extern "C" {
double HAL_GetVinVoltage(int32_t* status) {
return SimRoboRioData[0].GetVInVoltage();
}
double HAL_GetVinCurrent(int32_t* status) {
return SimRoboRioData[0].GetVInCurrent();
}
double HAL_GetUserVoltage6V(int32_t* status) {
return SimRoboRioData[0].GetUserVoltage6V();
}
double HAL_GetUserCurrent6V(int32_t* status) {
return SimRoboRioData[0].GetUserCurrent6V();
}
HAL_Bool HAL_GetUserActive6V(int32_t* status) {
return SimRoboRioData[0].GetUserActive6V();
}
int32_t HAL_GetUserCurrentFaults6V(int32_t* status) {
return SimRoboRioData[0].GetUserFaults6V();
}
double HAL_GetUserVoltage5V(int32_t* status) {
return SimRoboRioData[0].GetUserVoltage5V();
}
double HAL_GetUserCurrent5V(int32_t* status) {
return SimRoboRioData[0].GetUserCurrent5V();
}
HAL_Bool HAL_GetUserActive5V(int32_t* status) {
return SimRoboRioData[0].GetUserActive5V();
}
int32_t HAL_GetUserCurrentFaults5V(int32_t* status) {
return SimRoboRioData[0].GetUserFaults5V();
}
double HAL_GetUserVoltage3V3(int32_t* status) {
return SimRoboRioData[0].GetUserVoltage3V3();
}
double HAL_GetUserCurrent3V3(int32_t* status) {
return SimRoboRioData[0].GetUserCurrent3V3();
}
HAL_Bool HAL_GetUserActive3V3(int32_t* status) {
return SimRoboRioData[0].GetUserActive3V3();
}
int32_t HAL_GetUserCurrentFaults3V3(int32_t* status) {
return SimRoboRioData[0].GetUserFaults3V3();
}
} // extern "C"

View File

@@ -0,0 +1,109 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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"
#include "PortsInternal.h"
using namespace hal;
namespace {
struct Relay {
uint8_t channel;
bool fwd;
};
}
static IndexedHandleResource<HAL_RelayHandle, Relay, kNumRelayChannels,
HAL_HandleEnum::Relay>
relayHandles;
extern "C" {
HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
int32_t* status) {
if (*status != 0) return HAL_kInvalidHandle;
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
auto handle = relayHandles.Allocate(channel, status);
if (*status != 0)
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
auto port = relayHandles.Get(handle);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
if (!fwd) {
// Subtract number of headers to put channel in range
channel -= kNumRelayHeaders;
port->fwd = false; // set to reverse
SimRelayData[channel].SetInitializedReverse(true);
} else {
port->fwd = true; // set to forward
SimRelayData[channel].SetInitializedForward(true);
}
port->channel = static_cast<uint8_t>(channel);
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);
}
HAL_Bool HAL_CheckRelayChannel(int32_t channel) {
// 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);
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);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
if (port->fwd)
return SimRelayData[port->channel].GetForward();
else
return SimRelayData[port->channel].GetReverse();
}
}

View File

@@ -0,0 +1,54 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/SPI.h"
void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {}
int32_t HAL_TransactionSPI(HAL_SPIPort port, uint8_t* dataToSend,
uint8_t* dataReceived, int32_t size) {
return 0;
}
int32_t HAL_WriteSPI(HAL_SPIPort port, uint8_t* dataToSend, int32_t sendSize) {
return 0;
}
int32_t HAL_ReadSPI(HAL_SPIPort port, uint8_t* buffer, int32_t count) {
return 0;
}
void HAL_CloseSPI(HAL_SPIPort port) {}
void HAL_SetSPISpeed(HAL_SPIPort port, int32_t speed) {}
void HAL_SetSPIOpts(HAL_SPIPort port, HAL_Bool msbFirst,
HAL_Bool sampleOnTrailing, HAL_Bool clkIdleHigh) {}
void HAL_SetSPIChipSelectActiveHigh(HAL_SPIPort port, int32_t* status) {}
void HAL_SetSPIChipSelectActiveLow(HAL_SPIPort port, int32_t* status) {}
int32_t HAL_GetSPIHandle(HAL_SPIPort port) { return 0; }
void HAL_SetSPIHandle(HAL_SPIPort port, int32_t handle) {}
void HAL_InitSPIAccumulator(HAL_SPIPort port, int32_t period, int32_t cmd,
int32_t xferSize, int32_t validMask,
int32_t validValue, int32_t dataShift,
int32_t dataSize, HAL_Bool isSigned,
HAL_Bool bigEndian, int32_t* status) {}
void HAL_FreeSPIAccumulator(HAL_SPIPort port, int32_t* status) {}
void HAL_ResetSPIAccumulator(HAL_SPIPort port, int32_t* status) {}
void HAL_SetSPIAccumulatorCenter(HAL_SPIPort port, int32_t center,
int32_t* status) {}
void HAL_SetSPIAccumulatorDeadband(HAL_SPIPort port, int32_t deadband,
int32_t* status) {}
int32_t HAL_GetSPIAccumulatorLastValue(HAL_SPIPort port, int32_t* status) {
return 0;
}
int64_t HAL_GetSPIAccumulatorValue(HAL_SPIPort port, int32_t* status) {
return 0;
}
int64_t HAL_GetSPIAccumulatorCount(HAL_SPIPort port, int32_t* status) {
return 0;
}
double HAL_GetSPIAccumulatorAverage(HAL_SPIPort port, int32_t* status) {
return 0;
}
void HAL_GetSPIAccumulatorOutput(HAL_SPIPort port, int64_t* value,
int64_t* count, int32_t* status) {}

View File

@@ -0,0 +1,64 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/SerialPort.h"
extern "C" {
void HAL_InitializeSerialPort(HAL_SerialPort port, int32_t* status) {}
void HAL_SetSerialBaudRate(HAL_SerialPort port, int32_t baud, int32_t* status) {
}
void HAL_SetSerialDataBits(HAL_SerialPort port, int32_t bits, int32_t* status) {
}
void HAL_SetSerialParity(HAL_SerialPort port, int32_t parity, int32_t* status) {
}
void HAL_SetSerialStopBits(HAL_SerialPort port, int32_t stopBits,
int32_t* status) {}
void HAL_SetSerialWriteMode(HAL_SerialPort port, int32_t mode,
int32_t* status) {}
void HAL_SetSerialFlowControl(HAL_SerialPort port, int32_t flow,
int32_t* status) {}
void HAL_SetSerialTimeout(HAL_SerialPort port, double timeout,
int32_t* status) {}
void HAL_EnableSerialTermination(HAL_SerialPort port, char terminator,
int32_t* status) {}
void HAL_DisableSerialTermination(HAL_SerialPort port, int32_t* status) {}
void HAL_SetSerialReadBufferSize(HAL_SerialPort port, int32_t size,
int32_t* status) {}
void HAL_SetSerialWriteBufferSize(HAL_SerialPort port, int32_t size,
int32_t* status) {}
int32_t HAL_GetSerialBytesReceived(HAL_SerialPort port, int32_t* status) {
return 0;
}
int32_t HAL_ReadSerial(HAL_SerialPort port, char* buffer, int32_t count,
int32_t* status) {
return 0;
}
int32_t HAL_WriteSerial(HAL_SerialPort port, const char* buffer, int32_t count,
int32_t* status) {
return 0;
}
void HAL_FlushSerial(HAL_SerialPort port, int32_t* status) {}
void HAL_ClearSerial(HAL_SerialPort port, int32_t* status) {}
void HAL_CloseSerial(HAL_SerialPort port, int32_t* status) {}
}

View File

@@ -0,0 +1,120 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016-2017. 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 "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/IndexedHandleResource.h"
#include "MockData/PCMDataInternal.h"
#include "PortsInternal.h"
namespace {
struct Solenoid {
uint8_t module;
uint8_t channel;
};
}
using namespace hal;
static IndexedHandleResource<HAL_SolenoidHandle, Solenoid,
kNumPCMModules * kNumSolenoidChannels,
HAL_HandleEnum::Solenoid>
solenoidHandles;
extern "C" {
HAL_SolenoidHandle HAL_InitializeSolenoidPort(HAL_PortHandle portHandle,
int32_t* status) {
int16_t channel = getPortHandleChannel(portHandle);
int16_t module = getPortHandleModule(portHandle);
if (channel == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
if (!HAL_CheckSolenoidChannel(channel)) {
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
if (!HAL_CheckSolenoidModule(module)) {
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
auto handle =
solenoidHandles.Allocate(module * kNumSolenoidChannels + channel, status);
if (handle == HAL_kInvalidHandle) { // out of resources
*status = NO_AVAILABLE_RESOURCES;
return HAL_kInvalidHandle;
}
auto solenoidPort = solenoidHandles.Get(handle);
if (solenoidPort == nullptr) { // would only occur on thread issues
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
solenoidPort->module = static_cast<uint8_t>(module);
solenoidPort->channel = static_cast<uint8_t>(channel);
HALSIM_SetPCMSolenoidInitialized(module, channel, true);
return handle;
}
void HAL_FreeSolenoidPort(HAL_SolenoidHandle solenoidPortHandle) {
auto port = solenoidHandles.Get(solenoidPortHandle);
if (port == nullptr) return;
solenoidHandles.Free(solenoidPortHandle);
HALSIM_SetPCMSolenoidInitialized(port->module, port->channel, false);
}
HAL_Bool HAL_CheckSolenoidModule(int32_t module) {
return module < kNumPCMModules && module >= 0;
}
HAL_Bool HAL_CheckSolenoidChannel(int32_t channel) {
return channel < kNumSolenoidChannels && channel >= 0;
}
HAL_Bool HAL_GetSolenoid(HAL_SolenoidHandle solenoidPortHandle,
int32_t* status) {
auto port = solenoidHandles.Get(solenoidPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
return HALSIM_GetPCMSolenoidOutput(port->module, port->channel);
}
int32_t HAL_GetAllSolenoids(int32_t module, int32_t* status) {
int32_t total = 0;
for (int i = 0; i < kNumSolenoidChannels; i++) {
int32_t channel = HALSIM_GetPCMSolenoidOutput(module, i) ? 1 : 0;
total = total + (channel << i);
}
return total;
}
void HAL_SetSolenoid(HAL_SolenoidHandle solenoidPortHandle, HAL_Bool value,
int32_t* status) {
auto port = solenoidHandles.Get(solenoidPortHandle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
HALSIM_SetPCMSolenoidOutput(port->module, port->channel, value);
}
int32_t HAL_GetPCMSolenoidBlackList(int32_t module, int32_t* status) {
return 0;
}
HAL_Bool HAL_GetPCMSolenoidVoltageStickyFault(int32_t module, int32_t* status) {
return 0;
}
HAL_Bool HAL_GetPCMSolenoidVoltageFault(int32_t module, int32_t* status) {
return 0;
}
void HAL_ClearAllPCMStickyFaults(int32_t module, int32_t* status) {}
}

View File

@@ -0,0 +1,24 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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/Threads.h"
int32_t HAL_GetThreadPriority(NativeThreadHandle handle, HAL_Bool* isRealTime,
int32_t* status) {
return 0;
}
int32_t HAL_GetCurrentThreadPriority(HAL_Bool* isRealTime, int32_t* status) {
return 0;
}
HAL_Bool HAL_SetThreadPriority(NativeThreadHandle handle, HAL_Bool realTime,
int32_t priority, int32_t* status) {
return true;
}
HAL_Bool HAL_SetCurrentThreadPriority(HAL_Bool realTime, int32_t priority,
int32_t* status) {
return true;
}