• (2 of 2) continued. Software Buttons - Many buttons from just one hardware button

    Constructor and methods:

    • SWBtn(fnc,btn,d) is the software button class or constructor method. It accepts three (3) parameters:

      1. fnc one parameter function to be invoked on press sequence detection.
      2. btn symbol identifying the button or input pin; default: BTN1 / B12.
      3. d boolean disabled; default: false; allows to create button in disabled state.

    • .disable(b) method disables button according to passed b boolean interpreted value.

    • .c(e) method is invoked on every button / pin state change - press or release.

    • .e() method is invoked on every sequence detection and invokes (timer delayed) the function provided at construction or as set later.

    • .C prototype object is the configuration object holding the timing values shared by all instantiated SWBtn software buttons.

    Basic usage examples:

    var mySWBtn = new SWBtn(function(k){
        console.log("BTN1 detected " + k);
      });
    

    The code above creates an enabled software button. On press sequence detection it invokes the passed anonymous function with the pressed sequence as parameter value, for exmple "S" for one short press, "SS" for two, and "SL" for one short and one long press, etc. The implementation of the function writes the press sequence to the console:

    `
    1v64 Copyright 2014 G.Williams
    >echo(0);
    =undefined
    BTN1 detected S
    BTN1 detected SS
    BTN1 detected SL
    >
    `
    

    You may wonder why the button's returned key value for the press sequence is not a series of dots (.) and dashes (-). The reason is simple: the key as returned can be used as valid property or function name (of an object).

    For example, the following code - entered into the IDE command pane after sending the SWBnt class code to the Espruine - counts how many of the various sequences have been pressed,

    var presses = {};
    var mySWBtn = new SWBtn(function(k){ 
        presses[k] = ((presses[k]) ? presses[k] : 0) + 1;
      });
    

    and entering the following code into the IDE command pane gives a report about the counted various press sequences:

    for (var v in presses) console.log(
        [v," was pressed ",presses[v]," time(s)."].join("")
      );
    
    `
    S was pressed 6 time(s).
    L was pressed 5 time(s).
    LSL was pressed 1 time(s).
    SLLS was pressed 2 time(s).
    `
    

    To print this report and reset the counters on 'pressing' the "SSSSS" software button - 5 short presses of Btn1 - set following function on existing mySWBtn (or pass it on in a new creation).

    mySWBtn.f = function(k) {
        presses[k] = ((presses[k]) ? presses[k] : 0) + 1;
        if (k == "SSSSS") { // 'key' for triggering the presses report
            console.log("*** Presses since creation/last report:");
            for (var v in presses) console.log(
                [v," was pressed ",presses[v]," time(s)."].join("")
            );
            presses = {}; // reset counters
        }
      };
    

    The above example shows that the function can be set or changed on a button after its creation, and so can you also change the C(onfiguration) parameters for the timings - but remember, you will change the timing for every SWBtn - because they shard the one and only C(onfiguration).

    To control the RLL running led light shown in forum comment at What is LED1/A13? (addressing pins via variables),
    replace the Btn1 code with the SWBtn code, and replace the last line with

    var rllBtn = new SWBtn(function(k){ muej.pub(k); });
    

    Do disable and re-enable the button, invoke the .disable() method:

    rllBtn.disable(true); // disable
    
    rllBtn.disable(false); // re-enable
    
    

    Latter is the same as:

    rllBtn.disable(); // re-enable - :-o - oops...
    

    Above is not very obvious, but still as designed: the missing boolean argument evaluates to false... ;-)

    A button can be created in disabled state:

    var rllBtn = new SWBtn(function(k){ muej.pub(k); },true);
    

    This is especially helpful when other initialization processes have to happen or complete before user input can be accepted. To enable the button at desired point in time, just invoke the .disable() method:

    rllBtn.disable(false);  // enable
    
About

Avatar for allObjects @allObjects started