Skip to content

Instantly share code, notes, and snippets.

@sophistifunk
Created December 9, 2014 11:48
Show Gist options
  • Select an option

  • Save sophistifunk/bbafeca857f454be6e13 to your computer and use it in GitHub Desktop.

Select an option

Save sophistifunk/bbafeca857f454be6e13 to your computer and use it in GitHub Desktop.
#! /usr/local/bin/node
// ---[ Dependencies ]------------------------------------------------------------------------------
var dgram = require("dgram");
var mdns = require("mdns");
// ---[ Config ]------------------------------------------------------------------------------------
var mdnsTimeout = 20000; // ms
var serverHost;
var serverPort;
var numLights = 50;
var pi = Math.PI;
var pi2 = Math.PI * 2;
var radsPerLight = pi/numLights;
var fps = 24;
var radsPerTick = 0.03;
// ---[ Setup ]-------------------------------------------------------------------------------------
var radR = 0.0;
var radG = 0.3 * pi;
var radB = 0.7 * pi;
// Construct a blank buffer
var message = [];
for (var i = 0; i< 10 + (3*numLights); i++) {
message.push(0);
}
var messageBuffer = new Buffer(message);
// ---[ Message generation ]------------------------------------------------------------------------
// I know this is crap :D
function updateMessage() {
for (var j = 0; j < numLights; j++) {
var i = 10+(j*3);
var rads = radsPerLight * j;
messageBuffer[i] = Math.round(127.5 + (Math.sin(radR + (radsPerLight * j)) * 127.5));
messageBuffer[i+1] = Math.round(127.5 + (Math.sin(radB + (radsPerLight * j)) * 127.5));
messageBuffer[i+2] = Math.round(127.5 + (Math.sin(radG + (radsPerLight * j)) * 127.5));
}
}
// ---[ Message sending ]---------------------------------------------------------------------------
var client = dgram.createSocket("udp4");
function sendBuffer() {
client.send(messageBuffer,0,messageBuffer.length, serverPort,serverHost, function(err,bytes){
if (err) throw err;
});
}
// ---[ Animation loop ]----------------------------------------------------------------------------
function tick() {
radR += radsPerTick;
if (radR > pi2)
radR -= pi2;
radG += radsPerTick / 2;
if (radG > pi2)
radG -= pi2;
radB += radsPerTick / 4;
if (radB > pi2)
radB -= pi2;
updateMessage();
sendBuffer();
}
var interval ;
function startAnimating() {
clearInterval(interval);
interval = setInterval(tick,1000/fps);
}
// ---[ MDNS lookup - lazy and ugly ]---------------------------------------------------------------
console.log("Looking for your Holiday...");
var browser = mdns.createBrowser(mdns.udp("secretapi"));
browser.on('serviceUp', function(service) {
serverPort = service.port;
serverHost = service.host;
console.log("Found your holiday at",serverHost,"port", serverPort);
clearTimeout(timeout);
startAnimating();
});
browser.on('serviceDown', function(service) {
process.exit(0); // Might as well.
});
var timeout = setTimeout(function(){
console.log("Could not find a Holiday :(");
process.exit(1);
}, mdnsTimeout);
browser.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment