Replace arduino-timer with PositionWatcher
This commit is contained in:
@@ -51,7 +51,7 @@ void Configurator::addShadeConfiguredCallback(std::function<void (Shade)> callba
|
||||
bool Configurator::tryBuild(std::string key) {
|
||||
if (auto id = discoveredIds.find(key); id != discoveredIds.end()) {
|
||||
if (auto friendlyName = discoveredFriendlyNames.find(key); friendlyName != discoveredFriendlyNames.end()) {
|
||||
auto shade = Shade{id->second, key, friendlyName->second, -1, -1, 0, 0, nullptr};
|
||||
auto shade = Shade{id->second, key, friendlyName->second, "stopped", -1, -1};
|
||||
for (size_t i = 0; i < shadeConfiguredCallbacks.size(); i++) {
|
||||
shadeConfiguredCallbacks[i](shade);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
31
lib/position_watcher/PositionWatcher.h
Normal file
31
lib/position_watcher/PositionWatcher.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef POSITION_WATCHER_H
|
||||
#define POSITION_WATCHER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
class PositionWatcher
|
||||
{
|
||||
private:
|
||||
bool isActive;
|
||||
|
||||
int8_t targetValue;
|
||||
int8_t lastValue;
|
||||
int8_t consecutiveDuplicates;
|
||||
|
||||
uint32_t pollingInterval;
|
||||
|
||||
int8_t maxAttempts;
|
||||
int8_t maxConsecutiveDuplicates;
|
||||
|
||||
int8_t currentAttempt;
|
||||
uint32_t lastPollTime;
|
||||
|
||||
public:
|
||||
void start(int8_t newTargetValue);
|
||||
bool shouldFetch();
|
||||
bool isWatching();
|
||||
void fetchQueued();
|
||||
void fetchReceived(int8_t value);
|
||||
};
|
||||
|
||||
#endif // POSITION_WATCHER_H
|
||||
@@ -8,11 +8,13 @@ struct Shade {
|
||||
uint16_t ID;
|
||||
std::string key;
|
||||
std::string friendlyName;
|
||||
int8_t lastTargetPosition;
|
||||
|
||||
std::string state;
|
||||
|
||||
int8_t lastPosition;
|
||||
uint8_t samePositionCount;
|
||||
uint8_t positionFetchCount;
|
||||
void* timer;
|
||||
int8_t lastBattery;
|
||||
|
||||
bool modified;
|
||||
};
|
||||
|
||||
#endif // SHADE_H
|
||||
33
lib/shade_command/ShadeCommand.h
Normal file
33
lib/shade_command/ShadeCommand.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef SHADE_COMMAND_H
|
||||
#define SHADE_COMMAND_H
|
||||
|
||||
#include <variant>
|
||||
#include <stdint.h>
|
||||
#include <typeindex>
|
||||
|
||||
struct OpenCommand {
|
||||
uint16_t shadeID;
|
||||
};
|
||||
|
||||
struct CloseCommand {
|
||||
uint16_t shadeID;
|
||||
};
|
||||
|
||||
struct StopCommand {
|
||||
uint16_t shadeID;
|
||||
};
|
||||
|
||||
struct SetPositionCommand {
|
||||
uint16_t shadeID;
|
||||
float percentage;
|
||||
};
|
||||
|
||||
struct RefreshCommand {
|
||||
uint16_t shadeID;
|
||||
};
|
||||
|
||||
|
||||
using ShadeCommand = std::variant<OpenCommand, CloseCommand, StopCommand, SetPositionCommand, RefreshCommand>;
|
||||
|
||||
|
||||
#endif // SHADE_COMMAND_H
|
||||
Reference in New Issue
Block a user