Accurate control of button repeat rate: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
mNo edit summary
Line 7: Line 7:
The secret is in having a method of preventing a button from repeating until our chosen time has elapsed. It is possible, but messy, especially if you are doing this with a number of buttons, to do it inside the code for each button. It is much better to create a new type of button code. For this we need a class, and then create instances of this class for each controlled button. This, incidentally, is OOP, or Object Oriented Programming.
The secret is in having a method of preventing a button from repeating until our chosen time has elapsed. It is possible, but messy, especially if you are doing this with a number of buttons, to do it inside the code for each button. It is much better to create a new type of button code. For this we need a class, and then create instances of this class for each controlled button. This, incidentally, is OOP, or Object Oriented Programming.


Here is thew code to create the new class. It is called timedControl. I have added (unnecessary) open lines between each member of the class to make understanding easier.  
Here is thew code to create the new class. It is called timedControl. I have added (unnecessary) open lines between each member of the class to make understanding easier.
<syntaxhighlight lang="php" line>
<syntaxhighlight lang="php" line>
var timedControl = {
var timedControl = {
Line 19: Line 19:


       _call: func(fn, arg) {
       _call: func(fn, arg) {
         if (!arg) { me.last_busy = 0; me.tap = 0; return; } # Stop if arg = 0. Clear values.
         if (!arg) { me.last_busy = 0; return; }
         if (me.timeout_dur != nil) {
         if (me.timeout_dur != nil) {
           if ((me.timeout_dur == -1) and me.last_busy != 0) return; # One-shot
           if ((me.timeout_dur == -1) and me.last_busy != 0) return;
           var time = systime();
           var time = systime();
           if (time < me.timeout_dur + me.last_busy) return; # check timeout_dur has elapsed
           if (time < me.timeout_dur + me.last_busy) return;
           me.last_busy = time;
           me.last_busy = time;
         }
         }
Line 30: Line 30:
};
};
</syntaxhighlight>
</syntaxhighlight>
Let's assume that we have a function, ''myFunction'', which must only repeat every 0.25 seconds. We create it with
<syntaxhighlight lang="php" line>
var myFunction = timedControl.new();
</syntaxhighlight>
and set the repeat rate to 0.25 seconds with
<syntaxhighlight lang="php" line>
myFunction.timeout_dur = 0.25;
</syntaxhighlight>
=== How it works ===
''var myFunction = timedControl.new()'' calls lines 6, 7, 8 of ''timedControl''. This creates a new instance of ''timedControl'' named ''myFunction'' and returns a pointer to it. Now, whenever you use ''myControl'' it will point to where it exists in memory.
''myFunction.timeout_dur = 0.25''. ''myFunction'' points to the function in memory, ''.timout_dur'' then points to where ''timeout_dur'' exists in memory and changes its value to 0.25. Line 4 of the code set it to 1.0, just so it has ''a'' value.





Revision as of 06:35, 22 July 2013

WIP.png Work in progress
This article or section will be worked on in the upcoming hours or days.
See history for the latest developments.

Background

Sometimes you might want to have control over the speed that a repeatable button works - maybe you want it to repeat exactly every 1.325 seconds, or you just want to slow it down. At the end of this article you will be shown how to use a button to set the frequency of a radio. This is certainly a time when the default (system) repeat rate of a button is too fast.

How it is done

The secret is in having a method of preventing a button from repeating until our chosen time has elapsed. It is possible, but messy, especially if you are doing this with a number of buttons, to do it inside the code for each button. It is much better to create a new type of button code. For this we need a class, and then create instances of this class for each controlled button. This, incidentally, is OOP, or Object Oriented Programming.

Here is thew code to create the new class. It is called timedControl. I have added (unnecessary) open lines between each member of the class to make understanding easier.

var timedControl = {
      last_busy: 0,

      timeout_dur: 1.0,

      new: func() {
          return { parents:[me] };
      },

      _call: func(fn, arg) {
        if (!arg) { me.last_busy = 0; return; }
        if (me.timeout_dur != nil) {
          if ((me.timeout_dur == -1) and me.last_busy != 0) return;
          var time = systime();
          if (time < me.timeout_dur + me.last_busy) return;
          me.last_busy = time;
        }
        return call(fn, [arg], me);
      }
};

Let's assume that we have a function, myFunction, which must only repeat every 0.25 seconds. We create it with

var myFunction = timedControl.new();

and set the repeat rate to 0.25 seconds with

myFunction.timeout_dur = 0.25;

How it works

var myFunction = timedControl.new() calls lines 6, 7, 8 of timedControl. This creates a new instance of timedControl named myFunction and returns a pointer to it. Now, whenever you use myControl it will point to where it exists in memory.

myFunction.timeout_dur = 0.25. myFunction points to the function in memory, .timout_dur then points to where timeout_dur exists in memory and changes its value to 0.25. Line 4 of the code set it to 1.0, just so it has a value.