PowerNotify Beta

eww245

Member
Licensed User
PowerNotify Beta (update) v0.2

Updated to Beta v0.2

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.
 

Attachments

  • PowerNotifyBeta.v0.1.zip
    4.5 KB · Views: 51
  • PowerNotifyBeta.v0.2.zip
    4.7 KB · Views: 158
Last edited:

CryoGenID

Active Member
Licensed User
Longtime User
Wow, great Library, thanks a lot!

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
 

eww245

Member
Licensed User
Wow, great Library, thanks a lot!

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.

I'll add an update later today.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

eww245

Member
Licensed User
Updated to Beta 0.2

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.
 

eww245

Member
Licensed User
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.b4x.com/forum/code-samples-tips/4665-advanced-read-basic4ppc-thread-parameters.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

B4X:
public PowerNotify()
{
 if (!(bool)Thread.GetData(Thread.GetNamedDataSlot("cPPC")))
 {
  throw new Exception("Power Notifications are only available for the Device.");
 }
 else
 {
  code.......
 }
}
 

CryoGenID

Active Member
Licensed User
Longtime User
Updated to Beta 0.2

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)

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...

Best regards,

Chris
 

mjcoon

Well-Known Member
Licensed User
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...

Regards, Mike.
 

Zenerdiode

Active Member
Licensed User
...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".

This normally occurs when you try to load a source file that has been generated in a later version of Basic4PPC into an earlier version. I see from the header of the .sbp file it was generated with Basic4PPC V6.80; are you using an earlier version?
 

mjcoon

Well-Known Member
Licensed User
Yes, I'm using 6.50. But only because that I understood that I would have to pay another 60% purchase price for an upgrade when the only enhancements that I actually wanted ("Search" on device) I felt should have been there in the 1st place. But I may have misunderstood. It hadn't occurred to me I would be shut out of future source distributions!

Mike.
 

Ariel_Z

Active Member
Licensed User
Hi mjcoon,

The standard version gives you free updates for a year after purchase. That is, if you originally bought 6.5, probably you are allowed to get 6.8 free.
The enterprise version lets you update up to 36 months (3 years) from purchase.

After 12 (36 for enterprise) months, the upgrade offer you specified takes place (33% off).
 
Last edited:

mjcoon

Well-Known Member
Licensed User
Hi mjcoon,

The standard version gives you free updates for a year after purchase. That is, if you originally bought 6.5, probably you are allowed to get 6.8 free.
The enterprise version lets you update up to 36 months (3 years) from purchase.

After 12 months, the upgrade offer you specified takes place (33% off).

Thanks for that explanation, Erel. If I had got the upgrade when you first mentioned it was available it would have been within the year. But I waited (as I thought I was meant to do) until you specifically invited upgrades, by which time the year was up. And, as I implied, I am not going to keep paying just (from my point of view) to stand still! Maybe if it was a new version 7 with major changes it would seem more enticing.

Cheers, Mike.
 

Ariel_Z

Active Member
Licensed User
Hi Mike,

An update should have been automatically sent to everyone who purchased the standard version 12 months prior to the new version release (regardless of the time you noticed it: you don't have to explicitly ask for it). Apparently some error had occurred. Please send you order details to [email protected] and we will take care of this in a few days.
 

mjcoon

Well-Known Member
Licensed User
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.

...

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...

Regards, Mike.

I have to amend what I wrote: it turns out that there's a near-fatal flaw. Having started the PowerMonitor code I can bring other programs into the foreground (since the monitor isn't interesting to watch!). And on loss of external power, up it pops with its warning. But if I acknowledge it and try to put it back in the background then the whole PDA locks up and has to be soft-reset. Having hit this once I find that the lock-up happens even if I use the "X" button before the event has triggered. It's all fallen apart around my ears!

Cheers, Mike.
 

mjcoon

Well-Known Member
Licensed User
I have to amend what I wrote: it turns out that there's a near-fatal flaw. Having started the PowerMonitor code I can bring other programs into the foreground (since the monitor isn't interesting to watch!). And on loss of external power, up it pops with its warning. But if I acknowledge it and try to put it back in the background then the whole PDA locks up and has to be soft-reset. Having hit this once I find that the lock-up happens even if I use the "X" button before the event has triggered. It's all fallen apart around my ears!

Cheers, Mike.

... And it's even worse than I knew when I wrote the above. Having soft reset the icons on the "Today" page (which are set up for me by Launcher.dll) were mostly default instead of useful. This turned out to be because the \Program Files directory on my SD card (and other files and directories) was corrupt and unreadable. I had to restore the SD card from a back-up. This is doubly mysterious given that I was not intentionally writing to the SD card, and especially not to the \Program Files directory!

I'm going to have to be very circumspect about running these apparently tiny trivial programs!

Mike.
 

eww245

Member
Licensed User
... And it's even worse than I knew when I wrote the above. Having soft reset the icons on the "Today" page (which are set up for me by Launcher.dll) were mostly default instead of useful. This turned out to be because the \Program Files directory on my SD card (and other files and directories) was corrupt and unreadable. I had to restore the SD card from a back-up. This is doubly mysterious given that I was not intentionally writing to the SD card, and especially not to the \Program Files directory!

I'm going to have to be very circumspect about running these apparently tiny trivial programs!

Mike.

Hi Mike, I'm sorry for what happened at least you were able to recover some of what was lost.

I have since solved the issue with BacklightOff and added BacklightOn ScreenOn and On notifications, as they are all the same numerical value making it difficult to know what's what. My slideout keyboard also has triggered that event.
The second PowerStatus event is unfiltered and might also be why you would see the BacklightOff message, I will eventually remove that event.

255 is an unknown battery state I'll see about not having that returned, or nothing above 100.

I'm guessing the problem happened after unplugging the AC, or was it on low power?
In either way I would recommend to save the result in a global variable and if the same value appears within a few seconds, ignore it.
With low power I would definitely have it only return once or once every few minutes.
Unfortunately Some of these events could even be returned several times within a second, that's the way the PowerManager works.

If any program is writing to disk and there is a crash it can corrupt the files, especially if it is running from SD.
I usually try to wait for a while when my PPC locks up for what ever reason, and most times it will eventually recover.

I've been working with image transparency lately, but I will get a new version out soon.
 

mjcoon

Well-Known Member
Licensed User
Hi Mike,

An update should have been automatically sent to everyone who purchased the standard version 12 months prior to the new version release (regardless of the time you noticed it: you don't have to explicitly ask for it). Apparently some error had occurred. Please send you order details to [email protected] and we will take care of this in a few days.

Hi Ariel (apologies for addressing you as Erel; I was misled by the similar name and logo).

I sent a message promptly but have not heard back. Have you concluded that I should have qualified for a free upgrade? If so, how do I get it? The offer letter I got only refers to the 33% discount.

Cheers, Mike.
 
Top