• This code tried to become a module in http://espruino.com/modules a long time ago - at a time when I was not yet familiar enough to pull that thru. Even though the code is now pretty 'old', it is still in demand. Therefore, as a first step, I make a condensed version (w/ examples) that you can copy into your application to take advantage of SWButtons without having to get rid of all the noise by the comments. Second, I'll add files you can copy into your sandbox project modules folder to use the code as real module using var SWBtn = require("SWButton");.

    NOTE for previous users: I switched to .enable() from .disable().

    The example code does the following:

    • uses BTN (or BTN1 when more than one button is on the board)
    • short press toggles the red LED
    • long press toggles the green LED
    • short-long press sequence turns the red LED on
    • short-short press sequence turns the red LED off
    • long-long press sequence turns the green LED on
    • long-short press sequence turns the green LED off

    First, in-line module code w/ example:

    // SWButtonInline.js
    // 2022.0410 - (c) allObjects;
    
    var  SWBtn = // inline module emulation of  // var  SWBtn = require("SWButton"); 
    (function(){ var exports = {};
    // module begin:
    
    var SWBtn = function(f,b,e) {
      this.f = (f) ? f : function(){};
      this.b = (b) ? b : BTN1;
      this.t = null;
      this.k = null;
      this.w = null;
      this.enable(e);
     };
    SWBtn.prototype.C = // Config shared by all instances of SWBtn:
      { B: 20 // debounce [ms]
      , L: 0.250 // min Long press [s]
      , P: 220 // min Pause [ms]
      , D: 10 // delay of fnc function invocation [ms]
      };
    SWBtn.prototype.enable = function(e) {
      if (e === undefined || e) {
        if (!this.w) {
          this.d = false;
          this.k = "";
          var _this = this;
          this.w = setWatch( function(e){ _this.c(e); }, this.b
                           , { repeat:true
                           , edge:"both" // <--- required for built-in buttons!
                           , debounce:_this.C.B } );
        }
      } else {
        if (this.w) { 
          this.d = true;
          this.w = clearWatch(this.w);
          if (this.t) this.t = clearTimeout(this.t);
        }
      }
     };
    SWBtn.prototype.c = function(e){ // change of state - called by set watch
      if (e.state) {
        if (this.t) this.t = clearTimeout(this.t);
      } else {
        this.k = this.k + ((e.time - e.lastTime < this.C.L) ? "S" :"L");
        var _this = this;
        this.t = setTimeout(function(){ _this.e(); }, this.C.P);
      }
     };
    SWBtn.prototype.e = function() {
      this.t = null;
      var _k = this.k;
      if (_k.length > 0) {
        this.k = "";
        var _this = this;
        setTimeout(function(){ _this.f(_k); },this.C.D);
      }
     };
    exports = SWBtn;
    
    // module end
    return exports;
    })(); // :in-line module emulation end
    
    var functs = // function names match Short/Long key press pattern
    { S:  function(){ LED1.toggle(); }
    , L:  function(){ LED2.toggle(); }
    , SL: function(){ LED1.set();    }
    , SS: function(){ LED1.reset();  }
    , LL: function(){ LED2.set();    }
    , LS: function(){ LED2.reset();  }
    };
    var mySWBtn = new SWBtn(function(k){
        console.log("BTN1 detected " + k); // log detected key pattern and...
        if (functs[k]) { functs[k](); } // ...dispatch if defined
      },BTN1,false); // set it up disabled
    
    function onInit() {
      mySWBtn.enable();
    }
    
    setTimeout(onInit,999); // for dev only; remove before upload for save()
    
About

Avatar for allObjects @allObjects started