×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:How to get accelerometer data of N900 using Qt

From Nokia Developer Wiki
Jump to: navigation, search
flaviofabricioferreira (Talk | contribs)
flaviofabricioferreira (Talk | contribs)
Line 115: Line 115:
 
[[File:PostN900AccelerometerSnapshoots.png]]
 
[[File:PostN900AccelerometerSnapshoots.png]]
  
{{ Tip | The N900 allow that you use shortcuts with the combination of keys.
+
{{ Tip | The N900 allow that you use shortcuts with the combination of keys.}}
  
 
[[File:Minimizan900.png.png]|300px]
 
[[File:Minimizan900.png.png]|300px]
Line 122: Line 122:
 
[[File:Pasten900.png]|300px]
 
[[File:Pasten900.png]|300px]
  
}}
 
  
 
===Source Code===
 
===Source Code===

Revision as of 07:46, 24 March 2010


Contents

Introduction

In this post, I will demonstrate how to develop a Qt Application that will to get the information of accelerometer sensor at N900 device.

An accelerometer is a device that measures proper acceleration, the acceleration experienced relative to freefall.Single- and multi-axis models are available to detect magnitude and direction of the acceleration as a vector quantity, and can be used to sense orientation, vibration and shock. Micromachined accelerometers are increasingly present in portable electronic devices like mobile phones and video game controllers, to detect the orientation of the device or provide for game input.

Accelerometer Data at N900

The accelerometer data is sysfs file information.Sysfs is a virtual file system provided by Linux 2.6. Sysfs exports information about devices and drivers from the kernel device model to userspace, and is also used for configuration.

The three coordinates of accelerometer sensor is in a text file provided on one line and separated by white space. This file is at path below:

/sys/class/i2c-adapter/i2c-3/3-001d/coord

The values are in mG (milli G). 1000 = 1 G. The g-force experienced by an object is its acceleration relative to free-fall like described above.

At N900 device , the values are connected to the position showed below.

PostN900Accelerometer.png

The Code

  • At starts, i created a QThread. This thread will read the file with data at all the time.
class aThread : public QThread
{
Q_OBJECT
public:
aThread();
void run();
QString x;
QString y;
QString z;
 
public slots:
void getXYZ();
void process_data();
signals:
void dataProcessed(QString,QString,QString);
private:
QString data;
};
  • At implementation of our thread , the getXYZ() method is called always.
void aThread::run()
{
int a=1;
while(a)
{
getXYZ();
}
}
  • This method open the file, read and process the string found on the file.
void aThread::getXYZ()
{
QFile file("/sys/class/i2c-adapter/i2c-3/3-001d/coord");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
data = in.readAll();
process_data();
}
  • Then, in the processing of information, the string found is splited by the white spaces creating three different strings.These strings are the X,Y,Z coordinates of accelerometer.The signal dataProcessed() is emitted to allowing that other class can connect yours slots to this signal.
void aThread::process_data()
{
QStringList data_splited = data.split(" ");
 
x = data_splited[0];
y = data_splited[1];
z = data_splited[2];
 
emit dataProcessed(x,y,z);
}
  • At constructor of our superior class , i created the thread described above . Then i connected the signal dataProcessed of our thread with the slot printData of superior class.
Accelerometer::Accelerometer(QWidget *parent)
: QWidget(parent), ui(new Ui::Accelerometer)
{
ui->setupUi(this);
myThread = new aThread();
connect(myThread,SIGNAL(dataProcessed(QString,QString,QString)),this,SLOT(printData(QString,QString,QString)));
...
  • Then the printData method shows the three axis information of accelerometer sensor at labels at the screen.
void Accelerometer::printData(QString x,QString y,QString z)
{
xlabel->setText(x);
ylabel->setText(y);
zlabel->setText(z);
}

Snap Shoots

PostN900AccelerometerSnapshoots.png

Tip.pngTip: The N900 allow that you use shortcuts with the combination of keys.

[[File:Minimizan900.png.png]|300px] [[File:Printscreenn900.png]|300px] [[File:Copyn900.png]|300px] [[File:Pasten900.png]|300px]


Source Code

462 page views in the last 30 days.
×