Windows Mobile Devices and Power States

I’ve been doing some Windows Mobile development with the .NET Compact Framework recently and ran into a scenario where I needed the device to be in “full power” mode at all times with the back-light on.  The device is constantly powered, so battery life is not a concern.

The obvious choice is to go into the Brightness and Power control panels and turn off the appropriate settings, but I learned that there is a way to handle this at an application level so the behavior only occurs while the application is running.

Power State

An application can force a specific power state using the SetPowerRequirement method, and release that state using the ReleasePowerRequirement method.  Using P/Invoke, these methods look like the following:

   1: public enum CEDevicePowerState
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>     D0 = 0,    <span style="color: #008000">// Full On</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     D1,        <span style="color: #008000">// Low On</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>     D2,        <span style="color: #008000">// Standby</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>     D3,        <span style="color: #008000">// Sleep</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span>     D4,        <span style="color: #008000">// Off</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span> }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum9" style="color: #606060">   9:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum10" style="color: #606060">  10:</span> [DllImport(<span style="color: #006080">&quot;coredll.dll&quot;</span>, SetLastError=<span style="color: #0000ff">true</span>)]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum11" style="color: #606060">  11:</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> IntPtr SetPowerRequirement(<span style="color: #0000ff">string</span> device, CEDevicePowerState ceDevicePowerState, <span style="color: #0000ff">uint</span> deviceFlags, IntPtr systemState, <span style="color: #0000ff">ulong</span> stateFlags);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum12" style="color: #606060">  12:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum13" style="color: #606060">  13:</span> [DllImport(<span style="color: #006080">&quot;coredll.dll&quot;</span>, SetLastError=<span style="color: #0000ff">true</span>)]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span id="lnum14" style="color: #606060">  14:</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> <span style="color: #0000ff">int</span> ReleasePowerRequirement(IntPtr handle);</pre>

SetPowerRequirement will allow you to set a specific power state on a specific device.  In my scenario, I wanted to set the back-light to full power.  The name of the back-light on most (not all) Windows Mobile devices appears to be “BKL1:”.  So, to set the back-light to full power (the D0 state), you would call the method as follows:

Read More

VSLive! New York

I have been selected to present two sessions at VSLive! New York this September.  Here are the abstracts:

Title: Creating a Simple 2D Game Using XNA Game Studio to Run on a PC, Xbox 360 or Microsoft Zune

Date/Time: Wednesday, September 10th at 3:15pm

Description: This session will demonstrate how to build a very simple 2D game engine and game using the latest version of XNA Game Studio. Attendees will learn how to effectively use the content pipeline, import 2D sprites into XNA, manage input from the keyboard, mouse, Xbox 360 controller and Zune, manage game state, and debug the resulting game. The sample code will be built on a PC and run on the PC, Xbox 360, and Zune platforms when complete.

Title: Interfacing External Hardware Using Managed Code

Date/Time: Wednesday, September 10th at 4:45pm

Read More

WiimoteLib v1.5.2 Released

Sorry for all of the previous confusion.  Version 1.5.2 is now up at CodePlex.  This release now has the Wii Fit Balance Board working for everyone who has tried it.  Please let me know if you have any issues with it.  The changes:

v1.5.2.0

  • Ok, Balance Board support is really fixed this time (thanks to Manuel Schroeder, Eduard Kujit and Alex Wilkinson for testing)
  • LED checkboxes are properly set on the WiimoteTest tabs

Enjoy!
Read More

WiimoteLib v1.5.1 Released

UPDATE 2: Sorry, but I've removed this release from CodePlex.  It's just too buggy (though it continues to work just fine for me).  Look for version 1.5.2 very soon...

UPDATE:  It appears some people are still having issues with this build as well due to some Balance Boards being a bit finicky in their response times.  Stay tuned for build 1.5.2 soon…

Oops.  Apparently the one new thing in version 1.5, Balance Board support, was actually broken.  I have just put version 1.5.1 up at CodePlex which fixes the Balance Board operation.  Sorry about that…

Read More

WiimoteLib v1.5 Released

Wii-Balance-Board-1I’m really cranking out the releases these days.  Today I have released WiimoteLib v1.5 at CodePlex.  The big news is that the Wii Fit Balance Board is now supported.  The balance board is simply a very heavy piece of plastic with 4 weight sensors located at the corners.  The library reads those individual sensors to determine how much weight is applied to each sensor, and the total weight applied to the entire board.  You can use it as an impromptu (and very expensive) scale.

Comments and bugs welcome…

Read More