mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-28 02:11:43 +00:00
[sim] Add WPILib-class-taking constructors (#2538)
When not direct mapped, make index constructors private and add factory functions for channel and index. Co-authored-by: GabrielDeml <gabrielddeml@gmail.com>
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include <hal/simulation/AnalogOutData.h>
|
||||
|
||||
#include "CallbackStore.h"
|
||||
#include "frc/AnalogOutput.h"
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
|
||||
/**
|
||||
* Class to control a simulated analog output.
|
||||
*/
|
||||
class AnalogOutputSim {
|
||||
public:
|
||||
/**
|
||||
* Constructs from an AnalogOutput object.
|
||||
*
|
||||
* @param analogOutput AnalogOutput to simulate
|
||||
*/
|
||||
explicit AnalogOutputSim(const AnalogOutput& analogOutput)
|
||||
: m_index{analogOutput.GetChannel()} {}
|
||||
|
||||
/**
|
||||
* Constructs from an analog output channel number.
|
||||
*
|
||||
* @param channel Channel number
|
||||
*/
|
||||
explicit AnalogOutputSim(int channel) : m_index{channel} {}
|
||||
|
||||
std::unique_ptr<CallbackStore> RegisterVoltageCallback(
|
||||
NotifyCallback callback, bool initialNotify) {
|
||||
auto store = std::make_unique<CallbackStore>(
|
||||
m_index, -1, callback, &HALSIM_CancelAnalogOutVoltageCallback);
|
||||
store->SetUid(HALSIM_RegisterAnalogOutVoltageCallback(
|
||||
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
||||
return store;
|
||||
}
|
||||
|
||||
double GetVoltage() const { return HALSIM_GetAnalogOutVoltage(m_index); }
|
||||
|
||||
void SetVoltage(double voltage) {
|
||||
HALSIM_SetAnalogOutVoltage(m_index, voltage);
|
||||
}
|
||||
|
||||
std::unique_ptr<CallbackStore> RegisterInitializedCallback(
|
||||
NotifyCallback callback, bool initialNotify) {
|
||||
auto store = std::make_unique<CallbackStore>(
|
||||
m_index, -1, callback, &HALSIM_CancelAnalogOutInitializedCallback);
|
||||
store->SetUid(HALSIM_RegisterAnalogOutInitializedCallback(
|
||||
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
||||
return store;
|
||||
}
|
||||
|
||||
bool GetInitialized() const {
|
||||
return HALSIM_GetAnalogOutInitialized(m_index);
|
||||
}
|
||||
|
||||
void SetInitialized(bool initialized) {
|
||||
HALSIM_SetAnalogOutInitialized(m_index, initialized);
|
||||
}
|
||||
|
||||
void ResetData() { HALSIM_ResetAnalogOutData(m_index); }
|
||||
|
||||
private:
|
||||
int m_index;
|
||||
};
|
||||
} // namespace sim
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user