-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (80 loc) · 2.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
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
93
94
95
96
97
98
99
100
101
const EventEmitter = require('events').EventEmitter;
const Gpio = require('onoff').Gpio;
/**
* Creates a new Nodary 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 NodaryEncoder(pinA, pinB) {
this.gpioA = new Gpio(pinA, 'in', 'both');
this.gpioB = new Gpio(pinB, 'in', 'both');
this.a = 2;
this.b = 2;
this.value = 0;
this.state = "00";
this.direction = null;
this.gpioA.watch((err, value) => {
if (err) {
this.emit('error', err);
return;
}
this.a = value;
this.tick();
});
this.gpioB.watch((err, value) => {
if (err) {
this.emit('error', err);
return;
}
this.b = value;
this.tick();
});
}
NodaryEncoder.prototype = EventEmitter.prototype;
NodaryEncoder.prototype.tick = function tick() {
const { a, b } = this;
var newState = `S${a}${b}`;
if (this.state == "S00"){ // Resting position
if (newState == "S01") // Turned right 1
this.direction = "R"
else if (newState == "S10") // Turned left 1
this.direction = "L"
}else if (this.state == "S01"){ // R1 or L3 position
if (newState == "S11"){ // Turned right 1
this.direction = "R"
}else if (newState == "S00"){ // Turned left 1
if (this.direction == "L"){
this.value --
}
}
}else if (this.state == "S10"){ // R3 or L1
if (newState == "S11"){ // Turned left 1
this.direction = "L"
}else if (newState == "S00"){ // Turned right 1
if (this.direction == "R"){
this.value ++
}
}
}else{ // this.state == "11"
if (newState == "S01") // Turned left 1
this.direction = "L"
else if (newState == "S10") // Turned right 1
this.direction = "R"
else if (newState == "S00"){ // Skipped an intermediate 01 or 10 state, but if we know direction then a turn is complete
if (this.direction == "L")
this.value --
else if (this.direction == "R")
this.value ++
}
}
this.state = newState
this.emit('rotation', this.direction, this.value)
return this;
};
module.exports = function nodaryEncoder(pinA, pinB) {
return new NodaryEncoder(pinA, pinB);
};