forked from alba-ado/nfc-pcsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.js
62 lines (44 loc) · 1.43 KB
/
led.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
"use strict";
// #############
// Example: Controlling LED and buzzer on ACR122U
// - what is covered:
// - custom led blinks
// - custom buzzer output
// - repeated beeping on unsuccessful read/write operation
// - TODO:
// - document how to allow escape commands (direct communication without card)
// - meanwhile please see https://github.com/pokusew/nfc-pcsc/issues/13
// #############
import { NFC, CONNECT_MODE_DIRECT } from '../src/index';
import pretty from './pretty-logger';
const nfc = new NFC(pretty); // const nfc = new NFC(pretty); // optionally you can pass logger to see internal debug logs
nfc.on('reader', async reader => {
pretty.info(`device attached`, reader);
try {
await reader.connect(CONNECT_MODE_DIRECT);
await reader.setBuzzerOutput(false);
await reader.disconnect();
} catch (err) {
pretty.info(`initial sequence error`, reader, err);
}
reader.on('card', async card => {
pretty.info(`card detected`, reader, card);
try {
// red error
await reader.led(0b01011101, [0x02, 0x01, 0x05, 0x01]);
// green success
await reader.led(0b00101110, [0x01, 0x00, 0x01, 0x01]);
} catch (err) {
pretty.error(`error when writing led`, reader, err);
}
});
reader.on('error', err => {
pretty.error(`an error occurred`, reader, err);
});
reader.on('end', () => {
pretty.info(`device removed`, reader);
});
});
nfc.on('error', err => {
pretty.error(`an error occurred`, err);
});