Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Here is an ugly hack that adds interrupt driven support for waiting for a card detect event. #67

Open
i12maroa opened this issue Jan 27, 2019 · 1 comment

Comments

@i12maroa
Copy link

i12maroa commented Jan 27, 2019

I tryed this but not working. Could you give me the full Read.py file?

Here is an ugly hack that adds interrupt driven support for waiting for a card detect event.

diff --git a/MFRC522.py b/MFRC522.py
index 6f157c2..735f036 100644
--- a/MFRC522.py
+++ b/MFRC522.py
@@ -4,7 +4,7 @@
 import RPi.GPIO as GPIO
 import spi
 import signal
-import time
+import threading
 
 class MFRC522:
   NRSTPD = 22
@@ -108,9 +108,12 @@ class MFRC522:
   serNum = []
 
   def __init__(self, dev='/dev/spidev0.0', spd=1000000):
+    self.irq = threading.Event()
     spi.openSPI(device=dev,speed=spd)
     GPIO.setmode(GPIO.BOARD)
     GPIO.setup(22, GPIO.OUT)
+    GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
+    GPIO.add_event_detect(18, GPIO.FALLING, callback=self.input_cb)
     GPIO.output(self.NRSTPD, 1)
     self.MFRC522_Init()
 
@@ -157,6 +160,8 @@ class MFRC522:
       irqEn = 0x77
       waitIRq = 0x30
 
     self.Write_MFRC522(self.CommIEnReg, irqEn|0x80)
     self.ClearBitMask(self.CommIrqReg, 0x80)
     self.SetBitMask(self.FIFOLevelReg, 0x80)
@@ -210,6 +215,26 @@ class MFRC522:
 
     return (status,backData,backLen)
 
+  def input_cb(self, pin):
+    #print "foobarzoot"
+    self.irq.set()
+    self.Write_MFRC522(self.CommIrqReg, 0x00)
+    self.Write_MFRC522(self.DivIrqReg, 0x00)
+
+  def MFRC522_WaitForCard(self):
+    # enable IRQ on detect
+    self.MFRC522_Init()
+    self.irq.clear()
+    self.Write_MFRC522(self.CommIrqReg, 0x00)
+    self.Write_MFRC522(self.DivIrqReg, 0x00)
+    self.Write_MFRC522(self.CommIEnReg, 0xA0)
+    # wait for it
+    while not self.irq.wait(0.2):
+      self.Write_MFRC522(self.FIFODataReg, self.PICC_REQIDL)
+      self.Write_MFRC522(self.CommandReg, self.PCD_TRANSCEIVE)
+      self.Write_MFRC522(self.BitFramingReg, 0x87)
+    self.irq.clear()
+    self.MFRC522_Init()
diff --git a/Read.py b/Read.py
index 964cd47..6efb202 100755
--- a/Read.py
+++ b/Read.py
@@ -4,6 +4,7 @@
 import RPi.GPIO as GPIO
 import MFRC522
 import signal
+import sys
 
 continue_reading = True
 
@@ -13,21 +14,25 @@ def end_read(signal,frame):
   print "Ctrl+C captured, ending read."
   continue_reading = False
   GPIO.cleanup()
+  sys.exit()
 
-# Hook the SIGINT
-signal.signal(signal.SIGINT, end_read)
+def main():
+  # Hook the SIGINT
+  signal.signal(signal.SIGINT, end_read)
 
-# Create an object of the class MFRC522
-MIFAREReader = MFRC522.MFRC522()
+  # Create an object of the class MFRC522
+  MIFAREReader = MFRC522.MFRC522()
 
-# Welcome message
-print "Welcome to the MFRC522 data read example"
-print "Press Ctrl-C to stop."
+  # Welcome message
+  print "Welcome to the MFRC522 data read example"
+  print "Press Ctrl-C to stop."
 
-# This loop keeps checking for chips. If one is near it will get the UID and authenticate
-while continue_reading:
+  # This loop keeps checking for chips. If one is near it will get the UID and authenticate
+  while continue_reading:
+    # Wait for card
+    MIFAREReader.MFRC522_WaitForCard()
 
-    # Scan for cards    
+    # Scan card
     (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
 
     # If a card is found
@@ -59,3 +64,5 @@ while continue_reading:
         else:
           print "Authentication error"
 
+if __name__ == "__main__":
+  main()

Originally posted by @LudwigKnuepfer in #17 (comment)

@LudwigKnuepfer
Copy link

I'm using this now and I'm happy with it: https://github.com/ondryaso/pi-rc522

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants