Manually Controlling the LED

< BACK  NEXT >
[oR]

The notification functions use the LED as one way to notify the user of an event, but sometimes it is necessary to control the LED yourself. For example, you might want to notify the user of an event not supported by the notification functions. The NLedGetDeviceInfo function is used to determine the number of LEDs on the device It is conceivable that a special device may have more than one LED, and some devices may not have any at all. The NLedSetDevice function is used to turn the LED on and off. Both these functions interact with the LED driver written by the device's manufacturer.

There are various options that an LED driver can support beyond the simple default blinking behavior. The NLedSetDevice function allows the following options to be set:

  • Total Cycle Time: The total time the LED will blink before turning itself off

  • The time for which the LED will be on

  • The time for which the LED will be off

  • The on meta-cycle time

  • The off meta-cycle time

A LED can simply blink on and off using the on and off times, or it can perform a more complex sequence using the meta-cycle times. With a meta-cycle, the LED will blink for the meta-cycle time, and then turn off completely for the meta-cycle off time. It will then blink the LED for the on meta-cycle time, and so on for the total cycle time. Before you start implementing Morse code for the LED, you should note that most devices only support simple on-off blinking.

The functions NLedGetDeviceInfo and NLedSetDevice are implemented in coredll.dll, but are not generally declared in SDK header files. Therefore, you will need to add function prototypes. Also, the functions use structures that are declared in the header file NLed.H:

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

First, you will need to determine the number of LEDs present on the device, and then get the capabilities of the LED, using the function NLedGetDeviceInfo. The function takes an identifier as the first argument that specifies what information is being requested, and a pointer to an appropriate structure to receive the information in the second parameter. Table 7.2 shows the identifiers and the corresponding structures.

Table 7.2. Identifiers and structures for NLedGetDeviceInfo
Constant Structure Purpose
NLED_COUNT_INFO_ID NLED_COUNT_INFO Return the number of LEDs
NLED_SUPPORTS_INFO_ID NLED_SUPPORTS_INFO Determine LED capabilities
NLED_SETTINGS_INFO_ID NLED_SETTINGS_INFO Return current LED settings

Listing 7.6 shows calling NLedGetDeviceInfo first to determine the number of LEDs, which is returned in the cLeds member of NLED_COUNT_ INFO. Assuming there is one, the next call to NLedGetDeviceInfo will get the characteristics associated with LED number zero (the first). To do this, the NLED_SUPPORTS_INFO structure member LedNum is initialized with the LED number, and then the call is made.

Listing 7.6 Determines LED capabilites
 void Listing7_6() {   NLED_COUNT_INFO nci;   NLED_SUPPORTS_INFO  nsup;   if(!NLedGetDeviceInfo(NLED_COUNT_INFO_ID, (PVOID) &nci))   {     cout   _T("Could not get LED information")            endl;     return;   }   cout     _T("Number of LEDs: ")   (int)nci.cLeds            endl;   memset(&nsup, 0, sizeof(nsup));   nsup.LedNum = 0; // get information on first LED   if(!NLedGetDeviceInfo(NLED_SUPPORTS_INFO_ID,     (PVOID) &nsup))   {     cout   _T("Could not get LED support options")            endl;     return;   }   cout    _T("Cycle Adjust:") //0 = off 1 = on 2 = blink            nsup.lCycleAdjust   endl;   cout    _T("Adj. Total Cycle Time:")            nsup.fAdjustTotalCycleTime   endl;   cout    _T("Separate On Time:")            nsup.fAdjustOnTime   endl;   cout    _T("Separate Off Time:"            nsup.fAdjustOffTime   endl;   cout   _T("Can Meta Cycle On:"            nsup.fMetaCycleOn   endl;   cout   _T("Can Meta Cycle Off:")             nsup.fMetaCycleOff   endl; } 

The lCycleAdjust member indicates whether the LED can be turned on and off, or be made to blink. The remaining members are BOOL values indicating which timings, if any, can be changed.

The code in Listing 7.7 is used to toggle the LED between blinking and not blinking. The NLED_SETTINGS_INFO structure member LedNum is initialized with the LED number set, and OffOnBlink will be set to 2 to start blinking or 0 to stop blinking. This structure has other members to change cycle times and so on, but they are not used in this example.

Listing 7.7 Toggles LED blinking status
 void Listing7_7() {   NLED_SETTINGS_INFO nsi;   static int nLastSetting = 0;        // initially off   if(nLastSetting == 0)     nLastSetting = 2;  // blink   else     nLastSetting = 0;  // off     nsi.LedNum = 0;   nsi.OffOnBlink = nLastSetting;   if(!NLedSetDevice(NLED_SETTINGS_INFO_ID, &nsi))     cout   _T("Could not set LED settings")   endl; } 

< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net