aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Hovorka <[email protected]>2020-07-21 21:49:37 -0600
committerAdam Hovorka <[email protected]>2020-07-21 21:49:37 -0600
commite97ee92acc19b30c8e3c044049ea5951d8c45d63 (patch)
treec2b6624aa7db7afea5af8d30f6c19d9ef2128a0d
parent72713e0dfba06e4b3d91d500c60f34695c9715c1 (diff)
Chunk events into discrete sets of start/end points
-rwxr-xr-xindex.js14
-rw-r--r--lib/device.js (renamed from lib/index.js)0
-rw-r--r--lib/motion.js100
3 files changed, 107 insertions, 7 deletions
diff --git a/index.js b/index.js
index 0ff053e..72c8f66 100755
--- a/index.js
+++ b/index.js
@@ -1,13 +1,13 @@
#!/usr/bin/env node
-const Device = require("./lib");
+const Device = require("./lib/device");
+const Motion = require("./lib/motion");
const path = "/dev/input/"+process.argv[2];
console.log("Opening %s", path);
const device = new Device(path);
-device.on("error", e => console.error("Reader error:", e))
- .on("open", () => { console.log(device.id); device.grab(); })
- .on("EV_KEY", e => console.log(/*e.time,*/ "KEY", e.code, e.value))
- .on("EV_ABS", e => console.log(/*e.time,*/ "ABS", e.code, e.value))
- .on("EV_REL", e => console.log(/*e.time,*/ "REN", e.code, e.value))
- .on("EV_SYN", e => console.log(/*e.time,*/ "SYN", e.code, e.value))
+const motion = new Motion(device);
+device.on("open", () => { console.log(device.id); device.grab(); });
+motion.on("error", console.error)
+ .on("short", e => console.log("short", e))
+ .on("long", e => console.log("long", e));
diff --git a/lib/index.js b/lib/device.js
index a9fb71d..a9fb71d 100644
--- a/lib/index.js
+++ b/lib/device.js
diff --git a/lib/motion.js b/lib/motion.js
new file mode 100644
index 0000000..781d54b
--- /dev/null
+++ b/lib/motion.js
@@ -0,0 +1,100 @@
+const util = require("util")
+ , EventEmitter = require("events").EventEmitter;
+
+const longTime = 600;
+
+//const clone = x => 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<e.value; i++) this.addSlot();
+ this.currentSlot = this.slots[e.value];
+ break;
+
+ case "ABS_MT_TRACKING_ID":
+ if (!this.currentSlot) return;
+ if (e.value >= 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;