Skip to content

Instantly share code, notes, and snippets.

@MaBecker
Created January 17, 2018 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaBecker/28fcacfe2d0bd335be74889ca7f0e4a4 to your computer and use it in GitHub Desktop.
Save MaBecker/28fcacfe2d0bd335be74889ca7f0e4a4 to your computer and use it in GitHub Desktop.
/*
test_remap.js
sample how to remap a pixel on a sequential 8x8 wired panel
*/
var np = {
pin : D4,
neopixels : 0,
leds : [],
pannelList : [],
color : {r:0,g:0,b:0},
x : 0,
y : 0,
setup : function(_x,_y,_pannelList){
this.x = _x;
this.y = _y;
this.neopixels = _x *_y;
this.pannelList = (_pannelList) ? _pannelList : [[0],[0]]; // single pannel
this.leds = new Array(this.neopixels*3).fill(0);
pinMode(this.pin, 'output');
this.off();
},
off : function(){
this.leds.fill(0);
this.flip();
},
flip : function(){
//console.log('flip');
x = require("neopixel").write(this.pin,this.leds);
},
remap : function(_x,_y) {
return (this.pannelList[_x / 8 | 0][_y / 8 | 0] + _x + _y * 8 ) * 3;
},
setColor : function(_color){
this.color.r = _color.r;
this.color.g = _color.g;
this.color.b = _color.b;
},
setPixel : function(_x,_y){
o = this.remap(_x,_y);
//console.log(_x,_y,o);
this.leds[o] = this.color.g;
this.leds[o+1] = this.color.r;
this.leds[o+2] = this.color.b;
},
line : function(x1,y1,x2,y2) {
var dx = Math.abs(x2 - x1),
sx = x1 < x2 ? 1 : -1;
var dy = -Math.abs(y2 - y1),
sy = y1 < y2 ? 1 : -1;
var err = dx + dy,
e2; // error value e_xy
while (1) {
//print(x1,y1);
this.setPixel(x1, y1);
if (x1 == x2 && y1 == y2) break;
e2 = 2 * err;
if (e2 > dy) {
err += dy;
x1 += sx;
} // e_xy+e_x > 0
if (e2 < dx) {
err += dx;
y1 += sy;
} // e_xy+e_y < 0
}
}
};
//--------------------------------------------------
// p1 : [[0],[0]] x 0-7, y 0-7
//--------------------------------------------------
// p1 + p2 : [[0],[56]] x 0-15, y 0-7
//--------------------------------------------------
// p1 + p2 + p3 : [[0],[56],[112]]] x 0-23, y 0-7
//--------------------------------------------------
// p1 + p2 : [[0,64],[56,120]] x 0-15, y 0-15
// p3 + p4
np.setColor({r:0,g:10,b:0});
/*
// p1
np.setup(8,8,[[0],[0]]);
np.setPixel(0,0);np.setPixel(1,0);
np.setPixel(6,7);np.setPixel(7,7);
*/
/*
// p1+p2
np.setup(8,16,[[0],[56]]);
np.setPixel(0,0);np.setPixel(1,0);
np.setPixel(6,7);np.setPixel(7,7);
np.setPixel(8,0);np.setPixel(9,0);
np.setPixel(14,7);np.setPixel(15,7);
*/
/*
// p1+p2+p3
np.setup(8,24,[[0],[56],[112]]);
//p1
np.setColor({r:10,g:0,b:0});
np.setPixel(0,0);np.setPixel(1,0);np.setPixel(0,1);
np.setPixel(7,7);np.setPixel(6,7);np.setPixel(7,6);
//p2
np.setColor({r:0,g:10,b:0});
np.setPixel(8,0);np.setPixel(9,0);np.setPixel(8,1);
np.setPixel(14,7);np.setPixel(15,7);np.setPixel(15,6);
//p3
np.setColor({r:0,g:0,b:10});
np.setPixel(16,0);np.setPixel(17,0);np.setPixel(16,1);
np.setPixel(22,7);np.setPixel(23,7);np.setPixel(23,6);
*/
// p1 + p2
// p3 + p4
np.setup(16,16,[[0,64],[56,120]]);
//p1
np.setColor({r:10,g:0,b:0});
np.setPixel(0,0); np.setPixel(1,0); np.setPixel(0,1);
np.setPixel(7,7); np.setPixel(6,7); np.setPixel(7,6);
//p2
np.setColor({r:0,g:10,b:0});
np.setPixel(8,0);np.setPixel(9,0);np.setPixel(8,1);
np.setPixel(14,7);np.setPixel(15,7);np.setPixel(15,6);
//p3
np.setColor({r:0,g:0,b:10});
np.setPixel(0,8); np.setPixel(1,8); np.setPixel(0,9);
np.setPixel(6,15); np.setPixel(7,15); np.setPixel(7,14);
//p4
np.setColor({r:10,g:10,b:10});
np.setPixel(8,8); np.setPixel(9,8); np.setPixel(8,9);
np.setPixel(14,15); np.setPixel(15,15); np.setPixel(15,14);
np.flip();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment