WIP using new PositionWatcher
This commit is contained in:
53
lib/position_watcher/PositionWatcher.cpp
Normal file
53
lib/position_watcher/PositionWatcher.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "PositionWatcher.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
void PositionWatcher::start(int8_t newTargetValue)
|
||||
{
|
||||
targetValue = newTargetValue;
|
||||
lastPollTime = 0;
|
||||
pollingInterval = 2000;
|
||||
maxAttempts = 3;
|
||||
maxConsecutiveDuplicates = 2;
|
||||
consecutiveDuplicates = 0;
|
||||
currentAttempt = 0;
|
||||
isActive = true;
|
||||
}
|
||||
|
||||
bool PositionWatcher::shouldFetch()
|
||||
{
|
||||
if (isWatching() && millis() - lastPollTime > pollingInterval) {
|
||||
if (currentAttempt >= maxAttempts) {
|
||||
isActive = false;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool PositionWatcher::isWatching()
|
||||
{
|
||||
return isActive;
|
||||
}
|
||||
|
||||
void PositionWatcher::fetchQueued()
|
||||
{
|
||||
lastPollTime = millis();
|
||||
currentAttempt++;
|
||||
}
|
||||
|
||||
void PositionWatcher::fetchReceived(int8_t value)
|
||||
{
|
||||
currentAttempt = 0;
|
||||
if (value == targetValue) {
|
||||
isActive = false;
|
||||
} else if (lastValue == value) {
|
||||
consecutiveDuplicates++;
|
||||
if (consecutiveDuplicates >= maxConsecutiveDuplicates) {
|
||||
isActive = false;
|
||||
}
|
||||
} else {
|
||||
consecutiveDuplicates = 1;
|
||||
}
|
||||
lastValue = value;
|
||||
}
|
||||
Reference in New Issue
Block a user