-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
72 lines (49 loc) · 1023 Bytes
/
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
const emotiv = require('bindings')('emotiv');
let _open = false;
let running = false;
let _cb = () => { };
function setLogger(logger) {
emotiv.setLogger(logger);
}
function open() {
if (_open) return;
let rv = 1;
let retries = 0;
do {
if (retries == 10) break;
rv = emotiv.connect();
retries++;
} while (rv)
if (rv == 0) {
_open = true;
running = true;
}
return rv == 0;
}
function close() {
if (!_open) return;
emotiv.disconnect();
_open = false;
running = false;
}
function read(cb) {
_cb = cb;
if (!_open) return;
emotiv.read(_work)
}
function pause() {
if (!_open) return;
running = false;
}
function resume() {
if (!_open) return;
running = true;
emotiv.read(_work);
}
function _work(err, res) {
if (!running) return;
_cb(res);
emotiv.read(_work);
}
setLogger(msg => console.log(msg.trim()));
module.exports = { open, close, pause, read, resume, setLogger };