• @user85573, if by chance you have the setup, could you do some timing on these variations of the code and post the results?

    Variation 1(a))

    // V1a - flash an 8 LED neopixel array in a rainbow pattern
    var led_count = 8;
    var neoPin = NodeMCU.D4;
    var rgb = new Uint8ClampedArray(led_count * 3);
    var pos = 0;
    function getPattern() {
      pos++;
      for (var i=0;i<rgb.length;) {
        rgb[i++] = (1 + Math.sin((i+pos)*0.1324)) * 127;
        rgb[i++] = (1 + Math.sin((i+pos)*0.1654)) * 127;
        rgb[i++] = (1 + Math.sin((i+pos)*0.1)) * 127;
      }
      return rgb;
    }
    setInterval(function() {
      var t0 = getTime();
      require("neopixel").write(neoPin, getPattern());
      console.log(getTime() - t0);
    }, 200); // may need to change to allow console to write
    

    Variation 2(a))

    // V2a - flash an 8 LED neopixel array in a rainbow pattern
    var neo = require("neopixel");
    var led_count = 8;
    var neoPin = NodeMCU.D4;
    var rgb = new Uint8ClampedArray(led_count * 3);
    var len = rgb.length;
    var m = 127;
    var r = 0.1324*m;
    var g = 0.1654*m;
    var b = 0.1*m;
    var pos = 0;
    function getPattern() {
      pos++;
      for (var i=0; i<len;) {
        rgb[i++] = m + Math.sin((i+pos)*r;
        rgb[i++] = m + Math.sin((i+pos)*g;
        rgb[i++] = m + Math.sin((i+pos)*b;
      }
      return rgb;
    }
    function bow() {
      var t0 = getTime();
      neo.write(neoPin, getPattern());
      console.log(getTime() - t0);
    }
    
    function onInit() {
      setInterval(bow,200); // may need to change to allow console to write
    }
    
    setTimeout(onInit, 1000);
    

    And as begin curious what the calculation vs communication times are:

    // V1b - flash an 8 LED neopixel array in a rainbow pattern
    var led_count = 8;
    var neoPin = NodeMCU.D4;
    var rgb = new Uint8ClampedArray(led_count * 3);
    var pos = 0;
    function getPattern() {
      var t0 = getTime();
      pos++;
      for (var i=0;i<rgb.length;) {
        rgb[i++] = (1 + Math.sin((i+pos)*0.1324)) * 127;
        rgb[i++] = (1 + Math.sin((i+pos)*0.1654)) * 127;
        rgb[i++] = (1 + Math.sin((i+pos)*0.1)) * 127;
      }
      console.log(getTime() - t0);
      return rgb;
    }
    setInterval(function() {
      var t0 = getTime();
      require("neopixel").write(neoPin, getPattern());
    }, 200); // may need to change to allow console to write
    

    and

    // V2b - flash an 8 LED neopixel array in a rainbow pattern
    var neo = require("neopixel");
    var led_count = 8;
    var neoPin = NodeMCU.D4;
    var rgb = new Uint8ClampedArray(led_count * 3);
    var len = rgb.length;
    var m = 127;
    var r = 0.1324*m;
    var g = 0.1654*m;
    var b = 0.1*m;
    var pos = 0;
    function getPattern() {
      pos++;
      for (var i=0; i<len;) {
        rgb[i++] = m + Math.sin((i+pos)*r;
        rgb[i++] = m + Math.sin((i+pos)*g;
        rgb[i++] = m + Math.sin((i+pos)*b;
      }
      console.log(getTime() - t0);
      return rgb;
    }
    function bow() {
      neo.write(neoPin, getPattern());
    }
    
    function onInit() {
      setInterval(bow,200); // may need to change to allow console to write
    }
    
    setTimeout(onInit, 1000);
    

    The results may shed some light on how coding in Espruino can matter for execution speed.

    (The different start-up is not part of the timings. It just makes sure that the upload is not interfering)

About

Avatar for allObjects @allObjects started