Skip to content

Commit

Permalink
Added Examples
Browse files Browse the repository at this point in the history
Changed exports
changed default mapping function to snake cabling
  • Loading branch information
sudsy committed Oct 30, 2014
1 parent e752ca0 commit 5deb402
Show file tree
Hide file tree
Showing 20 changed files with 864 additions and 25 deletions.
25 changes: 25 additions & 0 deletions examples/sendSimple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// sendSimple.js

var Canvas = require("canvas");
var CanvastoE131 = require("../");

var canvas = new Canvas(10,10);

//create a simple canvas with an image on it
var context = canvas.getContext('2d');
context.fillStyle="white";
context.fillRect(0,0,10,10);


// connect the canvas to the sender with mapping
var output = new CanvastoE131(canvas, {host: "10.1.1.5"});


output.send();


process.on ("SIGINT", function(){
output.close();
process.exit(1);
});

28 changes: 28 additions & 0 deletions examples/sendSimpleFlipped.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// sendSimpleFlipped.js


var Canvas = require("canvas");
var CanvastoE131 = require("../");

var canvas = new Canvas(10,10);

//create a simple canvas with an image on it
var context = canvas.getContext('2d');
context.fillStyle="white";
context.fillRect(0,0,10,10);

// console.log(CanvastoE131);
// connect the canvas to the sender with mapping
// Pass in the mapping function this time - you can pass your own
// CanvastoE131.mapping.snake is the default, we are passing it through the flip function to flip the matrix diagonally
var output = new CanvastoE131(canvas, {host: "10.1.1.5"}, CanvastoE131.mapping.flipDiagonalFunction(CanvastoE131.mapping.snake));


output.send();


process.on ("SIGINT", function(){
output.close();
process.exit(1);
});

54 changes: 54 additions & 0 deletions examples/simpleAnimation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// simpleAnimation.js


var Canvas = require("canvas");
var CanvastoE131 = require("../");

var canvas = new Canvas(16,16);


var output = new CanvastoE131(canvas, {host: "10.1.1.5"});



function draw(x, y) {
var context = canvas.getContext('2d');
context.save();
// Each time before drawing the Rectangle in a new position the previous one was deleted
context.clearRect(0, 0, 16, 16);
context.fillStyle = "white";
context.fillRect(0, 0, 16, 16);

context.fillStyle = "black";
//We will create an rectangle whose X cordinate we will change in a loop .
context.fillRect(x, 5, 5, 5);

x++;
if(x > 15) { x = 0;}

// Uncomment the following to see snaps of the canvas
// var fs = require('fs'),
// out = fs.createWriteStream(__dirname + '/simpleAnimation.png'),
// stream = canvas.pngStream();

// stream.on('data', function(chunk){
// out.write(chunk);
// });


context.restore();
output.send();
setTimeout(function() {draw(x,y);}, 40);
}

draw(0,0);




process.on ("SIGINT", function(){
output.close();
process.exit(1);
});

// output.close();
Empty file added examples/simpleAnimation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// index.js

module.exports = require("./lib/CanvastoE131.js");
var CanvastoE131 = require("./lib/CanvastoE131.js");

CanvastoE131.mapping = {
rows : require("./lib/mapping/rows.js"),
snake : require("./lib/mapping/snake.js"),
flipDiagonalFunction : require("./lib/mapping/flipDiagonalFunction.js")
};

module.exports = CanvastoE131;
42 changes: 26 additions & 16 deletions lib/CanvastoE131.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@ var Controller = require("./E131Controller.js");
/**
* Constructor used to set up the section of the canvas to send to E131
* @param {canvas} aCanvas canvas instance to use as source
* @param {function} mappingFunction function for mapping pixels in the source to E131 universe, pixel numbers of form f(x,y){ return {universe:int,channel:int}}
* @param {string} options.host ip Address of the server (defaults to port 5568 for output)
* @param {function} [mappingFunction] function for mapping pixels in the source to E131 universe, pixel numbers of form f(x,y){ return {universe:int,channel:int}}
* @param {string} [options.host] ip Address of the server (defaults to port 5568 for output)
* @param {string} [options.channelOrder] order of the channels in the E1.31 universe either RGB or RBG (defaults to RGB)
* @param {int} [options.port] port of the server (defaults to port 5568 for output)
* @param {object} [options.canvasRect] {x,y,width,height} The rectangle to use as the source - if ommitted the whole canvas is used
* @return {CanvasToE131} an instance of the CanvastoE131 class
*/
function CanvastoE131(aCanvas, options, mappingFunction){
options = options || {};
options.channelOrder = options.channelOrder || "RGB";
options.port = options.port || 5568;
options.canvasRect = options.canvasRect || {x: 0, y: 0, width: aCanvas.width, height: aCanvas.height};
options.host = options.host || "127.0.0.1";

mappingFunction = mappingFunction || require("./mapping/snake.js");


if(!options.host){
throw new Error("must supply options.host");
}
if(!aCanvas){
throw new Error("Must Supply Canvas");
}
if(!mappingFunction){
throw new Error("Must Supply mappingFunction");
}

this.canvas = aCanvas;

this.options = options;

Expand All @@ -39,29 +40,38 @@ function CanvastoE131(aCanvas, options, mappingFunction){
// console.log((x+1)*(y+1));
var arrayPosition = x + (y * aCanvas.width);
// console.log(arrayPosition);
that.mappingArray[arrayPosition] = mappingFunction(x,y);
that.mappingArray[arrayPosition] = mappingFunction(x,y, aCanvas.width, aCanvas.height);

};
for(var x =0 , y = 0; x*y < (options.canvasRect.width -1) * (options.canvasRect.height -1) ; x++ >= options.canvasRect.width - 1 && (x=0, y++)){
mapPixel(x,y);
}
//map the last pixel
mapPixel(options.canvasRect.width-1, options.canvasRect.height -1);
// console.log(options.host);

//Create a new controller for this port and host
this.controller = new Controller(options.host, options.port);


}
//
CanvastoE131.prototype.send = function(pixelArray){

pixelArray = pixelArray || this.canvas.getContext('2d').getImageData(0,0, this.canvas.width, this.canvas.height).data;

CanvastoE131.prototype.sendE131 = function(pixelArray){
// console.log(pixelArray[0]);
for(var i = 0; i < this.mappingArray.length; i++){
//set the RGB values
var mappingElement = this.mappingArray[i];
// console.log(i);
this.controller.setChannel(mappingElement.universe, mappingElement.channel, pixelArray[i*4]);
this.controller.setChannel(mappingElement.universe, mappingElement.channel + 1, pixelArray[i*4 + 1]);
this.controller.setChannel(mappingElement.universe, mappingElement.channel + 2, pixelArray[i*4 + 2]);
var transparency = pixelArray[i*4 + 3] / 255;
var universe = mappingElement.universe;
var channel = mappingElement.channel;
this.controller.setChannel(universe, channel, pixelArray[i*4] * transparency);
channel++;
if(channel > 512){ universe++; channel = 1;}
this.controller.setChannel(universe, channel, pixelArray[i*4 + 1] * transparency);
channel++;
if(channel > 512){ universe++; channel = 1;}
this.controller.setChannel(universe, channel, pixelArray[i*4 + 2] * transparency);
}

this.controller.send();
Expand Down
8 changes: 4 additions & 4 deletions lib/E131Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ function E131Controller(host, port){
}

E131Controller.prototype.setChannel = function(universe, channel, value){
if(!this.universeControllers[universe]){
this.universeControllers[universe] = e131.createClient(this.host, this.port);
this.universeControllers[universe].UNIVERSE = universe;
}

// console.log("set universe " + universe);
this.universeControllers[universe] = this.universeControllers[universe] || e131.createClient(this.host, this.port, universe);


// console.log(this.port);
this.universeControllers[universe].dmxdata = this.universeControllers[universe].dmxdata || new Array(512);
Expand Down
7 changes: 7 additions & 0 deletions lib/mapping/flipDiagonalFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//flipDiagonalFunction.js

module.exports = function(fn){
return function(sourceX, sourceY, width, height){
return fn(sourceY, sourceX, height, width);
};
};
6 changes: 6 additions & 0 deletions lib/mapping/rows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//rows.js

module.exports = function(sourceX, sourceY, width, height){
var channelNumber = (sourceX * 3) + (sourceY * width * 3) + 1;
return{universe: (Math.floor(channelNumber / 512) + 1), channel: channelNumber % 512};
};
14 changes: 14 additions & 0 deletions lib/mapping/snake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// snake.js


module.exports = function(sourceX, sourceY, width, height){
var channelNumber;
if(sourceY % 2){
//We are on an odd row
channelNumber = ((width - sourceX - 1) * 3) + (sourceY * width * 3) + 1;
}else{
channelNumber = (sourceX * 3) + (sourceY * width * 3) + 1;
}

return{universe: (Math.floor(channelNumber / 512) + 1), channel: channelNumber % 512};
};
15 changes: 15 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
REPORTER = landing

test:
@NODE_ENV=test ./node_modules/.bin/mocha --reporter $(REPORTER)

test-debug:
@NODE_ENV=test ./node_modules/.bin/mocha debug --reporter $(REPORTER)

test-w:
@NODE_ENV=test ./node_modules/.bin/mocha \
--reporter $(REPORTER) \
--growl \
--watch

.PHONY: test test-w
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
"node-e131" : "*"
},
"devDependencies":{
"canvas" : "*"
"canvas" : "*",
"mocha" : "*",
"should" : "*",
"sinon" : "*",
"debug" : "*"
},
"keywords": [
"sAcn",
Expand Down
1 change: 0 additions & 1 deletion test/test.js → test/other/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ output.close();




//send the image to the server


File renamed without changes
7 changes: 5 additions & 2 deletions test/test2.js → test/other/test2.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
var Controller = require("../lib/E131Controller.js");
var Controller = require("../../lib/E131Controller.js");

var aController = new Controller("10.1.1.5");



var i =0;

setInterval(function(){
i++;
i = Math.min(i, 512);
console.log(i);
aController.setChannel(1,i,255);

aController.setChannel(2,i,255);

aController.send();
},40);
Loading

0 comments on commit 5deb402

Please sign in to comment.