Skip to content

Commit 0293139

Browse files
author
sbrinkmann
committed
Initial commit
0 parents  commit 0293139

12 files changed

+1967
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build/
2+
.idea/

README.md

Whitespace-only changes.

binding.gyp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "rc522",
5+
"sources": [
6+
"src/rc522.c",
7+
"src/rfid.c",
8+
"src/accessor.cc"
9+
],
10+
"libraries": [
11+
"-lbcm2835"
12+
]
13+
}
14+
]
15+
}

main.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var logger = require('dachshund-logger').getLogger(__filename);
2+
var spawn = require('child_process').spawn;
3+
var readline = require('readline');
4+
var registeredCallback = null;
5+
6+
logger.info("RFID accessor initialization");
7+
8+
module.exports = exports = function(givenCallback){
9+
registeredCallback = givenCallback;
10+
};
11+
12+
var initChildProcess = function()
13+
{
14+
var child = spawn("node", [__dirname + "/" + "rc522_output.js"]);
15+
var linereader = readline.createInterface(child.stdout, child.stdin);
16+
17+
linereader.on('line', function (rfidTagSerialNumber) {
18+
if(registeredCallback instanceof Function)
19+
{
20+
registeredCallback(rfidTagSerialNumber);
21+
}
22+
});
23+
24+
child.on('close', function(code) {
25+
initChildProcess();
26+
});
27+
};
28+
29+
initChildProcess();

package.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"author": {
3+
"name": "Sascha Brinkmann",
4+
"email": "[email protected]"
5+
},
6+
"name": "rc522-rfid",
7+
"description": "Library to access a RC522 rfid reader",
8+
"keywords": [
9+
"rfid",
10+
"rc522"
11+
],
12+
"version": "14.1.0",
13+
"repository": {
14+
"type": "git",
15+
"url": "git://github.com/blackbeam/poppler-simple.git"
16+
},
17+
"scripts": {
18+
"install": "(node-gyp rebuild) || (exit 0)",
19+
"preinstall": "(node-gyp configure) || (exit 0)",
20+
"clean": "((node-gyp clean) && (rm -rf node_modules)) || (exit 0)",
21+
"build-debug": "(node-gyp configure --debug && node-gyp rebuild --debug) || (exit 0)"
22+
},
23+
"main": "./main",
24+
"engines": {
25+
"node": ">=0.8.0 <0.12.0"
26+
},
27+
"dependencies": {
28+
},
29+
"optionalDependencies": {},
30+
"gitHead": "",
31+
"bugs": {
32+
"url": "https://github.com/sbrinkmann/rc522-rfid/issues"
33+
},
34+
"homepage": "",
35+
36+
"_shasum": "",
37+
"_from": "[email protected]",
38+
"_npmVersion": "2.1.0",
39+
"_nodeVersion": "0.10.32",
40+
"directories": {},
41+
"readme": "ERROR: No README data found!"
42+
}

rc522_output.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var logger = require('dachshund-logger').getLogger(__filename);
2+
var rc522 = require('./build/Release/rc522');
3+
4+
rc522(function(rfidTagSerialNumber) {
5+
console.log(rfidTagSerialNumber);
6+
});

src/accessor.cc

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include <node.h>
2+
#include <v8.h>
3+
#include <unistd.h>
4+
#include "rfid.h"
5+
#include "rc522.h"
6+
#include "bcm2835.h"
7+
8+
#define DEFAULT_SPI_SPEED 5000L
9+
10+
uint8_t initRfidReader(void);
11+
12+
char statusRfidReader;
13+
uint16_t CType=0;
14+
uint8_t serialNumber[10];
15+
uint8_t serialNumberLength=0;
16+
uint8_t noTagFoundCount=0;
17+
char rfidChipSerialNumber[23];
18+
char rfidChipSerialNumberRecentlyDetected[23];
19+
char *p;
20+
int loopCounter;
21+
22+
using namespace v8;
23+
24+
Handle<Value> RunCallback(const Arguments& args) {
25+
HandleScope scope;
26+
27+
Local<Function> callback = Local<Function>::Cast(args[0]);
28+
const unsigned argc = 1;
29+
30+
InitRc522();
31+
32+
for (;;) {
33+
statusRfidReader = find_tag(&CType);
34+
if (statusRfidReader == TAG_NOTAG) {
35+
36+
// The status that no tag is found is sometimes set even when a tag is within reach of the tag reader
37+
// to prevent that the reset is performed the no tag event has to take place multiple times (ger: entrprellen)
38+
if (noTagFoundCount > 2) {
39+
// Sets the content of the array 'rfidChipSerialNumberRecentlyDetected' back to zero
40+
memset(&rfidChipSerialNumberRecentlyDetected[0], 0, sizeof(rfidChipSerialNumberRecentlyDetected));
41+
noTagFoundCount = 0;
42+
}
43+
else {
44+
noTagFoundCount++;
45+
}
46+
47+
usleep(200000);
48+
continue;
49+
} else if (statusRfidReader != TAG_OK && statusRfidReader != TAG_COLLISION) {
50+
continue;
51+
}
52+
53+
if (select_tag_sn(serialNumber,&serialNumberLength) != TAG_OK) {
54+
continue;
55+
}
56+
57+
// Is a successful detected, the counter will be set to zero
58+
noTagFoundCount = 0;
59+
60+
p=rfidChipSerialNumber;
61+
for (loopCounter = 0; loopCounter < serialNumberLength; loopCounter++) {
62+
sprintf(p,"%02x", serialNumber[loopCounter]);
63+
p+=2;
64+
}
65+
66+
// Only when the serial number of the currently detected tag differs from the
67+
// recently detected tag the callback will be executed with the serial number
68+
if(strcmp(rfidChipSerialNumberRecentlyDetected, rfidChipSerialNumber) != 0)
69+
{
70+
Local<Value> argv[argc] = { Local<Value>::New(String::New(&rfidChipSerialNumber[1])) };
71+
callback->Call(Context::GetCurrent()->Global(), argc, argv);
72+
}
73+
74+
// Preserves the current detected serial number, so that it can be used
75+
// for future evaluations
76+
strcpy(rfidChipSerialNumberRecentlyDetected, rfidChipSerialNumber);
77+
78+
*(p++)=0;
79+
}
80+
81+
bcm2835_spi_end();
82+
bcm2835_close();
83+
84+
return scope.Close(Undefined());
85+
}
86+
87+
void Init(Handle<Object> exports, Handle<Object> module) {
88+
initRfidReader();
89+
module->Set(String::NewSymbol("exports"), FunctionTemplate::New(RunCallback)->GetFunction());
90+
}
91+
92+
uint8_t initRfidReader(void) {
93+
uint16_t sp;
94+
95+
sp=(uint16_t)(250000L / DEFAULT_SPI_SPEED);
96+
if (!bcm2835_init()) {
97+
return 1;
98+
}
99+
100+
bcm2835_spi_begin();
101+
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
102+
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
103+
bcm2835_spi_setClockDivider(sp); // The default
104+
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
105+
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
106+
return 0;
107+
}
108+
109+
NODE_MODULE(rc522, Init)

0 commit comments

Comments
 (0)