Brian Peek

A (Mirrored) Compendium of Random Uselessness
posts - 29, comments - 14, trackbacks - 0

Thursday, July 03, 2008

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
   2: {
   3:     D0 = 0,    // Full On
   4:     D1,        // Low On
   5:     D2,        // Standby
   6:     D3,        // Sleep
   7:     D4,        // Off
   8: }
   9:  
  10: [DllImport("coredll.dll", SetLastError=true)]
  11: static extern IntPtr SetPowerRequirement(string device, CEDevicePowerState ceDevicePowerState, uint deviceFlags, IntPtr systemState, ulong stateFlags);
  12:  
  13: [DllImport("coredll.dll", SetLastError=true)]
  14: static extern int ReleasePowerRequirement(IntPtr handle);

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:

IntPtr handle = SetPowerRequirement("BKL1:", CEDevicePowerState.D0, 1, IntPtr.Zero, 0);
 
The power state will be returned to its default settings when the application exits, or you may call ReleasePowerRequirement, passing in the handle returned from the call to SetPowerRequirement to reset it yourself.
 
Suspend
 
The above will leave the back-light on at all times, but it will not stop the device from going into a suspended state as configured in the control panel.  To stop this from happening, simply call the SystemIdleTimerReset method at a short, regular interval:
 
   1: [DllImport("coredll.dll", SetLastError=true)]
   2: static extern void SystemIdleTimerReset();
   3:  
   4: // reset the system's idle time every 10 seconds so it doesn't suspend
   5: Timer timer = new Timer(IdleReset, null, 0, 10000);
   6:  
   7: private static void IdleReset(object state)
   8: {
   9:     // no suspend
  10:     SystemIdleTimerReset();
  11: }

Lines 1-2 contain the P/Invoke signature.  Line 5 sets up a timer that will be called every 10 seconds (10000ms) to reset the idle timer, and lines 7-11 are the timer callback method which actually calls the SystemIdleTimerReset method.

And that’s that.  With both of these methods in place, my application remains running with the device at full power, never suspending, and with the back-light always on.

Cross Posted from www.brianpeek.com.

posted @ Thursday, July 03, 2008 6:00 AM | Feedback (0) |

Friday, June 27, 2008

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.

 

Ttile: Interfacing External Hardware Using Managed Code

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

Description: While developers write code to build software every day, not often are they exposed to code that drives and interfaces hardware. This session will attempt to bridge that gap and show how .NET can be used to effectively interface several hardware devices, including an RFID reader and tags, Phidget control boards with a variety of sensors, and a servo controller. Finally, the Nintendo Wiimote will be introduced along with my .NET Wiimote Library, demonstrating how to connect to a USB or Bluetooth HID device and use it from .NET, with examples showing what the Wiimote itself is capable of.

mh_logo Check out the full agenda and see what piques your interest.  Also check out the video above with some information on what to expect from the conference.

It’s a couple months off, but registrations are in full swing.  Register by July 9th and save some dough!

Cross Posted from www.brianpeek.com.

posted @ Friday, June 27, 2008 5:27 PM | Feedback (0) |

Sunday, June 15, 2008

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! Cross Posted from www.brianpeek.com.

posted @ Sunday, June 15, 2008 4:36 PM | Feedback (0) |

Wednesday, June 11, 2008

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…

Cross Posted from www.brianpeek.com.

posted @ Wednesday, June 11, 2008 5:34 AM | Feedback (0) |

Monday, June 09, 2008

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…

Cross Posted from www.brianpeek.com.

posted @ Monday, June 09, 2008 4:45 AM | Feedback (0) |

Tuesday, June 03, 2008

WiimoteLib v1.4 Released

In an effort to put out more frequent builds with fewer changes, I’ve released WiimoteLib v1.4 to CodePlex.  This release contains the single most requested feature:  multiple Wiimote support.  I’m very interested to hear bug reports and suggestions on this release, so please do let me know how it works for you.  The original Coding4Fun article has also been updated with some details on how to use multiple Wiimotes, as has the WiimoteTest application included with the distribution.

Changes

  • Multiple Wiimotes supported!
  • Slight change to ExtensionType enum for better extension detection
  • Decided I didn't like the dependency on System.Drawing for the 2D point so am now using my own Point structs.  Sorry...
  • WiimoteTest app updated to show multiple Wiimotes working
Cross Posted from www.brianpeek.com.

posted @ Tuesday, June 03, 2008 5:14 PM | Feedback (0) |

Thursday, May 29, 2008

Coding4Fun Book For Sale at Amazon!

The book I am co-authoring with Dan Fernandez for O'Reilly, currently titled "10 Coding4Fun Projects with .NET for Programmers, Hobbyists, and Game Developers", is already up for sale at Amazon!

I'm trying not to be a totally shameless shill, but this is exciting.  Feel free to get your pre-order in early.  :)

Cross Posted from www.brianpeek.com.

posted @ Thursday, May 29, 2008 1:41 AM | Feedback (0) |

Tuesday, May 27, 2008

WiimoteLib v1.3 Released

Time for another WiimoteLib release!  You'll find the new version in the usual place at CodePlex.  What's new you ask?  Why, check out this list:

  • All projects updated to Visual Studio 2008 format
  • SetReportType contains an overload taking a new IRSensitivity parameter which will set the IR camera sensitivity when using an IR report type
  • Created new WiimoteException type which is now thrown by the library
  • Moved InputReport enum to namespace level
  • Events now using the generic EventHandler class instead of custom delegates
  • Refactored the state structures to use Point/PointF and my own Point3/Point3F
  • Refactored IR sensors to be an array
  • Added support for the Guitar Hero controller (tested by Matthias Shapiro, Evan Jacovier)
  • Test app will run without Wiimote connected (Andrea Leganza)
  • ReadData now returns the proper amount of data for requests of more than 16 bytes (reported by David Hawley)
  • Test application updated with above changes
  • Lots of breaking changes, but the survey on my site said most didn't care about backwards compatibility...  :)

Bug reports and comments welcome.  Give it a try...

Cross Posted from www.brianpeek.com.

posted @ Tuesday, May 27, 2008 4:00 AM | Feedback (0) |

Tuesday, May 13, 2008

64-bit Development Discussion on .NET Rocks

I did an episode of the .NET Rocks show last month which debuted today on the site.  Now, instead of only reading my idiotic ramblings, you can actually listen to my idiotic ramblings.  Lucky you!

Anyway, the show is a discussion on 64-bit development with .NET and all the fun that comes with it.  Have a listen...

Cross Posted from www.brianpeek.com.

posted @ Tuesday, May 13, 2008 3:03 PM | Feedback (1) |

Saturday, May 10, 2008

Newsweek & The Wiimote

The Nintendo Wiimote is getting some very mainstream attention lately.  I was interviewed several weeks ago by Christopher Flavelle from Newsweek magazine for a story on developers using the Wiimote for uses other than playing games with their Wii console.  The article should be in the May 19th issue of the International Edition of Newsweek and can also be read online at http://www.newsweek.com/id/136381.  Have a read...

Cross Posted from www.brianpeek.com.

posted @ Saturday, May 10, 2008 5:20 PM | Feedback (1) |

Powered by: