Addressing neopixel via Uint24Array()

Posted on
  • Sharing a way how to setup neopixel, set color and send simple patterns - Many thanks to @Gordon who mentioned this possibility via Uint24Array().

    // load module
    var np = require('neopixel');
    
    //  pin data line is attached to
    var NPP= D16;
    
    // number of neopixel
    var countNP = 100;
    
    // use  Uint24Array  to store an address single pixel
    var neopixelStrip = new Uint24Array(countNP);
    
    // set color for  first and last pixel
    var color = {r:20,g:0,b:20};
    var c = color.g + (color.r << 8) + (color.b << 16);
    neopixelStrip[0] = c;
    neopixelStrip[countNP-1] = c;
    
    // flip neopixel
    np.write(NPP, neopixelStrip.buffer);
    
    

    Send a pattern all over the neopixel strip

    // Solution A
    // simple pattern "_  _  Green _  _"
    var fiveLEDs = new Uint24Array(5);
    fiveLEDs[2] = 30;
    // fill buffer with a pattern array and count 
    var nps = E.toUint8Array({data:fiveLEDs.buffer,cou­nt:20});
    
    // Solution B
    // fill buffer with pattern
    for ( i = 2; i < countNP; i +=5 ) { neopixelStrip.buffer[i] = 30;
    
  • Just a note that E.HSBtoRGB(h,s,b,24) works to get a 24 bit colour value if you ever fancy doing rainbows

  • Is there a way to optimize var nps = E.toUint8Array({data:fiveLEDs.buffer,cou­nt:20}); ?

  • There isn't anything I'm aware of. Ideally we'd need a new function that allows Espruino's interpreting of those {data,count} blocks to work on existing buffers, but that doesn't exist yet.

    Best bet on official boards is to use JIT:

    var countNP = 100;
    var neopixelStrip = new Uint24Array(countNP);
    var fiveLEDs = new Uint24Array(5);
    fiveLEDs[2] = 30;
    
    function repeatPattern(strip, pattern, count) { "jit"
      for (var i=0;i<count;i+=5) strip.set(pattern,i);
    }
    
    repeatPattern(neopixelStrip, fiveLEDs, countNP);
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Addressing neopixel via Uint24Array()

Posted by Avatar for MaBe @MaBe

Actions