[wpilibc] Refactor AnalogTrigger to use shared_ptr (#7010)

This commit is contained in:
Ryan Blue
2024-08-29 00:34:28 -04:00
committed by GitHub
parent c9ad26b723
commit dc18f71004
2 changed files with 61 additions and 22 deletions

View File

@@ -18,24 +18,36 @@
using namespace frc;
AnalogTrigger::AnalogTrigger(int channel)
: AnalogTrigger(new AnalogInput(channel)) {
: AnalogTrigger(std::make_shared<AnalogInput>(channel)) {
m_ownsAnalog = true;
wpi::SendableRegistry::AddChild(this, m_analogInput);
wpi::SendableRegistry::AddChild(this, m_analogInput.get());
}
AnalogTrigger::AnalogTrigger(AnalogInput* input) {
m_analogInput = input;
AnalogTrigger::AnalogTrigger(AnalogInput& input)
: AnalogTrigger{{&input, wpi::NullDeleter<AnalogInput>{}}} {}
AnalogTrigger::AnalogTrigger(AnalogInput* input)
: AnalogTrigger{{input, wpi::NullDeleter<AnalogInput>{}}} {}
AnalogTrigger::AnalogTrigger(std::shared_ptr<AnalogInput> input)
: m_analogInput{std::move(input)} {
int32_t status = 0;
m_trigger = HAL_InitializeAnalogTrigger(input->m_port, &status);
FRC_CheckErrorStatus(status, "Channel {}", input->GetChannel());
m_trigger = HAL_InitializeAnalogTrigger(m_analogInput->m_port, &status);
FRC_CheckErrorStatus(status, "Channel {}", m_analogInput->GetChannel());
int index = GetIndex();
HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, index + 1);
wpi::SendableRegistry::AddLW(this, "AnalogTrigger", index);
}
AnalogTrigger::AnalogTrigger(DutyCycle* input) {
m_dutyCycle = input;
AnalogTrigger::AnalogTrigger(DutyCycle& input)
: AnalogTrigger{{&input, wpi::NullDeleter<DutyCycle>{}}} {}
AnalogTrigger::AnalogTrigger(DutyCycle* input)
: AnalogTrigger{{input, wpi::NullDeleter<DutyCycle>{}}} {}
AnalogTrigger::AnalogTrigger(std::shared_ptr<DutyCycle> input)
: m_dutyCycle{input} {
int32_t status = 0;
m_trigger = HAL_InitializeAnalogTriggerDutyCycle(input->m_handle, &status);
FRC_CheckErrorStatus(status, "Channel {}", m_dutyCycle->GetSourceChannel());
@@ -49,10 +61,6 @@ AnalogTrigger::~AnalogTrigger() {
int32_t status = 0;
HAL_CleanAnalogTrigger(m_trigger, &status);
FRC_ReportError(status, "Channel {}", GetSourceChannel());
if (m_ownsAnalog) {
delete m_analogInput;
}
}
void AnalogTrigger::SetLimitsVoltage(double lower, double upper) {

View File

@@ -31,27 +31,61 @@ class AnalogTrigger : public wpi::Sendable,
explicit AnalogTrigger(int channel);
/**
* Construct an analog trigger given an analog input.
* Construct an analog trigger using an existing analog input.
*
* This should be used in the case of sharing an analog channel between the
* trigger and an analog input object.
*
* @param input The pointer to the existing AnalogInput object
* @param input A reference to the existing AnalogInput object
*/
explicit AnalogTrigger(AnalogInput& input);
/**
* Construct an analog trigger using an existing analog input.
*
* This should be used in the case of sharing an analog channel between the
* trigger and an analog input object.
*
* @param input A pointer to the existing AnalogInput object
*/
explicit AnalogTrigger(AnalogInput* input);
/**
* Construct an analog trigger given a duty cycle input.
* Construct an analog trigger using an existing analog input.
*
* @param dutyCycle The pointer to the existing DutyCycle object
* This should be used in the case of sharing an analog channel between the
* trigger and an analog input object.
*
* @param input A shared_ptr to the existing AnalogInput object
*/
explicit AnalogTrigger(std::shared_ptr<AnalogInput> input);
/**
* Construct an analog trigger using an existing duty cycle input.
*
* @param dutyCycle A reference to the existing DutyCycle object
*/
explicit AnalogTrigger(DutyCycle& dutyCycle);
/**
* Construct an analog trigger using an existing duty cycle input.
*
* @param dutyCycle A pointer to the existing DutyCycle object
*/
explicit AnalogTrigger(DutyCycle* dutyCycle);
~AnalogTrigger() override;
/**
* Construct an analog trigger using an existing duty cycle input.
*
* @param dutyCycle A shared_ptr to the existing DutyCycle object
*/
explicit AnalogTrigger(std::shared_ptr<DutyCycle> dutyCycle);
AnalogTrigger(AnalogTrigger&&) = default;
AnalogTrigger& operator=(AnalogTrigger&&) = default;
~AnalogTrigger() override;
/**
* Set the upper and lower limits of the analog trigger.
*
@@ -139,9 +173,6 @@ class AnalogTrigger : public wpi::Sendable,
/**
* Creates an AnalogTriggerOutput object.
*
* Gets an output object that can be used for routing. Caller is responsible
* for deleting the AnalogTriggerOutput object.
*
* @param type An enum of the type of output object to create.
* @return A pointer to a new AnalogTriggerOutput object.
*/
@@ -153,9 +184,9 @@ class AnalogTrigger : public wpi::Sendable,
private:
int GetSourceChannel() const;
std::shared_ptr<AnalogInput> m_analogInput;
std::shared_ptr<DutyCycle> m_dutyCycle;
hal::Handle<HAL_AnalogTriggerHandle> m_trigger;
AnalogInput* m_analogInput = nullptr;
DutyCycle* m_dutyCycle = nullptr;
bool m_ownsAnalog = false;
};