Using MCE interface for vibration activation in Maemo 5
Article Metadata
N900 device has vibrator, which can be controlled from application. There is MCE interface for controlling vibrator through DBus. MCE Vibrator interface D-Bus methods - online documentation.
One can use dbus-send command line utility to try those methods without code writing. dbus-send manual page - note, an example contains missing quote.
Let's make an application, which plays all predefined LED patterns in a loop. QTimer is used to activate VibraExample's slot every 5 seconds.
#include <QApplication>
#include <QTimer>
#include "vibraexample.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
VibraExample vibraExample;
vibraExample.show();
QTimer timer;
QObject::connect(&timer, SIGNAL(timeout()), &vibraExample, SLOT(activate()));
timer.start(5000);
return a.exec();
}
This is how vibraexample.h looks like:
#ifndef VIBRA_EXAMPLE_H
#define VIBRA_EXAMPLE_H
#include <QLabel>
#include <QStringList>
#include <QDBusInterface>
class VibraExample : public QLabel
{
Q_OBJECT
public:
VibraExample(QWidget *parent=0);
~VibraExample();
public slots:
void activate();
private:
QDBusInterface *interface;
QStringList patterns;
int index;
};
#endif //VIBRA_EXAMPLE_H
You can find list of predefined Vibrator patterns in /etc/mce/mce.ini on the device. In constructor we fill string list with all those predefined patterns. MCE DBus interface is quite simple. In order to select currently active pattern use MCE_ACTIVATE_VIBRATOR_PATTERN and MCE_DEACTIVATE_VIBRATOR_PATTERN with pattern name. We call those methods in activate() slot. And MCE_ENABLE_VIBRATOR and MCE_DISABLE_VIBRATOR switch vibrator actually on/off. We call those two methods in constructor and destructor respectively.
Implementation of VibratorExample:
#include <mce/dbus-names.h>
#include <QDBusMessage>
#include <QDebug>
#include "vibraexample.h"
VibraExample::VibraExample(QWidget *parent)
: QLabel(parent), index(-1)
{
setAlignment(Qt::AlignCenter);
setFont(QFont("Nokia Sans", 35, QFont::Bold));
patterns << "PatternIncomingCall" << "PatternIncomingMessage"
<< "PatternPowerKeyPress" << "PatternTouchscreen"
<< "PatternChatAndEmail";
interface = new QDBusInterface(MCE_SERVICE, MCE_REQUEST_PATH,
MCE_REQUEST_IF, QDBusConnection::systemBus(),
this);
QDBusMessage reply = interface->call(MCE_ENABLE_VIBRATOR);
if (reply.type() == QDBusMessage::ErrorMessage)
qDebug() << reply.errorMessage();
}
VibraExample::~VibraExample()
{
QDBusMessage reply;
reply = interface->call(MCE_DISABLE_VIBRATOR);
if (reply.type() == QDBusMessage::ErrorMessage)
qDebug() << reply.errorMessage();
if (index > -1) {
reply = interface->call(MCE_DEACTIVATE_VIBRATOR_PATTERN, patterns.at(index));
if (reply.type() == QDBusMessage::ErrorMessage)
qDebug() << reply.errorMessage();
}
}
void VibraExample::activate()
{
if (patterns.count() < 1)
return;
QDBusMessage reply;
if (index > -1) {
reply = interface->call(MCE_DEACTIVATE_VIBRATOR_PATTERN, patterns.at(index));
if (reply.type() == QDBusMessage::ErrorMessage)
qDebug() << reply.errorMessage();
}
index = index < patterns.count() - 1 ? ++index : 0;
setText(patterns.at(index));
reply = interface->call(MCE_ACTIVATE_VIBRATOR_PATTERN, patterns.at(index));
if (reply.type() == QDBusMessage::ErrorMessage)
qDebug() << reply.errorMessage();
}
Project file should be something like this:
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
QT += dbus
unix {
CONFIG += link_pkgconfig
PKGCONFIG += mce
}
# Input
HEADERS += vibraexample.h
SOURCES += main.cpp \
vibraexample.cpp


(no comments yet)