#pragma once template class AutoStart { public: void begin(MeasurementT minThreshold, MeasurementT maxThreshold, uint32_t maxNumMeasurementsBetweenPeeks) { minThreshold_ = minThreshold; maxThreshold_ = maxThreshold; maxNumMeasurementsBetweenPeeks_ = maxNumMeasurementsBetweenPeeks; measurementsSinceLastPeek_ = maxNumMeasurementsBetweenPeeks + 1; up_ = false; } bool autoStart(MeasurementT measurement) { 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_; }; // ------------------------------------------------------------------------------------------ template class AutoStop { public: void begin(MeasurementT threshold, uint32_t numMeasurementsBelowThreshold) { threshold_ = threshold; numMeasurementsBelowThreshold_ = numMeasurementsBelowThreshold; counter_ = 0; } 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_; };