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