| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the QtSensors module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 | #include <QTimer> | 
| 30 | #include <QDebug> | 
| 31 | #include <QCoreApplication> | 
| 32 | #include "mockcommon.h" | 
| 33 |  | 
| 34 | Q_GLOBAL_STATIC(mockcommonPrivate, mockcommonPrv) | 
| 35 |  | 
| 36 | mockcommonPrivate *mockcommonPrivate::instance() | 
| 37 | { | 
| 38 |     return mockcommonPrv(); | 
| 39 | } | 
| 40 |  | 
| 41 | mockcommonPrivate::mockcommonPrivate() | 
| 42 |     : QObject(),prevts(50), firstRun(0) | 
| 43 | { | 
| 44 |     readTimer = new QTimer(this); | 
| 45 |     readTimer->setSingleShot(true); | 
| 46 |     connect(sender: readTimer,SIGNAL(timeout()),receiver: this,SLOT(timerout())); | 
| 47 | } | 
| 48 |  | 
| 49 | void mockcommonPrivate::timerout() | 
| 50 | { | 
| 51 |     while (pFile.isOpen() && !pFile.atEnd()) { | 
| 52 |         QString line = pFile.readLine(); | 
| 53 |         if (!line.isNull()) { | 
| 54 |             if (parseData(line)) { | 
| 55 |                 readTimer->start(); | 
| 56 |                 break; | 
| 57 |             } | 
| 58 |         } | 
| 59 |     } | 
| 60 | } | 
| 61 |  | 
| 62 | bool mockcommonPrivate::setFile(const QString &filename) | 
| 63 | { | 
| 64 |     if (filename.isEmpty()) | 
| 65 |         return false; | 
| 66 |  | 
| 67 |     if (pFile.isOpen()) { | 
| 68 |         pFile.close(); | 
| 69 |     } | 
| 70 |     oldAccelTs = 0; | 
| 71 |     firstRun = true; | 
| 72 |     pFile.setFileName(QCoreApplication::instance()->applicationDirPath() + "/"  + filename); | 
| 73 |     bool ok = pFile.open(flags: QIODevice::ReadOnly); | 
| 74 |     if (!ok) { | 
| 75 |         pFile.setFileName(SRCDIR "/"  + filename); | 
| 76 |         ok = pFile.open(flags: QIODevice::ReadOnly); | 
| 77 |     } | 
| 78 |     return ok; | 
| 79 | } | 
| 80 |  | 
| 81 | bool mockcommonPrivate::parseData(const QString &line) | 
| 82 | { | 
| 83 |     bool ok = false; | 
| 84 |     QString sensorToken = line.section(in_sep: QLatin1String(":" ),start: 0, end: 0).trimmed(); | 
| 85 |     QString data = line.section(in_sep: QLatin1String(":" ),start: 1, end: 1).trimmed(); | 
| 86 |  | 
| 87 |     if (sensorToken == QLatin1String("accelerometer" )) { | 
| 88 |         if (!firstRun) { | 
| 89 |  | 
| 90 |             Q_EMIT accelData(data); | 
| 91 |             if (prevts == 0 || prevts > 90000 ) | 
| 92 |                 prevts = 20000; // use 20 Hz | 
| 93 |             if (prevts > 90000 ) // original slam timestamps are wrong | 
| 94 |                 prevts = 100000; //use 100 Hz | 
| 95 |             readTimer->setInterval((int)prevts/1000); | 
| 96 |         } else { | 
| 97 |             firstRun = false; | 
| 98 |         } | 
| 99 |         quint64 ts = data.section(in_sep: QLatin1String("," ), start: 0,end: 0).toULongLong(); | 
| 100 |         ok = true; | 
| 101 |         qreal difference; | 
| 102 |         if (oldAccelTs == 0) { | 
| 103 |             oldAccelTs = ts; | 
| 104 |         } | 
| 105 |         difference = ts - oldAccelTs; | 
| 106 |  | 
| 107 |         if (difference < 1 | 
| 108 |                 || difference == ts) { | 
| 109 |             int hertz = 50; | 
| 110 |             readTimer->setInterval((1.0/hertz)*1000); | 
| 111 |         } else { | 
| 112 |             if (firstRun) | 
| 113 |                 readTimer->setInterval((int)difference/1000); | 
| 114 |         } | 
| 115 |         oldAccelTs = ts; | 
| 116 |         prevts = difference; | 
| 117 |  | 
| 118 |     } else if (sensorToken == QLatin1String("irProximity" )) { | 
| 119 |         Q_EMIT irProxyData(data); | 
| 120 |     } else if (sensorToken == QLatin1String("orientation" )) { | 
| 121 |         Q_EMIT orientData(data); | 
| 122 |     } else if (sensorToken == QLatin1String("tap" )) { | 
| 123 |         Q_EMIT tapData(data); //just send this it takes only one to be detected | 
| 124 |     } else if (sensorToken == QLatin1String("proximity" )) { | 
| 125 |         Q_EMIT proxyData(data); | 
| 126 |     } | 
| 127 |     return ok; | 
| 128 | } | 
| 129 |  | 
| 130 |  | 
| 131 | mockcommon::mockcommon(QSensor *sensor) | 
| 132 |     : QSensorBackend(sensor), timer(0) | 
| 133 | { | 
| 134 |     mockcommonPrv()->readTimer->setInterval(0); | 
| 135 | } | 
| 136 |  | 
| 137 | void mockcommon::start() | 
| 138 | { | 
| 139 |     if (!mockcommonPrv()->readTimer->isActive()) { | 
| 140 |         mockcommonPrv()->readTimer->start(); | 
| 141 |     } | 
| 142 | } | 
| 143 |  | 
| 144 | void mockcommon::stop() | 
| 145 | { | 
| 146 |     if (mockcommonPrv()->readTimer->isActive()) { | 
| 147 |         mockcommonPrv()->readTimer->stop(); | 
| 148 |     } | 
| 149 | } | 
| 150 |  | 
| 151 | char const * const mockaccelerometer::id("mock.accelerometer" ); | 
| 152 |  | 
| 153 | mockaccelerometer::mockaccelerometer(QSensor *sensor) | 
| 154 |     : mockcommon(sensor) | 
| 155 | { | 
| 156 |     setReading<QAccelerometerReading>(&m_reading); | 
| 157 |     addDataRate(min: 50, max: 50); // 50 | 
| 158 |  | 
| 159 |     connect(sender: mockcommonPrv(),SIGNAL(accelData(QString)),receiver: this,SLOT(parseAccelData(QString))); | 
| 160 | } | 
| 161 |  | 
| 162 | void mockaccelerometer::parseAccelData(const QString &data) | 
| 163 | { | 
| 164 |     quint64 ts = data.section(in_sep: QLatin1String("," ), start: 0,end: 0).toULongLong(); | 
| 165 |     m_reading.setTimestamp(ts); | 
| 166 |     m_reading.setX(data.section(in_sep: QLatin1String("," ), start: 1,end: 1).toDouble()); | 
| 167 |     m_reading.setY(data.section(in_sep: QLatin1String("," ), start: 2,end: 2).toDouble()); | 
| 168 |     m_reading.setZ(data.section(in_sep: QLatin1String("," ), start: 3,end: 3).toDouble()); | 
| 169 |     newReadingAvailable(); | 
| 170 | } | 
| 171 |  | 
| 172 | char const * const mockorientationsensor::id("mock.orientation" ); | 
| 173 |  | 
| 174 | mockorientationsensor::mockorientationsensor(QSensor *sensor) | 
| 175 |     : mockcommon(sensor) | 
| 176 | { | 
| 177 |     setReading<QOrientationReading>(&m_reading); | 
| 178 |     addDataRate(min: 50, max: 50); // 50Hz | 
| 179 |     connect(sender: mockcommonPrv(),SIGNAL(orientData(QString)),receiver: this,SLOT(parseOrientData(QString))); | 
| 180 | } | 
| 181 |  | 
| 182 | void mockorientationsensor::parseOrientData(const QString &data) | 
| 183 | { | 
| 184 |     m_reading.setTimestamp(data.section(in_sep: QLatin1String("," ), start: 0,end: 0).toULongLong()); | 
| 185 |     m_reading.setOrientation(static_cast<QOrientationReading::Orientation>(data.section(in_sep: QLatin1String("," ), start: 1,end: 1).toInt())); | 
| 186 |  | 
| 187 |     newReadingAvailable(); | 
| 188 | } | 
| 189 |  | 
| 190 | char const * const mockirproximitysensor::id("mock.irproximity" ); | 
| 191 |  | 
| 192 | mockirproximitysensor::mockirproximitysensor(QSensor *sensor) | 
| 193 |     : mockcommon(sensor) | 
| 194 | { | 
| 195 |     setReading<QIRProximityReading>(&m_reading); | 
| 196 |     addDataRate(min: 50, max: 50); // 50Hz | 
| 197 |     connect(sender: mockcommonPrv(),SIGNAL(irProxyData(QString)),receiver: this,SLOT(parseIrProxyData(QString))); | 
| 198 | } | 
| 199 |  | 
| 200 | void mockirproximitysensor::parseIrProxyData(const QString &data) | 
| 201 | { | 
| 202 |     m_reading.setTimestamp(data.section(in_sep: QLatin1String("," ), start: 0,end: 0).toULongLong()); | 
| 203 |     m_reading.setReflectance(data.section(in_sep: QLatin1String("," ), start: 1,end: 1).toDouble()); | 
| 204 |  | 
| 205 |     newReadingAvailable(); | 
| 206 | } | 
| 207 |  | 
| 208 | char const * const mocktapsensor::id("mock.tap" ); | 
| 209 |  | 
| 210 | mocktapsensor::mocktapsensor(QSensor *sensor) | 
| 211 |     : mockcommon(sensor) | 
| 212 | { | 
| 213 |     setReading<QTapReading>(&m_reading); | 
| 214 |     addDataRate(min: 50, max: 50); // 50Hz | 
| 215 |     connect(sender: mockcommonPrv(),SIGNAL(tapData(QString)),receiver: this,SLOT(parseTapData(QString))); | 
| 216 | } | 
| 217 |  | 
| 218 | void mocktapsensor::parseTapData(const QString &data) | 
| 219 | { | 
| 220 |     m_reading.setTimestamp(data.section(in_sep: QLatin1String("," ), start: 0,end: 0).toULongLong()); | 
| 221 |     m_reading.setDoubleTap((data.section(in_sep: QLatin1String("," ), start: 1,end: 1).toInt() == 1)); | 
| 222 |  | 
| 223 |     newReadingAvailable(); | 
| 224 | } | 
| 225 |  | 
| 226 | char const * const mockproximitysensor::id("mock.proximity" ); | 
| 227 |  | 
| 228 | mockproximitysensor::mockproximitysensor(QSensor *sensor) | 
| 229 |     : mockcommon(sensor) | 
| 230 | { | 
| 231 |     setReading<QProximityReading>(&m_reading); | 
| 232 |     addDataRate(min: 50, max: 50); // 50Hz | 
| 233 |     connect(sender: mockcommonPrv(),SIGNAL(proxyData(QString)),receiver: this,SLOT(parseProxyData(QString))); | 
| 234 | } | 
| 235 |  | 
| 236 | void mockproximitysensor::parseProxyData(const QString &data) | 
| 237 | { | 
| 238 |     m_reading.setTimestamp(data.section(in_sep: QLatin1String("," ), start: 0,end: 0).toULongLong()); | 
| 239 |     m_reading.setClose((data.section(in_sep: QLatin1String("," ), start: 1,end: 1).toInt() == 1)); | 
| 240 |  | 
| 241 |     newReadingAvailable(); | 
| 242 | } | 
| 243 |  |