Fixed indentation
This commit is contained in:
parent
f14e5608c0
commit
2786a14420
1 changed files with 75 additions and 67 deletions
44
MFRC522.py
44
MFRC522.py
|
@ -103,7 +103,6 @@ class MFRC522:
|
||||||
|
|
||||||
serNum = []
|
serNum = []
|
||||||
|
|
||||||
|
|
||||||
def __init__(self,spd=1000000):
|
def __init__(self,spd=1000000):
|
||||||
spi.openSPI(speed=spd)
|
spi.openSPI(speed=spd)
|
||||||
GPIO.setmode(GPIO.BOARD)
|
GPIO.setmode(GPIO.BOARD)
|
||||||
|
@ -157,26 +156,31 @@ class MFRC522:
|
||||||
self.Write_MFRC522(self.CommIEnReg, irqEn|0x80)
|
self.Write_MFRC522(self.CommIEnReg, irqEn|0x80)
|
||||||
self.ClearBitMask(self.CommIrqReg, 0x80)
|
self.ClearBitMask(self.CommIrqReg, 0x80)
|
||||||
self.SetBitMask(self.FIFOLevelReg, 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)):
|
while(i<len(sendData)):
|
||||||
self.Write_MFRC522(self.FIFODataReg, sendData[i])
|
self.Write_MFRC522(self.FIFODataReg, sendData[i])
|
||||||
i = i+1
|
i = i+1
|
||||||
self.Write_MFRC522(self.CommandReg, command)
|
|
||||||
|
|
||||||
|
self.Write_MFRC522(self.CommandReg, command)
|
||||||
|
|
||||||
if command == self.PCD_TRANSCEIVE:
|
if command == self.PCD_TRANSCEIVE:
|
||||||
self.SetBitMask(self.BitFramingReg, 0x80)
|
self.SetBitMask(self.BitFramingReg, 0x80)
|
||||||
|
|
||||||
i = 2000
|
i = 2000
|
||||||
while True:
|
while True:
|
||||||
n = self.Read_MFRC522(self.CommIrqReg)
|
n = self.Read_MFRC522(self.CommIrqReg)
|
||||||
i = i - 1
|
i = i - 1
|
||||||
if ~((i!=0) and ~(n&0x01) and ~(n&waitIRq)):
|
if ~((i!=0) and ~(n&0x01) and ~(n&waitIRq)):
|
||||||
break
|
break
|
||||||
|
|
||||||
self.ClearBitMask(self.BitFramingReg, 0x80)
|
self.ClearBitMask(self.BitFramingReg, 0x80)
|
||||||
|
|
||||||
if i != 0:
|
if i != 0:
|
||||||
if (self.Read_MFRC522(self.ErrorReg) & 0x1B)==0x00:
|
if (self.Read_MFRC522(self.ErrorReg) & 0x1B)==0x00:
|
||||||
status = self.MI_OK
|
status = self.MI_OK
|
||||||
|
|
||||||
if n & irqEn & 0x01:
|
if n & irqEn & 0x01:
|
||||||
status = self.MI_NOTAGERR
|
status = self.MI_NOTAGERR
|
||||||
|
|
||||||
|
@ -187,6 +191,7 @@ class MFRC522:
|
||||||
backLen = (n-1)*8 + lastBits
|
backLen = (n-1)*8 + lastBits
|
||||||
else:
|
else:
|
||||||
backLen = n*8
|
backLen = n*8
|
||||||
|
|
||||||
if n == 0:
|
if n == 0:
|
||||||
n = 1
|
n = 1
|
||||||
if n > self.MAX_LEN:
|
if n > self.MAX_LEN:
|
||||||
|
@ -276,29 +281,32 @@ class MFRC522:
|
||||||
buf.append(pOut[0])
|
buf.append(pOut[0])
|
||||||
buf.append(pOut[1])
|
buf.append(pOut[1])
|
||||||
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buf)
|
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buf)
|
||||||
|
|
||||||
if (status == self.MI_OK) and (backLen == 0x18):
|
if (status == self.MI_OK) and (backLen == 0x18):
|
||||||
size = backData[0]
|
print "Size: " + str(backData[0])
|
||||||
|
return backData[0]
|
||||||
else:
|
else:
|
||||||
size = 0
|
return 0
|
||||||
return size
|
|
||||||
|
|
||||||
def MFRC522_Auth(self, authMode, BlockAddr, Sectorkey, serNum):
|
def MFRC522_Auth(self, authMode, BlockAddr, Sectorkey, serNum):
|
||||||
buff = []
|
buff = []
|
||||||
buff.append(authMode)
|
buff.append(authMode)
|
||||||
buff.append(BlockAddr)
|
buff.append(BlockAddr)
|
||||||
i = 0
|
i = 0
|
||||||
while(i < 6):
|
while(i < len(Sectorkey)):
|
||||||
buff.append(Sectorkey[i])
|
buff.append(Sectorkey[i])
|
||||||
i = i + 1
|
i = i + 1
|
||||||
i = 0
|
i = 0
|
||||||
while(i < 4):
|
while(i < len(serNum)):
|
||||||
buff.append(serNum[i])
|
buff.append(serNum[i])
|
||||||
i = i +1
|
i = i +1
|
||||||
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_AUTHENT,buff)
|
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_AUTHENT,buff)
|
||||||
if not(status == self.MI_OK):
|
if not(status == self.MI_OK):
|
||||||
print "AUTH ERROR!!"
|
print "AUTH ERROR!!"
|
||||||
if not(self.Read_MFRC522(self.Status2Reg) & 0x08):
|
if not (self.Read_MFRC522(self.Status2Reg) & 0x08) != 0:
|
||||||
print "AUTH ERROR(status2reg & 0x08 != 0"
|
print "AUTH ERROR(status2reg & 0x08) != 0"
|
||||||
|
|
||||||
|
return status
|
||||||
|
|
||||||
def MFRC522_Read(self, blockAddr):
|
def MFRC522_Read(self, blockAddr):
|
||||||
recvData = []
|
recvData = []
|
||||||
|
@ -326,6 +334,7 @@ class MFRC522:
|
||||||
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buff)
|
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buff)
|
||||||
if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):
|
if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):
|
||||||
status = self.MI_ERR
|
status = self.MI_ERR
|
||||||
|
|
||||||
print str(backLen)+" backdata &0x0F == 0x0A "+str(backData[0]&0x0F)
|
print str(backLen)+" backdata &0x0F == 0x0A "+str(backData[0]&0x0F)
|
||||||
if status == self.MI_OK:
|
if status == self.MI_OK:
|
||||||
i = 0
|
i = 0
|
||||||
|
@ -378,15 +387,14 @@ while continue_reading:
|
||||||
(status,backData) = MIFAREReader.MFRC522_Anticoll()
|
(status,backData) = MIFAREReader.MFRC522_Anticoll()
|
||||||
if status == MIFAREReader.MI_OK:
|
if status == MIFAREReader.MI_OK:
|
||||||
print "Card read UID: "+str(backData[0])+","+str(backData[1])+","+str(backData[2])+","+str(backData[3])+","+str(backData[4])
|
print "Card read UID: "+str(backData[0])+","+str(backData[1])+","+str(backData[2])+","+str(backData[3])+","+str(backData[4])
|
||||||
|
|
||||||
|
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
|
||||||
|
|
||||||
MIFAREReader.MFRC522_SelectTag(backData)
|
MIFAREReader.MFRC522_SelectTag(backData)
|
||||||
key = [0xFF,0xFF,0xFF,0xFF,0xFF]
|
|
||||||
|
|
||||||
|
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 11, key, backData)
|
||||||
MIFAREReader.MFRC522_Auth(0x60,0,keyA,backData)
|
if status == MIFAREReader.MI_OK:
|
||||||
MIFAREReader.MFRC522_Read(0)
|
print "AUTH OK"
|
||||||
i = 1
|
else:
|
||||||
while i < 64:
|
print "AUTH ERROR"
|
||||||
MIFAREReader.MFRC522_Auth(0x60,i,keyA,backData)
|
|
||||||
IFAREReader.MFRC522_Read(i)
|
|
||||||
i = i + 1
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue