Skip to content

Ran black, updated to pylint 2.x #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
source actions-ci/install.sh
- name: Pip install pylint, black, & Sphinx
run: |
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
- name: Library version
run: git describe --dirty --always --tags
- name: PyLint
Expand Down
311 changes: 163 additions & 148 deletions adafruit_pn532/adafruit_pn532.py

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions adafruit_pn532/i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@
from adafruit_pn532.adafruit_pn532 import PN532, BusyError, _reset

# pylint: disable=bad-whitespace
_I2C_ADDRESS = const(0x24)
_I2C_ADDRESS = const(0x24)


class PN532_I2C(PN532):
"""Driver for the PN532 connected over I2C."""

def __init__(self, i2c, *, irq=None, reset=None, req=None, debug=False):
"""Create an instance of the PN532 class using I2C. Note that PN532
uses clock stretching. Optional IRQ pin (not used),
Expand All @@ -61,7 +63,7 @@ def __init__(self, i2c, *, irq=None, reset=None, req=None, debug=False):
self._i2c = i2c_device.I2CDevice(i2c, _I2C_ADDRESS)
super().__init__(debug=debug, reset=reset)

def _wakeup(self): # pylint: disable=no-self-use
def _wakeup(self): # pylint: disable=no-self-use
"""Send any special commands/data to wake up PN532"""
if self._req:
self._req.direction = Direction.OUTPUT
Expand All @@ -83,27 +85,26 @@ def _wait_ready(self, timeout=1):
except OSError:
self._wakeup()
continue
if status == b'\x01':
if status == b"\x01":
return True # No longer busy
else:
time.sleep(0.05) # lets ask again soon!
time.sleep(0.05) # lets ask again soon!
# Timed out!
return False

def _read_data(self, count):
"""Read a specified count of bytes from the PN532."""
# Build a read request frame.
frame = bytearray(count+1)
frame = bytearray(count + 1)
with self._i2c as i2c:
i2c.readinto(frame, end=1) # read status byte!
if frame[0] != 0x01: # not ready
i2c.readinto(frame, end=1) # read status byte!
if frame[0] != 0x01: # not ready
raise BusyError
i2c.readinto(frame) # ok get the data, plus statusbyte
i2c.readinto(frame) # ok get the data, plus statusbyte
if self.debug:
print("Reading: ", [hex(i) for i in frame[1:]])
else:
time.sleep(0.1)
return frame[1:] # don't return the status byte
return frame[1:] # don't return the status byte

def _write_data(self, framebytes):
"""Write a specified count of bytes to the PN532"""
Expand Down
38 changes: 21 additions & 17 deletions adafruit_pn532/spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,28 @@
from adafruit_pn532.adafruit_pn532 import PN532

# pylint: disable=bad-whitespace
_SPI_STATREAD = const(0x02)
_SPI_DATAWRITE = const(0x01)
_SPI_DATAREAD = const(0x03)
_SPI_READY = const(0x01)
_SPI_STATREAD = const(0x02)
_SPI_DATAWRITE = const(0x01)
_SPI_DATAREAD = const(0x03)
_SPI_READY = const(0x01)


def reverse_bit(num):
"""Turn an LSB byte to an MSB byte, and vice versa. Used for SPI as
it is LSB for the PN532, but 99% of SPI implementations are MSB only!"""
result = 0
for _ in range(8):
result <<= 1
result += (num & 1)
result += num & 1
num >>= 1
return result


class PN532_SPI(PN532):
"""Driver for the PN532 connected over SPI. Pass in a hardware or bitbang
SPI device & chip select digitalInOut pin. Optional IRQ pin (not used),
reset pin and debugging output."""

def __init__(self, spi, cs_pin, *, irq=None, reset=None, debug=False):
"""Create an instance of the PN532 class using SPI"""
self.debug = debug
Expand All @@ -73,7 +76,7 @@ def _wakeup(self):
"""Send any special commands/data to wake up PN532"""
with self._spi as spi:
time.sleep(1)
spi.write(bytearray([0x00])) #pylint: disable=no-member
spi.write(bytearray([0x00])) # pylint: disable=no-member
time.sleep(1)

def _wait_ready(self, timeout=1):
Expand All @@ -83,27 +86,28 @@ def _wait_ready(self, timeout=1):
timestamp = time.monotonic()
with self._spi as spi:
while (time.monotonic() - timestamp) < timeout:
time.sleep(0.02) # required
spi.write_readinto(status_cmd, status_response) #pylint: disable=no-member
time.sleep(0.02) # required
spi.write_readinto(
status_cmd, status_response
) # pylint: disable=no-member
if reverse_bit(status_response[1]) == 0x01: # LSB data is read in MSB
return True # Not busy anymore!
else:
time.sleep(0.01) # pause a bit till we ask again
return True # Not busy anymore!
time.sleep(0.01) # pause a bit till we ask again
# We timed out!
return False

def _read_data(self, count):
"""Read a specified count of bytes from the PN532."""
# Build a read request frame.
frame = bytearray(count+1)
frame = bytearray(count + 1)
# Add the SPI data read signal byte, but LSB'ify it
frame[0] = reverse_bit(_SPI_DATAREAD)

with self._spi as spi:
time.sleep(0.02) # required
spi.write_readinto(frame, frame) #pylint: disable=no-member
time.sleep(0.02) # required
spi.write_readinto(frame, frame) # pylint: disable=no-member
for i, val in enumerate(frame):
frame[i] = reverse_bit(val) # turn LSB data to MSB
frame[i] = reverse_bit(val) # turn LSB data to MSB
if self.debug:
print("Reading: ", [hex(i) for i in frame[1:]])
return frame[1:]
Expand All @@ -116,5 +120,5 @@ def _write_data(self, framebytes):
if self.debug:
print("Writing: ", [hex(i) for i in rev_frame])
with self._spi as spi:
time.sleep(0.02) # required
spi.write(bytes(rev_frame)) #pylint: disable=no-member
time.sleep(0.02) # required
spi.write(bytes(rev_frame)) # pylint: disable=no-member
12 changes: 9 additions & 3 deletions adafruit_pn532/uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
import time
from adafruit_pn532.adafruit_pn532 import PN532, BusyError


class PN532_UART(PN532):
"""Driver for the PN532 connected over Serial UART"""

def __init__(self, uart, *, irq=None, reset=None, debug=False):
"""Create an instance of the PN532 class using Serial connection.
Optional IRQ pin (not used), reset pin and debugging output.
Expand All @@ -54,7 +56,7 @@ def __init__(self, uart, *, irq=None, reset=None, debug=False):

def _wakeup(self):
"""Send any special commands/data to wake up PN532"""
#self._write_frame([_HOSTTOPN532, _COMMAND_SAMCONFIGURATION, 0x01])
# self._write_frame([_HOSTTOPN532, _COMMAND_SAMCONFIGURATION, 0x01])
self.SAM_configuration()

def _wait_ready(self, timeout=1):
Expand All @@ -75,7 +77,11 @@ def _read_data(self, count):

def _write_data(self, framebytes):
"""Write a specified count of bytes to the PN532"""
while self._uart.read(1): # this would be a lot nicer if we could query the # of bytes
while self._uart.read(
1
): # this would be a lot nicer if we could query the # of bytes
pass
self._uart.write('\x55\x55\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') # wake up!
self._uart.write(
"\x55\x55\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
) # wake up!
self._uart.write(framebytes)
Loading