How to control the LED of your Pocket PC

Stephane Sibue (webmaster@codeppc.com), January 11, 2001.

The LED of the Pocket PC

Led on iPAQ The LED is a part of Pocket PC specification. Generally it allows to indicate either that the battery is attached or that the Pocket PC is directly connected to the cradle. A Pocket PC can implement several LEDS, but usually there is only one. This article is translated from CodePPC.com.

The necessary functions not declared in the SDK

Functions for handling LEDs can be found in 'coredll.dll' library, but they are not declared in the SDK (who knows why?). It is only the 'NLed.h' file that can make them appear, but the prototypes of the functions are not declared in this file.

The first thing to do is to include the 'NLed.h' file and to declare 2 functions for handling LEDs:

# include < nled.h > extern " C " { BOOL NLedGetDeviceInfo(INT nID, PVOID pOutput); BOOL NLedSetDevice(INT nID, PVOID pOutput); }

First of all, how many LEDs are there on my Pocket PC?

Initially, it is necessary to know how many LEDs are present, knowing that the number of the first one is 0. To carry out this checking, we will use the 'NLedGetDeviceInfo' function:

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

In case of a problem, this function returns 0. It also returns 0 if a Pocket PC does not have a LED to handle. In 99 cases out of a hundred this function returns 1.

Off, On or Blink

We can now modify the state of our LED. According to the contents of the 'NLed.h' file, the LED can appear in 3 states: off, on, and blink (stop, functioning, and blinking). Each state corresponds to a value of the 'OffOnBlink' parameter of the 'NLED_SETTINGS_INFO' structure. This structure also allows to implement other effects but this exceeds the topic of this article, we will return to this implementation later. Thus we will create a function, which takes the desired state of our LED as a parameter, which can be 0 for idle (off), 1 for fixed functioning (on), and 2 for the effect of blinking (blink):

void SetLedStatus(int wLed, int wStatus) { NLED_SETTINGS_INFO nsi; nsi.LedNum = (INT) wLed; nsi.OffOnBlink = (INT) wStatus; NLedSetDevice(NLED_SETTINGS_INFO_ID, &nsi); }

Thus, to light up the LED n°1, it is enough to write:

SetLedStatus(0, 1);

to make it blink:

SetLedStatus(0, 2);

and finally to estinguish it:

SetLedStatus(0, 0);