You are reading a single comment by @DrAzzy and its replies. Click here to read the full conversation.
  • On an ESP8266:

    
    SPI1.setup({sck:14,mosi:13,mode:1,order:­"msb",baud:1000000});
    
    
    numleds=5;
    
    var leds = {};
    leds.spi=SPI1;
    leds.num=numleds;
    leds.fbuf=new Uint8Array(numleds*4);
    leds.buff=new Uint8Array(numleds*3);
    leds.tbuf=new Uint8Array(numleds*3);
    leds.twinkle=new Int8Array(numleds*3);
    leds.twinklemin=new Int8Array(numleds*3);
    leds.twinklemax=new Int8Array(numleds*3);
    leds.overlay=new Uint8Array(numleds*3);
    leds.gdim=new Uint8Array(numleds);
    for (var tem=0;tem<numleds;tem++){
    	leds.gdim[tem]=31; 
    	for (var j=0;j<3;j++){
    		leds.twinklemin[tem*3+j]=-10;
    		leds.twinklemax[tem*3+j]=10;
    	}
    }
    
    
    leds.dotwinkle = function () {
    	for (var i=0;i<numleds*3;i++){
    		var n=Math.random();
          this.twinkle[i]=E.clip(this.twinkle[i]+(­n<0.75?(n>0.25?0:-1):1),this.twinklemin[­i],this.twinklemax[i]);
    		if (this.buff[i] != this.tbuf[i]){ //fade
              this.buff[i]=this.buff[i]+(this.tbuf[i]>­this.buff[i]?1:-1);
    		}
    
    	}
    };
    
    leds.setPixel = function (x, y, color) {
    	this.tbuf[x*3]=color[0];
    	this.tbuf[x*3+1]=color[1];
    	this.tbuf[x*3+2]=color[2];
    };
    
    leds.setPixel2 = function (x, y, color,mintwi,maxtwi) {
    	this.tbuf[x*3]=color[0];
    	this.tbuf[x*3+1]=color[1];
    	this.tbuf[x*3+2]=color[2];
    	this.twinklemax[x*3]=maxtwi[0];
    	this.twinklemax[x*3+1]=maxtwi[1];
    	this.twinklemax[x*3+2]=maxtwi[2];
    	this.twinklemin[x*3]=mintwi[0];
    	this.twinklemin[x*3+1]=mintwi[1];
    	this.twinklemin[x*3+2]=mintwi[2];
    };
    
    leds.flip = function () {
    	var tclb=new Uint8ClampedArray(this.num*3);
    	for (var i=0;i<(this.num*3);i++) {
          tclb[i]=leds.buff[i]+(leds.buff[i]?leds.­twinkle[i]:0)+leds.overlay[i];
    	}
      //console.log(tclb);
    	for (var i=0;i<numleds;i++) {
    		//var x = new Uint16Array([tclb[i*3]<<5,tclb[(i*3)+1]<­<5,tclb[(i*3)+2]<<5]);
    		var x = new Uint16Array([gtab[tclb[i*3]],gtab[tclb[(­i*3)+1]],gtab[tclb[(i*3)+2]]]);
    		
    		var ma = Math.max(x[0],x[1],x[2]);
    		var mi = Math.min(x[0],x[1],x[2]);
    		var mult=1;
    		if (this.gdim[i] == 31) {
    			if (ma <700 && mi < 200) {
    				this.gdim[i]=7;
    				mult=4.4;
    
    			} else if (ma <1700 && mi < 500) {
    				this.gdim[i]=15;
    				mult=2.06;
    			} 
    		} else if (this.gdim[i] == 15) {
    			if (ma <700 && mi < 200) {
    				this.gdim[i]=7;
    				mult=4.4;
    			} else if (ma >1980) {
    				this.gdim[i]=31;
    			} else {
    				mult=2.06;
    			} 
    		} else if (this.gdim[i] == 7) {
    			if (ma > 1980 ) {
    				this.gdim[i]=31;
    			} else if (ma >924) {
    				this.gdim[i]=15;
    				mult=2.06;
    			} else {
    				mult=4.4;
    			}
    		} 
    		x[0]=x[0]*mult;
    		x[1]=x[1]*mult;
    		x[2]=x[2]*mult;
    
    		this.fbuf[i*4]=(this.on?(this.gdim[i]|22­4):224);
    		this.fbuf[1+i*4]=(x[0]==0?0:Math.max(x[0­]>>4,1));
    		this.fbuf[2+i*4]=(x[1]==0?0:Math.max(x[1­]>>4,1));
    		this.fbuf[3+i*4]=(x[2]==0?0:Math.max(x[2­]>>4,1));
    
    		if (i==-1) {
    
          console.log(x);
          console.log(mult);
          console.log(this.fbuf[i*4]+" "+this.fbuf[1+i*4]+" "+this.fbuf[2+i*4]+" "+this.fbuf[3+i*4]);
    		}
    	}
    
    	this.spi.send([0,0,0,0]);
    	this.spi.send(this.fbuf);
        this.spi.send([0xFF,0xFF,0xFF,0xFF]);
    };
    
    ![](http://drazzy.com/e/espruino/etc/IMG­_20160125_0128587.jpg)
    
    
    
    function animate() {
      leds.flip();
      leds.dotwinkle();
      setTimeout("animate()",20);
    }
    //setBusyIndicator(LED1);
    leds.setPixel(0,0,[255,255,0]);
    
    gtab=new Uint16Array(256);
    gtab=new Uint16Array([0,1,3,5,8,12,15,19,23,28,33­,37,43,48,54,59,65,72,78,85,91,98,105,11­2,120,127,135,143,151,159,167,175,184,19­3,201,210,219,228,238,247,257,266,276,28­6,296,306,316,326,337,347,358,369,380,39­1,402,413,424,435,447,459,470,482,494,50­6,518,530,542,554,567,579,592,605,617,63­0,643,656,669,683,696,709,723,736,750,76­4,777,791,805,819,833,848,862,876,891,90­5,920,935,949,964,979,994,1009,1024,1039­,1055,1070,1085,1101,1116,1132,1148,1164­,1179,1195,1211,1227,1244,1260,1276,1292­,1309,1325,1342,1359,1375,1392,1409,1426­,1443,1460,1477,1494,1511,1528,1546,1563­,1581,1598,1616,1634,1651,1669,1687,1705­,1723,1741,1759,1777,1796,1814,1832,1851­,1869,1888,1906,1925,1944,1963,1981,2000­,2019,2038,2057,2077,2096,2115,2134,2154­,2173,2193,2212,2232,2252,2271,2291,2311­,2331,2351,2371,2391,2411,2431,2451,2472­,2492,2512,2533,2553,2574,2595,2615,2636­,2657,2678,2699,2720,2741,2762,2783,2804­,2825,2846,2868,2889,2911,2932,2954,2975­,2997,3019,3040,3062,3084,3106,3128,3150­,3172,3194,3216,3238,3261,3283,3305,3328­,3350,3373,3395,3418,3441,3463,3486,3509­,3532,3555,3578,3601,3624,3647,3670,3693­,3716,3740,3763,3786,3810,3833,3857,3881­,3904,3928,3952,3975,3999,4023,4047,4071­,4095]);
    
    
    

    The table needs to be adjusted a bit more still, I think.

    The "twinkle" effect is a slow drift of the LED color around the set point - in this way, a bunch of LEDs that are set to the "same" color won't all look identical, and their colors relative to eachother will subtly shift over time, giving a more organic effect.

    The diffusers look great now - I'm using a single halved pingpong ball for each, with the halves inside eachother (with the help of a bit of glue).

About

Avatar for DrAzzy @DrAzzy started