BBC BASIC
Development >> Raspberry Pi >> Wait - for action
http://bbcbasic.conforums.com/index.cgi?board=raspberry&action=display&num=1493096165

Wait - for action
Post by michael on Apr 25th, 2017, 04:56am

Ever since I got the Raspberry Pi 3, efficiency in code use has become important. I have used WAIT (num) to pace it so as to not overwork the pi.

I was wondering if there was a way to make the program wait for user mouse movement or keyboard action, before the program proceeds.

Would be super efficient and fast when action occurred.
Although, I am not looking for tracking of position, rather, just user action.

Any ideas?

Re: Wait - for action
Post by Richard Russell on Apr 25th, 2017, 09:09am

on Apr 25th, 2017, 04:56am, michael wrote:
I was wondering if there was a way to make the program wait for user mouse movement or keyboard action

Waiting for the keyboard is just INKEY(cs) of course but there's no equivalent for the mouse. So one usually ends up with an 'idle' loop which looks something like this:

Code:
      REPEAT        k% = INKEY(4)        MOUSE x%,y%,b%      UNTIL k%<>-1 OR b% 

(with an additional test for x%/y% changing if you are monitoring mouse movement as well as clicks).

You shouldn't find that this is too inefficient, because the mouse test should be a very small proportion of the 40 milliseconds waiting time. The profiler will quantify this if you need to know accurately (although not on the RPi). Note that a very short-duration mouse click won't always be detected by this code so if necessary it can be supplemented with an ON MOUSE interrupt which sets a flag.

Whatever you do there's a limit to how far you can reduce CPU usage because SDL was originally designed and optimised for games. For example rendering and event polling have to take place in the same thread, so BBCSDL cannot afford to wait too long for an event because that would stall rendering, and conversely while it is waiting to synchronise the rendering to the vertical sync, events will not be polled. This results in a rather ad-hoc arrangement of timeouts to try to balance the CPU usage without hitting performance too much.

Richard.

Re: Wait - for action
Post by michael on Apr 27th, 2017, 11:10am

Thanks Richard. I will work on some modifications to my tools
Re: Wait - for action
Post by michael on May 4th, 2017, 03:45am

Here is a possible sleep mode control for mouse and keyboard. ( I fixed a flaw )
Code:
 WAIT 1      REPEAT        k% = INKEY(4)        resp%=FNmoucheck      UNTIL k%<>-1 OR resp% >0      PRINT "done"      END      DEF FNmoucheck      PRIVATE mx%,my%,mb%,x%,y%,b%      MOUSE x%,y%,b%      IF mx%=0 AND my%=0 AND mb%=0 THEN mx%=x%:my%=y%:mb%=b%: =-1      IF mx%=x% AND my%=y% AND mb%=b% THEN =-1 ELSE mx%=x%:my%=y%:mb%=b%:=1