More configuration options
- wifi provisioning - auto start/stop of sessions
This commit is contained in:
79
firmware/lib/session/AutoStartStop.h
Normal file
79
firmware/lib/session/AutoStartStop.h
Normal file
@@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
|
||||
template <typename MeasurementT>
|
||||
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 <typename MeasurementT>
|
||||
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_;
|
||||
};
|
||||
@@ -4,18 +4,23 @@
|
||||
#include <WiFiUdp.h>
|
||||
#include <NTPClient.h>
|
||||
|
||||
#include "AutoStartStop.h"
|
||||
|
||||
template <typename SessionT>
|
||||
class SessionManager
|
||||
{
|
||||
public:
|
||||
using MeasurementType = typename SessionT::MeasurementType;
|
||||
|
||||
SessionManager(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount);
|
||||
|
||||
void begin();
|
||||
SessionManager();
|
||||
|
||||
void begin(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount, int valueRightShift, MeasurementType autoStartMinTh, MeasurementType autoStartMaxTh, uint32_t autoStartMeasuresBetweenPeaks,
|
||||
MeasurementType autoStopTh, uint32_t autoStopNumMeasures);
|
||||
|
||||
void tare();
|
||||
long tareOffset() const { return scale_.offset(); };
|
||||
int valueRightShift() const { return scale_.valueRightShift(); }
|
||||
|
||||
void startMeasurements();
|
||||
void stopMeasurements();
|
||||
bool isMeasuring() const { return measuring_; }
|
||||
@@ -24,10 +29,12 @@ public:
|
||||
void iteration();
|
||||
|
||||
private:
|
||||
void onMeasurementTaken(MeasurementType measurement);
|
||||
|
||||
WiFiUDP ntpUDP_;
|
||||
NTPClient timeClient_;
|
||||
|
||||
Scale<CONFIG_VALUE_DIVIDER> scale_;
|
||||
Scale scale_;
|
||||
//MockScale scale;
|
||||
SessionT session_;
|
||||
bool measuring_;
|
||||
@@ -36,45 +43,55 @@ private:
|
||||
int scaleDoutPin_;
|
||||
int scaleSckPin_;
|
||||
uint8_t tareAvgCount_;
|
||||
int valueRightShift_;
|
||||
|
||||
AutoStart<MeasurementT> autoStart_;
|
||||
AutoStop<MeasurementT> autoStop_;
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
template <typename SessionT>
|
||||
SessionManager<SessionT>::SessionManager(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount)
|
||||
SessionManager<SessionT>::SessionManager()
|
||||
: timeClient_(ntpUDP_, "pool.ntp.org"),
|
||||
measuring_(false),
|
||||
lastCallTime_(0),
|
||||
scaleDoutPin_(scaleDoutPin),
|
||||
scaleSckPin_(scaleSckPin),
|
||||
tareAvgCount_(tareAvgCount)
|
||||
lastCallTime_(0)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::tare()
|
||||
{
|
||||
if(measuring_)
|
||||
if (measuring_)
|
||||
stopMeasurements();
|
||||
Serial.println("Beginning tare");
|
||||
scale_.begin(scaleDoutPin_, scaleSckPin_);
|
||||
scale_.begin(scaleDoutPin_, scaleSckPin_, valueRightShift_);
|
||||
scale_.tare(CONFIG_TARE_AVG_COUNT);
|
||||
Serial.println("Finished tare");
|
||||
}
|
||||
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::begin()
|
||||
void SessionManager<SessionT>::begin(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount, int valueRightShift,
|
||||
MeasurementType autoStartMinTh, MeasurementType autoStartMaxTh, uint32_t autoStartMeasuresBetweenPeaks,
|
||||
MeasurementType autoStopTh, uint32_t autoStopNumMeasures)
|
||||
{
|
||||
scaleDoutPin_ = scaleDoutPin;
|
||||
scaleSckPin_ = scaleSckPin;
|
||||
tareAvgCount_ = tareAvgCount;
|
||||
|
||||
timeClient_.begin();
|
||||
timeClient_.update();
|
||||
tare();
|
||||
session_.init(timeClient_.getEpochTime());
|
||||
|
||||
autoStart_.begin(autoStartMinTh, autoStartMaxTh, autoStartMeasuresBetweenPeaks);
|
||||
autoStop_.begin(autoStopTh, autoStopNumMeasures);
|
||||
}
|
||||
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::startMeasurements()
|
||||
{
|
||||
if(measuring_ == true)
|
||||
if (measuring_ == true)
|
||||
return;
|
||||
measuring_ = true;
|
||||
lastCallTime_ = 0;
|
||||
@@ -84,23 +101,25 @@ void SessionManager<SessionT>::startMeasurements()
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::stopMeasurements()
|
||||
{
|
||||
if(measuring_ == false)
|
||||
if (measuring_ == false)
|
||||
return;
|
||||
session_.finalize();
|
||||
measuring_ = false;
|
||||
}
|
||||
|
||||
/*
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::iteration()
|
||||
{
|
||||
if (!measuring_) {
|
||||
if (!measuring_)
|
||||
{
|
||||
delay(1);
|
||||
return; // give control to HTTP server thread
|
||||
}
|
||||
|
||||
uint16_t measurement = -1;
|
||||
MeasurementType measurement = -1;
|
||||
bool measurementDone = false;
|
||||
while(!measurementDone)
|
||||
while (!measurementDone)
|
||||
measurementDone = scale_.measure(measurement);
|
||||
bool addPointSuccessful = session_.addPoint(measurement);
|
||||
//Serial.printf("Measured: %d\n", measurement);
|
||||
@@ -130,4 +149,68 @@ void SessionManager<SessionT>::iteration()
|
||||
}
|
||||
}
|
||||
lastCallTime_ = millis();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::iteration()
|
||||
{
|
||||
MeasurementType measurement = -1;
|
||||
bool measurementDone = false;
|
||||
while (!measurementDone)
|
||||
measurementDone = scale_.measure(measurement);
|
||||
|
||||
onMeasurementTaken(measurement);
|
||||
|
||||
if (lastCallTime_ != 0)
|
||||
{
|
||||
const long cycleDuration = millis() - lastCallTime_;
|
||||
if (cycleDuration <= CONFIG_MEASURE_DELAY)
|
||||
{
|
||||
delay(CONFIG_MEASURE_DELAY - cycleDuration);
|
||||
}
|
||||
else
|
||||
{
|
||||
const long skipped = (cycleDuration / CONFIG_MEASURE_DELAY);
|
||||
Serial.printf("Measurements skipped: %ld, cycleDuration %ld\n", skipped, cycleDuration);
|
||||
|
||||
for (int i = 0; i < skipped; ++i)
|
||||
onMeasurementTaken(measurement);
|
||||
|
||||
delay(CONFIG_MEASURE_DELAY * (skipped + 1) - cycleDuration);
|
||||
}
|
||||
}
|
||||
lastCallTime_ = millis();
|
||||
}
|
||||
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::onMeasurementTaken(MeasurementType measurement)
|
||||
{
|
||||
if (measuring_)
|
||||
{
|
||||
bool autoStop = autoStop_.autoStop(measurement);
|
||||
if (autoStop)
|
||||
{
|
||||
Serial.println("Auto stop");
|
||||
stopMeasurements();
|
||||
return;
|
||||
}
|
||||
|
||||
bool addPointSuccessful = session_.addPoint(measurement);
|
||||
if (!addPointSuccessful)
|
||||
{
|
||||
Serial.println("Maximum time of session reached - stopping");
|
||||
stopMeasurements();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (autoStart_.autoStart(measurement))
|
||||
{
|
||||
Serial.println("Auto start");
|
||||
startMeasurements();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user