It is currently Fri Dec 13, 2019 6:36 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 10:35 am 
Offline
New User
New User

Joined: Fri Jun 21, 2019 10:28 am
Posts: 7
Hello,
does anyone knows why the battery values doesn´t are updating during the running time of this program?

Code:
If GetSystemPowerStatus_(PowerStatus.SYSTEM_POWER_STATUS)
  OpenWindow(0,50,50,256,100,"Battery status",#PB_Window_SystemMenu | #PB_Window_MinimizeGadget)

  ACtxt.s=""
  batFlagTxt.s=""
  lifeTimeTxt.s=""
 
  TextGadget(0,10,10,200,14,"")
  TextGadget(1,10,30,200,14,"")
  TextGadget(2,10,50,200,14,"")
  TextGadget(3,10,70,200,14,"")
 
  Repeat
    eventID=WaitWindowEvent()
   
    ac.d=PowerStatus\ACLineStatus              ; 0-Battery, 1-AC, 255 ou $ff-Desconhecido
    If ac=0
      ACtxt="Off"
    Else
      ACtxt="On"
    EndIf
 
    batFlag.d=PowerStatus\BatteryFlag
    ; BatteryFlag bits: 0- carga média, 1-Carga elevada, 2-Carga baixa, 4-Carga critica
                             ; 8-Em carga, 128-sem bateria, 255-Estado desconhecido
 
    If batFlag=0
      batFlagTxt="média"
    ElseIf batFlag=1
      batFlagTxt="elevada"
    ElseIf batFlag=2
      batFlagTxt="baixa"
    ElseIf batFlag=4
      batFlagTxt="critico"
    ElseIf batFlag=8
      batFlagTxt="em carga"
    ElseIf batFlag=128
      batFlagTxt="sem bateria"
    ElseIf batFlag=255
      batFlagTxt="desconhecido"
    EndIf
     
    life100.d=PowerStatus\BatteryLifePercent        ; % de carga restante (255 ou $ff se desconhecida)
 
    lifeTime.l=PowerStatus\BatteryLifeTime           ; n. de segundos restantes (-1 ou $ffffffff se desconhecido)
    lifeTimeMin=lifeTime/60
 
    fullLife.l=PowerStatus\BatteryFullLifeTime       ; n. de segundos de autonomia (-1 ou $ffffffff se desconhecido)
   
    SetGadgetText(0,"Power "+ACtxt)
    SetGadgetText(1,"Carga da bateria: "+batFlagTxt)
    SetGadgetText(2,Str(life100)+" % de bateria restante")
    SetGadgetText(3,Str(lifeTimeMin)+" minutos de bateria restante")
   
  Until eventID=#PB_Event_CloseWindow
 
  CloseWindow(0)
EndIf


Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 11:27 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Dec 29, 2011 5:03 pm
Posts: 509
WaitWindowEvent pauses the loop until an event is fired. Unless you interact with the window or gadgets, no event will be fired and the loop will not run.

You can add a timer that will fire events:
Code:
If GetSystemPowerStatus_(PowerStatus.SYSTEM_POWER_STATUS)
  OpenWindow(0,50,50,256,100,"Battery status",#PB_Window_SystemMenu | #PB_Window_MinimizeGadget)
 
  ACtxt.s=""
  batFlagTxt.s=""
  lifeTimeTxt.s=""
 
  TextGadget(0,10,10,200,14,"")
  TextGadget(1,10,30,200,14,"")
  TextGadget(2,10,50,200,14,"")
  TextGadget(3,10,70,200,14,"")
 
  AddWindowTimer(0, 0, 500)
 
  Repeat
    eventID=WaitWindowEvent()
   
    Select eventID
       
      Case #PB_Event_Timer
        If EventTimer() = 0
         
          ac.d=PowerStatus\ACLineStatus              ; 0-Battery, 1-AC, 255 ou $ff-Desconhecido
          If ac=0
            ACtxt="Off"
          Else
            ACtxt="On"
          EndIf
         
          batFlag.d=PowerStatus\BatteryFlag
          ; BatteryFlag bits: 0- carga média, 1-Carga elevada, 2-Carga baixa, 4-Carga critica
          ; 8-Em carga, 128-sem bateria, 255-Estado desconhecido
         
          If batFlag=0
            batFlagTxt="média"
          ElseIf batFlag=1
            batFlagTxt="elevada"
          ElseIf batFlag=2
            batFlagTxt="baixa"
          ElseIf batFlag=4
            batFlagTxt="critico"
          ElseIf batFlag=8
            batFlagTxt="em carga"
          ElseIf batFlag=128
            batFlagTxt="sem bateria"
          ElseIf batFlag=255
            batFlagTxt="desconhecido"
          EndIf
         
          life100.d=PowerStatus\BatteryLifePercent        ; % de carga restante (255 ou $ff se desconhecida)
         
          lifeTime.l=PowerStatus\BatteryLifeTime           ; n. de segundos restantes (-1 ou $ffffffff se desconhecido)
          lifeTimeMin=lifeTime/60
         
          fullLife.l=PowerStatus\BatteryFullLifeTime       ; n. de segundos de autonomia (-1 ou $ffffffff se desconhecido)
         
          SetGadgetText(0,"Power "+ACtxt)
          SetGadgetText(1,"Carga da bateria: "+batFlagTxt)
          SetGadgetText(2,Str(life100)+" % de bateria restante")
          SetGadgetText(3,Str(lifeTimeMin)+" minutos de bateria restante")
         
        EndIf
       
    EndSelect
   
  Until eventID=#PB_Event_CloseWindow
 
  CloseWindow(0)
EndIf
Edited to correct incorrect use of EventType.


Last edited by wombats on Fri Jun 21, 2019 5:59 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 12:31 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Sep 22, 2010 1:17 pm
Posts: 313
Location: United Kingdom
wombats wrote:
You can add a timer that will fire events:

That code still won't work though.
1) #PB_Event_Timer is a WaitWindowEvent/WindowEvent result not an EventType result.
2) The call to GetSystemPowerStatus_ needs to be inside the loop to update the values.

Just a thought but querying the battery status twice every second is probably not the greatest idea if, in fact, you are concerned about battery life...


Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 12:38 pm 
Offline
New User
New User

Joined: Fri Jun 21, 2019 10:28 am
Posts: 7
Many thanks for your fast reply wombats :)

Will try to implement your solution

@spikey
Quote:
Just a thought but querying the battery status twice every second is probably not the greatest idea if, in fact, you are concerned about battery life...


Thanks for your advise, maybe your right.
Will try to implement a timer so could delay the querying.


Last edited by AlfaRomeo on Fri Jun 21, 2019 12:44 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 12:41 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3476
Maybe CreateThread() is your good approach

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 1:00 pm 
Offline
New User
New User

Joined: Fri Jun 21, 2019 10:28 am
Posts: 7
spikey wrote:
The call to GetSystemPowerStatus_ needs to be inside the loop to update the values.


You´re right spikey, when I moved the call to GetSystemPowerStatus_ to inside the main loop, the battery values started updating.

Thanks for your help :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 1:16 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3476
Using CreateThread()
- No interrupt by any event
- Use any time interval by using Delay()
- Full control when you start or stop
Code:
Global Quitthread

Procedure getbat(par)
  ACtxt.s=""
  batFlagTxt.s=""
  lifeTimeTxt.s=""
  Repeat
    GetSystemPowerStatus_(PowerStatus.SYSTEM_POWER_STATUS)
    ac.d=PowerStatus\ACLineStatus              ; 0-Battery, 1-AC, 255 ou $ff-Desconhecido
   
    If ac=0
      ACtxt="Off"
    Else
      ACtxt="On"
    EndIf
   
    batFlag.d=PowerStatus\BatteryFlag
    ; BatteryFlag bits: 0- carga média, 1-Carga elevada, 2-Carga baixa, 4-Carga critica
                             ; 8-Em carga, 128-sem bateria, 255-Estado desconhecido   
    If batFlag=0
      batFlagTxt="média"
    ElseIf batFlag=1
      batFlagTxt="elevada"
    ElseIf batFlag=2
      batFlagTxt="baixa"
    ElseIf batFlag=4
      batFlagTxt="critico"
    ElseIf batFlag=8
      batFlagTxt="em carga"
    ElseIf batFlag=128
      batFlagTxt="sem bateria"
    ElseIf batFlag=255
      batFlagTxt="desconhecido"
    EndIf
     
    life100.d=PowerStatus\BatteryLifePercent        ; % de carga restante (255 ou $ff se desconhecida)
   
    lifeTime.l=PowerStatus\BatteryLifeTime           ; n. de segundos restantes (-1 ou $ffffffff se desconhecido)
    lifeTimeMin=lifeTime/60
   
    fullLife.l=PowerStatus\BatteryFullLifeTime       ; n. de segundos de autonomia (-1 ou $ffffffff se desconhecido)
   
    SetGadgetText(0,"Power "+ACtxt)
    SetGadgetText(1,"Carga da bateria: "+batFlagTxt)
    SetGadgetText(2,Str(life100)+" % de bateria restante")
    SetGadgetText(3,Str(lifeTimeMin)+Str(x))
    x + 10
    Delay(1000)
  Until Quitthread = 1
EndProcedure

OpenWindow(0,0,0,256,180,"Battery status",#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
 
  TextGadget(0,10,10,200,20,"",#PB_Text_Border)
  TextGadget(1,10,40,200,20,"",#PB_Text_Border)
  TextGadget(2,10,70,200,20,"",#PB_Text_Border)
  TextGadget(3,10,100,200,20,"",#PB_Text_Border)
 
  ButtonGadget(10,10,130,60,20,"RUN")
  ButtonGadget(20,80,130,60,20,"STOP") 
Repeat
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      Quit = 1
     
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 10
          Quitthread = 0
          Thread = CreateThread(@getbat(),30)
        Case 20
          If IsThread(Thread)
            Quitthread = 1
            Delay(100)
            KillThread(thread)
          EndIf
          Debug IsThread(thread)
      EndSelect
  EndSelect   
Until Quit = 1

_________________
Egypt my love


Last edited by RASHAD on Fri Jun 21, 2019 2:24 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 1:21 pm 
Offline
New User
New User

Joined: Fri Jun 21, 2019 10:28 am
Posts: 7
RASHAD wrote:
Maybe CreateThread() is your good approach


Thanks for the hint RASHAD, it´s a good help :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 1:28 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Apr 18, 2019 8:17 am
Posts: 444
spikey wrote:
querying the battery status twice every second is probably not the greatest idea if, in fact, you are concerned about battery life

Is this true, or just a worry? Does querying it really use some vast amount of power? I have an app that checks it every minute.


Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 1:39 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 13659
Location: France
It's best to use a global variable to safely exits the thread instead of using KillThread() (which doesn't clean the stack correctly etc.)


Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 2:06 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3476
Hi Fred
What a shame it is already written in the help :)
Previous post updated

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 3:21 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Sep 22, 2010 1:17 pm
Posts: 313
Location: United Kingdom
BarryG wrote:
spikey wrote:
querying the battery status twice every second is probably not the greatest idea if, in fact, you are concerned about battery life

Is this true, or just a worry? Does querying it really use some vast amount of power? I have an app that checks it every minute.

Once a minute doesn't seem unreasonable - twice a second seems redundant, to me at least. It wouldn't be true to say 'a vast amount' by any stretch of the imagination though. However there is a Scottish proverb which says 'Many a pickle makes a mickle' - which, unpoetically, translates to 'many small amounts make a big amount'...


Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Fri Jun 21, 2019 6:00 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Dec 29, 2011 5:03 pm
Posts: 509
spikey wrote:
wombats wrote:
You can add a timer that will fire events:

That code still won't work though.
1) #PB_Event_Timer is a WaitWindowEvent/WindowEvent result not an EventType result.
2) The call to GetSystemPowerStatus_ needs to be inside the loop to update the values.

Just a thought but querying the battery status twice every second is probably not the greatest idea if, in fact, you are concerned about battery life...
You’re right. I edited my post to correct the first mistake. With the second, I cannot actually run the code since I am on a Mac, so perhaps I shouldn’t have tried to help since my help was wrong. :oops:


Top
 Profile  
Reply with quote  
 Post subject: Re: Update battery values during main loop
PostPosted: Sat Jun 22, 2019 1:15 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Apr 18, 2019 8:17 am
Posts: 444
spikey wrote:
It wouldn't be true to say 'a vast amount'

I never knew that it even used battery at all to query it. This is news to me. Thank you for your advice.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye