From a1ecc85fa88fdd318b379e35139c5d6486c78bd8 Mon Sep 17 00:00:00 2001 From: Lorn Potter Date: Mon, 13 Feb 2012 08:50:16 +1000 Subject: add slam gesture Change-Id: I0aa474d2398f59c0b766ed7fd7458ecd50b1fd4e Reviewed-by: Lorn Potter --- .../qtsensors/qslamgesturerecognizer.cpp | 203 +++++++++++++++++++++ .../qtsensors/qslamgesturerecognizer.h | 95 ++++++++++ .../qtsensors/qtsensorgestureplugin.cpp | 5 + src/plugins/sensorgestures/qtsensors/qtsensors.pro | 2 + 4 files changed, 305 insertions(+) create mode 100644 src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp create mode 100644 src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.h (limited to 'src/plugins/sensorgestures') diff --git a/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp b/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp new file mode 100644 index 00000000..37708d95 --- /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 + +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 00000000..2fecb5aa --- /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 +#include +#include + +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 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 f96cc945..eeedf1c9 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 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 01fef4d2..745b9863 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 -- cgit v1.2.3