-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
59 lines (45 loc) · 1.12 KB
/
index.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
const EventEmitter = require('events').EventEmitter;
const Gpio = require('onoff').Gpio;
/**
* Creates a new Rotary Encoder using two GPIO pins
* Expects the pins to be configured as pull-up
*
* @param pinA GPIO # of the first pin
* @param pinB GPIO # of the second pin
*
* @returns EventEmitter
*/
function RotaryEncoder(pinA, pinB) {
this.gpioA = new Gpio(pinA, 'in', 'both');
this.gpioB = new Gpio(pinB, 'in', 'both');
this.a = 2;
this.b = 2;
this.gpioA.watch((err, value) => {
if (err) {
this.emit('error', err);
return;
}
this.a = value;
});
this.gpioB.watch((err, value) => {
if (err) {
this.emit('error', err);
return;
}
this.b = value;
this.tick();
});
}
RotaryEncoder.prototype = EventEmitter.prototype;
RotaryEncoder.prototype.tick = function tick() {
const { a, b } = this;
if (a === 0 && b === 0 || a === 1 && b === 1) {
this.emit('rotation', 1);
} else if (a === 1 && b === 0 || a === 0 && b === 1 || a === 2 && b === 0) {
this.emit('rotation', -1);
}
return this;
};
module.exports = function rotaryEncoder(pinA, pinB) {
return new RotaryEncoder(pinA, pinB);
};