Skip to content

Instantly share code, notes, and snippets.

@danasf
Last active August 29, 2015 14:06
Show Gist options
  • Save danasf/0616ca13d1f09e61aaa4 to your computer and use it in GitHub Desktop.
Save danasf/0616ca13d1f09e61aaa4 to your computer and use it in GitHub Desktop.
ledbot simulator
<!doctype html>
<head>
<title>LEDBOT SIMULATOR</title>
<style type="text/css">
body { background:#333; color:#fff; }
#leds { margin:20px; }
</style>
</head>
<body>
<canvas width="1200" height="600" id="leds"></canvas>
<script src="/socket.io/socket.io.js"></script>
<script>
;(function() {
var socket = io('http://localhost');
var led = new LEDBoard("leds",64,32);
socket.on('render', function (data) {
led.draw(data.data);
});
function LEDBoard(canvas,xSize,ySize) {
this.can = document.getElementById(canvas);
this.con = this.can.getContext('2d');
this.size = { x:xSize, y:ySize };
this.spacing = { x:5, y:5 };
this.radius = 10;
}
LEDBoard.prototype.draw = function(data) {
this.con.clearRect(0,0,this.can.width,this.can.height);
for(var x = 0; x < this.size.x; x++) {
for(var y = 0; y < this.size.y; y++) {
this.con.fillStyle = "#"+data[(y*this.size.x)+x];
this.con.fillRect(x*(this.radius+this.spacing.x),y*(this.radius+this.spacing.y),this.radius,this.radius);
}
}
};
})();
</script>
</body>
{
"name": "ledsim",
"version": "0.0.0",
"description": "ledbot sim",
"main": "server.js",
"dependencies": {
"socket.io": "^1.0.6"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC"
}
var net = require('net'),
app = require('http').createServer(handler),
io = require('socket.io')(app),
fs =require('fs');
var HOST = '127.0.0.1';
var OPC_PORT = 7890;
var WEB_PORT = 8080;
// create OPC listener
net.createServer(function(opc) {
opc.on('data', function(data) {
var buf = new Buffer(data,"hex");
var pixels = buf.slice(4,buf.length).toString("hex").match(/[\s\S]{1,6}/g) || [];
io.sockets.emit('render', { len:pixels.length, data:pixels.reverse() });
});
opc.on('close', function(data) {
console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});
// web socket
io.on('connection', function (socket) {
console.log("new user connected!");
});
}).listen(OPC_PORT, HOST);
console.log('OPC socket server listening on ' + HOST +':'+ OPC_PORT);
console.log('Web server listening on ' + HOST +':'+ WEB_PORT);
// web handler
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
app.listen(WEB_PORT,HOST);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment