Skip to content

Commit cc2af66

Browse files
create pirc522 package
1 parent 6f5add0 commit cc2af66

File tree

7 files changed

+72
-26
lines changed

7 files changed

+72
-26
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
__pycache__
2+
*.pyc
3+
14
## Ignore Visual Studio temporary files, build results, and
25
## files generated by popular Visual Studio add-ons.
36

File renamed without changes.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
#Request tag
1212
(error, data) = rdr.request()
1313
if not error:
14-
print ("\nDetected")
14+
print("\nDetected")
1515

1616
(error, uid) = rdr.anticoll()
1717
if not error:
1818
#Print UID
19-
print ("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
19+
print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
2020

2121
#Set tag as used in util. This will call RFID.select_tag(uid)
2222
util.set_tag(uid)
@@ -45,5 +45,5 @@
4545
util.dump()
4646
#We must stop crypto
4747
util.deauth()
48-
48+
4949
time.sleep(1)
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import RPi.GPIO as GPIO
2-
import spi as SPI
3-
import signal
41
import time
2+
import signal
3+
4+
import spi as SPI
5+
import RPi.GPIO as GPIO
56

6-
class RFID:
7+
8+
__version__ = (1, 0, 0)
9+
10+
11+
class RFID(object):
712
pin_rst = 22
813
pin_ce = 0
914

@@ -179,7 +184,7 @@ def anticoll(self):
179184
serial_number = []
180185

181186
serial_number_check = 0
182-
187+
183188
self.dev_write(0x0D, 0x00)
184189
serial_number.append(self.act_anticl)
185190
serial_number.append(0x20)
@@ -194,7 +199,7 @@ def anticoll(self):
194199
error = True
195200
else:
196201
error = True
197-
202+
198203
return (error, back_data)
199204

200205
def calculate_crc(self, data):
@@ -306,7 +311,7 @@ def read(self, block_address):
306311
error = True
307312

308313
return (error, back_data)
309-
314+
310315
def write(self, block_address, data):
311316
"""
312317
Writes data to block. You should be authenticated before calling write.
@@ -326,7 +331,7 @@ def write(self, block_address, data):
326331
buf_w = []
327332
for i in range(16):
328333
buf_w.append(data[i])
329-
334+
330335
crc = self.calculate_crc(buf_w)
331336
buf_w.append(crc[0])
332337
buf_w.append(crc[1])
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import RFID
22

3-
class RFIDUtil:
3+
4+
class RFIDUtil(object):
45
rfid = None
56
method = None
67
key = None
@@ -20,19 +21,20 @@ def block_addr(self, sector, block):
2021

2122
def sector_string(self, block_address):
2223
"""
23-
Returns sector and it's block representation of block address, eg. S01B03 for sector trailer in second sector
24+
Returns sector and it's block representation of block address, eg.
25+
S01B03 for sector trailer in second sector.
2426
"""
2527
return "S" + str((block_address - (block_address % 4)) / 4) + "B" + str(block_address % 4)
2628

2729
def set_tag(self, uid):
2830
"""
29-
Sets tag for further operations.
30-
Calls deauth() if card is already set.
31+
Sets tag for further operations.
32+
Calls deauth() if card is already set.
3133
Calls RFID select_tag().
3234
Returns called select_tag() error state.
3335
"""
3436
if self.debug:
35-
print ("Selecting UID " + str(uid))
37+
print("Selecting UID " + str(uid))
3638

3739
if self.uid != None:
3840
self.deauth()
@@ -48,7 +50,7 @@ def auth(self, auth_method, key):
4850
self.key = key
4951

5052
if self.debug:
51-
print ("Changing used auth key to " + str(key) + " using method " + ("A" if auth_method == self.rfid.auth_a else "B"))
53+
print("Changing used auth key to " + str(key) + " using method " + ("A" if auth_method == self.rfid.auth_a else "B"))
5254

5355
def deauth(self):
5456
"""
@@ -59,12 +61,12 @@ def deauth(self):
5961
self.last_auth = None
6062

6163
if self.debug:
62-
print ("Changing auth key and method to None")
64+
print("Changing auth key and method to None")
6365

6466
if self.rfid.authed:
6567
self.rfid.stop_crypto()
6668
if self.debug:
67-
print ("Stopping crypto1")
69+
print("Stopping crypto1")
6870

6971
def is_tag_set_auth(self):
7072
return (self.uid != None) and (self.key != None) and (self.method != None)
@@ -77,13 +79,13 @@ def do_auth(self, block_address, force=False):
7779
auth_data = (block_address, self.method, self.key, self.uid)
7880
if (self.last_auth != auth_data) or force:
7981
if self.debug:
80-
print ("Calling card_auth on UID " + str(self.uid))
82+
print("Calling card_auth on UID " + str(self.uid))
8183

8284
self.last_auth = auth_data
8385
return self.rfid.card_auth(self.method, block_address, self.key, self.uid)
8486
else:
8587
if self.debug:
86-
print ("Not calling card_auth - already authed")
88+
print("Not calling card_auth - already authed")
8789
return False
8890

8991
def write_trailer(self, sector, key_a=(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF), auth_bits=(0xFF, 0x07, 0x80),
@@ -111,13 +113,13 @@ def rewrite(self, block_address, new_bytes):
111113
for i in range(len(new_bytes)):
112114
if new_bytes[i] != None:
113115
if self.debug:
114-
print ("Changing pos " + str(i) + " with current value " + str(data[i]) + " to " + str(new_bytes[i]))
116+
print("Changing pos " + str(i) + " with current value " + str(data[i]) + " to " + str(new_bytes[i]))
115117

116118
data[i] = new_bytes[i]
117-
119+
118120
error = self.rfid.write(block_address, data)
119121
if self.debug:
120-
print ("Writing " + str(data) + " to " + self.sector_string(block_address))
122+
print("Writing " + str(data) + " to " + self.sector_string(block_address))
121123

122124
return error
123125

@@ -131,9 +133,9 @@ def read_out(self, block_address):
131133
error = self.do_auth(block_address)
132134
if not error:
133135
(error, data) = self.rfid.read(block_address)
134-
print (self.sector_string(block_address) + ": " + str(data))
136+
print(self.sector_string(block_address) + ": " + str(data))
135137
else:
136-
print ("Error on " + self.sector_string(block_address))
138+
print("Error on " + self.sector_string(block_address))
137139

138140
def dump(self, sectors=16):
139141
for i in range(sectors * 4):

setup.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import sys
5+
from setuptools import setup, find_packages
6+
from setuptools.command.test import test as TestCommand
7+
8+
9+
# get version nr
10+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
11+
'pirc522')))
12+
from pirc522 import __version__ # flake8: noqa
13+
sys.path.pop(0)
14+
15+
setup(
16+
name='pi-rc522',
17+
packages=find_packages(),
18+
include_package_data=True,
19+
version=__version__,
20+
description='Raspberry Pi Python library for SPI RFID RC522 module.',
21+
long_description='Raspberry Pi Python library for SPI RFID RC522 module.',
22+
classifiers=[
23+
'Development Status :: 4 - Beta',
24+
'Intended Audience :: Developers',
25+
'License :: OSI Approved :: MIT License',
26+
'Operating System :: OS Independent',
27+
'Topic :: Software Development',
28+
'Programming Language :: Python :: 2',
29+
'Programming Language :: Python :: 2.7',
30+
'Programming Language :: Python :: 3',
31+
'Programming Language :: Python :: 3.5',
32+
],
33+
author='ondryaso',
34+
url='https://github.com/ondryaso/pi-rc522',
35+
license='MIT',
36+
)

0 commit comments

Comments
 (0)