2018-05-11 12:38:23 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-06-27 22:11:24 -07:00
|
|
|
/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */
|
2018-05-11 12:38:23 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2020-06-27 22:11:24 -07:00
|
|
|
#include <hal/simulation/PWMData.h>
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
#include "CallbackStore.h"
|
2020-07-04 10:10:43 -07:00
|
|
|
#include "frc/PWM.h"
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
namespace frc {
|
|
|
|
|
namespace sim {
|
2020-07-04 10:10:43 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to control a simulated PWM output.
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
class PWMSim {
|
|
|
|
|
public:
|
2020-07-04 10:10:43 -07:00
|
|
|
/**
|
|
|
|
|
* Constructs from a PWM object.
|
|
|
|
|
*
|
|
|
|
|
* @param pwm PWM to simulate
|
|
|
|
|
*/
|
|
|
|
|
explicit PWMSim(const PWM& pwm) : m_index{pwm.GetChannel()} {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs from a PWM channel number.
|
|
|
|
|
*
|
|
|
|
|
* @param channel Channel number
|
|
|
|
|
*/
|
|
|
|
|
explicit PWMSim(int channel) : m_index{channel} {}
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterInitializedCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelPWMInitializedCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterPWMInitializedCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
bool GetInitialized() const { return HALSIM_GetPWMInitialized(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetInitialized(bool initialized) {
|
|
|
|
|
HALSIM_SetPWMInitialized(m_index, initialized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterRawValueCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelPWMRawValueCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterPWMRawValueCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
int GetRawValue() const { return HALSIM_GetPWMRawValue(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetRawValue(int rawValue) { HALSIM_SetPWMRawValue(m_index, rawValue); }
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterSpeedCallback(NotifyCallback callback,
|
|
|
|
|
bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelPWMSpeedCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterPWMSpeedCallback(m_index, &CallbackStoreThunk,
|
|
|
|
|
store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
double GetSpeed() const { return HALSIM_GetPWMSpeed(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetSpeed(double speed) { HALSIM_SetPWMSpeed(m_index, speed); }
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterPositionCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelPWMPositionCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterPWMPositionCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
double GetPosition() const { return HALSIM_GetPWMPosition(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetPosition(double position) {
|
|
|
|
|
HALSIM_SetPWMPosition(m_index, position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterPeriodScaleCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelPWMPeriodScaleCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterPWMPeriodScaleCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
int GetPeriodScale() const { return HALSIM_GetPWMPeriodScale(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetPeriodScale(int periodScale) {
|
|
|
|
|
HALSIM_SetPWMPeriodScale(m_index, periodScale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterZeroLatchCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelPWMZeroLatchCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterPWMZeroLatchCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
bool GetZeroLatch() const { return HALSIM_GetPWMZeroLatch(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetZeroLatch(bool zeroLatch) {
|
|
|
|
|
HALSIM_SetPWMZeroLatch(m_index, zeroLatch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResetData() { HALSIM_ResetPWMData(m_index); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int m_index;
|
|
|
|
|
};
|
|
|
|
|
} // namespace sim
|
|
|
|
|
} // namespace frc
|