Commit e45efc6 1 parent 02d4fd3 commit e45efc6 Copy full SHA for e45efc6
File tree 8 files changed +35
-64
lines changed
8 files changed +35
-64
lines changed Original file line number Diff line number Diff line change 2
2
"""Example."""
3
3
4
4
from argparse import ArgumentParser
5
+ from contextlib import contextmanager
6
+
7
+ from RPi .GPIO import GPIO # noqa: N814
5
8
6
9
from pn532 import (
7
10
PN532_SPI ,
10
13
)
11
14
12
15
13
- def spi ():
14
- """Return default spi device."""
15
- return PN532_SPI (debug = False , reset = 20 , cs = 4 )
16
+ @contextmanager
17
+ def spi (debug = False , reset = 20 , cs = 4 ):
18
+ """Yield a spi device."""
19
+ try :
20
+ yield PN532_SPI (debug = debug , reset = reset , cs = cs )
21
+ finally :
22
+ GPIO .cleanup ()
16
23
17
24
18
- def i2c ():
19
- """Return default i2c device."""
20
- return PN532_I2C (debug = False , reset = 20 , req = 16 )
25
+ @contextmanager
26
+ def i2c (debug = False , reset = 20 , req = 16 ):
27
+ """Yield a i2c device."""
28
+ try :
29
+ yield PN532_I2C (debug = debug , reset = reset , req = req )
30
+ finally :
31
+ GPIO .cleanup ()
21
32
22
33
23
- def uart ():
24
- """Return default uart device."""
25
- return PN532_UART (debug = False , reset = 20 )
34
+ @contextmanager
35
+ def uart (debug = False , reset = 20 ):
36
+ """Yield a uart device."""
37
+ try :
38
+ yield PN532_UART (debug = debug , reset = reset )
39
+ finally :
40
+ GPIO .cleanup ()
26
41
27
42
28
43
def mode (value ):
Original file line number Diff line number Diff line change 5
5
This example shows connecting to the PN532 and reading the GPIOs.
6
6
"""
7
7
8
- import RPi .GPIO as GPIO # noqa: N814
9
-
10
8
from pn532 .example import parse_mode
11
9
12
10
13
11
def run (mode ):
14
12
"""Run."""
15
- try :
16
- pn532 = mode ()
17
-
13
+ with mode () as pn532 :
18
14
ic , ver , rev , support = pn532 .get_firmware_version ()
19
15
print ("Found PN532 with firmware version: {0}.{1}" .format (ver , rev ))
20
16
@@ -29,8 +25,6 @@ def run(mode):
29
25
print ("i0:" , pn532 .read_gpio ("I0" ))
30
26
print ("i1:" , pn532 .read_gpio ("I1" ))
31
27
print ("P34:" , pn532 .read_gpio ("P34" ))
32
- finally :
33
- GPIO .cleanup ()
34
28
35
29
36
30
def main ():
Original file line number Diff line number Diff line change 5
5
This example shows connecting to the PN532 and writing the GPIOs.
6
6
"""
7
7
8
- import RPi .GPIO as GPIO # noqa: N814
9
-
10
8
from pn532 .example import parse_mode
11
9
12
10
13
11
def run (mode ):
14
12
"""Run."""
15
- try :
16
- pn532 = mode ()
17
-
13
+ with mode () as pn532 :
18
14
ic , ver , rev , support = pn532 .get_firmware_version ()
19
15
print ("Found PN532 with firmware version: {0}.{1}" .format (ver , rev ))
20
16
@@ -50,8 +46,6 @@ def run(mode):
50
46
)
51
47
print ("2. P71/P72 are always HIGH in SPI mode." )
52
48
print ("3. DO NOT reset the P32 and P34 pins." )
53
- finally :
54
- GPIO .cleanup ()
55
49
56
50
57
51
def main ():
Original file line number Diff line number Diff line change 6
6
type RFID tag
7
7
"""
8
8
9
- import RPi .GPIO as GPIO # noqa: N814
10
-
11
9
import pn532 .pn532 as nfc
12
10
13
11
from pn532 .example import parse_mode
14
12
15
13
16
14
def run (mode ):
17
15
"""Run."""
18
- try :
19
- pn532 = mode ()
20
-
16
+ with mode () as pn532 :
21
17
ic , ver , rev , support = pn532 .get_firmware_version ()
22
18
print ("Found PN532 with firmware version: {0}.{1}" .format (ver , rev ))
23
19
@@ -57,8 +53,6 @@ def run(mode):
57
53
except nfc .PN532Error as e :
58
54
print (e .errmsg )
59
55
break
60
- finally :
61
- GPIO .cleanup ()
62
56
63
57
64
58
def main ():
Original file line number Diff line number Diff line change 1
1
# -*- coding: utf-8 -*-
2
2
"""
3
+ Read/write mifare.
4
+
3
5
This example shows connecting to the PN532 and writing an M1
4
6
type RFID tag
5
7
13
15
2. Block 0 is unwritable.
14
16
"""
15
17
16
- import RPi .GPIO as GPIO # noqa: N814
17
-
18
18
import pn532 .pn532 as nfc
19
19
from pn532 .example import parse_mode
20
20
21
21
22
22
def run (mode ):
23
23
"""Run."""
24
- try :
25
- pn532 = mode ()
26
-
24
+ with mode () as pn532 :
27
25
ic , ver , rev , support = pn532 .get_firmware_version ()
28
26
print ("Found PN532 with firmware version: {0}.{1}" .format (ver , rev ))
29
27
@@ -86,8 +84,6 @@ def run(mode):
86
84
print ("write block %d successfully" % block_number )
87
85
except nfc .PN532Error as e :
88
86
print (e .errmsg )
89
- finally :
90
- GPIO .cleanup ()
91
87
92
88
93
89
def main ():
Original file line number Diff line number Diff line change 6
6
type RFID tag
7
7
"""
8
8
9
- import RPi .GPIO as GPIO # noqa: N814
10
-
11
9
import pn532 .pn532 as nfc
12
10
from pn532 .example import parse_mode
13
11
14
12
15
13
def run (mode ):
16
14
"""Run."""
17
- try :
18
- pn532 = mode ()
19
-
15
+ with mode () as pn532 :
20
16
ic , ver , rev , support = pn532 .get_firmware_version ()
21
- print (
22
- " Found PN532 with firmware version: {0}.{1}" .format (ver , rev )
23
- )
17
+ print (f"Found PN532 with firmware version: { ver } .{ rev } " )
24
18
25
19
# Configure PN532 to communicate with NTAG215 cards
26
20
pn532 .SAM_configuration ()
@@ -48,8 +42,6 @@ def run(mode):
48
42
except nfc .PN532Error as e :
49
43
print (e .errmsg )
50
44
break
51
- finally :
52
- GPIO .cleanup ()
53
45
54
46
55
47
def main ():
Original file line number Diff line number Diff line change 6
6
type RFID tag
7
7
"""
8
8
9
- import RPi .GPIO as GPIO # noqa: N814
10
-
11
9
import pn532 .pn532 as nfc
12
10
13
11
from pn532 .example import parse_mode
14
12
15
13
16
14
def run (mode ):
17
15
"""Run."""
18
- try :
19
- pn532 = mode ()
20
-
16
+ with mode () as pn532 :
21
17
ic , ver , rev , support = pn532 .get_firmware_version ()
22
- print ("Found PN532 with firmware version: {0 }.{1}" . format ( ver , rev ) )
18
+ print ("Found PN532 with firmware version: {ver }.{rev}" )
23
19
24
20
# Configure PN532 to communicate with NTAG215 cards
25
21
pn532 .SAM_configuration ()
@@ -44,8 +40,6 @@ def run(mode):
44
40
print ("write block %d successfully" % block_number )
45
41
except nfc .PN532Error as e :
46
42
print (e .errmsg )
47
- finally :
48
- GPIO .cleanup ()
49
43
50
44
51
45
def main ():
Original file line number Diff line number Diff line change 8
8
After initialization, try waving various 13.56MHz RFID cards over it!
9
9
"""
10
10
11
- import RPi .GPIO as GPIO # noqa: N814
12
-
13
11
from pn532 .example import parse_mode
14
12
15
13
16
14
def run (mode ):
17
15
"""Run."""
18
- try :
19
- pn532 = mode ()
20
-
16
+ with mode () as pn532 :
21
17
ic , ver , rev , support = pn532 .get_firmware_version ()
22
18
print ("Found PN532 with firmware version: {0}.{1}" .format (ver , rev ))
23
19
@@ -33,10 +29,6 @@ def run(mode):
33
29
if uid is None :
34
30
continue
35
31
print ("Found card with UID:" , [hex (i ) for i in uid ])
36
- except Exception as e :
37
- print (e )
38
- finally :
39
- GPIO .cleanup ()
40
32
41
33
42
34
def main ():
You can’t perform that action at this time.
0 commit comments