Showing posts with label Accelerometer. Show all posts
Showing posts with label Accelerometer. Show all posts

Saturday, June 5, 2010

Snake game for Maemo

Past few weeks I was working on Snake game implementation for maemo. I tried to implement classic snake game in Qt as learning exercise. To make game more interesting, game is using Accelerometer to direct snake and there is also support for keyboard to direct snake.

Source code is already in gitorous. and installation file is also uploaded on maemo extra-testing repository. To install game on n900 enable either extra-devel or extra-testing and then from terminal type apt-get install snake.

Here are fews snaps from game for overview.

Friday, March 5, 2010

Puyo game for N900, created using Qt

I have created simple game named Puyo for N900 using Qt, its kind of Tatris game. It use N900's accelerometer to move smiles left or right and smiles will disappear if three or more smilies with same color appear in row or column.

Here are screenshots of my little game. Its still under development and might have some bugs. If you want to try this game, I am attaching debian installer and also instruction for installation.






Download debian package from here. This package depends on Maemo Qt Extra Library , so you need to install Qt extra library as well. Find instruction here to install Maemo Qt Extra library.

You will also need root access to install this debian packge. If you don't have root access to device, Install rootsh script from application manager that will help you to get root access.

Sunday, February 28, 2010

Using Accelerometers API on Maemo

Maemo is very powerful platform for creating application and Qt as application development framework make development on maemo fun. Maemo provide various API to developer to create application so i thought to try out accelorometer API on maemo.

Accelerometer API can be used in two way. Reading file which store orientation data or Using Dbus API to get orientation data.

You can find more details here http://wiki.maemo.org/Accelerometers

For my purpose reading data from file was quite enough. My puropose to detect in which direction user has Shake the phone.

Here is code for using acceleromete API.

class MotionSenser : public QObject
{
Q_OBJECT

public:
    explicit MotionSenser(QObject *parent = 0); 
private slots:
    void timerEvent(QTimerEvent* event );

private:
    void readCurrentPos();

private:
    QPointF mLastPos;
    QPointF mCurrentPos;
    QObject* mParent;

};


static const QString PROC_ENTRY = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";

MotionSenser::MotionSenser(QObject *parent)
: QObject(parent),mParent(parent)
{
    mLastPos = mCurrentPos = QPointF(0,0);
    this->startTimer(100);   
}
void MotionSenser::timerEvent(QTimerEvent* /*event*/)
{
    if( qAbs( mLastPos.y() - mCurrentPos.y() ) < 250 ) {
        //ignore event
        return;
    }

    if( mCurrentPos.y() > 150 ) {
        qDebug() << "####### Posting right event";
        QCoreApplication::postEvent(mParent,new QKeyEvent(QEvent::KeyPress,Qt::Key_Right,Qt::NoModifier));
    }
    else if ( mCurrentPos.y() < -150 ) {
        qDebug() << "####### Posting left event";
        QCoreApplication::postEvent(mParent,new QKeyEvent(QEvent::KeyPress,Qt::Key_Left,Qt::NoModifier));
    }
    mLastPos = mCurrentPos;
}

void MotionSenser::readCurrentPos()
{
    QFile f(PROC_ENTRY);
    if (!f.exists() or !f.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "could not read motion data";
    }
    else {
        QByteArray line = f.readLine();
        QRegExp rx("([0-9-]+) ([0-9-]+) ([0-9-]+)");

        rx.indexIn(line);
        qreal x = rx.cap(1).toInt();
        qreal y = rx.cap(2).toInt();
        qreal z = rx.cap(3).toInt();

        mCurrentPos.setX(x);
        mCurrentPos.setY(y);

        f.close();
    }
}