diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/motion.js | 52 | 
1 files changed, 30 insertions, 22 deletions
| diff --git a/lib/motion.js b/lib/motion.js index 781d54b..3e24962 100644 --- a/lib/motion.js +++ b/lib/motion.js @@ -1,18 +1,19 @@  const util = require("util")      , EventEmitter = require("events").EventEmitter; -const longTime = 600; +//const slotToPoint = s => ({xi:s.xi, yi:s.yi, xf:s.x, yf:s.y}); +const slotToPoint = s => ({ +  i:{x:s.xi, y:s.yi}, f:{x:s.x, y:s.y}, +  m:{x:(s.xi+s.x)/2, y:(s.yi+s.y)/2} +}); -//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) { +function Motion(device, longTime) {    this.device = device; -  this.slotsUsed = 0; +  this.slotsActive = 0;    this.slots = [];    this.points = [];    this.longed = false; +  this.longTime = longTime || 600;    device.on("EV_ABS", e => this.doABS(e));    device.on("EV_SYN", e => this.doSYN(e)); @@ -44,13 +45,16 @@ switch (e.code) {      break;    case "ABS_MT_TRACKING_ID": -    if (!this.currentSlot) return; +    if (this.currentSlot === undefined) { +      this.addSlot(); +      this.currentSlot = this.slots[0]; +    } +      if (e.value >= 0) {        this.currentSlot.id = e.value; -      if (this.slotsUsed++ === 0) { -        //this.start = e.time; +      if (this.slotsActive++ === 0) {          this.longTimeout = setTimeout(() => -          this.emitLong(), longTime); +          this.emitLong(), this.longTime);        }      } else { @@ -61,26 +65,25 @@ switch (e.code) {        this.currentSlot.yi = -1;        this.currentSlot.x = 0;        this.currentSlot.y = 0; -      if (--this.slotsUsed === 0) { -        //this.end = e.time; +      if (--this.slotsActive === 0) {          clearTimeout(this.longTimeout);        }      } break;    case "ABS_MT_POSITION_X": -    if (!this.currentSlot) return; +    if (this.currentSlot === undefined) return;      if (this.currentSlot.id < 0) return; -    this.currentSlot.x = e.value;      if (this.currentSlot.xi < 0)        this.currentSlot.xi = e.value; +    this.currentSlot.x = e.value;      break;    case "ABS_MT_POSITION_Y": -    if (!this.currentSlot) return; +    if (this.currentSlot === undefined) return;      if (this.currentSlot.id < 0) return; -    this.currentSlot.y = e.value;      if (this.currentSlot.yi < 0)        this.currentSlot.yi = e.value; +    this.currentSlot.y = e.value;      break;    //default: @@ -88,11 +91,16 @@ switch (e.code) {  }};  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 = []; +  if (e.code === "SYN_REPORT") { +    //console.log("SYN", this.slotsActive, this.points.length); +    if (this.slotsActive <= 0) { +      this.slotsActive = 0; + +      if (this.longed) { this.longed = false; +      } else if (this.points.length > 0) { +        this.emit("short", this.points); +        this.points = []; +      }      }    }  }; | 
