Author |
Topic: Battery State (Read 225 times) |
|
roy
New Member
member is offline


Posts: 44
|
 |
Battery State
« Thread started on: Mar 17th, 2018, 6:48pm » |
|
Hi All
I've been trying to make an Batter state app. That would tell you if the battery is charging, how full the battery is in present and so on.
As a starting point I looked at this site:
https://wiki.libsdl.org/SDL_GetPowerInfo?highlight=%28battery%29 " target="_blank"> https://wiki.libsdl.org/SDL_GetPowerInfo?highlight=%28battery%29
Please use second link and tried to convert it.
Needles to say it doesn't work. It's hard to know where to start.
Anyway any ideas would be appreciated
Regards Roy
Code: 10 mode 6 20 30 rem Batter State 40 secs% = 0 : pct% = 0 50 if sys "SDL_GetPowerInfo",secs%, pct% = sys "SDL_POWERSTATE_ON_BATTERY" then 90 print"Battery is draining: " 100 if (secs% =-1) then 110 print"unknown time left" 120 endif 130 else 140 print"seconds left", secs% 150 endif 160 170 if (pct% = -1) then 180 print"unknown percentage left" 190 else 200 print" percent left", pct% 210 endif 220 230 240 rem int secs, pct; 250 rem if (SDL_GetPowerInfo(&secs, &pct) == SDL_POWERSTATE_ON_BATTERY) { 260 rem printf("Battery is draining: "); 270 rem if (secs == -1) { 280 rem printf("(unknown time left)\n"); 290 rem } else { 300 rem printf("(%d seconds left)\n", secs); 310 rem } 320 rem 330 rem if (pct == -1) { 340 rem printf("(unknown percentage left)\n"); 350 rem } else { 360 rem printf("(%d percent left)\n", pct); 370 rem } 380 rem }
|
| « Last Edit: Mar 17th, 2018, 6:50pm by roy » |
Logged
|
|
|
|
vbi
Guest
|
 |
Re: Battery State
« Reply #1 on: Mar 17th, 2018, 8:54pm » |
|
Hello Roy,
Below is a modified version of your example.
Leave out the CASE statement if you don't care about reporting any other status than running on battery.
My laptop reports percent but not seconds, so depending on the hardware you may not always get both.
Code: REM SDL_POWERSTATE Constants SDL_POWERSTATE_UNKNOWN = -1 SDL_POWERSTATE_ON_BATTERY = 1 SDL_POWERSTATE_NO_BATTERY = 2 SDL_POWERSTATE_CHARGING = 3 SDL_POWERSTATE_CHARGED = 4 MODE 6 REM Battery State SYS "SDL_GetPowerInfo", ^secs%, ^pct% TO status% CASE status% OF WHEN SDL_POWERSTATE_UNKNOWN : PRINT "Battery Status Unknown" WHEN SDL_POWERSTATE_ON_BATTERY : PRINT "Running On Battery" WHEN SDL_POWERSTATE_NO_BATTERY : PRINT "No Battery Present" WHEN SDL_POWERSTATE_CHARGING : PRINT "Battery is Charging" WHEN SDL_POWERSTATE_CHARGED : PRINT "Battery is Charged" OTHERWISE PRINT "Battery Status Unrecognised" ENDCASE IF status% = SDL_POWERSTATE_ON_BATTERY THEN PRINT "Battery is draining: " IF secs% = -1 THEN PRINT "unknown time left" ELSE PRINT "seconds left: "+STR$secs% ENDIF IF pct% = -1 THEN PRINT "unknown percentage left" ELSE PRINT "percent left: "+STR$pct% ENDIF ENDIF END
|
| « Last Edit: Mar 17th, 2018, 9:03pm by vbi » |
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 803
|
 |
Re: Battery State
« Reply #2 on: Mar 17th, 2018, 9:51pm » |
|
on Mar 17th, 2018, 6:48pm, roy wrote:| Needles to say it doesn't work. It's hard to know where to start. |
|
I think you mis-read the API documentation. SDL_GetPowerInfo takes two parameters, each of which is an 'int*' (i.e. a pointer to an int); but in your listing you pass a value, not a pointer! It should hopefully be obvious that if an API function is to return a value to you, you must pass a pointer to where that value should be stored.
So your:
Code: SYS "SDL_GetPowerInfo", secs%, pct% should be:
Code: SYS "SDL_GetPowerInfo", ^secs%, ^pct% Richard.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 803
|
 |
Re: Battery State
« Reply #3 on: Mar 17th, 2018, 10:03pm » |
|
on Mar 17th, 2018, 8:54pm, vbi wrote:| Below is a modified version of your example. |
|
In your code you seem to be assuming that SDL_GetPowerInfo will return valid information only when the battery is discharging, but that's not (necessarily) the case. On my laptop, for example, it returns a 'percentage charged' value also when the battery is charging.
Therefore to be more generally useful I would recommend that you change this line:
Code: IF status% = SDL_POWERSTATE_ON_BATTERY THEN to:
Code: IF status% = SDL_POWERSTATE_ON_BATTERY OR status% = SDL_POWERSTATE_CHARGING THEN Richard.
|
|
Logged
|
|
|
|
roy
New Member
member is offline


Posts: 44
|
 |
Re: Battery State
« Reply #4 on: Mar 18th, 2018, 1:18pm » |
|
Thanks Richard and vbi
Pointer, yes I think I've got that now
Some Constants have been set up ie SDL_POWERSTATE_ON_BATTERY = 1. How do you know what value to give them?
Some apps when you touch the screen vibrate under your finger and some apps have popups with a short message. I have searched on how to do this but can fined anything. Any body any ideas.
Thanks Roy
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 803
|
 |
Re: Battery State
« Reply #5 on: Mar 18th, 2018, 3:01pm » |
|
on Mar 18th, 2018, 1:18pm, roy wrote:| Some Constants have been set up ie SDL_POWERSTATE_ON_BATTERY = 1. How do you know what value to give them? |
|
In this particular case the constants come from the SDL_PowerState enumeration. Unless otherwise stated, the first item in an enumeration has the value zero and the others correspond to consecutively increasing integers. Note that vbi has an incorrect value for SDL_POWERSTATE_UNKNOWN in his listing, it should be zero, not -1:
Code: REM SDL_PowerState Constants SDL_POWERSTATE_UNKNOWN = 0 Quote:| some apps have popups with a short message. I have searched on how to do this but can fined anything. Any body any ideas. |
|
Message popups are easily created using SDL_ShowSimpleMessageBox:
Code: SDL_MESSAGEBOX_INFORMATION = 2 SYS "SDL_ShowSimpleMessageBox", SDL_MESSAGEBOX_INFORMATION, "My popup", "My message", @hwnd% Richard.
|
|
Logged
|
|
|
|
roy
New Member
member is offline


Posts: 44
|
 |
Re: Battery State
« Reply #6 on: Mar 18th, 2018, 3:24pm » |
|
Thanks for that, Richard
|
|
Logged
|
|
|
|
roy
New Member
member is offline


Posts: 44
|
 |
Re: Battery State
« Reply #7 on: Mar 18th, 2018, 3:35pm » |
|
I was wounding if a popup box could popup without the OK button and show the message for a few second and then disappear.
Regards Roy
|
|
Logged
|
|
|
|
vbi
Guest
|
 |
Re: Battery State
« Reply #8 on: Mar 18th, 2018, 7:00pm » |
|
on Mar 18th, 2018, 3:01pm, Richard Russell wrote:Note that vbi has an incorrect value for SDL_POWERSTATE_UNKNOWN in his listing, it should be zero, not -1 |
|
Whoops, thanks for the correction. That will teach me to pontificate on things that are above my pay grade.
|
|
Logged
|
|
|
|
roy
New Member
member is offline


Posts: 44
|
 |
Re: Battery State
« Reply #9 on: Mar 18th, 2018, 8:14pm » |
|
Hi All
Here is the app. There doesn't seem to be a way to include a zip file or a screen shot so posted code
Thanks for help
Regards roy
Code: 20 on error if err=17 then proc_CleanUp else mode 3 : print report$; " At line " erl : wait 100 : end 30 on close proc_CleanUp 40 50 rem Battery State 60 rem Using BBC Basic for Android 70 rem Virsion 0.01 80 rem March 2018 90 rem Author: Roy Shepherd 100 rem Thanks to Richard and vbi for help 110 120 mode 6 130 140 proc_Init 150 repeat 160 *refresh off 170 cls 180 proc_GetBatteryState 190 proc_DrawBattery 200 *refresh on 210 wait 100 220 mouse mx%, my%, b% 230 until b% 240 proc_CleanUp 250 end 260 270 !------------------------------------------------ 280 rem Do once at first run of app 290 !------------------------------------------------ 300 defproc_Init 310 rem SDL_POWERSTATE Constants 320 SDL_POWERSTATE_UNKNOWN = 0 330 SDL_POWERSTATE_ON_BATTERY = 1 340 SDL_POWERSTATE_NO_BATTERY = 2 350 SDL_POWERSTATE_CHARGING = 3 360 SDL_POWERSTATE_CHARGED = 4 370 VERSION$ = "0.01 (SDL)" 380 sys "SDL_SetWindowTitle", @hwnd%, "Battey State v"+VERSION$ 390 proc_GetScreenWidthHeight(screenW%, screenH%) 400 off : colour 0, 50, 50, 50 : cls 410 endproc 420 430 !------------------------------------------------ 440 rem Get batter state and print it 450 !------------------------------------------------ 460 defproc_GetBatteryState 470 sys "SDL_GetPowerInfo", ^secs%, ^pct% to status% 480 print tab(13,0)"Battery State" 490 case status% of 500 when SDL_POWERSTATE_UNKNOWN : print "Battery Status Unknown" 510 when SDL_POWERSTATE_ON_BATTERY : print "Running On Battery" 520 when SDL_POWERSTATE_NO_BATTERY : print "No Battery Present" 530 when SDL_POWERSTATE_CHARGING : print "Battery is Charging" 540 when SDL_POWERSTATE_CHARGED : print "Battery is Charged" 550 otherwise print "Battery Status Unrecognised" 560 endcase 570 580 if status% = SDL_POWERSTATE_ON_BATTERY or status% = SDL_POWERSTATE_CHARGING then 590 print "Battery is draining: " 600 endif 610 if secs% = -1 then 620 print "unknown time left" 630 else 640 print "Minutes left: ";secs% div 60 650 endif 660 670 if pct% = -1 then 680 print "unknown percentage left" 690 else 700 print "percent left: "+str$pct% 710 endif 720 endproc 730 740 !------------------------------------------------ 750 rem Draw the battery and showing how full it is 760 !------------------------------------------------ 770 defproc_DrawBattery 780 gcol 7 : rectangle screenW%, 50, 400, 800 790 gcol 3 : rectangle fill screenW% + 150, 850, 100, 50 800 if pct% < 10 then gcol 1 else gcol 2 810 rectangle fill screenW% + 2, 52, 396, pct% * 8 820 endproc 830 840 !------------------------------------------------ 850 rem Get the screen width and height 860 !------------------------------------------------ 870 defproc_GetScreenWidthHeight(return sx%, return sy%) 880 vdu 26 890 if pos rem SDL thread sync 900 sx% = @vdu%!208 : sy% = @vdu%!212 910 endproc 920 930 !------------------------------------------------ 940 rem Set things back to default and close 950 !------------------------------------------------ 960 defproc_CleanUp 970 *refresh on 980 on 990 quit 1000 endproc 1010 1020
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 803
|
 |
Re: Battery State
« Reply #10 on: Mar 18th, 2018, 10:30pm » |
|
on Mar 18th, 2018, 8:14pm, roy wrote:| There doesn't seem to be a way to include a zip file or a screen shot so posted code |
|
Zip file: include a hyperlink in your post (it's the button with a picture of the earth, second from the left in the second row of buttons when posting).
Screen shot: insert an image into your post (it's the button showing a framed picture, fourth from the left in the second row of buttons when posting).
Of course for both you need somewhere with public access to host the files, but these days with free cloud services like Dropbox this isn't the issue it once was.
Richard.
|
|
Logged
|
|
|
|
roy
New Member
member is offline


Posts: 44
|
 |
Re: Battery State
« Reply #11 on: Mar 19th, 2018, 07:33am » |
|
Thanks for the info Richard. Dropbox I'll look into that
Regards Roy
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 803
|
 |
Re: Battery State
« Reply #13 on: Mar 19th, 2018, 1:29pm » |
|
on Mar 19th, 2018, 08:41am, roy wrote:| I couldn't get the image link to work... Help !! |
|
I don't really know, but I wonder if it's because Dropbox public URLs are secure (https://) whereas Conforums' aren't.
Richard.
|
|
Logged
|
|
|
|
roy
New Member
member is offline


Posts: 44
|
 |
Re: Battery State
« Reply #14 on: Mar 19th, 2018, 6:22pm » |
|
Thanks Richard
I'll keep trying
Regards Roy
|
|
Logged
|
|
|
|
|