Created
May 13, 2015 23:40
-
-
Save anselm/f5b1c2dc0117757a1141 to your computer and use it in GitHub Desktop.
send mouse positions to gcode using node js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a node.js application. It relies on "npm serialport". | |
Save it as penwriter.js | |
Run it using | |
node penwriter.js | |
Visit it with a browser such as | |
http://127.0.0.1:9876 | |
You will get a blank screen. If you open the browser developer debug console you will see that when you press the mouse down and move that it will report the mouse positions. It also sends them back to the server and pipes them out to the serial device. | |
You will need to hack the code appropriately to write the correct stuff to the correct serial port. | |
*/ | |
var homepage = "\ | |
<script> \n\ | |
var ispressed = 0; \n\ | |
document.ontouchstart=function(){ispressed=1} \n\ | |
document.ontouchend=function(){ispressed=0} \n\ | |
document.onmousedown=function(){ispressed=1} \n\ | |
document.onmouseup=function(){ispressed=0} \n\ | |
document.onmousemove=function(event){ \n\ | |
if(!ispressed)return; \n\ | |
var query = '/xy?x='+event.pageX+'&y='+event.pageY; \n\ | |
var req = new XMLHttpRequest(); \n\ | |
req.open('GET',query,false); \n\ | |
req.send(); \n\ | |
console.log(event.pageX+'...'+event.pageY); \n\ | |
} \n\ | |
</script> \n\ | |
"; | |
// an http hserver | |
var http = require('http'); | |
var url = require('url'); | |
function start_webserver() { | |
http.createServer(function(request,response) { | |
var query = url.parse(request.url); | |
if(!query.query) { | |
console.log("A short request - assuming it is for the client\n"); | |
response.writeHead(200); | |
response.write(homepage); | |
response.end(); | |
return; | |
} else { | |
var parts = query.query.split("&"); | |
var part1 = parts[0].split("="); | |
var part2 = parts[1].split("="); | |
var x = part1[1]; | |
var y = part2[1]; | |
console.log("x = " + x + " y = " + y ); | |
serial_write(x,y); | |
response.writeHead(200); | |
response.write("delicious thanks\n"); | |
} | |
response.end(); | |
}).listen(9876); | |
}; | |
// a serial port | |
var SerialPort = require("serialport").SerialPort; | |
var ser = 0; | |
function serial_write(x,y) { | |
if(!ser) return; | |
console.log("writing data to serial port"); | |
var data = "X"+x+"\n"; | |
ser.write(data,function(err,results) { | |
}); | |
var data = "Y"+Y+"\n"; | |
ser.write(data,function(err,results) { | |
}); | |
} | |
// start web server if serial opens | |
function start_serial() { | |
ser.on("open",function() { | |
console.log("successfully opened serial port, now starting web server"); | |
start_webserver(); | |
}); | |
} | |
/* | |
try { | |
ser = new SerialPort("/dev/tty-usbserial1", { baudrate: 57000 }); | |
start_serial(); | |
} catch(error) { | |
console.log("no such port - running server anyway\n"); | |
} | |
*/ | |
start_webserver(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment