PowerNotify allows Basic4PPC to receive Power Notifications from Power Management.
This is an early Beta release as I am still testing the Power Management functions of Windows Mobile and also for the lack of devices for debugging.
It is only for the Device and requires at least .Net Compact Framework v2.
The source will not be included until the final release.
An example is attached below with the Library.
BacklightOff is triggered in situations that the backlight is on, the example includes a workaround.
The backlight turning on does not seem to trigger a BackLightOn value.
Due to hardware limitations events will not be fired from a suspend or poweroff state until after resuming.
The two events, PowerStatus and PowerStatus2, are triggered in different ways. PowerStatus sets PowerState while PowerStatus2 is only a triggered event.
Please post any comments and suggestions.
The following is included:
AcLineStatus Returns 1 when AC line is attached BacklightOff Turns off the backlight BacklightOn Turns on the backlight BatteryPercent Returns the battery level as percentage CurrentPowerState Returns the current power state as a string, is directly from the system and unchanged Dispose Disposes the resources, handled by the Garbage Collector New1 Initializes the PowerNotify Class PowerOff Powers off the System PowerOn Powers on the System, if this doesn't work use PowerOn2 PowerOn2 Powers on the System PowerState Returns the current power state as a string, from a numerical value in a byte[] Array ScreenOff Turns off the screen Start Requests power notifications from Power Management Stop Stops the power notifications Suspend Suspends the system
Events:
Battery Fired when the battery level or AC status changes, sets AcLineStatus and BatteryPercent PowerStatus Fired by the Power Manager, sets PowerState values PowerStatus2 Fired by the Power Manager, use with CurrentPowerState
The following values are returned from PowerState:
UserIdle, On, ON, Unattended, Off, Resuming, ScreenOff, Suspend, BacklightOff, Critical, Boot, Reboot
Some values may not be returned by all devices.
There are two On values as both appear to be valid.
Is there any change to let the application still react on events etc. when I have sent the "ScreenOff"-Command?
Background: I want to have my application save power if it is not used for a time X and switch off the display... But it should wake up again when I register a movement of the device (using the htc-sensor-library here with the event of the GSensors...)?
Is there any change to let the application still react on events etc. when I have sent the "ScreenOff"-Command?
Background: I want to have my application save power if it is not used for a time X and switch off the display... But it should wake up again when I register a movement of the device (using the htc-sensor-library here with the event of the GSensors...)?
Any help is highly appreciated :-)
But anyway: Thanks for this great library!!
Best regards,
Chris
I'm assuming it doesn't turn it on after the screen has been turned off with ScreenOff method. If so then it's probably since entered into suspend or unattended state.
Does it normally wake with the sensor if it's off or suspended?
If I added this option it would actually use more power due to the device staying in a ScreenOff state, preventing a lower power state.
After turning the screen off try to immediately turn it back on with a keypress or with the sensor to see how it reacts. It should turn on as long as it is still in that state.
One option that might work is if I add a PowerOn method, then within the sensor event run PowerOn.
I am trying to add the BacklightOff value, unfortunately it appears once after every state, sliding out a keyboard, when the AC line is attached, and even when using a high amount of CPU.
There is a workaround but it will have to be done in the PowerStatus event with Basic4PPC.
I'm guessing this will probably not help you though.
Great work. This library will help many users. Thank you for sharing.
I see that running it on the desktop crashes the IDE. You can read the cPPC value as described here: http://www.basic4ppc.com/forum/code-...arameters.html
If the program is running on the desktop (cPPC = false) just throw an exception saying that this library is not supported by the desktop.
Added PowerOn and PowerOn2 methods, if PowerOn doesn't work use PowerOn2
PowerStatus now returns BacklightOff, Reboot, Boot and Critical.
BacklightOff is triggered in situations that the backlight is on, the example includes a workaround.
The method for returning AcLineStatus and BatteryPercent has changed.
Great work. This library will help many users. Thank you for sharing.
I see that running it on the desktop crashes the IDE. You can read the cPPC value as described here: http://www.basic4ppc.com/forum/code-...arameters.html
If the program is running on the desktop (cPPC = false) just throw an exception saying that this library is not supported by the desktop.
Hey Erel,
I tried to add the exception but I receive a DllNotFoundException.
It appears to be coredll for the DllImports is there any way around this?
I'm not sure why other libraries work fine with the imports.
I guessing I'll have to make a Dummy.
Thanks,
E
Code:
public PowerNotify() { if (!(bool)Thread.GetData(Thread.GetNamedDataSlot("cPPC"))) { throw new Exception("Power Notifications are only available for the Device."); } else { code....... } }
Added PowerOn and PowerOn2 methods, if PowerOn doesn't work use PowerOn2
PowerStatus now returns BacklightOff, Reboot, Boot and Critical.
BacklightOff is triggered in situations that the backlight is on, the example includes a workaround.
The method for returning AcLineStatus and BatteryPercent has changed.
Thanks a lot for your answer and your update!
Sorry that I didn't report back any sooner...
I am currently trying to figure out if "PowerOn" works...
(The problem is that my whole ppc sometimes freezes when I try to run/quit the proof-of-concept application)
Quote:
Does it normally wake with the sensor if it's off or suspended?
No if the device is off or suspended, no events seem to be able to trigger a wakeup :-(
But that's exactly what I need ;-)
In order to make at least the proof-of-concept work, I would already be happy if the screen turned itself off and back on if an event from the g-sensor comes in...
I think that this library is going to do what I want (which is to be notified if the external power that I've set up for my PDA fails while it is in use).
But since it is "beta" I feel obliged to report any strange things that I see. It may illuminate behaviour with disparate devices.
First, nothing to do with the device, the sample SBP file in the zips (I assume it is the same in each) won't load in Basic4PPC; it says: "Error loading file./Input string was not in a correct format". I wondered if this were a UTF16 problem so tried opening with Notepad (which worked OK) and writing in ANSI and in Unicode rather than the UTF-8 that Notepad offered me. But none of these made any difference. Puzzling since I assume everyone else has used it OK!
Then when I use my simple application I get two notification PowerNotify_Battery events when I remove the power. The code merely displays as follows:
Msgbox("Battery = " & PowerNotify.BatteryPercent & "%" & CRLF & PowerNotify.PowerState)
The first notification displays "255" as the BatteryPercent which is not believable (;-). The second, extra, event shows a correct "100" (I haven't tried with a partially-discharged battery yet). Sometimes I have seen a string saying the backlight is off, but this is intermittent (as well as being incorrect!). But sometimes I get just the one event with "100%", so it is a bit mysterious.
Conclusion: it's useful for what I wanted, but maybe a bit variable on my hardware; if anyone would want my code (it may be a tiny part of the proffered sample for all I know) they would be welcome...