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

Python 3 and SPI lib updates #80

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions MFRC522.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class MFRC522:
serNum = []

def __init__(self, dev='/dev/spidev0.0', spd=1000000):
spi.openSPI(device=dev,speed=spd)
self.dev_dictionary = spi.openSPI(device=dev,speed=spd)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.NRSTPD, GPIO.OUT)
GPIO.output(self.NRSTPD, 1)
Expand All @@ -138,18 +138,18 @@ def MFRC522_Reset(self):
self.Write_MFRC522(self.CommandReg, self.PCD_RESETPHASE)

def Write_MFRC522(self, addr, val):
spi.transfer(((addr<<1)&0x7E,val))
spi.transfer(self.dev_dictionary, ((addr<<1)&0x7E,val))

def Read_MFRC522(self, addr):
val = spi.transfer((((addr<<1)&0x7E) | 0x80,0))
val = spi.transfer(self.dev_dictionary, (((addr<<1)&0x7E) | 0x80,0))
return val[1]

def SetBitMask(self, reg, mask):
tmp = self.Read_MFRC522(reg)
self.Write_MFRC522(reg, tmp | mask)

def ClearBitMask(self, reg, mask):
tmp = self.Read_MFRC522(reg);
tmp = self.Read_MFRC522(reg)
self.Write_MFRC522(reg, tmp & (~mask))

def AntennaOn(self):
Expand Down Expand Up @@ -181,7 +181,7 @@ def MFRC522_ToCard(self,command,sendData):
self.ClearBitMask(self.CommIrqReg, 0x80)
self.SetBitMask(self.FIFOLevelReg, 0x80)

self.Write_MFRC522(self.CommandReg, self.PCD_IDLE);
self.Write_MFRC522(self.CommandReg, self.PCD_IDLE)

while(i<len(sendData)):
self.Write_MFRC522(self.FIFODataReg, sendData[i])
Expand Down Expand Up @@ -224,7 +224,7 @@ def MFRC522_ToCard(self,command,sendData):
i = 0
while i<n:
backData.append(self.Read_MFRC522(self.FIFODataReg))
i = i + 1;
i += 1
else:
status = self.MI_ERR

Expand All @@ -238,7 +238,7 @@ def MFRC522_Request(self, reqMode):

self.Write_MFRC522(self.BitFramingReg, 0x07)

TagType.append(reqMode);
TagType.append(reqMode)
(status,backData,backBits) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, TagType)

if ((status != self.MI_OK) | (backBits != 0x10)):
Expand Down Expand Up @@ -275,7 +275,7 @@ def MFRC522_Anticoll(self):

def CalulateCRC(self, pIndata):
self.ClearBitMask(self.DivIrqReg, 0x04)
self.SetBitMask(self.FIFOLevelReg, 0x80);
self.SetBitMask(self.FIFOLevelReg, 0x80)
i = 0
while i<len(pIndata):
self.Write_MFRC522(self.FIFODataReg, pIndata[i])
Expand Down Expand Up @@ -307,7 +307,7 @@ def MFRC522_SelectTag(self, serNum):
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buf)

if (status == self.MI_OK) and (backLen == 0x18):
print "Size: " + str(backData[0])
print("Size: {}".format(backData[0]))
return backData[0]
else:
return 0
Expand Down Expand Up @@ -338,9 +338,9 @@ def MFRC522_Auth(self, authMode, BlockAddr, Sectorkey, serNum):

# Check if an error occurred
if not(status == self.MI_OK):
print "AUTH ERROR!!"
print("AUTH ERROR!!")
if not (self.Read_MFRC522(self.Status2Reg) & 0x08) != 0:
print "AUTH ERROR(status2reg & 0x08) != 0"
print("AUTH ERROR(status2reg & 0x08) != 0")

# Return the status
return status
Expand All @@ -357,10 +357,10 @@ def MFRC522_Read(self, blockAddr):
recvData.append(pOut[1])
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, recvData)
if not(status == self.MI_OK):
print "Error while reading!"
print("Error while reading!")
i = 0
if len(backData) == 16:
print "Sector "+str(blockAddr)+" "+str(backData)
print("Sector {} {}".format(blockAddr, backData))

def MFRC522_Write(self, blockAddr, writeData):
buff = []
Expand All @@ -373,7 +373,7 @@ def MFRC522_Write(self, blockAddr, writeData):
if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):
status = self.MI_ERR

print "%s backdata &0x0F == 0x0A %s" % (backLen, backData[0]&0x0F)
print("{} backdata &0x0F == 0x0A {}".format(backLen, backData[0] & 0x0F))
if status == self.MI_OK:
i = 0
buf = []
Expand All @@ -385,9 +385,9 @@ def MFRC522_Write(self, blockAddr, writeData):
buf.append(crc[1])
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE,buf)
if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):
print "Error while writing"
print("Error while writing")
if status == self.MI_OK:
print "Data written"
print("Data written")

def MFRC522_DumpClassic1K(self, key, uid):
i = 0
Expand All @@ -397,13 +397,13 @@ def MFRC522_DumpClassic1K(self, key, uid):
if status == self.MI_OK:
self.MFRC522_Read(i)
else:
print "Authentication error"
print("Authentication error")
i = i+1

def MFRC522_Init(self):
GPIO.output(self.NRSTPD, 1)

self.MFRC522_Reset();
self.MFRC522_Reset()


self.Write_MFRC522(self.TModeReg, 0x8D)
Expand Down
12 changes: 6 additions & 6 deletions Read.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
print("Ctrl+C captured, ending read.")
continue_reading = False
GPIO.cleanup()

Expand All @@ -41,8 +41,8 @@ def end_read(signal,frame):
MIFAREReader = MFRC522.MFRC522()

# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."
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:
Expand All @@ -52,7 +52,7 @@ def end_read(signal,frame):

# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
print("Card detected")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wat speel hier vandaag: UID alle vier de id controleren


# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
Expand All @@ -61,7 +61,7 @@ def end_read(signal,frame):
if status == MIFAREReader.MI_OK:

# Print UID
print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
print("Card read UID: {},{},{},{}".format(uid[0], uid[1], uid[2], uid[3]))

# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
Expand All @@ -77,5 +77,5 @@ def end_read(signal,frame):
MIFAREReader.MFRC522_Read(8)
MIFAREReader.MFRC522_StopCrypto1()
else:
print "Authentication error"
print("Authentication error")

30 changes: 15 additions & 15 deletions Write.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
print("Ctrl+C captured, ending read.")
continue_reading = False
GPIO.cleanup()

Expand All @@ -48,7 +48,7 @@ def end_read(signal,frame):

# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
print("Card detected")

# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
Expand All @@ -57,7 +57,7 @@ def end_read(signal,frame):
if status == MIFAREReader.MI_OK:

# Print UID
print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
print("Card read UID: {},{},{},{}".format(uid[0], uid[1], uid[2], uid[3]))

# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
Expand All @@ -67,7 +67,7 @@ def end_read(signal,frame):

# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
print "\n"
print("\n")

# Check if authenticated
if status == MIFAREReader.MI_OK:
Expand All @@ -79,39 +79,39 @@ def end_read(signal,frame):
for x in range(0,16):
data.append(0xFF)

print "Sector 8 looked like this:"
print("Sector 8 looked like this:")
# Read block 8
MIFAREReader.MFRC522_Read(8)
print "\n"
print("\n")

print "Sector 8 will now be filled with 0xFF:"
print("Sector 8 will now be filled with 0xFF:")
# Write the data
MIFAREReader.MFRC522_Write(8, data)
print "\n"
print("\n")

print "It now looks like this:"
print("It now looks like this:")
# Check to see if it was written
MIFAREReader.MFRC522_Read(8)
print "\n"
print("\n")

data = []
# Fill the data with 0x00
for x in range(0,16):
data.append(0x00)

print "Now we fill it with 0x00:"
print("Now we fill it with 0x00:")
MIFAREReader.MFRC522_Write(8, data)
print "\n"
print("\n")

print "It is now empty:"
print("It is now empty:")
# Check to see if it was written
MIFAREReader.MFRC522_Read(8)
print "\n"
print("\n")

# Stop
MIFAREReader.MFRC522_StopCrypto1()

# Make sure to stop reading for cards
continue_reading = False
else:
print "Authentication error"
print("Authentication error")