Skip to content

Commit

Permalink
create pirc522 package
Browse files Browse the repository at this point in the history
  • Loading branch information
thijstriemstra committed Sep 9, 2016
1 parent 6f5add0 commit cc2af66
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__pycache__
*.pyc

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions ChipReader/UtilExample.py → examples/UtilExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
#Request tag
(error, data) = rdr.request()
if not error:
print ("\nDetected")
print("\nDetected")

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

#Set tag as used in util. This will call RFID.select_tag(uid)
util.set_tag(uid)
Expand Down Expand Up @@ -45,5 +45,5 @@
util.dump()
#We must stop crypto
util.deauth()

time.sleep(1)
21 changes: 13 additions & 8 deletions ChipReader/RFID.py → pirc522/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import RPi.GPIO as GPIO
import spi as SPI
import signal
import time
import signal

import spi as SPI
import RPi.GPIO as GPIO

class RFID:

__version__ = (1, 0, 0)


class RFID(object):
pin_rst = 22
pin_ce = 0

Expand Down Expand Up @@ -179,7 +184,7 @@ def anticoll(self):
serial_number = []

serial_number_check = 0

self.dev_write(0x0D, 0x00)
serial_number.append(self.act_anticl)
serial_number.append(0x20)
Expand All @@ -194,7 +199,7 @@ def anticoll(self):
error = True
else:
error = True

return (error, back_data)

def calculate_crc(self, data):
Expand Down Expand Up @@ -306,7 +311,7 @@ def read(self, block_address):
error = True

return (error, back_data)

def write(self, block_address, data):
"""
Writes data to block. You should be authenticated before calling write.
Expand All @@ -326,7 +331,7 @@ def write(self, block_address, data):
buf_w = []
for i in range(16):
buf_w.append(data[i])

crc = self.calculate_crc(buf_w)
buf_w.append(crc[0])
buf_w.append(crc[1])
Expand Down
32 changes: 17 additions & 15 deletions ChipReader/RFIDUtil.py → pirc522/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import RFID

class RFIDUtil:

class RFIDUtil(object):
rfid = None
method = None
key = None
Expand All @@ -20,19 +21,20 @@ def block_addr(self, sector, block):

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

def set_tag(self, uid):
"""
Sets tag for further operations.
Calls deauth() if card is already set.
Sets tag for further operations.
Calls deauth() if card is already set.
Calls RFID select_tag().
Returns called select_tag() error state.
"""
if self.debug:
print ("Selecting UID " + str(uid))
print("Selecting UID " + str(uid))

if self.uid != None:
self.deauth()
Expand All @@ -48,7 +50,7 @@ def auth(self, auth_method, key):
self.key = key

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

def deauth(self):
"""
Expand All @@ -59,12 +61,12 @@ def deauth(self):
self.last_auth = None

if self.debug:
print ("Changing auth key and method to None")
print("Changing auth key and method to None")

if self.rfid.authed:
self.rfid.stop_crypto()
if self.debug:
print ("Stopping crypto1")
print("Stopping crypto1")

def is_tag_set_auth(self):
return (self.uid != None) and (self.key != None) and (self.method != None)
Expand All @@ -77,13 +79,13 @@ def do_auth(self, block_address, force=False):
auth_data = (block_address, self.method, self.key, self.uid)
if (self.last_auth != auth_data) or force:
if self.debug:
print ("Calling card_auth on UID " + str(self.uid))
print("Calling card_auth on UID " + str(self.uid))

self.last_auth = auth_data
return self.rfid.card_auth(self.method, block_address, self.key, self.uid)
else:
if self.debug:
print ("Not calling card_auth - already authed")
print("Not calling card_auth - already authed")
return False

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

data[i] = new_bytes[i]

error = self.rfid.write(block_address, data)
if self.debug:
print ("Writing " + str(data) + " to " + self.sector_string(block_address))
print("Writing " + str(data) + " to " + self.sector_string(block_address))

return error

Expand All @@ -131,9 +133,9 @@ def read_out(self, block_address):
error = self.do_auth(block_address)
if not error:
(error, data) = self.rfid.read(block_address)
print (self.sector_string(block_address) + ": " + str(data))
print(self.sector_string(block_address) + ": " + str(data))
else:
print ("Error on " + self.sector_string(block_address))
print("Error on " + self.sector_string(block_address))

def dump(self, sectors=16):
for i in range(sectors * 4):
Expand Down
36 changes: 36 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python

import os
import sys
from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand


# get version nr
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
'pirc522')))
from pirc522 import __version__ # flake8: noqa
sys.path.pop(0)

setup(
name='pi-rc522',
packages=find_packages(),
include_package_data=True,
version=__version__,
description='Raspberry Pi Python library for SPI RFID RC522 module.',
long_description='Raspberry Pi Python library for SPI RFID RC522 module.',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Topic :: Software Development',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
],
author='ondryaso',
url='https://github.com/ondryaso/pi-rc522',
license='MIT',
)

0 comments on commit cc2af66

Please sign in to comment.