Code Examples Sensors API: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
'''English''' [[Code Examples Sensors API Spanish|Spanish]] | '''English''' [[Code Examples Sensors API Spanish|Spanish]] | ||
[[Category:Developing with Qt::QtMobility]] | |||
[toc align_right= | [toc align_right="yes" depth="2"] | ||
= Overview = | = Overview = | ||
This page points to and provides quick overviews of existing code examples using the | This page points to and provides quick overviews of existing code examples using the "Sensors API":http://doc.qt.nokia.com/qtmobility-1.1.0/sensors-api.html, one of the suite of Qt Mobilty APIs. | ||
== Fall detector == | == Fall detector == | ||
The | The "Fall Detector example":http://wiki.forum.nokia.com/index.php/Qt_Mobility_example_application:_Fall_Detector from Forum Nokia uses the accelerometer in a mobile device to detect if the user has fallen, then uses the | ||
* | * "Location API":http://doc.qt.nokia.com/qtmobility-1.1.0/location-overview.html to get the user's location | ||
* the | * the "Contacts API":http://doc.qt.nokia.com/qtmobility-1.1.0/contacts.html to store and access an email address to be notified in the event of a fall | ||
* and the | * and the "Messaging API ":http://doc.qt.nokia.com/qtmobility-1.1.0/messaging.html to send an email notification with photo attachment. | ||
[[Image:http://www.forum.nokia.com/piazza/wiki/images/1/15/Falldetector--screens.png?20100324100739|Fall Dector Screen Shot]] | [[Image:http://www.forum.nokia.com/piazza/wiki/images/1/15/Falldetector--screens.png?20100324100739|Fall Dector Screen Shot]] | ||
Here's the code snippet for the fall detector using the Sensors API: | |||
<code>#include | <code>#include <QAccelerometer> | ||
// Neccessary for Qt Mobility API usage | // Neccessary for Qt Mobility API usage | ||
QTM_USE_NAMESPACE | |||
class AccelerationInfo : public QObject, public QAccelerometerFilter | class AccelerationInfo : public QObject, public QAccelerometerFilter | ||
{ | |||
Q_OBJECT | |||
public: | public: | ||
AccelerationInfo(QObject* parent = 0) : QObject(parent) | AccelerationInfo(QObject* parent = 0) : QObject(parent) | ||
{ | |||
m_sensor = new QAccelerometer(this); | |||
m_sensor->addFilter(this); | |||
m_sensor->start(); | |||
} | |||
private slots: | private slots: | ||
// Override of QAcclerometerFilter::filter(QAccelerometerReading*) | // Override of QAcclerometerFilter::filter(QAccelerometerReading*) | ||
void filter(QAccelerometerReading* reading) | |||
{ | |||
qreal x = reading->x(); | |||
qreal y = reading->y(); | |||
qreal z = reading->z(); | |||
// Process acceleration sensor readings … | // Process acceleration sensor readings … | ||
qDebug( | qDebug("Current device acceleration: x=%f y=%f z=%f", x, y, z); | ||
} | |||
private: | private: | ||
QAccelerometer* m_sensor; | QAccelerometer* m_sensor; | ||
}; | |||
</code> | </code> | ||
Line 45: | Line 62: | ||
== Use accelerometer to control an Open GL-ES 3D model == | == Use accelerometer to control an Open GL-ES 3D model == | ||
This sensors tutorial from | This sensors tutorial from "Mobile Qt-Entwicklung":http://www.mobileqt.de/wiki/daten_des_accelerometer_sensors_mit_einem_opengl_objekt_verknuepfen (Mobile Qt Development) shows the accelerometer in an N900 manipulating a 3D model created using Open GL. The "descriptive text is in German":http://www.mobileqt.de/wiki/daten_des_accelerometer_sensors_mit_einem_opengl_objekt_verknuepfen, but all "the code is available in a downloadable package":http://www.mobileqt.de/tutorials/6/glsensordemo-0.1.zip and this "demo video":http://www.youtube.com/watch?v=uJpw0yeHJl8 shows you how it works. | ||
Here is the relevant accelerometer code: | Here is the relevant accelerometer code: | ||
<code>GLWidget::GLWidget(QWidget *parent) : | <code>GLWidget::GLWidget(QWidget *parent) : | ||
QGLWidget(parent) | |||
{ | { | ||
setWindowTitle(tr("Sensor-GL-Demo")); | |||
makeCurrent(); | |||
setAttribute(Qt::WA_PaintOnScreen); | setAttribute(Qt::WA_PaintOnScreen); | ||
setAttribute(Qt::WA_NoSystemBackground); | |||
setAutoBufferSwap(false); | |||
xRot = 0; | xRot = 0; | ||
yRot = 0; | |||
zRot = 0; | |||
_rotationSensorAvailable = false; | _rotationSensorAvailable = false; | ||
_rotationSensor = new QtMobility::QAccelerometer(this); | |||
_rotationSensor->connect(); | |||
if (!_rotationSensor->isAvailable()) { | |||
qWarning("No acceleration sensor available!"); | |||
} else { | |||
_rotationSensorAvailable = true; | |||
_rotationSensor->setSignalEnabled(false); // we get the values from the sensor itself | |||
_rotationSensor->setUpdateInterval(100); // as quickly as possible | |||
_rotationSensor->start(); | |||
} | |||
QTimer *timer = new QTimer(this); | QTimer *timer = new QTimer(this); | ||
timer->setInterval(10); | |||
QObject::connect(timer, SIGNAL (timeout()), this, SLOT (updateGL())); | |||
timer->start(); | |||
showFullScreen(); | |||
} |
Revision as of 10:49, 25 February 2015
English Spanish
[toc align_right="yes" depth="2"]
Overview
This page points to and provides quick overviews of existing code examples using the "Sensors API":http://doc.qt.nokia.com/qtmobility-1.1.0/sensors-api.html, one of the suite of Qt Mobilty APIs.
Fall detector
The "Fall Detector example":http://wiki.forum.nokia.com/index.php/Qt_Mobility_example_application:_Fall_Detector from Forum Nokia uses the accelerometer in a mobile device to detect if the user has fallen, then uses the
- "Location API":http://doc.qt.nokia.com/qtmobility-1.1.0/location-overview.html to get the user's location
- the "Contacts API":http://doc.qt.nokia.com/qtmobility-1.1.0/contacts.html to store and access an email address to be notified in the event of a fall
- and the "Messaging API ":http://doc.qt.nokia.com/qtmobility-1.1.0/messaging.html to send an email notification with photo attachment.
Fall Dector Screen Shot Here's the code snippet for the fall detector using the Sensors API:
#include <QAccelerometer>
// Neccessary for Qt Mobility API usage
QTM_USE_NAMESPACE
class AccelerationInfo : public QObject, public QAccelerometerFilter
{
Q_OBJECT
public:
AccelerationInfo(QObject* parent = 0) : QObject(parent)
{
m_sensor = new QAccelerometer(this);
m_sensor->addFilter(this);
m_sensor->start();
}
private slots:
// Override of QAcclerometerFilter::filter(QAccelerometerReading*)
void filter(QAccelerometerReading* reading)
{
qreal x = reading->x();
qreal y = reading->y();
qreal z = reading->z();
// Process acceleration sensor readings …
qDebug("Current device acceleration: x=%f y=%f z=%f", x, y, z);
}
private:
QAccelerometer* m_sensor;
};
Use accelerometer to control an Open GL-ES 3D model
This sensors tutorial from "Mobile Qt-Entwicklung":http://www.mobileqt.de/wiki/daten_des_accelerometer_sensors_mit_einem_opengl_objekt_verknuepfen (Mobile Qt Development) shows the accelerometer in an N900 manipulating a 3D model created using Open GL. The "descriptive text is in German":http://www.mobileqt.de/wiki/daten_des_accelerometer_sensors_mit_einem_opengl_objekt_verknuepfen, but all "the code is available in a downloadable package":http://www.mobileqt.de/tutorials/6/glsensordemo-0.1.zip and this "demo video":http://www.youtube.com/watch?v=uJpw0yeHJl8 shows you how it works.
Here is the relevant accelerometer code:
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
setWindowTitle(tr("Sensor-GL-Demo"));
makeCurrent();
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground);
setAutoBufferSwap(false);
xRot = 0;
yRot = 0;
zRot = 0;
_rotationSensorAvailable = false;
_rotationSensor = new QtMobility::QAccelerometer(this);
_rotationSensor->connect();
if (!_rotationSensor->isAvailable()) {
qWarning("No acceleration sensor available!");
} else {
_rotationSensorAvailable = true;
_rotationSensor->setSignalEnabled(false); // we get the values from the sensor itself
_rotationSensor->setUpdateInterval(100); // as quickly as possible
_rotationSensor->start();
}
QTimer *timer = new QTimer(this);
timer->setInterval(10);
QObject::connect(timer, SIGNAL (timeout()), this, SLOT (updateGL()));
timer->start();
showFullScreen();
}