forked from espruino/EspruinoDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyPad.js
68 lines (62 loc) · 1.83 KB
/
KeyPad.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
/* Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. */
/*
Module for connecting to a simple matrix keypad. Supply two arrays, one of wires connected to columns,
one of wires connected to rows.
If a third argument (a callback function) is supplied, watches will be set up, and the callback
will be called automatically as soon as a button is pressed. If it isn't, it's up to the user to
use ```keypad.read()``` to find out what key is pressed. -1 will be returned if no key is pressed.
```
require("KeyPad").connect([B2,B3,B4,B5],[B6,B7,B8,B9], function(e) {
print("123A456B789C*0#D"[e]);
});
```
or
```
var keypad = require("KeyPad").connect([B2,B3,B4,B5],[B6,B7,B8,B9]);
print("123A456B789C*0#D"[keypad.read()]);
```
*/
exports.connect = function (columns, rows, callback) {
var watches = [];
var lastState = 0;
var readState = function() {
var press = -1;
for (var i in rows) {
digitalWrite(rows, 1 << i);
var v = digitalRead(columns);
for (var j in columns)
if (v & (1<<j))
press = j+i*columns.length;
}
// reset state
digitalWrite(rows, 0xFFFFFFFF);
return press;
};
var setup = function() {
for (var i in columns) pinMode(columns[i], "input_pulldown");
digitalWrite(rows, 0xFFFFFFFF);
};
var onWatch = function() {
var s = digitalRead(columns);
if (s!=lastState) {
lastState = s;
removeWatches();
var c = readState();
addWatches();
if (c>=0) callback(c);
}
};
var addWatches = function() {
for (var i in columns)
watches[i] = setWatch(onWatch, columns[i], { repeat:true, edge:"both" });
};
var removeWatches = function() {
for (var i in watches)
clearWatch(watches[i]);
};
setup();
if (callback!==undefined) addWatches();
return {
read: readState
};
};