Skip to content

Commit 790340e

Browse files
committed
Add ability to power down pn532
1 parent 62ddaaf commit 790340e

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

adafruit_pn532/adafruit_pn532.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -164,39 +164,21 @@
164164
_FRAME_START = b"\x00\x00\xFF"
165165

166166

167-
def _reset(pin):
168-
"""Perform a hardware reset toggle"""
169-
pin.direction = Direction.OUTPUT
170-
pin.value = True
171-
time.sleep(0.1)
172-
pin.value = False
173-
time.sleep(0.5)
174-
pin.value = True
175-
time.sleep(0.1)
176-
177-
178167
class BusyError(Exception):
179168
"""Base class for exceptions in this module."""
180169

181170

182171
class PN532:
183172
"""PN532 driver base, must be extended for I2C/SPI/UART interfacing"""
184173

185-
def __init__(self, *, debug=False, reset=None):
174+
def __init__(self, *, debug=False, irq=None, reset=None):
186175
"""Create an instance of the PN532 class
187176
"""
177+
self.low_power = True
188178
self.debug = debug
189-
if reset:
190-
if debug:
191-
print("Resetting")
192-
_reset(reset)
193-
194-
try:
195-
self._wakeup()
196-
_ = self.firmware_version # first time often fails, try 2ce
197-
return
198-
except (BusyError, RuntimeError):
199-
pass
179+
self._irq = irq
180+
self._reset_pin = reset
181+
self.reset()
200182
_ = self.firmware_version
201183

202184
def _read_data(self, count):
@@ -218,6 +200,18 @@ def _wakeup(self):
218200
# Send special command to wake up
219201
raise NotImplementedError
220202

203+
def reset(self):
204+
"""Perform a hardware reset toggle"""
205+
if self._reset_pin:
206+
if self.debug:
207+
print("Resetting")
208+
self._reset_pin.direction = Direction.OUTPUT
209+
self._reset_pin.value = False
210+
time.sleep(0.1)
211+
self._reset_pin.value = True
212+
time.sleep(0.1)
213+
self._wakeup()
214+
221215
def _write_frame(self, data):
222216
"""Write a frame to the PN532 with the specified data bytearray."""
223217
assert (
@@ -306,6 +300,9 @@ def send_command(
306300
Will wait up to timeout seconds for the acknowlegment and return True.
307301
If no acknowlegment is received, False is returned.
308302
"""
303+
if self.low_power:
304+
self._wakeup()
305+
309306
# Build frame data with command and parameters.
310307
data = bytearray(2 + len(params))
311308
data[0] = _HOSTTOPN532
@@ -316,7 +313,6 @@ def send_command(
316313
try:
317314
self._write_frame(data)
318315
except OSError:
319-
self._wakeup()
320316
return False
321317
if not self._wait_ready(timeout):
322318
return False
@@ -342,6 +338,17 @@ def process_response(self, command, response_length=0, timeout=1):
342338
# Return response data.
343339
return response[2:]
344340

341+
def power_down(self):
342+
if self._reset_pin: # Hard Power Down if the reset pin is connected
343+
self._reset_pin.value = False
344+
self.low_power = True
345+
else:
346+
# Soft Power Down otherwise. Enable wakeup on I2C, SPI, UART
347+
response = self.call_function(_COMMAND_POWERDOWN, params=[0xb0, 0x00])
348+
self.low_power = response[0] == 0x00
349+
time.sleep(0.005)
350+
return self.low_power
351+
345352
@property
346353
def firmware_version(self):
347354
"""Call PN532 GetFirmwareVersion function and return a tuple with the IC,

0 commit comments

Comments
 (0)