/
calibrationhandler.cpp
127 lines (110 loc) · 3.91 KB
/
calibrationhandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
@file calibrationhandler.cpp
@brief Helper class for maintaining magnetometer calibration
<p>
Copyright (C) 2009-2010 Nokia Corporation
@author Timo Rongas <ext-timo.2.rongas@nokia.com>
@author Lihan Guo <ext-lihan.4.guo@nokia.com>
@author Antti Virtanen <antti.i.virtanen@nokia.com>
This file is part of Sensord.
Sensord is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation.
Sensord is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Sensord. If not, see <http://www.gnu.org/licenses/>.
</p>
*/
#include "calibrationhandler.h"
#include "sensormanager.h"
#include "logging.h"
#include "abstractsensor.h"
#include "config.h"
const QString CalibrationHandler::SENSOR_NAME("magnetometersensor");
CalibrationHandler::CalibrationHandler(QObject* parent) :
QObject(parent),
m_sensor(NULL),
m_sessionId(-1),
m_level(-1)
{
m_timer.setSingleShot(true);
m_calibRate = SensorFrameworkConfig::configuration()->value<int>("magnetometer/calibration_rate", 100);
m_calibTimeout = SensorFrameworkConfig::configuration()->value<int>("magnetometer/calibration_timeout", 60000);
}
CalibrationHandler::~CalibrationHandler()
{
if (m_sessionId > 0)
{
SensorManager& sm = SensorManager::instance();
sm.releaseSensor(SENSOR_NAME, m_sessionId);
m_sensor = NULL;
}
}
bool CalibrationHandler::initiateSession()
{
SensorManager& sm = SensorManager::instance();
sensordLogD() << "Loading MagnetometerSensorPlugin";
if (!sm.loadPlugin(SENSOR_NAME))
{
sensordLogW() << "Failed to load magnetometer plug-in";
return false;
}
m_sessionId = sm.requestSensor(SENSOR_NAME);
if (m_sessionId <= 0)
{
sensordLogW() << "Failed to get session for magnetometersensor.";
}
else
{
m_sensor = reinterpret_cast<MagnetometerSensorChannel*>(sm.getSensorInstance(SENSOR_NAME)->sensor_);
}
// Connect timeout
connect(&m_timer, SIGNAL(timeout()), this, SLOT(calibrationTimeout()));
resumeCalibration();
return true;
}
void CalibrationHandler::sampleReceived(const MagneticField& sample)
{
//Reset timer when level changes
if ((sample.level() != m_level))
{
m_level = sample.level();
m_timer.start(m_calibTimeout);
}
}
void CalibrationHandler::stopCalibration()
{
if (m_sensor && m_timer.isActive())
{
m_timer.stop();
m_sensor->setStandbyOverrideRequest(m_sessionId, false);
m_sensor->stop();
disconnect(m_sensor, SIGNAL(internalData(const MagneticField&)), this, SLOT(sampleReceived(const MagneticField&)));
sensordLogD() << "Stopping magnetometer background calibration due to PSM on";
}
}
void CalibrationHandler::calibrationTimeout()
{
if (m_sensor)
{
sensordLogD() << "Stopping magnetometer background calibration due to timeout.";
m_sensor->setStandbyOverrideRequest(m_sessionId, false);
m_sensor->stop();
disconnect(m_sensor, SIGNAL(internalData(const MagneticField&)), this, SLOT(sampleReceived(const MagneticField&)));
}
}
void CalibrationHandler::resumeCalibration()
{
sensordLogD() << "Resuming magnetometer background calibration";
if (m_sensor && !m_timer.isActive())
{
m_sensor->start();
m_sensor->setIntervalRequest(m_sessionId, m_calibRate);
m_sensor->setStandbyOverrideRequest(m_sessionId, true);
connect(m_sensor, SIGNAL(internalData(const MagneticField&)), this, SLOT(sampleReceived(const MagneticField&)));
}
m_timer.start(m_calibTimeout);
}