From c8ce4940c5be641b753ef943cf336963596705a4 Mon Sep 17 00:00:00 2001 From: Maurice Kalinowski Date: Wed, 15 Feb 2017 11:58:01 +0100 Subject: Move series updates to C++ Instead of jumping to and from the QML engine for each sensor update, rather stay inside C++ and let QML only do the visual updates. This only updates the items which use Charts, as updates to a series is expensive. Change-Id: Idc7a98d2865e7095264f226c0ac5bbb5a977d18a Reviewed-by: Titta Heikkala --- tradeshow/iot-sensortag/SensorTagDemo.pro | 8 +- tradeshow/iot-sensortag/main.cpp | 6 + .../iot-sensortag/resources/base/GyroChart.qml | 25 +-- .../resources/base/MagnetometerChart.qml | 21 +-- .../resources/base/TemperatureChart.qml | 25 +-- tradeshow/iot-sensortag/resources/base/main.qml | 2 + tradeshow/iot-sensortag/seriesstorage.cpp | 191 +++++++++++++++++++++ tradeshow/iot-sensortag/seriesstorage.h | 101 +++++++++++ 8 files changed, 321 insertions(+), 58 deletions(-) create mode 100644 tradeshow/iot-sensortag/seriesstorage.cpp create mode 100644 tradeshow/iot-sensortag/seriesstorage.h (limited to 'tradeshow') diff --git a/tradeshow/iot-sensortag/SensorTagDemo.pro b/tradeshow/iot-sensortag/SensorTagDemo.pro index c6687fc..c1a9fcd 100644 --- a/tradeshow/iot-sensortag/SensorTagDemo.pro +++ b/tradeshow/iot-sensortag/SensorTagDemo.pro @@ -1,7 +1,7 @@ TEMPLATE = app QT += 3dcore 3drender 3dinput 3dquick 3dlogic core gui qml quick 3dquickextras widgets -QT += bluetooth network +QT += bluetooth network charts CONFIG += c++11 DEFINES += QT_NO_FOREACH @@ -45,7 +45,8 @@ SOURCES += main.cpp \ clouddataprovider.cpp \ dataproviderpool.cpp \ clouddataproviderpool.cpp \ - demodataproviderpool.cpp + demodataproviderpool.cpp \ + seriesstorage.cpp HEADERS += \ sensortagdataprovider.h \ @@ -55,7 +56,8 @@ HEADERS += \ dataproviderpool.h \ clouddataproviderpool.h \ demodataproviderpool.h \ - bluetoothapiconstants.h + bluetoothapiconstants.h \ + seriesstorage.h BLUETOOTH_LINUX_HOST { !winrt:CONFIG += UPDATE_TO_CLOUD diff --git a/tradeshow/iot-sensortag/main.cpp b/tradeshow/iot-sensortag/main.cpp index ea2325b..f676081 100644 --- a/tradeshow/iot-sensortag/main.cpp +++ b/tradeshow/iot-sensortag/main.cpp @@ -67,6 +67,7 @@ #ifdef CLOUD_UPLOAD #include "cloudupdate.h" #endif +#include "seriesstorage.h" Q_DECLARE_LOGGING_CATEGORY(boot2QtDemos) Q_LOGGING_CATEGORY(boot2QtDemos, "boot2qt.demos.iot") @@ -80,6 +81,7 @@ int main(int argc, char *argv[]) app.setFont(QFont("Titillium Web", 13)); DataProviderPool *dataProviderPool = 0; + SeriesStorage seriesStorage; QCommandLineParser parser; parser.addOptions({{"source", "Sensor data source", "cloud | sensor | mock"}, {"fullscreen", "Fullscreen mode", "true | false"}}); @@ -125,9 +127,12 @@ int main(int argc, char *argv[]) return 1; } + seriesStorage.setDataProviderPool(dataProviderPool); + qmlRegisterType("SensorTag.DataProvider", 1, 0, "SensorTagData"); qmlRegisterType("SensorTag.DataProvider", 1, 0, "ProviderState"); qmlRegisterType("SensorTag.DataProvider", 1, 0, "DataProviderPool"); + qmlRegisterType("SensorTag.SeriesStorage", 1, 0, "SeriesStorage"); #if defined(RUNS_AS_HOST) && defined(CLOUD_UPLOAD) CloudUpdate update; @@ -212,6 +217,7 @@ int main(int argc, char *argv[]) item->setProperty("dataProviderPool", QVariant::fromValue(dataProviderPool)); item->setProperty("contentFile", mainFile); + item->setProperty("seriesStorage", QVariant::fromValue(&seriesStorage)); } int returnValue = app.exec(); dataProviderPool->stopScanning(); diff --git a/tradeshow/iot-sensortag/resources/base/GyroChart.qml b/tradeshow/iot-sensortag/resources/base/GyroChart.qml index 30aba64..f7d2b71 100644 --- a/tradeshow/iot-sensortag/resources/base/GyroChart.qml +++ b/tradeshow/iot-sensortag/resources/base/GyroChart.qml @@ -51,12 +51,12 @@ import QtQuick 2.5 import QtCharts 2.1 import SensorTag.DataProvider 1.0 import QtGraphicalEffects 1.0 +import QtQml 2.2 BaseChart { id: gyroHolderRect // Replace with actual gyro properties - property int gyroSeriesIndex: 0 property int maxGyroReadings: 24 readonly property string xColor: "#15bdff" @@ -64,12 +64,6 @@ BaseChart { readonly property string zColor: "red" readonly property color textColor: "white" - onSensorChanged: { - if (sensor) { - sensor.rotationValuesChanged.connect(contentItem.updateRotation); - } - } - onClicked: { if (sensor) sensor.recalibrate(); @@ -81,19 +75,12 @@ BaseChart { content: Item { anchors.fill: parent - function updateRotation() { - gyroSeriesX.append(gyroSeriesIndex, sensor.rotationX); - gyroSeriesY.append(gyroSeriesIndex, sensor.rotationY); - gyroSeriesZ.append(gyroSeriesIndex, sensor.rotationZ); - - if (gyroSeriesIndex >= maxGyroReadings) { - gyroSeriesX.remove(gyroSeriesX.at(gyroSeriesIndex-maxGyroReadings)); - gyroSeriesY.remove(gyroSeriesY.at(gyroSeriesIndex-maxGyroReadings)); - gyroSeriesZ.remove(gyroSeriesZ.at(gyroSeriesIndex-maxGyroReadings)); - valueAxisX.min++; - valueAxisX.max++; + Connections { + target: mainWindow + onSeriesStorageChanged: { + if (seriesStorage) + seriesStorage.setGyroSeries(gyroSeriesX, gyroSeriesY, gyroSeriesZ); } - gyroSeriesIndex++; } Glow { diff --git a/tradeshow/iot-sensortag/resources/base/MagnetometerChart.qml b/tradeshow/iot-sensortag/resources/base/MagnetometerChart.qml index 6c4ba79..3bac8e6 100644 --- a/tradeshow/iot-sensortag/resources/base/MagnetometerChart.qml +++ b/tradeshow/iot-sensortag/resources/base/MagnetometerChart.qml @@ -55,7 +55,6 @@ import QtGraphicalEffects 1.0 BaseChart { id: magnetHolderRect - property int magneticSeriesIndex: 0 property int maxNumOfMagnReadings: 24 readonly property color chartColor: "#15bdff" @@ -64,28 +63,18 @@ BaseChart { readonly property string zColor: "red" readonly property color textColor: "white" - onSensorChanged: if (sensor) sensor.magnetometerMicroTChanged.connect(contentItem.updateMagneticGraph) - title: qsTr("Magnetometer") rightSide: true content: Item { anchors.fill: parent - function updateMagneticGraph() - { - magnSeriesX.append(magneticSeriesIndex, sensor.magnetometerMicroT_xAxis); - magnSeriesY.append(magneticSeriesIndex, sensor.magnetometerMicroT_yAxis); - magnSeriesZ.append(magneticSeriesIndex, sensor.magnetometerMicroT_zAxis); - - if (magneticSeriesIndex >= maxNumOfMagnReadings) { - magnSeriesX.remove(magnSeriesX.at(magneticSeriesIndex-maxNumOfMagnReadings)); - magnSeriesY.remove(magnSeriesY.at(magneticSeriesIndex-maxNumOfMagnReadings)); - magnSeriesZ.remove(magnSeriesY.at(magneticSeriesIndex-maxNumOfMagnReadings)); - valueAxisX.min++; - valueAxisX.max++; + Connections { + target: mainWindow + onSeriesStorageChanged: { + if (seriesStorage) + seriesStorage.setMagnetoMeterSeries(magnSeriesX, magnSeriesY, magnSeriesZ); } - magneticSeriesIndex++; } Image { diff --git a/tradeshow/iot-sensortag/resources/base/TemperatureChart.qml b/tradeshow/iot-sensortag/resources/base/TemperatureChart.qml index dab2479..8bb1a25 100644 --- a/tradeshow/iot-sensortag/resources/base/TemperatureChart.qml +++ b/tradeshow/iot-sensortag/resources/base/TemperatureChart.qml @@ -55,7 +55,6 @@ import QtGraphicalEffects 1.0 BaseChart { id: tempHolderRect - property int temperatureSeriesIndex: 0 property int maxNumOfTempReadings: 30 property real minimum: 10 property real maximum: 40 @@ -77,29 +76,15 @@ BaseChart { content: Item { anchors.fill: parent - Component.onCompleted: { - // Initialize series - var i = 0 - while (i < maxNumOfTempReadings) { - avgTempSeries.append(i, defaultAvgValue) - i++ + Connections { + target: mainWindow + onSeriesStorageChanged: { + if (seriesStorage) + seriesStorage.setTemperatureSeries(avgTempSeries); } - temperatureSeriesIndex = i } function updateTemperatureGraph() { - // Make sure defaultAvgValue is the last valuea in the series - avgTempSeries.remove(temperatureSeriesIndex - 1, defaultAvgValue); - avgTempSeries.append(temperatureSeriesIndex - 1, sensor.infraredAmbientTemperature); - avgTempSeries.append(temperatureSeriesIndex, defaultAvgValue); - - if (temperatureSeriesIndex >= maxNumOfTempReadings) { - avgTempSeries.remove(avgTempSeries.at(temperatureSeriesIndex - maxNumOfTempReadings)); - valueAxisX.min++; - valueAxisX.max++; - } - temperatureSeriesIndex++; - var value = sensor.infraredAmbientTemperature; if (minValue > value) minValue = value; diff --git a/tradeshow/iot-sensortag/resources/base/main.qml b/tradeshow/iot-sensortag/resources/base/main.qml index 679a8dc..c55d8ee 100644 --- a/tradeshow/iot-sensortag/resources/base/main.qml +++ b/tradeshow/iot-sensortag/resources/base/main.qml @@ -50,12 +50,14 @@ import QtQuick 2.6 import QtQuick.Window 2.0 import SensorTag.DataProvider 1.0 +import SensorTag.SeriesStorage 1.0 Window { id: mainWindow property alias contentFile: contentLoader.source property DataProviderPool dataProviderPool + property SeriesStorage seriesStorage // Size defaults to the small display width: 1920 diff --git a/tradeshow/iot-sensortag/seriesstorage.cpp b/tradeshow/iot-sensortag/seriesstorage.cpp new file mode 100644 index 0000000..54ac977 --- /dev/null +++ b/tradeshow/iot-sensortag/seriesstorage.cpp @@ -0,0 +1,191 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of Qt for Device Creation. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "seriesstorage.h" + +Q_DECLARE_METATYPE(QAbstractSeries *) +Q_DECLARE_METATYPE(QAbstractAxis *) + +inline void updatePoints(QList &list, int newIndex, float newValue, int maxItems) +{ + list.append(QPointF(newIndex, newValue)); + if (list.size() > maxItems) + list.removeFirst(); +} + +SeriesStorage::SeriesStorage(QObject *parent) : QObject(parent) +{ + for (int i = 0; i < 30; i++) + m_temperatureList.append(QPointF(i, 30)); +} + +void SeriesStorage::setDataProviderPool(DataProviderPool *pool) +{ + m_providerPool = pool; + if (!m_providerPool) + return; + connect(m_providerPool, &DataProviderPool::dataProvidersChanged, this, &SeriesStorage::dataProviderPoolChanged); +} + +void SeriesStorage::setTemperatureSeries(QAbstractSeries *series) +{ + m_temperature = qobject_cast(series); +} + +void SeriesStorage::setGyroSeries(QAbstractSeries *xSeries, QAbstractSeries *ySeries, QAbstractSeries *zSeries) +{ + m_gyroX = qobject_cast(xSeries); + m_gyroY = qobject_cast(ySeries); + m_gyroZ = qobject_cast(zSeries); +} + +void SeriesStorage::setMagnetoMeterSeries(QAbstractSeries *xSeries, QAbstractSeries *ySeries, QAbstractSeries *zSeries) +{ + m_magnetoX = qobject_cast(xSeries); + m_magnetoY = qobject_cast(ySeries); + m_magnetoZ = qobject_cast(zSeries); +} + +void SeriesStorage::dataProviderPoolChanged() +{ + m_gyroProvider = m_providerPool->getProvider(SensorTagDataProvider::Rotation); + if (m_gyroProvider) { + connect(m_gyroProvider, &SensorTagDataProvider::rotationValuesChanged, this, &SeriesStorage::changeRotationSeries); + } + + m_magnetoProvider = m_providerPool->getProvider(SensorTagDataProvider::Magnetometer); + if (m_magnetoProvider) { + connect(m_magnetoProvider, &SensorTagDataProvider::magnetometerMicroTChanged, this, &SeriesStorage::changeMagnetoSeries); + } + + m_temperatureProvider = m_providerPool->getProvider(SensorTagDataProvider::AmbientTemperature); + if (m_temperatureProvider) { + connect(m_temperatureProvider, &SensorTagDataProvider::infraredAmbientTemperatureChanged, this, &SeriesStorage::changeTemperatureSeries); + } +} + +void SeriesStorage::changeRotationSeries() +{ + if (!m_gyroX || !m_gyroY || !m_gyroZ) + return; + + static int gyroSeriesIndex = 0; + static const int maxGyroReadings = 24; + static int axisMin = 0; + static int axisMax = maxGyroReadings + 1; + + updatePoints(m_gyroListX, gyroSeriesIndex, m_gyroProvider->getRotationX(), maxGyroReadings); + m_gyroX->replace(m_gyroListX); + updatePoints(m_gyroListY, gyroSeriesIndex, m_gyroProvider->getRotationY(), maxGyroReadings); + m_gyroY->replace(m_gyroListY); + updatePoints(m_gyroListZ, gyroSeriesIndex, m_gyroProvider->getRotationZ(), maxGyroReadings); + m_gyroZ->replace(m_gyroListZ); + + if (gyroSeriesIndex >= maxGyroReadings) { + QAbstractAxis *axis = m_gyroX->attachedAxes().at(0); + axisMin++; + axisMax++; + axis->setRange(axisMin, axisMax); + } + gyroSeriesIndex++; +} + +void SeriesStorage::changeMagnetoSeries() +{ + if (!m_magnetoX || !m_magnetoY || !m_magnetoZ) + return; + + static int magneticSeriesIndex = 0; + static const int maxMagneticReadings = 24; + static int axisMin = 0; + static int axisMax = maxMagneticReadings; + + updatePoints(m_magnetoListX, magneticSeriesIndex, m_magnetoProvider->getMagnetometerMicroT_xAxis(), maxMagneticReadings); + m_magnetoX->replace(m_magnetoListX); + updatePoints(m_magnetoListY, magneticSeriesIndex, m_magnetoProvider->getMagnetometerMicroT_yAxis(), maxMagneticReadings); + m_magnetoY->replace(m_magnetoListY); + updatePoints(m_magnetoListZ, magneticSeriesIndex, m_magnetoProvider->getMagnetometerMicroT_zAxis(), maxMagneticReadings); + m_magnetoZ->replace(m_magnetoListZ); + + if (magneticSeriesIndex >= maxMagneticReadings) { + QAbstractAxis *axis = m_magnetoX->attachedAxes().at(0); + axisMin++; + axisMax++; + axis->setRange(axisMin, axisMax); + } + magneticSeriesIndex++; +} + +void SeriesStorage::changeTemperatureSeries() +{ + if (!m_temperature) + return; + + static const int maxTemperatureReadings = 30; + static int temperatureSeriesIndex = maxTemperatureReadings; + static int axisMin = 0; + static int axisMax = maxTemperatureReadings - 1; + static const int defaultAvgValue = 25; + + m_temperatureList.removeLast(); + m_temperatureList.append(QPointF(temperatureSeriesIndex -1, m_temperatureProvider->getInfraredAmbientTemperature())); + m_temperatureList.append(QPointF(temperatureSeriesIndex, defaultAvgValue)); + + if (m_temperatureList.size() >= maxTemperatureReadings) + m_temperatureList.removeFirst(); + + if (temperatureSeriesIndex >= maxTemperatureReadings) { + QAbstractAxis *axis = m_temperature->attachedAxes().at(0); + axisMin++; + axisMax++; + axis->setRange(axisMin, axisMax); + } + m_temperature->replace(m_temperatureList); + temperatureSeriesIndex++; +} diff --git a/tradeshow/iot-sensortag/seriesstorage.h b/tradeshow/iot-sensortag/seriesstorage.h new file mode 100644 index 0000000..343c65e --- /dev/null +++ b/tradeshow/iot-sensortag/seriesstorage.h @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of Qt for Device Creation. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef SERIESSTORAGE_H +#define SERIESSTORAGE_H + +#include "dataproviderpool.h" + +#include +#include + +class SeriesStorage : public QObject +{ + Q_OBJECT +public: + explicit SeriesStorage(QObject *parent = nullptr); + + void setDataProviderPool(DataProviderPool *pool); + Q_INVOKABLE void setTemperatureSeries(QAbstractSeries *series); + Q_INVOKABLE void setGyroSeries(QAbstractSeries *xSeries, QAbstractSeries *ySeries, + QAbstractSeries *zSeries); + Q_INVOKABLE void setMagnetoMeterSeries(QAbstractSeries *xSeries, QAbstractSeries *ySeries, + QAbstractSeries *zSeries); +signals: + +public slots: + void dataProviderPoolChanged(); + + void changeRotationSeries(); + void changeMagnetoSeries(); + void changeTemperatureSeries(); + +private: + DataProviderPool *m_providerPool; + SensorTagDataProvider *m_temperatureProvider{0}; + SensorTagDataProvider *m_gyroProvider{0}; + SensorTagDataProvider *m_magnetoProvider{0}; + QSplineSeries *m_gyroX{0}; + QSplineSeries *m_gyroY{0}; + QSplineSeries *m_gyroZ{0}; + QSplineSeries *m_magnetoX{0}; + QSplineSeries *m_magnetoY{0}; + QSplineSeries *m_magnetoZ{0}; + QLineSeries *m_temperature{0}; + QList m_gyroListX; + QList m_gyroListY; + QList m_gyroListZ; + QList m_magnetoListX; + QList m_magnetoListY; + QList m_magnetoListZ; + QList m_temperatureList; + +}; + +#endif // SERIESSTORAGE_H -- cgit v1.2.3