• Rather than going with graphics, when you just want to control a small number of LEDs - for example, you want to control the status lights on a control panel with one pin.

    I made a little object that provides a simple interface, and also allows the overall brightness to be dimmed (for example, in response to an ambient light level detector) while remembering the state of the lights. It allows updating by pixel, or color by color. The latter makes particular sense, for example, when using the WS2811 with multiple LEDs, rather than an RGB led.

    stLD={};
      stLD.leds=new Uint8Array(6);
      stLD.set=function(led,color,brightness){­
        if (led<2 && color <3) {
          stLD.leds[led*3+color]=brightness;
          this.upd();
        } else {
          throw "Invalid led or color";
        }
      };
      stLD.upd=function(){
        var tled=new Uint8Array(this.leds);
        var rat=this.maxb/256;
        for(var i=0;i<6;i++){
            tled[i]*=rat;
        }
        SPI1.send4bit(tled,1,3);
      };
      stLD.maxb=256;
      stLD.sMax=function(a){
        if (this.maxb!=a) {
          this.maxb=a;
          this.upd();
        }
      };
      stLD.sets=function(led,color){
        if (led<2 && color.length==3) {
          stLD.leds[led*3]=color[0];
          stLD.leds[led*3+1]=color[1];
          stLD.leds[led*3+2]=color[2];
          this.upd();
        } else {
          throw "Invalid led or color";
        }
      };
    

    Anyway, I like it for making status leds. That example is a 2 LED long string.

  • Nice :)

    Not sure if it helps, but there's also Uint8ClampedArray now which makes sure that if you give it a number >255 it doesn't wrap. It's not significantly slower than Uint8Array either.

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

Cute way to interact with small numbers of WS2811/12's

Posted by Avatar for DrAzzy @DrAzzy

Actions