• Just to add to this, there's another 'hack' for things like this - since you can add to the built-in classes in Espruino, it is possible to just extend Pin:

    Pin.prototype.blink = function(t) { ... }
    
    LED1.blink();
    

    The gotcha here is that there's no storage for fields (like the interval's ID) in the Pin itself, so you'd have to store them elsewhere, like maybe in a closure:

    (function() {
      var ids = [];
      Pin.prototype.on = function() { 
        this.blink(0);
        this.set();
      };  
      Pin.prototype.off = function() { 
        this.blink(0);
        this.reset();
      };
      Pin.prototype.blink = function(t) { 
        this.write(0);
        if (ids[this]) clearInterval(ids[this]);
        if (t) ids[this] = setInterval(_=>this.toggle(), t);
        else delete ids[this];
      };
    })();
    
    
    LED1.blink(100);
    ...
    LED1.off();
    

    In reality it's maybe not as clean as @MaBe's suggestion, but it's an interesting thing to be aware of :)

About

Avatar for Gordon @Gordon started