• for me it does not crash, however I am turning neopixel off as I don't have it. with your code I started with

    >handleNextFrame()
    412.4755859375 14.98413085937 385.1318359375
    

    and ended with

    >handleNextFrame()
    98.876953125 5.615234375 256.04248046875
    

    cachin globals to local variables give speedup and also the native code produced is smaller,
    here is code , I don't know what values it computes, it does not crash and can be timed repeatedly

    //let Neopixel = require("neopixel");
      let rounded = Math.round;
      let clamped = E.clip;
      let max     = Math.max;
      let DisplaySize = 16*16;
      let Display     = new Uint8ClampedArray(DisplaySize*3);
      let PixelOffset = new Int16Array(256);
        for (let i = 0; i < 16; i++) {
          for (let j = 0; j < 16; j++) {
            let Index = i*16 + j;
            if (i % 2 === 0) {
              PixelOffset[Index] = Index*3;
            } else {
              PixelOffset[Index] = (i*16 + 15 - j)*3;
            }
          }
        }
      let normalized = new Uint8ClampedArray(16*16);
        for (let i = 0; i < 256; i++) {
          normalized[i] = (
            i === 0
            ? 0
            : rounded(1 + 256/4*(i/256)*(i/256))
          );
        }
      let TemperatureMap = new Uint8ClampedArray(DisplaySize);
        for (let i = 0; i < 256; i++) {
          TemperatureMap[i] = 0;
        }
      let ColorMap = new Array(16);
        for (let i = 0; i < 16; i++) {
          ColorMap[i] = E.HSBtoRGB(i/16, 1, 1, true);
        }
    /**** computeDiffusion ****/
      function computeDiffusion () { "compiled";
        let rounded = Math.round;
        let tmap=TemperatureMap;
        let i = 0, j = 0, k = 0;
        for (i = 15; i > 0; i--) {             // intensity diffusion for upper rows
          k = i*16;
          tmap[k] = rounded((
            6*tmap[k] + 2*tmap[k-16] + tmap[k-15]
          )/9);
          for (j = 1; j < 15; j++) {
            k++;
            tmap[k] = rounded((
              6*tmap[k] + tmap[k-17] + 2*tmap[k-16] + tmap[k-15]
            )/10);
          }
          k++;
          tmap[k] = rounded((
            6*tmap[k] + tmap[k-17] + 2*tmap[k-16]
          )/9);
        }
      }
    /**** computeHeating ****/
      function computeHeating () { "compiled";
        let random=Math.random;
        let clamped = E.clip;
        let rounded = Math.round;
        let HeatX     = rounded(16*random());         // where to insert new heat
        let HeatCount = rounded(12*random());           // how long to heat there
        let tmap=TemperatureMap;
          HeatCount -= 1;
        if (HeatCount < 0) {                    // heat around HeatX, cool elsewhere
          HeatX     = rounded(16*random());
          HeatCount = rounded(12*random());
        }
        let i, l = HeatX-4;
        for (i = 0/*, l = HeatX-4*/; i < l; i++) {
          tmap[i] = clamped(tmap[i] - 8, 0,255);
        }
          i = HeatX-4;
          i++; if (i >= 0) { tmap[i] = clamped(tmap[i] + 1, 0,255); }
          i++; if (i >= 0) { tmap[i] = clamped(tmap[i] + 3, 0,255); }
          i++; if (i >= 0) { tmap[i] = clamped(tmap[i] + 4, 0,255); }
          i++;               tmap[i] = clamped(tmap[i] + 5, 0,255);
          i++; if (i < 16) { tmap[i] = clamped(tmap[i] + 4, 0,255); }
          i++; if (i < 16) { tmap[i] = clamped(tmap[i] + 3, 0,255); }
          i++; if (i < 16) { tmap[i] = clamped(tmap[i] + 1, 0,255); }
          i++;
        for (i = i; i < 16; i++) {
          tmap[i] = clamped(tmap[i] - 8, 0,255);
        }
      }
    /**** prepare display ****/
      let normalizedRGB = new Uint8ClampedArray(3);
      function prepareDisplay () { "compiled";
        let nRGB=normalizedRGB;
        let n=normalized;
        let tmap=TemperatureMap;
        let colormap=ColorMap;
        let rounded = Math.round;
        let max     = Math.max;
        for (let i = 0; i < 16; i++) {                                   // row-wise
          let RowStart = i*16; let RowEnd = RowStart + 15;
          let k = RowStart;
          for (let j = 0/*, k = RowStart*/; j < 16; j++/*, k++*/) {   // column-wise
            let RGB         = colormap[j];
            let Temperature = tmap[k];
              if (Temperature < 16) {
                Temperature = 0;
              } else {
                Temperature = max(48,Temperature)/256;
              }
            nRGB[0] = n[rounded(RGB[1]*Temperature)];
            nRGB[1] = n[rounded(RGB[0]*Temperature)];
            nRGB[2] = n[rounded(RGB[2]*Temperature)];
            Display.set(nRGB, PixelOffset[k]);
            k++;
          }
        }
      }
    /**** handleNextFrame ****/
      function handleNextFrame () {
     let d1,d2,d3,d = Date();
     computeDiffusion();d1=Date().ms-d.ms;d=D­ate();
      computeHeating();d2=Date().ms-d.ms;d=Dat­e();
      prepareDisplay();d3=Date().ms-d.ms;d=Dat­e();
      print(d1,d2,d3);
      /**** show display ****/
        //Neopixel.write(B15,Display);
      /**** wait for next frame and proceed ****/
    //    setTimeout(handleNextFrame,10);
      }
    /**** start automatically ****/
    //  setTimeout(handleNextFrame,1000);   
    
  • which device are you using? your code still crashes for me even without Neopixel-specific calls (which you may - and perhaps should - leave enabled as they do not expect any feedback but, effectively, just send pulses to pin B15)

    Amendment: moving outer variables into the three main functions (as you suggested) also already hangs up my code.

    Personally, I'd recommend to "diff" the code generated for the working and the non-working versions of my source for the original Espruino and look at the changes as s.th. really weird must be going on...

About

Avatar for fanoush @fanoush started