accelerationsensor.cpp Example File
accelgame/src/accelerationsensor.cpp
#include <QDebug>
#include "accelerationsensor.hpp"
AccelerationSensor::AccelerationSensor(QObject *parent)
: QObject(parent)
, m_x(0)
, m_y(0)
, m_z(0)
{
if (!m_accelerationSensor.connectToBackend())
qWarning() << "Cannot connect to acceleration sensor backend!";
m_accelerationSensor.addFilter(this);
m_accelerationSensor.setAccelerationMode(QAccelerometer::Gravity);
m_accelerationSensor.setAxesOrientationMode(QAccelerometer::AutomaticOrientation);
}
qreal AccelerationSensor::x() const
{
return m_x;
}
qreal AccelerationSensor::y() const
{
return m_y;
}
qreal AccelerationSensor::z() const
{
return m_z;
}
void AccelerationSensor::start()
{
m_accelerationSensor.start();
}
void AccelerationSensor::stop()
{
m_accelerationSensor.stop();
}
bool AccelerationSensor::filter(QAccelerometerReading *reading)
{
const qreal oldX = m_x;
const qreal oldY = m_y;
const qreal oldZ = m_z;
m_x = reading->x();
m_y = reading->y();
m_z = reading->z();
if (oldX != m_x)
emit xChanged();
if (oldY != m_y)
emit yChanged();
if (oldZ != m_z)
emit zChanged();
return false;
}