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

133 lines
3.3 KiB
C
Raw Normal View History

2020-06-21 16:03:00 +02:00
// NTP headers
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
template <typename SessionT>
class SessionManager
{
public:
2020-06-25 22:01:53 +02:00
using MeasurementType = typename SessionT::MeasurementType;
2020-06-21 16:03:00 +02:00
SessionManager(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount);
void begin();
void tare();
long tareOffset() const { return scale_.offset(); };
2020-06-21 16:03:00 +02:00
void startMeasurements();
void stopMeasurements();
bool isMeasuring() const { return measuring_; }
SessionT &session() { return session_; }
void iteration();
private:
WiFiUDP ntpUDP_;
NTPClient timeClient_;
Scale<CONFIG_VALUE_DIVIDER> scale_;
//MockScale scale;
SessionT session_;
bool measuring_;
long lastCallTime_;
int scaleDoutPin_;
int scaleSckPin_;
uint8_t tareAvgCount_;
};
// ------------------------------------------------------------------------------------------------
template <typename SessionT>
SessionManager<SessionT>::SessionManager(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount)
: timeClient_(ntpUDP_, "pool.ntp.org"),
measuring_(false),
lastCallTime_(0),
scaleDoutPin_(scaleDoutPin),
scaleSckPin_(scaleSckPin),
tareAvgCount_(tareAvgCount)
{
}
template <typename SessionT>
void SessionManager<SessionT>::tare()
{
if(measuring_)
stopMeasurements();
2020-06-21 16:03:00 +02:00
Serial.println("Beginning tare");
scale_.begin(scaleDoutPin_, scaleSckPin_);
scale_.tare(CONFIG_TARE_AVG_COUNT);
Serial.println("Finished tare");
}
template <typename SessionT>
void SessionManager<SessionT>::begin()
{
timeClient_.begin();
timeClient_.update();
tare();
session_.init(timeClient_.getEpochTime());
}
template <typename SessionT>
void SessionManager<SessionT>::startMeasurements()
{
if(measuring_ == true)
return;
2020-06-21 16:03:00 +02:00
measuring_ = true;
lastCallTime_ = 0;
session_.init(timeClient_.getEpochTime());
}
template <typename SessionT>
void SessionManager<SessionT>::stopMeasurements()
{
if(measuring_ == false)
return;
2020-06-21 16:03:00 +02:00
session_.finalize();
measuring_ = false;
}
template <typename SessionT>
void SessionManager<SessionT>::iteration()
{
2020-06-28 10:30:01 +02:00
if (!measuring_) {
delay(1);
return; // give control to HTTP server thread
}
2020-06-21 16:03:00 +02:00
uint16_t measurement = -1;
bool measurementDone = false;
while(!measurementDone)
measurementDone = scale_.measure(measurement);
2020-06-21 16:03:00 +02:00
bool addPointSuccessful = session_.addPoint(measurement);
2020-06-28 11:16:34 +02:00
//Serial.printf("Measured: %d\n", measurement);
2020-06-21 16:03:00 +02:00
if (!addPointSuccessful)
{
Serial.println("Maximum time of session reached - stopping");
stopMeasurements();
2020-06-28 10:30:01 +02:00
delay(1); // give control to HTTP server thread
2020-06-21 16:03:00 +02:00
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);
2020-06-28 11:16:34 +02:00
Serial.printf("Measurements skipped: %ld, cycleDuration %ld\n", skipped, cycleDuration);
2020-06-21 16:03:00 +02:00
for (int i = 0; i < skipped; ++i)
session_.addPoint(measurement);
delay(CONFIG_MEASURE_DELAY * (skipped + 1) - cycleDuration);
}
}
lastCallTime_ = millis();
}