Showing posts with label vibration. Show all posts
Showing posts with label vibration. Show all posts

Saturday, August 10, 2013

Using VibrationController API wit BB10 Cascades QML

I tried to add vibration support to my BB10 application Counter. Application is simple, which just increment the count when you press add button and decrements count on minus button. Simple but useful, I use it quite often to count the stuffs.

But you need to see at screen to see if it button is really pressed, so I thought to add vibration support, so I know button is pressed even without looking at screen. This is first time I am adding Vibration support to any of BB10 application. So I thought to share code.

Code is much simple, only trick is that you need to register VibrationController c++ class to QML system.

Here is how it goes.

First we need to link device lib which has VibrationController API. Add following line to your .pro file and you are good to go.

LIBS += -lbbdevice

Now in main.cpp you need to add proper header to locate VibrarionController API and the register it to QML registry.
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

#include <bb/device/VibrationController>

using namespace bb::cascades;
using namespace bb::device;

int main(int argc, char **argv)
{
    Application app(argc, argv);

    qmlRegisterType<vibrationcontroller>
        ("bb.vibrationController", 1, 0, "VibrationController");

    new Counter(&app);

    return Application::exec();
}

Now we are ready to use VibrarionController with QML code. First you need to import VibraionController QML by import statement. Then we are creating it by defining it in attachedObjects. Once this is done, we can use it's start API to vibrate phone with certain intensity for certain durarion.

import bb.cascades 1.0
import bb.vibrationController 1.0

Page {
    id: page
    Container {
        id: root
        layout: DockLayout {}
        preferredHeight: 1280
        preferredWidth: 768
        touchPropagationMode: TouchPropagationMode.Full;
        
        attachedObjects: [
            VibrationController {
                id: vib
            }
        ]

        function vibrate() {
            //first parameter intensity, second parameter duration for vibration
            vib.start(80, 100);
        }

        Button{
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center 
            onClicked: {
                root.vibrate();
            }
        }
    }
}

Thant's all, hope this will be helpful.

Sunday, June 27, 2010

Using vibration API in Maemo

Recently I was creating some simple application that give notification when some new information is available in one site that I visit often.

This application play sound and vibrate when new information is available. In this blog I will share code required to enable vibration support in Maemo application and I am using Qt framework to implement my application.

In Maemo, Vibration API is based on DBUS call, with proper DBUS call you can enable disable vibration and apply certain vibration pattern.

To be able to use DBUS api, Qt required to enable DBUS support, which can be done by putting this line "Qt += dbus" in to .pro file.

Now we can use DBUS API to enable and apply vibration. API are from following headers.

#include <QtDBus>

#ifdef Q_WS_MAEMO_5
#include <mce mode-names.h>
#include <mce dbus-names.h>
#endif

by sending MCE_ENABLE_VIBRATOR dbus call, we tell OS to enable vibration support.

void Sample::enableVibration()
{
    #ifdef Q_WS_MAEMO_5
    mDbusInterface = new QDBusInterface(MCE_SERVICE, MCE_REQUEST_PATH,
                                     MCE_REQUEST_IF, QDBusConnection::systemBus(),
                                     this);

    mDbusInterface->call(MCE_ENABLE_VIBRATOR);

    #endif
}

Now that, vibration is enabled, we can apply certain vibration pattern. For my application is am enabling "PatternIncomingCall", After this call vibration will be on until we deactivate applied vibration patter.

void Sample::vibrate(int aTimeout)
{
    #ifdef Q_WS_MAEMO_5

    mDbusInterface->call(MCE_ACTIVATE_VIBRATOR_PATTERN, "PatternIncomingCall");
    QTimer::singleShot(aTimeout,this,SLOT(stopVibration()));
    #endif
}

And finally "MCE_DEACTIVATE_VIBRATOR_PATTERN" dbus call will deactivate vibration patter.

void Sample::stopVibration()
{
    #ifdef Q_WS_MAEMO_5
    mDbusInterface->call(MCE_DEACTIVATE_VIBRATOR_PATTERN, "PatternIncomingCall");
    #endif
} 

So this was all that required to enable vibration support for Maemo application.