/
magnetometeradaptor-ncdk.cpp
171 lines (139 loc) · 4.8 KB
/
magnetometeradaptor-ncdk.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
@file magnetometeradaptor-ncdk.cpp
@brief MagnetometerAdaptor for ncdk
<p>
Copyright (C) 2010-2011 Nokia Corporation
@author Shenghua Liu <ext-shenghua.1.liu@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 "magnetometeradaptor-ncdk.h"
#include <QString>
#include "config.h"
#include <errno.h>
#include "datatypes/utils.h"
#include "logging.h"
#include <unistd.h>
MagnetometerAdaptorNCDK::MagnetometerAdaptorNCDK(const QString& id) :
SysfsAdaptor(id, SysfsAdaptor::IntervalMode),
powerState_(false)
{
intervalCompensation_ = SensorFrameworkConfig::configuration()->value<int>("magnetometer/interval_compensation", 0);
powerStateFilePath_ = SensorFrameworkConfig::configuration()->value<QByteArray>("magnetometer/path_power_state", "");
sensAdjFilePath_ = SensorFrameworkConfig::configuration()->value<QByteArray>("magnetometer/path_sens_adjust", "");
magnetometerBuffer_ = new DeviceAdaptorRingBuffer<CalibratedMagneticFieldData>(128);
setAdaptedSensor("magnetometer", "Internal magnetometer coordinates", magnetometerBuffer_);
setDescription("Magnetometer adaptor (ak8975) for NCDK");
//get sensitivity adjustment
getSensitivityAdjustment(x_adj, y_adj, z_adj);
overflowLimit_ = SensorFrameworkConfig::configuration()->value<int>("magnetometer/overflow_limit", 8000);
}
MagnetometerAdaptorNCDK::~MagnetometerAdaptorNCDK()
{
delete magnetometerBuffer_;
}
void MagnetometerAdaptorNCDK::processSample(int pathId, int fd)
{
Q_UNUSED(pathId);
if (!powerState_)
return;
char buf[32];
int x = 0, y = 0, z = 0;
QList<QByteArray> strList;
int bytesRead = 0;
bool isOK = (bytesRead = read(fd, &buf, sizeof(buf))) > 0;
if (isOK) {
strList = QByteArray(buf, bytesRead).split(':');
if (strList.size() == 3) {
x = adjustPos(strList.at(0).toInt(), x_adj);
y = adjustPos(strList.at(1).toInt(), y_adj);
z = adjustPos(strList.at(2).toInt(), z_adj);
}
} else {
sensordLogW() << "Reading magnetometer error: " << strerror(errno);
return;
}
sensordLogT() << "Magnetometer Reading: " << x << ", " << y << ", " << z;
CalibratedMagneticFieldData* sample = magnetometerBuffer_->nextSlot();
sample->timestamp_ = Utils::getTimeStamp();
sample->x_ = x;
sample->y_ = y;
sample->z_ = z;
magnetometerBuffer_->commit();
magnetometerBuffer_->wakeUpReaders();
}
bool MagnetometerAdaptorNCDK::setPowerState(bool value) const
{
sensordLogD() << "Setting power state for compass driver" << " to " << value;
QByteArray powerStateStr = QByteArray::number(value);
if (!writeToFile(powerStateFilePath_, powerStateStr))
{
sensordLogW() << "Unable to set power state for compass driver";
return false;
}
return true;
}
void MagnetometerAdaptorNCDK::getSensitivityAdjustment(int &x, int &y, int &z) const
{
QByteArray byteArray = readFromFile(sensAdjFilePath_);
QList<QByteArray> strList = byteArray.split(':');
if (strList.size() == 3)
{
x = strList.at(0).toInt();
y = strList.at(1).toInt();
z = strList.at(2).toInt();
}
}
int MagnetometerAdaptorNCDK::adjustPos(const int value, const int adj) const
{
return value * ( adj + 128 ) / 256;
}
bool MagnetometerAdaptorNCDK::startSensor()
{
if (!setPowerState(true))
{
sensordLogW() << "Unable to set power on for compass driver";
}
else
{
powerState_ = true;
}
return SysfsAdaptor::startSensor();
}
void MagnetometerAdaptorNCDK::stopSensor()
{
if (!setPowerState(false))
{
sensordLogW() << "Unable to set power off for compass driver";
}
else
{
powerState_ = false;
}
SysfsAdaptor::stopSensor();
}
bool MagnetometerAdaptorNCDK::setInterval(const unsigned int value, const int sessionId)
{
if(intervalCompensation_)
{
return SysfsAdaptor::setInterval((int)value > intervalCompensation_ ? value - intervalCompensation_ : 0, sessionId);
}
return SysfsAdaptor::setInterval(value, sessionId);
}
void MagnetometerAdaptorNCDK::setOverflowLimit(int limit)
{
overflowLimit_ = limit;
}
int MagnetometerAdaptorNCDK::overflowLimit() const
{
return overflowLimit_;
}