-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sensor.cpp
160 lines (136 loc) · 4.86 KB
/
Sensor.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
#include "Sensor.hpp"
Sensor::Sensor(QObject *parent, QList<QVariant> args)
: QObject{parent}, _name{args.at(0).toString()},
_rawSensorID{static_cast<quint16>(args.at(1).toUInt())},
_sensorNode{static_cast<quint16>(args.at(2).toUInt())},
_convertedSensorID{static_cast<quint16>(args.at(3).toUInt())}
{
//_sensorID = _rawSensorID + 1;
_convertedSensorID = _rawSensorID + 1;
}
void Sensor::onSensorReceived(quint16 ID_A, quint32 ID_B, QList<QByteArray> data)
{
qInfo() << "Enter Sensor::onSensorReceived() function with object ID: " << _sensorID;
// check for sensor ID before doing anything
if (_convertedSensorID == ID_A)
{
setConvertedValue(static_cast<float>((data.at(0)+ data.at(1)).toInt(nullptr, 16))/10);
return;
}
if (data.length() >= 5)
{
if (_convertedSensorID == data.at(2).toInt(nullptr, 16))
{
setConvertedValue(static_cast<float>((data.at(3)+ data.at(4)).toInt(nullptr, 16))/10);
return;
}
}
if (data.length() >= 8)
{
if (_convertedSensorID == data.at(2).toInt(nullptr, 16))
{
setConvertedValue(static_cast<float>((data.at(6) + data.at(7)).toInt(nullptr, 16))/10);
return;
}
}
// need to find out how to let QML directly read these values
// or handle the valueChanged() signal and update the values in the GUI
// try with QMLtestclass
}
void Sensor::onSensorReceivedFD(const QList<QByteArray>& data)
{
qInfo() << "Enter Sensor::onSensorReceivedFD() function with object ID: " << _sensorID;
for (int i = 0; i < data.length(); i = i + 16)
{
if(_rawSensorID == data.at(i).toUInt(nullptr, 16))
{
setState(data.at(i+1).toUInt(nullptr, 16));
quint64 currentTimestamp = static_cast<quint64>(data.at(i+9).toUInt(nullptr, 16)) +
(static_cast<quint64>(data.at(i+8).toUInt(nullptr, 16)) << 8) +
(static_cast<quint64>(data.at(i+7).toUInt(nullptr, 16)) << 16) +
(static_cast<quint64>(data.at(i+6).toUInt(nullptr, 16)) << 24) +
(static_cast<quint64>(data.at(i+5).toUInt(nullptr, 16)) << 32) +
(static_cast<quint64>(data.at(i+4).toUInt(nullptr, 16)) << 40) +
(static_cast<quint64>(data.at(i+3).toUInt(nullptr, 16)) << 48) +
(static_cast<quint64>(data.at(i+2).toUInt(nullptr, 16)) << 56);
setTimestamp(currentTimestamp);
quint16 currentRawValue = data.at(i+7).toUInt(nullptr, 16) + (data.at(i+6).toUInt(nullptr, 16) << 8);
setRawValue(currentRawValue);
quint8 u_8x4[4] = { static_cast<quint8>(data.at(i+12).toUInt(nullptr,16)),
static_cast<quint8>(data.at(i+13).toUInt(nullptr,16)),
static_cast<quint8>(data.at(i+14).toUInt(nullptr,16)),
static_cast<quint8>(data.at(i+15).toUInt(nullptr,16))};
float currentConvertedValue = 0.0f;
memcpy(¤tConvertedValue, u_8x4, 4);
setConvertedValue(currentConvertedValue);
break;
}
}
}
void Sensor::emitUpdateGraphQML_rawValue()
{
emit updateGraphQML_rawValue(static_cast<float>(_timestamp)/1'000'000, _rawValue); // for Connections in QML
}
void Sensor::emitUpdateGraphQML_convertedValue()
{
emit updateGraphQML_convertedValue(static_cast<float>(_timestamp)/1'000'000, _convertedValue); // for Connections in QML
}
quint64 Sensor::getTimestamp() const
{
return _timestamp;
}
void Sensor::setTimestamp(quint64 newTimestamp)
{
if (_timestamp == newTimestamp)
return;
_timestamp = newTimestamp;
emit timestampChanged();
}
QString Sensor::name() const
{
return _name;
}
float Sensor::rawValue() const
{
return _rawValue;
//return _rawValue;
}
void Sensor::setRawValue(float newRawValue)
{
if(qFuzzyCompare(_rawValue,newRawValue))
return;
_rawValue = newRawValue;
emit updateSensorQML_rawValue(_rawValue);
emit rawValueChanged(); // QML will handle this signal
}
float Sensor::convertedValue() const
{
return _convertedValue;
}
void Sensor::setConvertedValue(float newConvertedValue)
{
if(qFuzzyCompare(_convertedValue, newConvertedValue))
return;
_convertedValue = newConvertedValue;
emit updateSensorQML_convertedValue(_convertedValue);
emit convertedValueChanged();
}
QVariant Sensor::state() const
{
return _state;
}
void Sensor::setState(QVariant newState)
{
if(_state == newState)
return;
_state = newState;
emit stateChanged();
}
quint16 Sensor::rawSensorID()
{
return _rawSensorID;
}
Node::NodeID Sensor::sensorNode() const
{
return _sensorNode;
}