swimtracker-firmware/firmware/lib/session/AutoStartStop.h

102 lines
2.2 KiB
C
Raw Normal View History

#pragma once
template <typename MeasurementT>
class AutoStart
{
public:
2020-09-03 15:23:15 +02:00
AutoStart() : enabled_(false) {}
void begin(MeasurementT minThreshold, MeasurementT maxThreshold, uint32_t maxNumMeasurementsBetweenPeeks)
{
minThreshold_ = minThreshold;
maxThreshold_ = maxThreshold;
maxNumMeasurementsBetweenPeeks_ = maxNumMeasurementsBetweenPeeks;
measurementsSinceLastPeek_ = maxNumMeasurementsBetweenPeeks + 1;
up_ = false;
2020-09-03 15:23:15 +02:00
enabled_ = true;
}
void enable(bool enable=true) {
enabled_ = enable;
}
2020-09-03 15:23:15 +02:00
bool enabled() const { return enabled_; };
bool autoStart(MeasurementT measurement)
{
2020-09-03 15:23:15 +02:00
if(!enabled_)
return false;
measurementsSinceLastPeek_ += 1;
if (!up_ && measurement > maxThreshold_)
{
up_ = true;
bool doAutoStart = measurementsSinceLastPeek_ < maxNumMeasurementsBetweenPeeks_;
measurementsSinceLastPeek_ = 0;
return doAutoStart;
}
if (up_ && measurement < minThreshold_)
up_ = false;
return false;
}
private:
MeasurementT minThreshold_;
MeasurementT maxThreshold_;
uint32_t maxNumMeasurementsBetweenPeeks_;
// time of last peak
uint32_t measurementsSinceLastPeek_;
// if measurements have been below min threshold (false) or above max threshold (true)
bool up_;
2020-09-03 15:23:15 +02:00
bool enabled_;
};
// ------------------------------------------------------------------------------------------
template <typename MeasurementT>
class AutoStop
{
public:
2020-09-03 15:23:15 +02:00
AutoStop() : enabled_(false) {}
void begin(MeasurementT threshold, uint32_t numMeasurementsBelowThreshold)
{
threshold_ = threshold;
numMeasurementsBelowThreshold_ = numMeasurementsBelowThreshold;
counter_ = 0;
2020-09-03 15:23:15 +02:00
enabled_ = true;
}
void enable(bool enable=true) {
enabled_ = enable;
}
2020-09-03 15:23:15 +02:00
bool enabled() const { return enabled_; };
bool autoStop(MeasurementT measurement)
{
if (measurement > threshold_)
{
counter_ = 0;
return false;
}
else
{
++counter_;
if (counter_ >= numMeasurementsBelowThreshold_)
{
counter_ = 0;
return true;
}
return false;
}
}
private:
MeasurementT threshold_;
uint32_t numMeasurementsBelowThreshold_;
uint32_t counter_;
2020-09-03 15:23:15 +02:00
bool enabled_;
};