Software Buttons - Many buttons from just one hardware button

Posted on
Page
of 2
Prev
/ 2
  • Updated github... version at https://raw.githubusercontent.com/muet/EĀ­spruinoDocs/master/modules/SWButton.js is back to work... ;-)

    Lesson(s) learned: Don't trust any defaults! ...they may change and make your life a misery.

  • thank you so much, it works great ;)

  • @bigplik, urvw, anytime... - ao

  • 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/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 turn 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()
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Software Buttons - Many buttons from just one hardware button

Posted by Avatar for allObjects @allObjects

Actions