Wednesday, 27 October 2010

Accessing the phone vibrator

How can I access the vibrate function from my apps you may say...?

Well the vibrate function is part of the NLED (or notification LED) driver. You can use the NLED functions to pulse or turn on/off the motor in the same way as any LED. To find the vibrator search through the list of LEDs until you find one with a cycle time of -1, this indicates the vibrator.

The following code is complete example of searching through the available LEDs and turning on and off only the vibrate function. This should work on any Windows Mobile or Windows CE with a compatible NLED driver:


#include "nled.h"

enum Status
{
OFF = 0,
ON,
BLINK
};

int GetLedCount()
{
int count = 0;
NLED_COUNT_INFO nci;
if (NLedGetDeviceInfo(NLED_COUNT_INFO_ID, &nci))
{
count = nci.cLeds;
}
return count;
}

int ledCount = GetLedCount();

void SetLedStatus(Status status)
{
NLED_SETTINGS_INFO nsi;
NLED_SUPPORTS_INFO nInfo;
nsi.OffOnBlink = (uint)status;
for (int i = 0; i < ledCount; i++)
{
// request information from this led, we're looking for a
// cycleAdjust of -1 which indicates the vibrator
nInfo.LedNum = i;
NLedGetDeviceInfo(NLED_SUPPORTS_INFO_ID, &nInfo);
if (nInfo.lCycleAdjust == -1)
{
nsi.LedNum = (uint)i;
NLedSetDevice(NLED_SETTINGS_INFO_ID, &nsi);
}
}
}

void VibrateOn()
{
SetLedStatus(Status::ON);
}

void VibrateOff()
{
SetLedStatus(Status::OFF);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
VibrateOn();
Sleep(500);
VibrateOff();
}

No comments: