forked from espruino/EspruinoDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncoder.js
42 lines (38 loc) · 1.16 KB
/
Encoder.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
/* Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. */
/*
Module for connecting to Rotary encoder.
```
var step = 0;
require("Encoder").connect(A1,A2,function (direction) {
step += direction;
print(step);
});
```
*/
function Encoder(/*=PIN*/pina, /*=PIN*/pinb, callback) {
this.PINA = pina;
this.PINB = pinb;
this.callback = callback;
var encoder = this;
var onChange = function() {
var a = digitalRead(encoder.PINA);
var b = digitalRead(encoder.PINB);
var s = 0;
switch (this.last) {
case 0b00 : if (a) s++; if (b) s--; break;
case 0b01 : if (!a) s--; if (b) s++; break;
case 0b10 : if (a) s--; if (!b) s++; break;
case 0b11 : if (!a) s++; if (!b) s--; break;
}
this.last = a | (b<<1);
if (s!==0) callback(s);
};
pinMode(this.PINA, "input_pulldown");
pinMode(this.PINB, "input_pulldown");
onChange(); // initialize the state of a, b and state: no callback will occur
setWatch(onChange, this.PINA, { repeat: true });
setWatch(onChange, this.PINB, { repeat: true });
}
exports.connect = function(pina, pinb, callback) {
return new Encoder(pina, pinb, callback);
};