English Spanish

Overview

This page points to and provides quick overviews of existing code examples using the Sensors API [doc.qt.nokia.com], one of the suite of Qt Mobilty APIs.

Fall detector

The Fall Detector example [wiki.forum.nokia.com] from Forum Nokia uses the accelerometer in a mobile device to detect if the user has fallen, then uses the

  • Location API [doc.qt.nokia.com] to get the user’s location
  • the Contacts API [doc.qt.nokia.com] to store and access an email address to be notified in the event of a fall
  • and the Messaging API [doc.qt.nokia.com] 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:

  1. #include <QAccelerometer>
  2.  
  3. // Neccessary for Qt Mobility API usage
  4. QTM_USE_NAMESPACE
  5.  
  6. class AccelerationInfo : public QObject, public QAccelerometerFilter
  7. {
  8.     Q_OBJECT
  9.  
  10. public:
  11.  
  12.     AccelerationInfo(QObject* parent = 0) : QObject(parent)
  13.     {
  14.         m_sensor = new QAccelerometer(this);
  15.         m_sensor->addFilter(this);
  16.         m_sensor->start();
  17.     }
  18.  
  19. private slots:
  20.  
  21.     // Override of QAcclerometerFilter::filter(QAccelerometerReading*)
  22.     void filter(QAccelerometerReading* reading)
  23.     {
  24.         qreal x = reading->x();
  25.         qreal y = reading->y();
  26.         qreal z = reading->z();
  27.  
  28.         // Process acceleration sensor readings ...
  29.  
  30.         qDebug("Current device acceleration: x=%f y=%f z=%f", x, y, z);
  31.     }
  32.  
  33. private:
  34.  
  35.     QAccelerometer* m_sensor;
  36. };

Use accelerometer to control an Open GL-ES 3D model

This sensors tutorial from Mobile Qt-Entwicklung [mobileqt.de] (Mobile Qt Development) shows the accelerometer in an N900 manipulating a 3D model created using Open GL. The descriptive text is in German [mobileqt.de], but all the code is available in a downloadable package [mobileqt.de] and this demo video [youtube.com] shows you how it works.

Here is the relevant accelerometer code:

  1. GLWidget::GLWidget(QWidget *parent) :
  2.     QGLWidget(parent)
  3.  
  4. {
  5.     setWindowTitle(tr("Sensor-GL-Demo"));
  6.     makeCurrent();
  7.  
  8.     setAttribute(Qt::WA_PaintOnScreen);
  9.     setAttribute(Qt::WA_NoSystemBackground);
  10.     setAutoBufferSwap(false);
  11.  
  12.     xRot = 0;
  13.     yRot = 0;
  14.     zRot = 0;
  15.  
  16.     _rotationSensorAvailable = false;
  17.     _rotationSensor = new QtMobility::QAccelerometer(this);
  18.     _rotationSensor->connect();
  19.     if (!_rotationSensor->isAvailable()) {
  20.         qWarning("No acceleration sensor available!");
  21.     } else {
  22.         _rotationSensorAvailable = true;
  23.         _rotationSensor->setSignalEnabled(false); // we get the values from the sensor itself
  24.         _rotationSensor->setUpdateInterval(100); // as quickly as possible
  25.         _rotationSensor->start();
  26.     }
  27.  
  28.     QTimer *timer = new QTimer(this);
  29.     timer->setInterval(10);
  30.     QObject::connect(timer, SIGNAL(timeout()), this, SLOT(updateGL()));
  31.     timer->start();
  32.     showFullScreen();
  33. }

Categories: