Code Examples Sensors API: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[Code-Examples-Sensors-API-Spanish|Spanish]]
'''English''' [[Code Examples Sensors API Spanish|Spanish]]<br />[[Category:Developing with Qt::QtMobility]]


=Overview=
[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]


This page points to and provides quick overviews of existing code examples using the [http://doc.qt.nokia.com/qtmobility-1.1.0/sensors-api.html Sensors <span class="caps">API</span>] ''[doc.qt.nokia.com]'', one of the suite of Qt Mobilty <span class="caps">API</span>s.
= Overview =


==Fall detector==
This page points to and provides quick overviews of existing code examples using the &quot;Sensors API&amp;quot;:http://doc.qt.nokia.com/qtmobility-1.1.0/sensors-api.html, one of the suite of Qt Mobilty APIs.


The [http://wiki.forum.nokia.com/index.php/Qt_Mobility_example_application:_Fall_Detector 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
== Fall detector ==


* [http://doc.qt.nokia.com/qtmobility-1.1.0/location-overview.html Location <span class="caps">API</span>] ''[doc.qt.nokia.com]'' to get the user’s location
The &quot;Fall Detector example&amp;quot;: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


* the [http://doc.qt.nokia.com/qtmobility-1.1.0/contacts.html Contacts <span class="caps">API</span>] ''[doc.qt.nokia.com]'' to store and access an email address to be notified in the event of a fall
* &quot;Location API&amp;quot;:http://doc.qt.nokia.com/qtmobility-1.1.0/location-overview.html to get the user's location


* and the [http://doc.qt.nokia.com/qtmobility-1.1.0/messaging.html Messaging <span class="caps">API</span>] ''[doc.qt.nokia.com]'' to send an email notification with photo attachment.
* the &quot;Contacts API&amp;quot;: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


[[Image:Falldetector--screens.png|Fall Dector Screen Shot]]<br /> Here’s the code snippet for the fall detector using the Sensors <span class="caps">API</span>:
* and the &quot;Messaging API &quot;:http://doc.qt.nokia.com/qtmobility-1.1.0/messaging.html to send an email notification with photo attachment.


==Use accelerometer to control an Open GL-ES 3D model==
[[Image:http://www.forum.nokia.com/piazza/wiki/images/1/15/Falldetector--screens.png?20100324100739|Fall Dector Screen Shot]]<br />Here's the code snippet for the fall detector using the Sensors API:


This sensors tutorial from [http://www.mobileqt.de/wiki/daten_des_accelerometer_sensors_mit_einem_opengl_objekt_verknuepfen Mobile Qt-Entwicklung] ''[mobileqt.de]'' (Mobile Qt Development) shows the accelerometer in an N900 manipulating a 3D model created using Open GL. The [http://www.mobileqt.de/wiki/daten_des_accelerometer_sensors_mit_einem_opengl_objekt_verknuepfen descriptive text is in German] ''[mobileqt.de]'', but all [http://www.mobileqt.de/tutorials/6/glsensordemo-0.1.zip the code is available in a downloadable package] ''[mobileqt.de]'' and this [http://www.youtube.com/watch?v=uJpw0yeHJl8 demo video] ''[youtube.com]'' shows you how it works.
<code>#include &lt;QAccelerometer&amp;gt;
 
// Neccessary for Qt Mobility API usage<br />QTM_USE_NAMESPACE
 
class AccelerationInfo : public QObject, public QAccelerometerFilter<br />{<br /> Q_OBJECT
 
public:
 
AccelerationInfo(QObject* parent = 0) : QObject(parent)<br /> {<br /> m_sensor = new QAccelerometer(this);<br /> m_sensor-&gt;addFilter(this);<br /> m_sensor-&gt;start();<br /> }
 
private slots:
 
// Override of QAcclerometerFilter::filter(QAccelerometerReading*)<br /> void filter(QAccelerometerReading* reading)<br /> {<br /> qreal x = reading-&gt;x();<br /> qreal y = reading-&gt;y();<br /> qreal z = reading-&gt;z();
 
// Process acceleration sensor readings …
 
qDebug(&quot;Current device acceleration: x=%f y=%f z=%f&amp;quot;, x, y, z);<br /> }
 
private:
 
QAccelerometer* m_sensor;<br />};
 
</code>
 
== Use accelerometer to control an Open GL-ES 3D model ==
 
This sensors tutorial from &quot;Mobile Qt-Entwicklung&amp;quot;: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 &quot;descriptive text is in German&amp;quot;:http://www.mobileqt.de/wiki/daten_des_accelerometer_sensors_mit_einem_opengl_objekt_verknuepfen, but all &quot;the code is available in a downloadable package&amp;quot;:http://www.mobileqt.de/tutorials/6/glsensordemo-0.1.zip and this &quot;demo video&amp;quot;:http://www.youtube.com/watch?v=uJpw0yeHJl8 shows you how it works.


Here is the relevant accelerometer code:
Here is the relevant accelerometer code:


===Categories:===
<code>GLWidget::GLWidget(QWidget *parent) :<br /> QGLWidget(parent)
 
{<br /> setWindowTitle(tr(&quot;Sensor-GL-Demo&amp;quot;));<br /> makeCurrent();
 
setAttribute(Qt::WA_PaintOnScreen);<br /> setAttribute(Qt::WA_NoSystemBackground);<br /> setAutoBufferSwap(false);
 
xRot = 0;<br /> yRot = 0;<br /> zRot = 0;
 
_rotationSensorAvailable = false;<br /> _rotationSensor = new QtMobility::QAccelerometer(this);<br /> _rotationSensor-&gt;connect();<br /> if (!_rotationSensor-&gt;isAvailable()) {<br /> qWarning(&quot;No acceleration sensor available!&quot;);<br /> } else {<br /> _rotationSensorAvailable = true;<br /> _rotationSensor-&gt;setSignalEnabled(false); // we get the values from the sensor itself<br /> _rotationSensor-&gt;setUpdateInterval(100); // as quickly as possible<br /> _rotationSensor-&gt;start();<br /> }


* [[:Category:Developing with Qt|Developing_with_Qt]]
QTimer *timer = new QTimer(this);<br /> timer-&gt;setInterval(10);<br /> QObject::connect(timer, SIGNAL (timeout()), this, SLOT (updateGL()));<br /> timer-&gt;start();<br /> showFullScreen();<br />}
** [[:Category:Developing with Qt::QtMobility|QtMobility]]

Revision as of 10:29, 24 February 2015

English Spanish

[toc align_right="yes&quot; depth="2&quot;]

Overview

This page points to and provides quick overviews of existing code examples using the "Sensors API&quot;: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&quot;: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

Fall Dector Screen Shot
Here's the code snippet for the fall detector using the Sensors API:

#include &lt;QAccelerometer&amp;gt;

// Neccessary for Qt Mobility API usage<br />QTM_USE_NAMESPACE

class AccelerationInfo : public QObject, public QAccelerometerFilter<br />{<br /> Q_OBJECT

public:

AccelerationInfo(QObject* parent = 0) : QObject(parent)<br /> {<br /> m_sensor = new QAccelerometer(this);<br /> m_sensor-&gt;addFilter(this);<br /> m_sensor-&gt;start();<br /> }

private slots:

// Override of QAcclerometerFilter::filter(QAccelerometerReading*)<br /> void filter(QAccelerometerReading* reading)<br /> {<br /> qreal x = reading-&gt;x();<br /> qreal y = reading-&gt;y();<br /> qreal z = reading-&gt;z();

// Process acceleration sensor readings …

qDebug(&quot;Current device acceleration: x=%f y=%f z=%f&amp;quot;, x, y, z);<br /> }

private:

QAccelerometer* m_sensor;<br />};

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

This sensors tutorial from "Mobile Qt-Entwicklung&quot;: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&quot;:http://www.mobileqt.de/wiki/daten_des_accelerometer_sensors_mit_einem_opengl_objekt_verknuepfen, but all "the code is available in a downloadable package&quot;:http://www.mobileqt.de/tutorials/6/glsensordemo-0.1.zip and this "demo video&quot;: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&quot;));
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();
}