From bfeab4969849bba3af654007d84e9f27ee9bca2c Mon Sep 17 00:00:00 2001 From: Sebastian Pozoga Date: Sun, 14 Jun 2015 18:59:06 +0200 Subject: [PATCH 1/4] correct code for new version v8 Nodejs (version 11, 12 and newest) use new v8. It contains come changes. The commit update code to new javascript engine v8 library. --- src/accessor.cc | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/accessor.cc b/src/accessor.cc index 17e4386..85050f5 100644 --- a/src/accessor.cc +++ b/src/accessor.cc @@ -21,8 +21,10 @@ int loopCounter; using namespace v8; -Handle RunCallback(const Arguments& args) { - HandleScope scope; +void RunCallback(const FunctionCallbackInfo& args) { + Isolate* isolate = Isolate::GetCurrent(); + HandleScope scope(isolate); + Local context = Context::New(isolate); Local callback = Local::Cast(args[0]); const unsigned argc = 1; @@ -67,8 +69,11 @@ Handle RunCallback(const Arguments& args) { // recently detected tag the callback will be executed with the serial number if(strcmp(rfidChipSerialNumberRecentlyDetected, rfidChipSerialNumber) != 0) { - Local argv[argc] = { Local::New(String::New(&rfidChipSerialNumber[1])) }; - callback->Call(Context::GetCurrent()->Global(), argc, argv); + Local argv[argc] = { + Local::New(isolate, String::NewFromUtf8(isolate, &rfidChipSerialNumber[1])) + }; + + callback->Call(context->Global(), argc, argv); } // Preserves the current detected serial number, so that it can be used @@ -80,13 +85,14 @@ Handle RunCallback(const Arguments& args) { bcm2835_spi_end(); bcm2835_close(); - - return scope.Close(Undefined()); } void Init(Handle exports, Handle module) { + Isolate* isolate = Isolate::GetCurrent(); initRfidReader(); - module->Set(String::NewSymbol("exports"), FunctionTemplate::New(RunCallback)->GetFunction()); + module->Set( + Symbol::For(isolate, String::NewFromUtf8(isolate, "exports")), + FunctionTemplate::New(isolate, RunCallback)->GetFunction() ); } uint8_t initRfidReader(void) { From 129e77ec2deb49f7bdceacef06b5e8955f4a914d Mon Sep 17 00:00:00 2001 From: Sebastian P Date: Thu, 18 Jun 2015 19:53:47 +0200 Subject: [PATCH 2/4] correct lose letter bug & correct v8 callback --- src/accessor.cc | 154 +++++++++++++++++++++++------------------------- 1 file changed, 74 insertions(+), 80 deletions(-) diff --git a/src/accessor.cc b/src/accessor.cc index 85050f5..ab9cb63 100644 --- a/src/accessor.cc +++ b/src/accessor.cc @@ -10,10 +10,10 @@ uint8_t initRfidReader(void); char statusRfidReader; -uint16_t CType=0; +uint16_t CType = 0; uint8_t serialNumber[10]; -uint8_t serialNumberLength=0; -uint8_t noTagFoundCount=0; +uint8_t serialNumberLength = 0; +uint8_t noTagFoundCount = 0; char rfidChipSerialNumber[23]; char rfidChipSerialNumberRecentlyDetected[23]; char *p; @@ -22,94 +22,88 @@ int loopCounter; using namespace v8; void RunCallback(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); - HandleScope scope(isolate); - Local context = Context::New(isolate); - - Local callback = Local::Cast(args[0]); - const unsigned argc = 1; - - InitRc522(); - - for (;;) { - statusRfidReader = find_tag(&CType); - if (statusRfidReader == TAG_NOTAG) { - - // The status that no tag is found is sometimes set even when a tag is within reach of the tag reader - // to prevent that the reset is performed the no tag event has to take place multiple times (ger: entrprellen) - if (noTagFoundCount > 2) { - // Sets the content of the array 'rfidChipSerialNumberRecentlyDetected' back to zero - memset(&rfidChipSerialNumberRecentlyDetected[0], 0, sizeof(rfidChipSerialNumberRecentlyDetected)); - noTagFoundCount = 0; - } - else { - noTagFoundCount++; - } - - usleep(200000); - continue; - } else if (statusRfidReader != TAG_OK && statusRfidReader != TAG_COLLISION) { - continue; - } - - if (select_tag_sn(serialNumber,&serialNumberLength) != TAG_OK) { - continue; - } - - // Is a successful detected, the counter will be set to zero - noTagFoundCount = 0; - - p=rfidChipSerialNumber; - for (loopCounter = 0; loopCounter < serialNumberLength; loopCounter++) { - sprintf(p,"%02x", serialNumber[loopCounter]); - p+=2; - } - - // Only when the serial number of the currently detected tag differs from the - // recently detected tag the callback will be executed with the serial number - if(strcmp(rfidChipSerialNumberRecentlyDetected, rfidChipSerialNumber) != 0) - { - Local argv[argc] = { - Local::New(isolate, String::NewFromUtf8(isolate, &rfidChipSerialNumber[1])) + Isolate* isolate = Isolate::GetCurrent(); + HandleScope scope(isolate); + + Local callback = Local::Cast(args[0]); + const unsigned argc = 1; + + InitRc522(); + + for (;;) { + statusRfidReader = find_tag(&CType); + if (statusRfidReader == TAG_NOTAG) { + + // The status that no tag is found is sometimes set even when a tag is within reach of the tag reader + // to prevent that the reset is performed the no tag event has to take place multiple times (ger: entrprellen) + if (noTagFoundCount > 2) { + // Sets the content of the array 'rfidChipSerialNumberRecentlyDetected' back to zero + memset(&rfidChipSerialNumberRecentlyDetected[0], 0, sizeof (rfidChipSerialNumberRecentlyDetected)); + noTagFoundCount = 0; + } else { + noTagFoundCount++; + } + + usleep(200000); + continue; + } else if (statusRfidReader != TAG_OK && statusRfidReader != TAG_COLLISION) { + continue; + } + + if (select_tag_sn(serialNumber, &serialNumberLength) != TAG_OK) { + continue; + } + + // Is a successful detected, the counter will be set to zero + noTagFoundCount = 0; + + p = rfidChipSerialNumber; + for (loopCounter = 0; loopCounter < serialNumberLength; loopCounter++) { + sprintf(p, "%02x", serialNumber[loopCounter]); + p += 2; + } + + // Only when the serial number of the currently detected tag differs from the + // recently detected tag the callback will be executed with the serial number + if (strcmp(rfidChipSerialNumberRecentlyDetected, rfidChipSerialNumber) != 0) { + Local argv[argc] = { + String::NewFromUtf8(isolate, &rfidChipSerialNumber[1]) }; - - callback->Call(context->Global(), argc, argv); - } - // Preserves the current detected serial number, so that it can be used - // for future evaluations + callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); + } + + // Preserves the current detected serial number, so that it can be used + // for future evaluations strcpy(rfidChipSerialNumberRecentlyDetected, rfidChipSerialNumber); - *(p++)=0; - } + *(p++) = 0; + } - bcm2835_spi_end(); - bcm2835_close(); + bcm2835_spi_end(); + bcm2835_close(); } void Init(Handle exports, Handle module) { - Isolate* isolate = Isolate::GetCurrent(); - initRfidReader(); - module->Set( - Symbol::For(isolate, String::NewFromUtf8(isolate, "exports")), - FunctionTemplate::New(isolate, RunCallback)->GetFunction() ); + initRfidReader(); + NODE_SET_METHOD(module, "exports", RunCallback); } uint8_t initRfidReader(void) { - uint16_t sp; - - sp=(uint16_t)(250000L / DEFAULT_SPI_SPEED); - if (!bcm2835_init()) { - return 1; - } - - bcm2835_spi_begin(); - bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default - bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default - bcm2835_spi_setClockDivider(sp); // The default - bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default - bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default - return 0; + uint16_t sp; + + sp = (uint16_t) (250000L / DEFAULT_SPI_SPEED); + if (!bcm2835_init()) { + return 1; + } + + bcm2835_spi_begin(); + bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default + bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default + bcm2835_spi_setClockDivider(sp); // The default + bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default + bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default + return 0; } NODE_MODULE(rc522, Init) \ No newline at end of file From bc8850701ecb483a7d2a59d5af2ad4f587849360 Mon Sep 17 00:00:00 2001 From: Sebastian P Date: Thu, 18 Jun 2015 20:18:39 +0200 Subject: [PATCH 3/4] correct omnit letter --- src/accessor.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/accessor.cc b/src/accessor.cc index ab9cb63..27ed3e0 100644 --- a/src/accessor.cc +++ b/src/accessor.cc @@ -67,7 +67,7 @@ void RunCallback(const FunctionCallbackInfo& args) { // recently detected tag the callback will be executed with the serial number if (strcmp(rfidChipSerialNumberRecentlyDetected, rfidChipSerialNumber) != 0) { Local argv[argc] = { - String::NewFromUtf8(isolate, &rfidChipSerialNumber[1]) + String::NewFromUtf8(isolate, &rfidChipSerialNumber[0]) }; callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); From d37a5a3b03e66fa6a0fcc3f39b93aeb57a8faaee Mon Sep 17 00:00:00 2001 From: Sebastian Pozoga Date: Tue, 1 Mar 2016 09:46:00 +0100 Subject: [PATCH 4/4] Update README.md --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c97dd6f..51c4709 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,14 @@ #rc522-rfid node.js module to access a rfid reader with rc522 chipset which is connected a raspberry pi +## Warning +The library work with RPI v1 only. It is based on bcm2835 module. The RIPv2 has different addresses. +``` +On RPI 2, the peripheral addresses are different and the bcm2835 library gets them from reading /proc/device-tree/soc/ranges. This is only availble with recent versions of the kernel on RPI 2. +``` +Source: http://www.airspayce.com/mikem/bcm2835/ +Issue: https://github.com/sbrinkmann/rc522-rfid/issues/2 + ## Purpose This node module is to access RFID reader with a rc522 chipset (e.g. http://amzn.com/B00GYR1KJ8) via GPIO interface of the raspberry pi. @@ -33,4 +41,4 @@ var rc522 = require("rc522-rfid"); rc522(function(rfidSerialNumber){ console.log(rfidSerialNumber); }); -``` \ No newline at end of file +```