Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
strfry committed Oct 10, 2012
0 parents commit dc4b706
Show file tree
Hide file tree
Showing 17 changed files with 802 additions and 0 deletions.
47 changes: 47 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

/**
* Module dependencies.
*/

var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, ambience = require('./routes/ambience')
, http = require('http')
, path = require('path')
, light = require('./lib/light')

var app = express();

app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
app.use(express.errorHandler());
});

app.get('/ambience/color', ambience.color);
app.get('/ambience/color/*', ambience.color);
app.get('/', routes.index);

var server = http.createServer(app);
var io = require('socket.io');
var socket = io.listen(server);

light.init(socket.of('ambience'));

server.listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
});



32 changes: 32 additions & 0 deletions lib/artnet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var dgram = require('dgram');
var Buffer = require('buffer').Buffer;

function ArtNetClient(host, port) {
this._host = host;
this._port = port;
this.HEADER = [65, 114, 116, 45, 78, 101, 116, 0, 0, 80, 0, 14]; // 0 - 11
this.SEQUENCE = [0]; // 12
this.PHYSICAL = [0]; // 13
this.UNIVERSE = [0, 0]; // 14 - 15
//this.LENGTH = [0, 13]; // 16 - 17
}
exports.ArtNetClient = ArtNetClient;

exports.createClient = function(host, port) {
return new ArtNetClient(host, port);
}

ArtNetClient.prototype.send = function(data) {
// Calcualte the length
var length_upper = Math.floor(data.length / 256);
var length_lower = data.length % 256;

var data = this.HEADER.concat(this.SEQUENCE).concat(this.PHYSICAL).concat(this.UNIVERSE).concat([length_upper, length_lower]).concat(data);
var buf = Buffer(data);


var sock = dgram.createSocket("udp4");
sock.send(buf, 0, buf.length, this._port, this._host, function() {
sock.close();
});
}
54 changes: 54 additions & 0 deletions lib/light.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var artnet = require('./artnet');
var onecolor = require('onecolor');

var client = artnet.createClient("2.0.0.2", 6454);

var light_map = [3, 6, 9, 12, 15,
19, 22, 25, 28, 31,
35, 38, 41, 44, 47,
51, 54, 57, 60, 64,
67, 70, 73, 76, 79];

var maincolor = onecolor("yellow");

exports.setMainColor = function(color) {
console.log(color);
console.log(onecolor(color));
maincolor = onecolor(color);
update();
}

exports.getMainColor = function() {
return maincolor;
}

exports.init = function(socket) {
socket.on('connection', function(client) {
socket.on('color', function(data) {
setMainColor(data);
});
});
}

function update() {

dmx = new Array(128);

var r = maincolor.r() * 255;
var g = maincolor.g() * 255;
var b = maincolor.b() * 255;

for (var bar = 0; bar < 5; bar++) {
var offset = bar * 16;
for (var light = 0; light < 5; light++) {
var light_off = offset + light * 3;
dmx[light_off + 1] = r;
dmx[light_off + 2] = g;
dmx[light_off + 3] = b;
}
}


client.send(dmx);
}

12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "3.0.0rc4",
"jade": "*"
}
}
Loading

0 comments on commit dc4b706

Please sign in to comment.