Update battery values during main loop

Just starting out? Need help? Post your questions and find answers here.
AlfaRomeo
New User
New User
Posts: 7
Joined: Fri Jun 21, 2019 10:28 am

Update battery values during main loop

Post by AlfaRomeo »

Hello,
does anyone knows why the battery values doesn´t are updating during the running time of this program?

Code: Select all

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
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: Update battery values during main loop

Post by wombats »

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: Select all

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.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Update battery values during main loop

Post by spikey »

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...
AlfaRomeo
New User
New User
Posts: 7
Joined: Fri Jun 21, 2019 10:28 am

Re: Update battery values during main loop

Post by AlfaRomeo »

Many thanks for your fast reply wombats :)

Will try to implement your solution

@spikey
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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Update battery values during main loop

Post by RASHAD »

Maybe CreateThread() is your good approach
Egypt my love
AlfaRomeo
New User
New User
Posts: 7
Joined: Fri Jun 21, 2019 10:28 am

Re: Update battery values during main loop

Post by AlfaRomeo »

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 :)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Update battery values during main loop

Post by RASHAD »

Using CreateThread()
- No interrupt by any event
- Use any time interval by using Delay()
- Full control when you start or stop

Code: Select all

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
Last edited by RASHAD on Fri Jun 21, 2019 2:24 pm, edited 2 times in total.
Egypt my love
AlfaRomeo
New User
New User
Posts: 7
Joined: Fri Jun 21, 2019 10:28 am

Re: Update battery values during main loop

Post by AlfaRomeo »

RASHAD wrote:Maybe CreateThread() is your good approach
Thanks for the hint RASHAD, it´s a good help :)
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Update battery values during main loop

Post by BarryG »

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.
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Update battery values during main loop

Post by Fred »

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.)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Update battery values during main loop

Post by RASHAD »

Hi Fred
What a shame it is already written in the help :)
Previous post updated
Egypt my love
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Update battery values during main loop

Post by spikey »

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'...
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: Update battery values during main loop

Post by wombats »

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:
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Update battery values during main loop

Post by BarryG »

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