Page 1 of 2
Processor friendly waiting
Posted: Wed Jan 28, 2015 8:32 pm
by Walter
I have an app where it loops waiting for a button press. If no button is pressed, there is nothing for it to do.
Is there a better way to wait for a button press? It is using up a lot of battery waiting for the button press. I ran the app for around an hour last night, and Battery Usage shows smart BASIC as responsible for 73% of battery drain for the past 24 hours. The next biggest app is 8%.
99.9% of the time the app is just in this loop waiting.
Code: Select all
loop:
if button_pressed("button") then
... do stuff ...
endif
goto loop
Re: Processor friendly waiting
Posted: Wed Jan 28, 2015 8:39 pm
by Mr. Kibernetik
Yes, event driven behavior is a good request. It is in TO DO list.
Re: Processor friendly waiting
Posted: Thu Jan 29, 2015 3:23 pm
by Walter
Event driven would be the best! I'd love to be able to have code connected to a button directly somehow.
If that takes a long time, maybe you could give us a FAST and SLOW command like the UNDERGROUND command that would cause the currently running program to run at full speed, or a slow speed that uses a lot less processor?
Or how about a variation on the GOTO command that would wait a reasonable time before continuing execution. That way the code would run at full speed, but empty loops would slow down?
Re: Processor friendly waiting
Posted: Thu Jan 29, 2015 3:29 pm
by Mr. Kibernetik
Walter wrote:Or how about a variation on the GOTO command that would wait a reasonable time before continuing execution. That way the code would run at full speed, but empty loops would slow down?
You are absolutely right.
It could be some kind of SLOWDOWN command which will delay execution and reduce CPU usage, or a kind of SLOW GOTO for slow looping. I am still thinking about it.
Re: Processor friendly waiting
Posted: Thu Jan 29, 2015 4:01 pm
by Mr. Kibernetik
What delay value you would consider appropriate without looping being too frozen?
Re: Processor friendly waiting
Posted: Thu Jan 29, 2015 4:26 pm
by Walter
I would try a delay of 1/10 - 1/3 of a second. That way the loop would execute several times a second. Just a guess. I'd say try some values and see what seems responsive.
Re: Processor friendly waiting
Posted: Thu Jan 29, 2015 4:31 pm
by Mr. Kibernetik
Walter wrote:I would try a delay of 1/10 - 1/3 of a second. That way the loop would execute several times a second. Just a guess. I'd say try some values and see what seems responsive.
Ok. My experiments show that 0.05 s delay leaves CPU in almost idle state. As it is twice as fast as your suggestion, let it be like that.
Re: Processor friendly waiting
Posted: Thu Jan 29, 2015 5:02 pm
by Mr. Kibernetik
In version 4.8 this feature will be implemented as SLOWDOWN command.
So, code like:
Code: Select all
LOOP: SLOWDOWN
IF NOT BUTTON_PRESS() THEN LOOP
will leave CPU almost idle.
Re: Processor friendly waiting
Posted: Thu Jan 29, 2015 5:18 pm
by Walter
Thanks! Looking forward to it. This should be great to save our batteries until you can get the event driven stuff done.
Re: Processor friendly waiting
Posted: Mon Apr 27, 2015 1:09 am
by ericmengyi
What is the difference between Slowdown Command and Pause Command? I mean, could I use "pause 0.05" in the loops to get the same effect?