-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerMate.js
92 lines (84 loc) · 2.47 KB
/
PowerMate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var fs = require('fs'),
glob = require("glob"),
EventEmitter = require('events').EventEmitter;
// Expose as a nice JavaScript API
function PowerMate() {
if (process.arch.match('64')) {
this.offset = 0;
this.arch = 64;
} else {
this.offset = 8;
this.arch = 32;
}
this.wrap("onOpen");
this.wrap("onRead");
this.position = 0;
this.buffer = new Buffer(this.arch);
self = this;
glob("/dev/input/by-id/*PowerMate*", function (err, files) {
if (err) return self.emit("error", err);
if (!files[0]) return self.emit("error", 'PowerMate not found');
self.path = files[0];
fs.open(self.path, "r", self.onOpen);
});
}
PowerMate.prototype = Object.create(EventEmitter.prototype, {
constructor: {value: PowerMate}
});
// Register a bound version of a method and route errors
PowerMate.prototype.wrap = function (name) {
var self = this;
var fn = this[name];
this[name] = function (err) {
if (err) return self.emit("error", err);
return fn.apply(self, Array.prototype.slice.call(arguments, 1));
};
};
PowerMate.prototype.onOpen = function (fd) {
this.fd = fd;
this.startRead();
};
PowerMate.prototype.startRead = function () {
fs.read(this.fd, this.buffer, 0, this.arch, null, this.onRead);
};
PowerMate.prototype.onRead = function (bytesRead) {
var data = {
axis: this.buffer.readUInt16LE(16 - this.offset),
action: this.buffer.readUInt16LE(20 - this.offset),
};
var self = this;
if (data.axis == 1) {
if (data.action == 1) { //down
self.position = 1;
self.down = setTimeout(function() {
self.emit('longPress');
}, 1000);
} else if (data.action == 0) { //up
self.position = 0;
if (self.down._idleNext) self.emit('press');
clearTimeout(self.down);
}
} if (data.axis == 2) {
if (data.action == 1) { //right
clearTimeout(self.down);
if (self.position) {
self.emit('downRight');
} else {
self.emit('right');
}
} else if (data.action > 1) { //left
clearTimeout(self.down);
if (self.position) {
self.emit('downLeft');
} else {
self.emit('left');
}
}
}
if (this.fd) this.startRead();
};
PowerMate.prototype.close = function (callback) {
fs.close(this.fd, callback);
this.fd = undefined;
};
module.exports = PowerMate;