Refactor pin initialization in MFRC522 class

- Raise error for invalid pin type for `rst` or `cs` if not `int` or `Pin` instance
- Set `self.rst` based on `rst` type
- Set `self.cs` based on `cs` type
This commit is contained in:
Kumi 2023-08-18 11:50:19 +02:00
parent b3937dd93c
commit b1baecaf09
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -80,6 +80,10 @@ class MFRC522:
if isinstance(rst, int): if isinstance(rst, int):
self.rst = Pin(rst, Pin.OUT) self.rst = Pin(rst, Pin.OUT)
elif isinstance(rst, Pin):
self.rst = rst
else:
raise TypeError("Invalid pin type for rst")
self.rst.value(1) self.rst.value(1)
else: else:
@ -89,6 +93,10 @@ class MFRC522:
if isinstance(cs, int): if isinstance(cs, int):
self.cs = Pin(cs, Pin.OUT) self.cs = Pin(cs, Pin.OUT)
elif isinstance(cs, Pin):
self.cs = cs
else:
raise TypeError("Invalid pin type for cs")
self.cs.value(1) self.cs.value(1)