diff options
author | Lorn Potter <lorn.potter@nokia.com> | 2012-02-13 08:50:16 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-02-13 23:47:53 +0100 |
commit | a1ecc85fa88fdd318b379e35139c5d6486c78bd8 (patch) | |
tree | 4b4b2f8c567f4d630ae01f06d6b78aff599529c0 | |
parent | 3b030528fbc3d2d7681113dc6c2727d4cff7a07a (diff) | |
download | qtsensors-a1ecc85fa88fdd318b379e35139c5d6486c78bd8.tar.gz |
add slam gesture
Change-Id: I0aa474d2398f59c0b766ed7fd7458ecd50b1fd4e
Reviewed-by: Lorn Potter <lorn.potter@nokia.com>
5 files changed, 361 insertions, 15 deletions
diff --git a/examples/sensors/shakeit/shakeit.qml b/examples/sensors/shakeit/shakeit.qml index fe0a8e1..25b78bc 100644 --- a/examples/sensors/shakeit/shakeit.qml +++ b/examples/sensors/shakeit/shakeit.qml @@ -121,13 +121,10 @@ Rectangle { }, State { name: "whipped" - PropertyChanges { target: triangle1; rotation: 0; x:0; - } - PropertyChanges { target: triangle2; rotation: 0; x:0; - } + PropertyChanges { target: triangle1; rotation: 0; x:0; } + PropertyChanges { target: triangle2; rotation: 0; x:0; y:triangle1.x + triangle1.height; } PropertyChanges { target: triangle3; rotation: 0; x:0; - y: triangle2.y + triangle2.height; - } + y: triangle2.y + triangle2.height; } }, State { name: "twisted" @@ -171,25 +168,47 @@ Rectangle { x: (window.width / 3 + window.width / 3) - triangle3.width / 2; y: triangle3.height } - }/*, + }, State { - name :"turnedover" - }*/ + name :"slammed" + PropertyChanges { target: triangle1; rotation: 0; + x: 0; + y: 0 + 30 + } + PropertyChanges { target: triangle2; rotation: 0; + x: window.width - triangle2.width; + y: 0 + 30 + ;} + PropertyChanges { target: triangle3; rotation: 0; + x: window.width / 2 - triangle3.width / 2; + y:window.height - triangle3.height; + } + } ] - transitions: Transition { + transitions: [ + Transition { + ParallelAnimation { running: true - NumberAnimation { properties: "x,y"; easing.type: Easing.OutBounce;duration: 3000; } + NumberAnimation { properties: "x,y"; easing.type: Easing.OutBounce;duration: 2000; } RotationAnimation { id: t1Rotation; target: triangle1; duration: 1000; direction: RotationAnimation.Clockwise } - RotationAnimation { id: t2Rotation; target: triangle2; duration: 3000; + RotationAnimation { id: t2Rotation; target: triangle2; duration: 2000; direction: RotationAnimation.Counterclockwise } - RotationAnimation { id: t3Rotation; target: triangle3; duration: 3000; + RotationAnimation { id: t3Rotation; target: triangle3; duration: 2000; direction: RotationAnimation.Clockwise } } - } + + }, Transition { + to: "slammed" + SequentialAnimation { + NumberAnimation { properties: "x"; easing.type: Easing.OutBounce;duration: 500; } + + } + } + ] //! [1] SensorGesture { @@ -200,7 +219,7 @@ Rectangle { //! [3] //! [2] gestures : ["QtSensors.shake", "QtSensors.whip", "QtSensors.twist", "QtSensors.cover", - "QtSensors.hover", "QtSensors.turnover", "QtSensors.pickup"] + "QtSensors.hover", "QtSensors.turnover", "QtSensors.pickup", "QtSensors.slam" ] //! [2] //! [4] onDetected:{ @@ -209,28 +228,50 @@ Rectangle { if (gesture == "shake") { window.state == "rotated" ? window.state = "default" : window.state = "rotated" + timer.start() } if (gesture == "whip") { window.state == "whipped" ? window.state = "default" : window.state = "whipped" + timer.start() } if (gesture == "twistRight") { window.state == "twisted" ? window.state = "default" : window.state = "twisted" + timer.start() } if (gesture == "cover") { window.state == "covered" ? window.state = "default" : window.state = "covered" + timer.start() } if (gesture == "hover") { window.state == "hovered" ? window.state = "default" : window.state = "hovered" + timer.start() } if (gesture == "turnover") { window.state = "default" loopy2a_mono.play(); + timer.start() } if (gesture == "pickup") { window.state = "default" phone.play() + timer.start() + } + if (gesture == "slam") { + window.state == "slammed" ? window.state = "default" : window.state = "slammed" + timer.start() } } //! [4] } + Timer { + id: timer + running: false + repeat: false + interval: 3000 + onTriggered: { + console.log("timer triggered") + window.state = "default" + label.text = "Try another gesture" + } + } } diff --git a/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp b/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp new file mode 100644 index 0000000..37708d9 --- /dev/null +++ b/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp @@ -0,0 +1,203 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qslamgesturerecognizer.h" +#include <QtCore/qmath.h> + +QT_BEGIN_NAMESPACE + +QSlamSensorGestureRecognizer::QSlamSensorGestureRecognizer(QObject *parent) : + QSensorGestureRecognizer(parent), + accelRange(0), + active(0), + lastX(0), + lastY(0), + lastZ(0), + detecting(0), + slamOk(0) +{ +} + +QSlamSensorGestureRecognizer::~QSlamSensorGestureRecognizer() +{ +} + +void QSlamSensorGestureRecognizer::create() +{ + accel = new QAccelerometer(this); + accel->connectToBackend(); + + orientation = new QOrientationSensor(this); + orientation->connectToBackend(); + + timer = new QTimer(this); + + qoutputrangelist outputranges = accel->outputRanges(); + + if (outputranges.count() > 0) + accelRange = (int)(outputranges.at(0).maximum *2);//39 + else + accelRange = 40; //this should never happen + + connect(timer,SIGNAL(timeout()),this,SLOT(timeout())); + timer->setSingleShot(true); + timer->setInterval(1000); +} + + +QString QSlamSensorGestureRecognizer::id() const +{ + return QString("QtSensors.slam"); +} + +bool QSlamSensorGestureRecognizer::start() +{ + connect(accel,SIGNAL(readingChanged()),this,SLOT(accelChanged())); + accel->setDataRate(10); + active = accel->start(); + orientation->start(); + + return active; +} + +bool QSlamSensorGestureRecognizer::stop() +{ + accel->stop(); + orientation->stop(); + active = accel->isActive(); + disconnect(accel,SIGNAL(readingChanged()),this,SLOT(accelChanged())); + return !active; +} + +bool QSlamSensorGestureRecognizer::isActive() +{ + + return active; +} + +#define SLAM_FACTOR 5.0 + +void QSlamSensorGestureRecognizer::accelChanged() +{ + qreal x = accel->reading()->x(); + qreal y = accel->reading()->y(); + qreal z = accel->reading()->z(); + +//// very hacky + QOrientationReading::Orientation currentOrientation = orientation->reading()->orientation(); + + if (currentOrientation == QOrientationReading::FaceUp) { + z = z - 9.8; + } + if (currentOrientation == QOrientationReading::LeftUp + || currentOrientation == QOrientationReading::RightUp) { + x = x - 9.8; + } + if (currentOrientation == QOrientationReading::TopUp + ||currentOrientation == QOrientationReading::TopDown ) { + y = y - 9.8; + } + + qreal diffX = lastX - x; + qreal diffY = lastY - y; + qreal diffZ = lastZ - z; + + if (detecting && slamMap.count() > 5 && slamMap.at(5) == true) { + checkForSlam(); + } + + if (slamMap.count() > 5) + slamMap.removeLast(); + + if (fabs(x) > SLAM_FACTOR + || fabs(y) > SLAM_FACTOR + || fabs(z) > SLAM_FACTOR) { + + slamMap.insert(0,true); + + + if (!detecting && !timer->isActive() + && (fabs(diffX) < SLAM_FACTOR + 3 + ||fabs(diffY) < SLAM_FACTOR + 3 + ||fabs(diffZ) < SLAM_FACTOR + 3)) { + timer->start(); + detecting = true; + } + } else { + slamMap.insert(0,false); + } + + lastX = x; + lastY = y; + lastZ = z; +} + +void QSlamSensorGestureRecognizer::timeout() +{ + detecting = false; + slamMap.clear(); +} + +void QSlamSensorGestureRecognizer:: checkForSlam() +{ + slamOk = false; + + for (int i = 0; i < slamMap.count() - 1; i++) { + if (!slamMap.at(i)) { + slamOk = true; + } else { + detecting = false; + slamOk = false; + timer->stop(); + + return; + } + } + if (slamOk) { + Q_EMIT slam(); + Q_EMIT detected("slam"); + detecting = false; + slamMap.clear(); + timer->stop(); + } +} + +QT_END_NAMESPACE diff --git a/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.h b/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.h new file mode 100644 index 0000000..2fecb5a --- /dev/null +++ b/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#ifndef QSLAMSENSORGESTURERECOGNIZER_H +#define QSLAMSENSORGESTURERECOGNIZER_H + +#include <qsensorgesturerecognizer.h> +#include <QtSensors> +#include <QtSensors/QOrientationSensor> + +QT_BEGIN_NAMESPACE + +class QSlamSensorGestureRecognizer : public QSensorGestureRecognizer +{ + Q_OBJECT +public: + explicit QSlamSensorGestureRecognizer(QObject *parent = 0); + ~QSlamSensorGestureRecognizer(); + + void create(); + + QString id() const; + bool start(); + bool stop(); + bool isActive(); + +Q_SIGNALS: + void slam(); + +private slots: + void accelChanged(); + void timeout(); +private: + + QAccelerometer *accel; + QOrientationSensor *orientation; + QTimer *timer; + int accelRange; + bool active; + + qreal lastX; + qreal lastY; + qreal lastZ; + + + bool detecting; + bool slamOk; + + QList<bool> slamMap; + + void checkForSlam(); + +}; + +QT_END_NAMESPACE +#endif // QSLAMSENSORGESTURERECOGNIZER_H diff --git a/src/plugins/sensorgestures/qtsensors/qtsensorgestureplugin.cpp b/src/plugins/sensorgestures/qtsensors/qtsensorgestureplugin.cpp index f96cc94..eeedf1c 100644 --- a/src/plugins/sensorgestures/qtsensors/qtsensorgestureplugin.cpp +++ b/src/plugins/sensorgestures/qtsensors/qtsensorgestureplugin.cpp @@ -53,6 +53,7 @@ #include "qhoversensorgesturerecognizer.h" #include "qpickupsensorgesturerecognizer.h" #include "qshake2recognizer.h" +#include "qslamgesturerecognizer.h" #include "qturnoversensorgesturerecognizer.h" #include "qwhipsensorgesturerecognizer.h" @@ -76,6 +77,7 @@ QStringList QtSensorGesturePlugin::supportedIds() const list << "QtSensors.hover"; list << "QtSensors.pickup"; list << "QtSensors.shake2"; + list << "QtSensors.slam"; list << "QtSensors.turnover"; list << "QtSensors.twist"; list << "QtSensors.whip"; @@ -104,6 +106,9 @@ QList <QSensorGestureRecognizer *> QtSensorGesturePlugin::createRecognizers() QSensorGestureRecognizer *sRec6 = new QShake2SensorGestureRecognizer(this); recognizers.append(sRec6); + QSensorGestureRecognizer *sRec10 = new QSlamSensorGestureRecognizer(this); + recognizers.append(sRec10); + QSensorGestureRecognizer *sRec7 = new QTurnoverSensorGestureRecognizer(this); recognizers.append(sRec7); diff --git a/src/plugins/sensorgestures/qtsensors/qtsensors.pro b/src/plugins/sensorgestures/qtsensors/qtsensors.pro index 01fef4d..745b986 100644 --- a/src/plugins/sensorgestures/qtsensors/qtsensors.pro +++ b/src/plugins/sensorgestures/qtsensors/qtsensors.pro @@ -13,6 +13,7 @@ HEADERS += qtsensorgestureplugin.h \ qhoversensorgesturerecognizer.h \ qpickupsensorgesturerecognizer.h \ qshake2recognizer.h \ + qslamgesturerecognizer.h \ qturnoversensorgesturerecognizer.h \ qtwistsensorgesturerecognizer.h \ qwhipsensorgesturerecognizer.h @@ -23,6 +24,7 @@ SOURCES += qtsensorgestureplugin.cpp \ qhoversensorgesturerecognizer.cpp \ qpickupsensorgesturerecognizer.cpp \ qshake2recognizer.cpp \ + qslamgesturerecognizer.cpp \ qturnoversensorgesturerecognizer.cpp \ qtwistsensorgesturerecognizer.cpp \ qwhipsensorgesturerecognizer.cpp |