Skip to content

Instantly share code, notes, and snippets.

@sdumetz
Last active August 29, 2015 14:22
Show Gist options
  • Save sdumetz/61c1d61c00a4326feae1 to your computer and use it in GitHub Desktop.
Save sdumetz/61c1d61c00a4326feae1 to your computer and use it in GitHub Desktop.
evdev events read and parse
var fs = require('fs');
var read = function(device){
, stream;
try {
stream = fs.createReadStream(device, {
flags: 'r',
encoding: null
})
.on('data', function(buf){
var i,j,chunk = 24;
for (i=0,j=buf.length; i<j; i+=chunk) {
parse(buf.slice(i,i+chunk));
}
})
.on("error",function(e){
console.log("Error %s - When reading %s",e,device);
});
} catch(e){
return e;
}
return stream;
};
var parse = function(event){
var event = {
time : {tv_sec:null,tv_usec:null},
type : null,
code : null,
value : null
}
, low = 0;
low = buf.readInt32LE(0);
event.time.tv_sec = buf.readInt32LE(4) * 4294967296.0 + low;
if (low < 0) time.tv_sec += 4294967296;
low = buf.readInt32LE(8);
event.time.tv_usec = buf.readInt32LE(12) * 4294967296.0 + low;
if (low < 0) time.tv_usec += 4294967296;
event.type = buf.readUInt16LE(16);
event.code = buf.readUInt16LE(18);
event.value = buf.readUInt32LE(20);
return event;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment