From 2b815846bc0813df94cf6c0bb1a50304d6026fb0 Mon Sep 17 00:00:00 2001 From: Asadullah Shaikh Date: Sat, 14 Dec 2024 06:14:58 +0530 Subject: [PATCH 1/4] trim whitespace --- mfrc522/MFRC522.py | 8 ++++---- mfrc522/SimpleMFRC522.py | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/mfrc522/MFRC522.py b/mfrc522/MFRC522.py index 1698351..bcf01a5 100644 --- a/mfrc522/MFRC522.py +++ b/mfrc522/MFRC522.py @@ -136,18 +136,18 @@ def __init__(self, bus=0, device=0, spd=1000000, pin_mode=10, pin_rst=-1, debugL self.logger.setLevel(level) gpioMode = GPIO.getmode() - + if gpioMode is None: GPIO.setmode(pin_mode) else: pin_mode = gpioMode - + if pin_rst == -1: if pin_mode == 11: pin_rst = 15 else: pin_rst = 22 - + GPIO.setup(pin_rst, GPIO.OUT) GPIO.output(pin_rst, 1) self.MFRC522_Init() @@ -312,7 +312,7 @@ def MFRC522_SelectTag(self, serNum): buf = [] buf.append(self.PICC_SElECTTAG) buf.append(0x70) - + for i in range(5): buf.append(serNum[i]) diff --git a/mfrc522/SimpleMFRC522.py b/mfrc522/SimpleMFRC522.py index 91eca06..24a5e4d 100644 --- a/mfrc522/SimpleMFRC522.py +++ b/mfrc522/SimpleMFRC522.py @@ -2,17 +2,17 @@ from . import MFRC522 import RPi.GPIO as GPIO - + class SimpleMFRC522: READER = None - + KEY = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF] BLOCK_ADDRS = [8, 9, 10] - + def __init__(self): self.READER = MFRC522() - + def read(self): id, text = self.read_no_block() while not id: @@ -33,7 +33,7 @@ def read_id_no_block(self): if status != self.READER.MI_OK: return None return self.uid_to_num(uid) - + def read_no_block(self): (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL) if status != self.READER.MI_OK: @@ -48,14 +48,14 @@ def read_no_block(self): text_read = '' if status == self.READER.MI_OK: for block_num in self.BLOCK_ADDRS: - block = self.READER.MFRC522_Read(block_num) + block = self.READER.MFRC522_Read(block_num) if block: data += block if data: text_read = ''.join(chr(i) for i in data) self.READER.MFRC522_StopCrypto1() return id, text_read - + def write(self, text): id, text_in = self.write_no_block(text) while not id: @@ -82,7 +82,7 @@ def write_no_block(self, text): i += 1 self.READER.MFRC522_StopCrypto1() return id, text[0:(len(self.BLOCK_ADDRS) * 16)] - + def uid_to_num(self, uid): n = 0 for i in range(0, 5): From 7be5ee53ea018ad7beec882b2f4e95eec9159d58 Mon Sep 17 00:00:00 2001 From: Asadullah Shaikh Date: Sat, 14 Dec 2024 06:19:59 +0530 Subject: [PATCH 2/4] set indentation to 4 spaces --- mfrc522/SimpleMFRC522.py | 148 +++++++++++++++++++-------------------- 1 file changed, 74 insertions(+), 74 deletions(-) diff --git a/mfrc522/SimpleMFRC522.py b/mfrc522/SimpleMFRC522.py index 24a5e4d..9334c7a 100644 --- a/mfrc522/SimpleMFRC522.py +++ b/mfrc522/SimpleMFRC522.py @@ -5,86 +5,86 @@ class SimpleMFRC522: - READER = None + READER = None - KEY = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF] - BLOCK_ADDRS = [8, 9, 10] + KEY = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF] + BLOCK_ADDRS = [8, 9, 10] - def __init__(self): - self.READER = MFRC522() + def __init__(self): + self.READER = MFRC522() - def read(self): - id, text = self.read_no_block() - while not id: - id, text = self.read_no_block() - return id, text + def read(self): + id, text = self.read_no_block() + while not id: + id, text = self.read_no_block() + return id, text - def read_id(self): - id = self.read_id_no_block() - while not id: - id = self.read_id_no_block() - return id + def read_id(self): + id = self.read_id_no_block() + while not id: + id = self.read_id_no_block() + return id - def read_id_no_block(self): - (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL) - if status != self.READER.MI_OK: - return None - (status, uid) = self.READER.MFRC522_Anticoll() - if status != self.READER.MI_OK: - return None - return self.uid_to_num(uid) + def read_id_no_block(self): + (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL) + if status != self.READER.MI_OK: + return None + (status, uid) = self.READER.MFRC522_Anticoll() + if status != self.READER.MI_OK: + return None + return self.uid_to_num(uid) - def read_no_block(self): - (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL) - if status != self.READER.MI_OK: - return None, None - (status, uid) = self.READER.MFRC522_Anticoll() - if status != self.READER.MI_OK: - return None, None - id = self.uid_to_num(uid) - self.READER.MFRC522_SelectTag(uid) - status = self.READER.MFRC522_Auth(self.READER.PICC_AUTHENT1A, 11, self.KEY, uid) - data = [] - text_read = '' - if status == self.READER.MI_OK: - for block_num in self.BLOCK_ADDRS: - block = self.READER.MFRC522_Read(block_num) - if block: - data += block - if data: - text_read = ''.join(chr(i) for i in data) - self.READER.MFRC522_StopCrypto1() - return id, text_read + def read_no_block(self): + (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL) + if status != self.READER.MI_OK: + return None, None + (status, uid) = self.READER.MFRC522_Anticoll() + if status != self.READER.MI_OK: + return None, None + id = self.uid_to_num(uid) + self.READER.MFRC522_SelectTag(uid) + status = self.READER.MFRC522_Auth(self.READER.PICC_AUTHENT1A, 11, self.KEY, uid) + data = [] + text_read = '' + if status == self.READER.MI_OK: + for block_num in self.BLOCK_ADDRS: + block = self.READER.MFRC522_Read(block_num) + if block: + data += block + if data: + text_read = ''.join(chr(i) for i in data) + self.READER.MFRC522_StopCrypto1() + return id, text_read - def write(self, text): - id, text_in = self.write_no_block(text) - while not id: - id, text_in = self.write_no_block(text) - return id, text_in + def write(self, text): + id, text_in = self.write_no_block(text) + while not id: + id, text_in = self.write_no_block(text) + return id, text_in - def write_no_block(self, text): - (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL) - if status != self.READER.MI_OK: - return None, None - (status, uid) = self.READER.MFRC522_Anticoll() - if status != self.READER.MI_OK: - return None, None - id = self.uid_to_num(uid) - self.READER.MFRC522_SelectTag(uid) - status = self.READER.MFRC522_Auth(self.READER.PICC_AUTHENT1A, 11, self.KEY, uid) - self.READER.MFRC522_Read(11) - if status == self.READER.MI_OK: - data = bytearray() - data.extend(bytearray(text.ljust(len(self.BLOCK_ADDRS) * 16).encode('ascii'))) - i = 0 - for block_num in self.BLOCK_ADDRS: - self.READER.MFRC522_Write(block_num, data[(i*16):(i+1)*16]) - i += 1 - self.READER.MFRC522_StopCrypto1() - return id, text[0:(len(self.BLOCK_ADDRS) * 16)] + def write_no_block(self, text): + (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL) + if status != self.READER.MI_OK: + return None, None + (status, uid) = self.READER.MFRC522_Anticoll() + if status != self.READER.MI_OK: + return None, None + id = self.uid_to_num(uid) + self.READER.MFRC522_SelectTag(uid) + status = self.READER.MFRC522_Auth(self.READER.PICC_AUTHENT1A, 11, self.KEY, uid) + self.READER.MFRC522_Read(11) + if status == self.READER.MI_OK: + data = bytearray() + data.extend(bytearray(text.ljust(len(self.BLOCK_ADDRS) * 16).encode('ascii'))) + i = 0 + for block_num in self.BLOCK_ADDRS: + self.READER.MFRC522_Write(block_num, data[(i*16):(i+1)*16]) + i += 1 + self.READER.MFRC522_StopCrypto1() + return id, text[0:(len(self.BLOCK_ADDRS) * 16)] - def uid_to_num(self, uid): - n = 0 - for i in range(0, 5): - n = n * 256 + uid[i] - return n + def uid_to_num(self, uid): + n = 0 + for i in range(0, 5): + n = n * 256 + uid[i] + return n From a2c68b985cdbad3445879b4143f92d9ebb62452d Mon Sep 17 00:00:00 2001 From: Asadullah Shaikh Date: Sat, 14 Dec 2024 06:24:13 +0530 Subject: [PATCH 3/4] lint --- mfrc522/MFRC522.py | 2 -- mfrc522/SimpleMFRC522.py | 2 -- mfrc522/__init__.py | 2 +- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/mfrc522/MFRC522.py b/mfrc522/MFRC522.py index bcf01a5..419064c 100644 --- a/mfrc522/MFRC522.py +++ b/mfrc522/MFRC522.py @@ -22,8 +22,6 @@ # import RPi.GPIO as GPIO import spidev -import signal -import time import logging class MFRC522: diff --git a/mfrc522/SimpleMFRC522.py b/mfrc522/SimpleMFRC522.py index 9334c7a..2b5251a 100644 --- a/mfrc522/SimpleMFRC522.py +++ b/mfrc522/SimpleMFRC522.py @@ -1,11 +1,9 @@ # Code by Simon Monk https://github.com/simonmonk/ from . import MFRC522 -import RPi.GPIO as GPIO class SimpleMFRC522: - READER = None KEY = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF] BLOCK_ADDRS = [8, 9, 10] diff --git a/mfrc522/__init__.py b/mfrc522/__init__.py index 5b0eae8..5f034ea 100644 --- a/mfrc522/__init__.py +++ b/mfrc522/__init__.py @@ -1,4 +1,4 @@ from .MFRC522 import MFRC522 from .SimpleMFRC522 import SimpleMFRC522 -name = "mfrc522" +__all__ = ["MFRC522", "SimpleMFRC522"] From d1d10ecd858518d50bdef5adabb88ea3af5e3586 Mon Sep 17 00:00:00 2001 From: Asadullah Shaikh Date: Sat, 14 Dec 2024 06:26:42 +0530 Subject: [PATCH 4/4] PEP8 --- mfrc522/MFRC522.py | 6 ++++-- mfrc522/SimpleMFRC522.py | 5 ++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/mfrc522/MFRC522.py b/mfrc522/MFRC522.py index 419064c..83a2815 100644 --- a/mfrc522/MFRC522.py +++ b/mfrc522/MFRC522.py @@ -19,10 +19,12 @@ # # You should have received a copy of the GNU Lesser General Public License # along with MFRC522-Python. If not, see . -# + +import logging + import RPi.GPIO as GPIO import spidev -import logging + class MFRC522: MAX_LEN = 16 diff --git a/mfrc522/SimpleMFRC522.py b/mfrc522/SimpleMFRC522.py index 2b5251a..74a8714 100644 --- a/mfrc522/SimpleMFRC522.py +++ b/mfrc522/SimpleMFRC522.py @@ -1,10 +1,9 @@ # Code by Simon Monk https://github.com/simonmonk/ -from . import MFRC522 - -class SimpleMFRC522: +from .MFRC522 import MFRC522 +class SimpleMFRC522: KEY = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF] BLOCK_ADDRS = [8, 9, 10]