From e97ee92acc19b30c8e3c044049ea5951d8c45d63 Mon Sep 17 00:00:00 2001 From: Adam Hovorka Date: Tue, 21 Jul 2020 21:49:37 -0600 Subject: Chunk events into discrete sets of start/end points --- lib/device.js | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/index.js | 87 -------------------------------------------------- lib/motion.js | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+), 87 deletions(-) create mode 100644 lib/device.js delete mode 100644 lib/index.js create mode 100644 lib/motion.js (limited to 'lib') diff --git a/lib/device.js b/lib/device.js new file mode 100644 index 0000000..a9fb71d --- /dev/null +++ b/lib/device.js @@ -0,0 +1,87 @@ +const fs = require("fs") + , util = require("util") + , EventEmitter = require("events").EventEmitter + , {eviocgrab, eviocgid} = require("bindings")("ioctl.node") + , events = require("./events"); + +const arch = (process.arch.indexOf("64")>=0)?64:32; + +function Device(path) { + this.grabbed = false; + if (path) this.open(path); +} + +util.inherits(Device, EventEmitter); + +Device.prototype.open = function(path) { + this.stream = fs + .createReadStream(path, {flags: "r", autoClose: true}) + .on("error", e => this.emit("error", e)) + .on("open", fd => { this.fd = fd; + try { this.id = eviocgid(fd); } + catch(e) { this.emit("error", + new Error("Reader eviocgid error:"+e)); } + if (this.grabbed) eviocgrab(this.fd, 1); + this.emit("open", fd); }) + .on("data", buf => { + var i, j, chunk = (arch===64)?24:16; + for (i=0, j=buf.length; i=0)?64:32; - -function Device(path) { - this.grabbed = false; - if (path) this.open(path); -} - -util.inherits(Device, EventEmitter); - -Device.prototype.open = function(path) { - this.stream = fs - .createReadStream(path, {flags: "r", autoClose: true}) - .on("error", e => this.emit("error", e)) - .on("open", fd => { this.fd = fd; - try { this.id = eviocgid(fd); } - catch(e) { this.emit("error", - new Error("Reader eviocgid error:"+e)); } - if (this.grabbed) eviocgrab(this.fd, 1); - this.emit("open", fd); }) - .on("data", buf => { - var i, j, chunk = (arch===64)?24:16; - for (i=0, j=buf.length; i JSON.parse(JSON.stringify(x)); // Deep +const clone = x => Object.assign({}, x); // Shallow +const slotToPoint = s => ({xi:s.xi, yi:s.yi, xf:s.x, yf:s.y}); + +function Motion(device) { + this.device = device; + this.slotsUsed = 0; + this.slots = []; + this.points = []; + this.longed = false; + + device.on("EV_ABS", e => this.doABS(e)); + device.on("EV_SYN", e => this.doSYN(e)); + device.on("error", e => this.emit("error", "Device Error: "+e)); + this.addSlot(); +} + +util.inherits(Motion, EventEmitter); + +Motion.prototype.addSlot = function() { + this.slots.push({id: -1, xi: -1, yi: -1, x: 0, y: 0}); +}; + +Motion.prototype.emitLong = function() { + this.slots.forEach(s => { if (s.id >= 0) + this.points.push(slotToPoint(s)); }); + this.emit("long", this.points); + this.points = []; + this.longed = true; +}; + +Motion.prototype.doABS = function(e) { +switch (e.code) { + + case "ABS_MT_SLOT": + for (let i=this.slots.length-1; + i= 0) { + this.currentSlot.id = e.value; + if (this.slotsUsed++ === 0) { + //this.start = e.time; + this.longTimeout = setTimeout(() => + this.emitLong(), longTime); + } + + } else { + if (!this.longed && this.currentSlot.xi >= 0) + this.points.push(slotToPoint(this.currentSlot)); + this.currentSlot.id = -1; + this.currentSlot.xi = -1; + this.currentSlot.yi = -1; + this.currentSlot.x = 0; + this.currentSlot.y = 0; + if (--this.slotsUsed === 0) { + //this.end = e.time; + clearTimeout(this.longTimeout); + } + } break; + + case "ABS_MT_POSITION_X": + if (!this.currentSlot) return; + if (this.currentSlot.id < 0) return; + this.currentSlot.x = e.value; + if (this.currentSlot.xi < 0) + this.currentSlot.xi = e.value; + break; + + case "ABS_MT_POSITION_Y": + if (!this.currentSlot) return; + if (this.currentSlot.id < 0) return; + this.currentSlot.y = e.value; + if (this.currentSlot.yi < 0) + this.currentSlot.yi = e.value; + break; + + //default: + // console.log(e.code, e.value); +}}; + +Motion.prototype.doSYN = function(e) { + if (e.code === "SYN_REPORT" && this.slotsUsed === 0) { + if (this.longed) { this.longed = false; + } else if (this.points.length > 0) { + this.emit("short", this.points); + this.points = []; + } + } +}; + +module.exports = Motion; -- cgit v1.2.3-70-g09d2