• [1v64] - Hi,

    from a programming point of view I feel like Richard, and from a resourceful one I understand very well Gordon's implementation. Since obviously RAM is the constraining factor, I might cave in on some of my programming paradigms and expectations. I do not (yet) know engine implementation details, but could imagine a solution with a federated symbol table - (current) RAM symbol table and a ROM symbol table. Lookup - and many other things - will of course have to be adjusted. The 'many other things' is I guess what Gordon refers to by 'reasonably big change'.

    For myself - in the spirit of my user name - I solved the variable access challenge as follows:

    LED is a singleton or class object knows all about the LEDs and has a s(et) method accepting the led i(d) and a on/off (true/false) evaluating v(alue) as parameters:

    var LED = // leds 'class'/singleton object
    { ls: [LED1,LED2,LED3] // leds
    , s: function(i,v) { // 'value tolerant' set method
        this.leds[i].write( (v) ? 1 : 0);
      }
    };
    

    Switching LED2 on and LED3 off is achieved by the following statements:

    LED.s(1,1);
    LED.s(2);
    

    The LED1..LED3 are mapped to the *i*ds 0..2. If you like to preserve the index of the led, modify the set method to look like this:

        this.leds[i - 1].write( (v) ? 1 : 0 );
    

    The 'program' for a running light Red(LED1),Green(LED2),Blue(LED3) is implemented as an singleton object as well:

    var RLL = // running led lights 'class'/singleton object
    { i: 2
    , t: null 
    , r: function(s) { // run(1) = start / run(0) = stop
        LED.s(this.i,0);
        if (s) {
          this.i = (this.i < 2) ? this.i + 1 : 0;
          LED.s(this.i,1);
          this.t = setTimeout("RLL.r(1)",166);
        } else {
          clearTimeout(this.t);
        }
      } 
    };
    

    The commands RLL.r(1); and RLL.r(0); start and stop the running lights. To start and stop the running light with the espruino onboard BTN1 button, add a button up .c() check method that toggles .run() with start/stop. Note the start of the repetitive button checking by its immediate invocation after definition: RLL.c();. The immediate invocation of the repetitive button check can be left out, but has then to be issued as a command.

    Paste the complete code below into the edit area of the IDE, send it to Espruino, and push BTN1 button.

    var LED = // leds 'class'/singleton object
    { ls: [LED1,LED2,LED3] // leds
    , s: function(i,v) { // 'value tolerant' set
        this.ls[i].write((v)?1:0);
      }
    };
    var RLL = // running led lights
    { i: 2         // current LED index
    , t: null      // timeout
    , a: false     // running light state (active)
    , b: false     // last button state (pressed)
    , r: function(s) { // run(1) = start / run(0) = stop
        LED.s(this.i);
        if (s) {
          this.i = (this.i < 2) ? this.i + 1 : 0;
          LED.s(this.i,1);
          this.t = setTimeout("RLL.r(1)",166);
        } else {
          clearTimeout(this.t);
        }
      }
    , c: function() { // check BTN1 (detect 'up' and toggle start/stop
        var b = digitalRead(BTN1) == 1;
        if (this.b && !b) this.r(this.a = !this.a);
        this.b = b;
        setTimeout("RLL.c()",100);
      }
    };
    RLL.c();
    
About

Avatar for allObjects @allObjects started