Using MCE interface for LED activation in Maemo 5
Article Metadata
N900 device has LED, which can be controlled from application. There is MCE interface for controlling LED through DBus. MCE LED 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 LedExample's slot every 5 seconds.
#include <QApplication>
#include <QTimer>
#include "ledexample.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LedExample ledExample;
ledExample.show();
QTimer timer;
QObject::connect(&timer, SIGNAL(timeout()), &ledExample, SLOT(activate()));
timer.start(5000);
return a.exec();
}
This is how ledexample.h looks like.
#ifndef LED_EXAMPLE_H
#define LED_EXAMPLE_H
#include <QLabel>
#include <QStringList>
#include <QDBusInterface>
class LedExample : public QLabel
{
Q_OBJECT
public:
LedExample(QWidget *parent=0);
~LedExample();
public slots:
void activate();
private:
QDBusInterface *interface;
QStringList patterns;
int index;
};
#endif //LED_EXAMPLE_H
You can find list of predefined LED 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 patter use MCE_ACTIVATE_LED_PATTERN and MCE_DEACTIVATE_LED_PATTERN with pattern name. We call those methods in activate() slot. And MCE_ENABLE_LED and MCE_DISABLE_LED switch LED actually on/off. We call those two methods in constructor and destructor respectively.
Implementation of LedExample:
#include <mce/dbus-names.h>
#include <QDBusMessage>
#include <QDebug>
#include "ledexample.h"
LedExample::LedExample(QWidget *parent)
: QLabel(parent), index(-1)
{
setAlignment(Qt::AlignCenter);
setFont(QFont("Nokia Sans", 35, QFont::Bold));
patterns << "PatternError" << "PatternDeviceOn" << "PatternPowerOn"
<< "PatternPowerOff" << "PatternCommunicationCall"
<< "PatternCommunicationIM" << "PatternCommunicationSMS"
<< "PatternCommunicationEmail" << "PatternCommonNotification"
<< "PatternWebcamActive" << "PatternBatteryCharging"
<< "PatternBatteryFull" << "PatternDeviceSoftOff";
interface = new QDBusInterface(MCE_SERVICE, MCE_REQUEST_PATH,
MCE_REQUEST_IF, QDBusConnection::systemBus(),
this);
QDBusMessage reply = interface->call(MCE_ENABLE_LED);
if (reply.type() == QDBusMessage::ErrorMessage)
qDebug() << reply.errorMessage();
}
LedExample::~LedExample()
{
QDBusMessage reply;
reply = interface->call(MCE_DISABLE_LED);
if (reply.type() == QDBusMessage::ErrorMessage)
qDebug() << reply.errorMessage();
if (index > -1) {
reply = interface->call(MCE_DEACTIVATE_LED_PATTERN, patterns.at(index));
if (reply.type() == QDBusMessage::ErrorMessage)
qDebug() << reply.errorMessage();
}
}
void LedExample::activate()
{
if (patterns.count() < 1)
return;
QDBusMessage reply;
if (index > -1) {
reply = interface->call(MCE_DEACTIVATE_LED_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_LED_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 += ledexample.h
SOURCES += main.cpp \
ledexample.cpp


(no comments yet)