summaryrefslogtreecommitdiff
path: root/sensorPlugins/heartrateSensor.cpp
diff options
context:
space:
mode:
authorArseniy-Movshev <dodoradio@outlook.com>2023-03-06 20:59:36 +0000
committerArseniy-Movshev <dodoradio@outlook.com>2023-03-07 21:44:42 +0000
commit222f776d9f9d1f362038d26f835c96918a7af5fe (patch)
treee1c5b01753d52603bc9d8e5cb91c1cf3287a8ba9 /sensorPlugins/heartrateSensor.cpp
parent31a0592f40ccbe0ed31abcd8bf39c2c00dab699a (diff)
Refactor sensors into separate files
- this should make things a lot easier to maintain. instead of having everything in the same file, each sensor provides a constructor that sets it up correctly etc. - this should also get the step counter running when the service starts, which is necessary, since the sensor won't count on most devices unless the service is running. - this also temporarily removes the gps module from the service. it's not implemented right now anyway.
Diffstat (limited to 'sensorPlugins/heartrateSensor.cpp')
-rw-r--r--sensorPlugins/heartrateSensor.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/sensorPlugins/heartrateSensor.cpp b/sensorPlugins/heartrateSensor.cpp
new file mode 100644
index 0000000..3ae18ee
--- /dev/null
+++ b/sensorPlugins/heartrateSensor.cpp
@@ -0,0 +1,53 @@
+#include <QDateTime>
+#include <QTimer>
+#include <QtSensors/QHrmSensor>
+#include <QDebug>
+#include <QString>
+
+#include "../logger.h"
+
+#include "heartrateSensor.h"
+
+HeartrateSensorPlugin::HeartrateSensorPlugin(QObject *parent, int initInterval) :
+ QObject(parent){
+ interval = initInterval;
+
+ hrmSensor = new QHrmSensor(this);
+ connect(hrmSensor,SIGNAL(readingChanged()),this,SLOT(finishRecording()));
+
+ qDebug() << "heartrate sensor is enabled. interval is (ms) " << interval;
+ recordIntervalTimer = new QTimer(this);
+ connect(recordIntervalTimer,SIGNAL(timeout()),this,SLOT(triggerRecording()));
+ recordIntervalTimer->setSingleShot(true);
+ recordIntervalTimer->start(interval);
+ lastRecordTime = QDateTime::currentDateTime();
+}
+
+void HeartrateSensorPlugin::timeUpdate() {
+ uint elapsed = QDateTime::currentMSecsSinceEpoch() - lastRecordTime.toMSecsSinceEpoch();
+ qDebug() << "time until next steps recording" << recordIntervalTimer->remainingTime() << " elapsed = " << elapsed << " lastRecordTime " << lastRecordTime.toMSecsSinceEpoch();
+ if (elapsed > interval) { //if too much time has passed, reset the timer and record
+ triggerRecording();
+ lastRecordTime = QDateTime::currentDateTime();
+ } else { //otherwise, restart the timer and compensate for time spent in suspend
+ recordIntervalTimer->start(interval - elapsed);
+ }
+}
+
+void HeartrateSensorPlugin::triggerRecording() {
+ qDebug() << "heartrate interval recording";
+ recordIntervalTimer->start(interval);
+ hrmSensor->start();
+}
+
+void HeartrateSensorPlugin::finishRecording() {
+ qDebug() << "bpm update received";
+ int bpm = hrmSensor->reading()->bpm();
+ qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << " : " << bpm << hrmSensor->status() << hrmSensor->isActive();
+ if ((bpm == 0) || (hrmSensor->status() < 3)) {
+ qDebug() << "hrm sensor accuracy insufficient. waiting.";
+ return;
+ }
+ writeReadingToFile(QString::number(QDateTime::currentSecsSinceEpoch()) + " : " + QString::number(bpm) + "\n", "hrmOut.txt");
+ hrmSensor->stop();
+}