2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-11-01 23:41:30 -07:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
2020-07-15 23:48:09 -07:00
|
|
|
#include "frc/simulation/CallbackStore.h"
|
2019-11-01 23:41:30 -07:00
|
|
|
|
|
|
|
|
namespace frc {
|
2020-07-15 23:48:09 -07:00
|
|
|
|
|
|
|
|
class DutyCycle;
|
|
|
|
|
|
2019-11-01 23:41:30 -07:00
|
|
|
namespace sim {
|
2020-07-04 10:10:43 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to control a simulated duty cycle digital input.
|
|
|
|
|
*/
|
2019-11-01 23:41:30 -07:00
|
|
|
class DutyCycleSim {
|
|
|
|
|
public:
|
2020-07-04 10:10:43 -07:00
|
|
|
/**
|
|
|
|
|
* Constructs from a DutyCycle object.
|
|
|
|
|
*
|
|
|
|
|
* @param dutyCycle DutyCycle to simulate
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
explicit DutyCycleSim(const DutyCycle& dutyCycle);
|
2020-07-04 10:10:43 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a DutyCycleSim for a digital input channel.
|
|
|
|
|
*
|
|
|
|
|
* @param channel digital input channel
|
|
|
|
|
* @return Simulated object
|
|
|
|
|
* @throws std::out_of_range if no DutyCycle is configured for that channel
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
static DutyCycleSim CreateForChannel(int channel);
|
2020-07-04 10:10:43 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a DutyCycleSim for a simulated index.
|
|
|
|
|
* The index is incremented for each simulated DutyCycle.
|
|
|
|
|
*
|
|
|
|
|
* @param index simulator index
|
|
|
|
|
* @return Simulated object
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
static DutyCycleSim CreateForIndex(int index);
|
2019-11-01 23:41:30 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback to be run when this duty cycle input is initialized.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback
|
|
|
|
|
* @param initialNotify whether to run the callback with the initial state
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
|
|
|
|
[[nodiscard]] std::unique_ptr<CallbackStore> RegisterInitializedCallback(
|
2020-07-15 23:48:09 -07:00
|
|
|
NotifyCallback callback, bool initialNotify);
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Check whether this duty cycle input has been initialized.
|
|
|
|
|
*
|
|
|
|
|
* @return true if initialized
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
bool GetInitialized() const;
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Define whether this duty cycle input has been initialized.
|
|
|
|
|
*
|
|
|
|
|
* @param initialized whether this object is initialized
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetInitialized(bool initialized);
|
2019-11-01 23:41:30 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback to be run whenever the frequency changes.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback
|
|
|
|
|
* @param initialNotify whether to call the callback with the initial state
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
|
|
|
|
[[nodiscard]] std::unique_ptr<CallbackStore> RegisterFrequencyCallback(
|
2020-07-15 23:48:09 -07:00
|
|
|
NotifyCallback callback, bool initialNotify);
|
2019-11-01 23:41:30 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Measure the frequency.
|
|
|
|
|
*
|
|
|
|
|
* @return the duty cycle frequency
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
int GetFrequency() const;
|
2019-11-01 23:41:30 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the duty cycle frequency.
|
|
|
|
|
*
|
|
|
|
|
* @param frequency the new frequency
|
|
|
|
|
*/
|
2021-10-14 18:09:38 -07:00
|
|
|
void SetFrequency(int frequency);
|
2019-11-01 23:41:30 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback to be run whenever the output changes.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback
|
|
|
|
|
* @param initialNotify whether to call the callback with the initial state
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
|
|
|
|
[[nodiscard]] std::unique_ptr<CallbackStore> RegisterOutputCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify);
|
2019-11-01 23:41:30 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Measure the output from this duty cycle port.
|
|
|
|
|
*
|
|
|
|
|
* @return the output value
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
double GetOutput() const;
|
2019-11-01 23:41:30 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the duty cycle output.
|
|
|
|
|
*
|
|
|
|
|
* @param output the new output value
|
|
|
|
|
*/
|
2021-10-14 18:09:38 -07:00
|
|
|
void SetOutput(double output);
|
2019-11-01 23:41:30 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Reset all simulation data for the duty cycle output.
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void ResetData();
|
2019-11-01 23:41:30 -07:00
|
|
|
|
|
|
|
|
private:
|
2020-07-04 10:10:43 -07:00
|
|
|
explicit DutyCycleSim(int index) : m_index{index} {}
|
|
|
|
|
|
2019-11-01 23:41:30 -07:00
|
|
|
int m_index;
|
|
|
|
|
};
|
|
|
|
|
} // namespace sim
|
|
|
|
|
} // namespace frc
|