UNPKG

ev3js

Version:

LEGO Mindstorms EV3 API for Node.js

54 lines (43 loc) 1.34 kB
var fs = require('fs'), EventEmitter = require('events').EventEmitter; var Button = function() { this.poll(); //this.on('newListener', function(){console.log(this.listenerCount(), arguments);}.bind(this)); }; Button.prototype = new EventEmitter(); (function(){ this.PATH = '/dev/input/by-path/platform-gpio-keys.0-event'; this.BUFFER_SIZE = 16; this.ESC = 14; this.ENTER = 28; this.UP = 103; this.LEFT = 105; this.RIGHT = 106; this.DOWN = 108; this.polling = false; this.pollingTime = 10; this.poll = function(){ fs.open(this.PATH, 'r', function(err, fd){ if(err){ throw err; } this.polling = this.pollingTime; (function(){ var buf = new Buffer(this.BUFFER_SIZE); fs.readSync(fd, buf, 0, this.BUFFER_SIZE, null); this.parse(buf); this.polling && setTimeout(arguments.callee, this.polling); }.bind(this))(); fs.close(fd); }.bind(this)); } this.parse = function(data){ if(data && data[8]){ this.emit(data[12] ? 'release' : 'press', data[10]); } }; this.abort = function(){ this.polling = false; }; }.bind(Button.prototype))(); module.exports = Button;