mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Adding callbacks for notifying when the distance per pulse changes (#861)
This commit is contained in:
committed by
Peter Johnson
parent
ee33296e1f
commit
ca36d1dce6
@@ -38,6 +38,8 @@ void EncoderData::ResetData() {
|
||||
m_reverseDirectionCallbacks = nullptr;
|
||||
m_samplesToAverage = 0;
|
||||
m_samplesToAverageCallbacks = nullptr;
|
||||
m_distancePerPulse = 0;
|
||||
m_distancePerPulseCallbacks = nullptr;
|
||||
}
|
||||
|
||||
int32_t EncoderData::RegisterInitializedCallback(HAL_NotifyCallback callback,
|
||||
@@ -330,6 +332,42 @@ void EncoderData::SetSamplesToAverage(int32_t samplesToAverage) {
|
||||
}
|
||||
}
|
||||
|
||||
int32_t EncoderData::RegisterDistancePerPulseCallback(
|
||||
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<wpi::mutex> lock(m_registerMutex);
|
||||
m_distancePerPulseCallbacks =
|
||||
RegisterCallback(m_distancePerPulseCallbacks, "DistancePerPulse",
|
||||
callback, param, &newUid);
|
||||
}
|
||||
if (initialNotify) {
|
||||
// We know that the callback is not null because of earlier null check
|
||||
HAL_Value value = MakeDouble(GetDistancePerPulse());
|
||||
callback("DistancePerPulse", param, &value);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
void EncoderData::CancelDistancePerPulseCallback(int32_t uid) {
|
||||
m_distancePerPulseCallbacks =
|
||||
CancelCallback(m_distancePerPulseCallbacks, uid);
|
||||
}
|
||||
|
||||
void EncoderData::InvokeDistancePerPulseCallback(HAL_Value value) {
|
||||
InvokeCallback(m_distancePerPulseCallbacks, "DistancePerPulse", &value);
|
||||
}
|
||||
|
||||
double EncoderData::GetDistancePerPulse() { return m_distancePerPulse; }
|
||||
|
||||
void EncoderData::SetDistancePerPulse(double distancePerPulse) {
|
||||
double oldValue = m_distancePerPulse.exchange(distancePerPulse);
|
||||
if (oldValue != distancePerPulse) {
|
||||
InvokeDistancePerPulseCallback(MakeDouble(distancePerPulse));
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetEncoderData(int32_t index) {
|
||||
SimEncoderData[index].ResetData();
|
||||
@@ -511,5 +549,7 @@ void HALSIM_RegisterEncoderAllCallbacks(int32_t index,
|
||||
initialNotify);
|
||||
SimEncoderData[index].RegisterSamplesToAverageCallback(callback, param,
|
||||
initialNotify);
|
||||
SimEncoderData[index].RegisterDistancePerPulseCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
Reference in New Issue
Block a user