Skip to content

Commit

Permalink
Improve examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pokusew committed Dec 28, 2018
1 parent 02d0ef4 commit ab2938a
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 267 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ When a NFC tag (card) is attached to the reader, the following is done:

> ### Running examples locally
> If you want see it in action, clone this repository, install dependencies with npm and run `npm run example`.
> Of course, instead of npm you can Yarn if you want.
> See scripts section of [package.json](/package.json) for all available examples run commands.
> ```bash
> git clone https://github.com/pokusew/nfc-pcsc.git
> npm install
Expand Down Expand Up @@ -260,10 +262,12 @@ reader.on('card', async card => {
📦📦📦 You can find more examples in [examples folder](/examples), including:
* [index.js](/examples/index.js) – detecting, authenticating, reading and writing cards (including instructions for Mifare Classic)
* [led.js](/examples/led.js) – controlling LED and buzzer of ACR122U look
* [desfire.js](/examples/desfire.js) – accessing and authenticating Mifare DESFire cards
* [uid-logger.js](/examples/uid-logger.js)
* [read-write.js](/examples/read-write.js) – detecting, reading and writing cards
* [mifare-classic.js](/examples/mifare-classic.js) – authenticating, reading and writing Mifare Classic cards
* [mifare-desfire.js](/examples/mifare-desfire.js) – authenticating and accessing data on Mifare DESFire cards
* [basic.js](/examples/basic.js) – reader events explanation
* [led.js](/examples/led.js) – controlling LED and buzzer of ACR122U reader
* [uid-logger.js](/examples/uid-logger.js) – logs uid when a card is detected
Feel free to open pull request, if you have any useful example, that you'd like to add.
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

// #############
// Basic usage
// Example: Basic usage
// - see "Basic usage" section in README for an explanation
// #############

Expand Down
181 changes: 0 additions & 181 deletions examples/index.js

This file was deleted.

64 changes: 18 additions & 46 deletions examples/led.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
"use strict";

// #############
// ACR122U example controlling LED and buzzer
// - custom buzzer output
// - repeated beeping on unsuccessful read/write operation
// 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)
// #############

import { NFC, TAG_ISO_14443_3, TAG_ISO_14443_4, KEY_TYPE_A, KEY_TYPE_B, CONNECT_MODE_DIRECT } from '../src/index';
import pretty from './pretty';
import pretty from './pretty-logger';


// minilogger for debugging
const nfc = new NFC(pretty); // const nfc = new NFC(pretty); // optionally you can pass logger to see internal debug logs

function log() {
console.log(...arguments);
}

const minilogger = {
log: log,
debug: log,
info: log,
warn: log,
error: log,
};

const nfc = new NFC(minilogger); // const nfc = new NFC(minilogger); // optionally you can pass logger to see internal debug logs

let readers = [];

nfc.on('reader', async reader => {

pretty.info(`device attached`, { reader: reader.name });

readers.push(reader);


// needed for reading tags emulated with Android HCE AID
// see https://developer.android.com/guide/topics/connectivity/nfc/hce.html
reader.aid = 'F222222222';

console.log();

try {
await reader.connect(CONNECT_MODE_DIRECT);
await reader.setBuzzerOutput(false);
Expand Down Expand Up @@ -106,14 +90,14 @@ nfc.on('reader', async reader => {

const data = await reader.read(4, 16);

pretty.info(`data read`, { reader: reader.name, card, data });
pretty.info(`data read`, reader, data);

const payload = data.readInt16BE();
const payload = data.readInt16BE(0);

pretty.info(`data converted`, payload);

} catch (err) {
pretty.error(`error when reading data`, { reader: reader.name, card, err });
pretty.error(`error when reading data`, reader, err);
await reader.led(0b01011101, [0x02, 0x01, 0x05, 0x01]);
return;
}
Expand All @@ -128,51 +112,39 @@ nfc.on('reader', async reader => {
// ! Caution! data.length must be divisible by blockSize

const data = Buffer.allocUnsafe(16);
data.writeInt16BE(800);
data.writeInt16BE(800, 0);

await reader.write(4, data);

pretty.info(`data written`, { reader: reader.name, card });
pretty.info(`data written`, reader, data);

} catch (err) {
pretty.error(`error when writing data`, { reader: reader.name, card, err });
pretty.error(`error when writing data`, reader, err);
await reader.led(0b01011101, [0x02, 0x01, 0x05, 0x01]);
return;
}


try {

await reader.led(0b00101110, [0x01, 0x00, 0x01, 0x01]);

} catch (err) {
pretty.error(`error when writing led`);
pretty.error(`error when writing led`, err);
}


});

reader.on('error', err => {

pretty.error(`an error occurred`, { reader: reader.name, err });
pretty.error(`an error occurred`, reader, err);

});

reader.on('end', () => {

pretty.info(`device removed`, { reader: reader.name });

delete readers[readers.indexOf(reader)];

console.log(readers);

pretty.info(`device removed`, reader);
});


});

nfc.on('error', err => {

pretty.error(`an error occurred`, err);

});
Loading

0 comments on commit ab2938a

Please sign in to comment.