Settings API

This commit is contained in:
Martin Bauer
2020-09-03 15:23:15 +02:00
parent adf31dab4f
commit 27eea7dd1c
4 changed files with 144 additions and 65 deletions

View File

@@ -4,6 +4,8 @@ template <typename MeasurementT>
class AutoStart
{
public:
AutoStart() : enabled_(false) {}
void begin(MeasurementT minThreshold, MeasurementT maxThreshold, uint32_t maxNumMeasurementsBetweenPeeks)
{
minThreshold_ = minThreshold;
@@ -11,10 +13,18 @@ public:
maxNumMeasurementsBetweenPeeks_ = maxNumMeasurementsBetweenPeeks;
measurementsSinceLastPeek_ = maxNumMeasurementsBetweenPeeks + 1;
up_ = false;
enabled_ = true;
}
void enable(bool enable=true) {
enabled_ = enable;
}
bool enabled() const { return enabled_; };
bool autoStart(MeasurementT measurement)
{
if(!enabled_)
return false;
measurementsSinceLastPeek_ += 1;
if (!up_ && measurement > maxThreshold_)
{
@@ -38,6 +48,8 @@ private:
uint32_t measurementsSinceLastPeek_;
// if measurements have been below min threshold (false) or above max threshold (true)
bool up_;
bool enabled_;
};
// ------------------------------------------------------------------------------------------
@@ -46,13 +58,22 @@ template <typename MeasurementT>
class AutoStop
{
public:
AutoStop() : enabled_(false) {}
void begin(MeasurementT threshold, uint32_t numMeasurementsBelowThreshold)
{
threshold_ = threshold;
numMeasurementsBelowThreshold_ = numMeasurementsBelowThreshold;
counter_ = 0;
enabled_ = true;
}
void enable(bool enable=true) {
enabled_ = enable;
}
bool enabled() const { return enabled_; };
bool autoStop(MeasurementT measurement)
{
if (measurement > threshold_)
@@ -76,4 +97,5 @@ private:
MeasurementT threshold_;
uint32_t numMeasurementsBelowThreshold_;
uint32_t counter_;
bool enabled_;
};

View File

@@ -28,6 +28,11 @@ public:
void iteration();
void enableAutoStart(bool enable) { autoStart_.enable(enable); }
void enableAutoStop(bool enable) { autoStop_.enable(enable); }
bool autoStartEnabled() const { return autoStart_.enabled(); };
bool autoStopEnabled() const { return autoStop_.enabled(); }
private:
void onMeasurementTaken(MeasurementType measurement);
@@ -75,13 +80,15 @@ void SessionManager<SessionT>::begin(int scaleDoutPin, int scaleSckPin, uint8_t
MeasurementType autoStartMinTh, MeasurementType autoStartMaxTh, uint32_t autoStartMeasuresBetweenPeaks,
MeasurementType autoStopTh, uint32_t autoStopNumMeasures)
{
if(measuring_)
stopMeasurements();
scaleDoutPin_ = scaleDoutPin;
scaleSckPin_ = scaleSckPin;
tareAvgCount_ = tareAvgCount;
timeClient_.begin();
timeClient_.update();
tare();
session_.init(timeClient_.getEpochTime());
autoStart_.begin(autoStartMinTh, autoStartMaxTh, autoStartMeasuresBetweenPeaks);
@@ -107,51 +114,6 @@ void SessionManager<SessionT>::stopMeasurements()
measuring_ = false;
}
/*
template <typename SessionT>
void SessionManager<SessionT>::iteration()
{
if (!measuring_)
{
delay(1);
return; // give control to HTTP server thread
}
MeasurementType measurement = -1;
bool measurementDone = false;
while (!measurementDone)
measurementDone = scale_.measure(measurement);
bool addPointSuccessful = session_.addPoint(measurement);
//Serial.printf("Measured: %d\n", measurement);
if (!addPointSuccessful)
{
Serial.println("Maximum time of session reached - stopping");
stopMeasurements();
delay(1); // give control to HTTP server thread
return;
}
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)
session_.addPoint(measurement);
delay(CONFIG_MEASURE_DELAY * (skipped + 1) - cycleDuration);
}
}
lastCallTime_ = millis();
}
*/
template <typename SessionT>
void SessionManager<SessionT>::iteration()
{

View File

@@ -93,16 +93,13 @@ private:
Measurement_T *startPtr = chunk->getDataPointer() + existingMeasurements;
file.write((uint8_t *)(startPtr), measurementsToWrite * sizeof(Measurement_T));
Serial.printf(" %ld Incr Save: before header patch\n", millis());
file.seek(arrayHeaderOffset);
StreamingMsgPackEncoder<portablefs::File> encoder(&file);
encoder.template sendArrayHeader<Measurement_T>(numMeasurements);
Serial.printf(" %ld total measurements up to now %u\n", millis(), numMeasurements);
}
else
{
Serial.println("First save");
auto file = portablefs::open(filename.c_str(), "w");
StreamingMsgPackEncoder<portablefs::File> encoder(&file);
chunk->serialize(encoder);