Skip to content

Commit

Permalink
Fixes for rfid reader (MiczFlor#2250)
Browse files Browse the repository at this point in the history
* fix wait_for_tag for both modes

* fix function names

* move rc522 settings init

* fix reader readCard indirection (MiczFlor#551)

access readCard function of Reader class itself.
readCard added for "experimental"

* moved rc522 reader initialization

* updated rc522 implementation in "experimental.multi"

* highlight default setting
  • Loading branch information
AlvinSchiller authored Feb 10, 2024
1 parent 2ceef1b commit 5dca35f
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 33 deletions.
2 changes: 1 addition & 1 deletion components/rfid-reader/RC522/setup_rc522.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ printf "Please make sure that the RC522 reader is wired up correctly to the GPIO
question "Continue"

printf "Use backward-compatible card ID (not suggested for new installations)?\n"
read -p "(y/n) " choice
read -p "(y/N) " choice
case "$choice" in
y|Y ) printf "OFF" > "${JUKEBOX_HOME_DIR}"/settings/Rfidreader_Rc522_Readmode_UID;;
* ) printf "ON" > "${JUKEBOX_HOME_DIR}"/settings/Rfidreader_Rc522_Readmode_UID;;
Expand Down
2 changes: 0 additions & 2 deletions scripts/Reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ def get_devices():


class Reader:
reader = None

def __init__(self):
self.reader = self
path = os.path.dirname(os.path.realpath(__file__))
self.keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
if not os.path.isfile(path + '/deviceName.txt'):
Expand Down
2 changes: 0 additions & 2 deletions scripts/Reader.py.Multi
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ def get_devices():


class Reader:
reader = None

def __init__(self):
self.reader = self
devs = list()
path = os.path.dirname(os.path.realpath(__file__))
self.keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
Expand Down
30 changes: 18 additions & 12 deletions scripts/Reader.py.experimental
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ class UsbReader(object):


class Mfrc522Reader(object):
def __init__(self, readmode_uid=False):
def __init__(self):
import pirc522
self.device = pirc522.RFID()
self.readmode_uid = readmode_uid
self.readCard = self.readCard_normal if self.readmode_uid else self.readCard_legacy
path = os.path.dirname(os.path.realpath(__file__))
readmode_uid = False
if os.path.isfile(path + '/../settings/Rfidreader_Rc522_Readmode_UID'):
with open(path + '/../settings/Rfidreader_Rc522_Readmode_UID', 'r') as f:
readmode_uid = f.read().rstrip().split(';', 1)[0] == 'ON'
self._read_function = self._readCard_normal if readmode_uid else self._readCard_legacy

def readCard_legacy(self):
def _readCard_legacy(self):
# Scan for cards
self.device.wait_for_tag()
(error, tag_type) = self.device.request()

if not error:
Expand All @@ -72,7 +75,7 @@ class Mfrc522Reader(object):
logger.debug("No Device ID found.")
return None

def readCard_normal(self):
def _readCard_normal(self):
# Scan for cards
uid = self.device.read_id(as_number=True)
if not uid:
Expand All @@ -83,6 +86,11 @@ class Mfrc522Reader(object):
logger.info(card_id)
return card_id

def readCard(self):
# Scan for cards
self.device.wait_for_tag()
return self._read_function()

@staticmethod
def cleanup():
GPIO.cleanup()
Expand Down Expand Up @@ -201,14 +209,9 @@ class Reader(object):
else:
with open(path + '/deviceName.txt', 'r') as f:
device_name = f.read().rstrip().split(';', 1)[0]
if os.path.isfile(path + '/../settings/Rfidreader_Rc522_Readmode_UID'):
with open(path + '/../settings/Rfidreader_Rc522_Readmode_UID', 'r') as f:
readmode_uid = f.read().rstrip().split(';', 1)[0] == 'ON'
else:
readmode_uid = False

if device_name == 'MFRC522':
self.reader = Mfrc522Reader(readmode_uid)
self.reader = Mfrc522Reader()
elif device_name == 'RDM6300':
# The Rdm6300Reader supports 2 Additional Number Formats which can bee choosen by an optional parameter dictionary:
# {'numberformat':'card_id_float'} or {'numberformat':'card_id_dec'}
Expand All @@ -221,3 +224,6 @@ class Reader(object):
self.reader = UsbReader(device)
except IndexError:
sys.exit('Could not find the device %s.\n Make sure it is connected' % device_name)

def readCard(self):
return self.reader.readCard()
27 changes: 24 additions & 3 deletions scripts/Reader.py.experimental.Multi
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ class UsbReader(object):

class Mfrc522Reader(object):
def __init__(self):
import pirc522
self.device = pirc522.RFID()
path = os.path.dirname(os.path.realpath(__file__))
readmode_uid = False
if os.path.isfile(path + '/../settings/Rfidreader_Rc522_Readmode_UID'):
with open(path + '/../settings/Rfidreader_Rc522_Readmode_UID', 'r') as f:
readmode_uid = f.read().rstrip().split(';', 1)[0] == 'ON'
self._read_function = self._readCard_normal if readmode_uid else self._readCard_legacy

def readCard(self):
def _readCard_legacy(self):
# Scan for cards
self.device.wait_for_tag()
(error, tag_type) = self.device.request()

if not error:
Expand All @@ -88,6 +94,22 @@ class Mfrc522Reader(object):
logger.debug("No Device ID found.")
return None

def _readCard_normal(self):
# Scan for cards
uid = self.device.read_id(as_number=True)
if not uid:
logger.debug("No Device ID found.")
return None
card_id = str(uid)
logger.info("Card detected.")
logger.info(card_id)
return card_id

def readCard(self):
# Scan for cards
self.device.wait_for_tag()
return self._read_function()

@staticmethod
def cleanup():
GPIO.cleanup()
Expand Down Expand Up @@ -158,7 +180,6 @@ class Pn532Reader:

class Reader(object):
def __init__(self):
self.reader = self
self.devs = list()
path = os.path.dirname(os.path.realpath(__file__))
if not os.path.isfile(path + '/deviceName.txt'):
Expand Down
2 changes: 0 additions & 2 deletions scripts/Reader.py.original
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ def get_devices():


class Reader:
reader = None

def __init__(self):
logger.debug('Initialize Reader')
self.reader = self
path = os.path.dirname(os.path.realpath(__file__))
self.keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
deviceNameFile = os.path.join(path, 'deviceName.txt')
Expand Down
4 changes: 1 addition & 3 deletions scripts/Reader.py.pcsc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ from smartcard.util import *

class Reader:

reader = None

def __init__(self):
self.reader = self
pass

def readCard(self):

Expand Down
9 changes: 1 addition & 8 deletions scripts/daemon_rfid_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,7 @@ def handler(signum, frame):
signal.alarm(1)

# reading the card id
# NOTE: it's been reported that KKMOON Reader might need the following line altered.
# Instead of:
# cardid = reader.reader.readCard()
# change the line to:
# cardid = reader.readCard()
# See here for (German ;) details:
# https://github.com/MiczFlor/RPi-Jukebox-RFID/issues/551
cardid = reader.reader.readCard()
cardid = reader.readCard()

# disable the alarm after a successful read
signal.alarm(0)
Expand Down

0 comments on commit 5dca35f

Please sign in to comment.