writing modules using new feature class

Posted on
  • Like to share how to write a module containing a class

    // modules/stopWatch.js
    
    class stopWatch {
          constructor() {
            var timeStart = 0;
            var timeStop = 0;
          }
        // Method start()
          start() { 
            this.timeStart = Date.now();
            this.timeStop = 0;
          }
        // Method stop()
          stop() { 
            this.timeStop = Date.now();
          }
          // Method duration()
          duration(){
            return this.timeStop-this.timeStart;
          }
    }
    
    exports  = stopWatch
    

    and use it.

    // projects/test_class_stopWatch.js
    
    stopWatch = new (require('stopWatch'))();
    
    function onInit(){
          stopWatch.start();
          setTimeout( function(){
            stopWatch.stop();
            console.log('duration in sec: ',
                    ( stopWatch.duration() / 1000 ).toFixed(2) );
          }, 1000);
    }
    
    setTimeout(onInit,1000);
    

    // output
    duration in sec: 1.00

  • Thanks for this. It might tidy modules up quite a bit.

  • Here is one more I named LED. It is a very simple class the can handle three led states:

    on / off / blink( duration in ms for off and on)

    Some led are switched on with Pin.write(1) and some with Pin.write(0) thats why the constructor needs two parameter, one for the pin and one for the value used to switch the led on.

    class LED {
          constructor(led, on) {
            this.led = led;
            this.ON = on;
            this.id = 0;
          }
          on() {
            this.off();
            this.led.write(this.ON);
          }
          off(){
            if ( this.id ) {
              clearInterval(this.id);
              this.id = 0;
            }
            this.led.write(!this.ON);
         }
          blink(t){
            this.off();
            this.id = setInterval(function(led){led.toggle();}­,t, this.led);
          }
    }
    

    Code to test class LED on a Pico

    
    /* Pico 
    LED1  red
    LED2  green
    */
    
    led1 = new LED(LED1, true);
    led1.blink(500);
    
    led2 = new LED(LED2, true);
    led2.blink(250);
    
    // stop after 10 sec
    setTimeout(function(){led1.off();led2.of­f();},1E4);
    

    Hope it is useful ;-)

  • 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 :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

writing modules using new feature class

Posted by Avatar for MaBe @MaBe

Actions