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 +``` diff --git a/src/accessor.cc b/src/accessor.cc index 17e4386..27ed3e0 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; @@ -21,89 +21,89 @@ int loopCounter; using namespace v8; -Handle RunCallback(const Arguments& args) { - HandleScope scope; - - 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(String::New(&rfidChipSerialNumber[1])) }; - callback->Call(Context::GetCurrent()->Global(), argc, argv); - } - - // Preserves the current detected serial number, so that it can be used - // for future evaluations +void RunCallback(const FunctionCallbackInfo& args) { + 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[0]) + }; + + 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(); - - return scope.Close(Undefined()); + bcm2835_spi_end(); + bcm2835_close(); } void Init(Handle exports, Handle module) { - initRfidReader(); - module->Set(String::NewSymbol("exports"), FunctionTemplate::New(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