Skip to content

Ran black, updated to pylint 2.x #16

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 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
126 changes: 66 additions & 60 deletions adafruit_bluefruitspi.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,47 @@
from micropython import const

# pylint: disable=bad-whitespace
_MSG_COMMAND = const(0x10) # Command message
_MSG_RESPONSE = const(0x20) # Response message
_MSG_ALERT = const(0x40) # Alert message
_MSG_ERROR = const(0x80) # Error message

_SDEP_INITIALIZE = const(0xBEEF) # Resets the Bluefruit device
_SDEP_ATCOMMAND = const(0x0A00) # AT command wrapper
_SDEP_BLEUART_TX = const(0x0A01) # BLE UART transmit data
_SDEP_BLEUART_RX = const(0x0A02) # BLE UART read data

_ARG_STRING = const(0x0100) # String data type
_ARG_BYTEARRAY = const(0x0200) # Byte array data type
_ARG_INT32 = const(0x0300) # Signed 32-bit integer data type
_ARG_UINT32 = const(0x0400) # Unsigned 32-bit integer data type
_ARG_INT16 = const(0x0500) # Signed 16-bit integer data type
_ARG_UINT16 = const(0x0600) # Unsigned 16-bit integer data type
_ARG_INT8 = const(0x0700) # Signed 8-bit integer data type
_ARG_UINT8 = const(0x0800) # Unsigned 8-bit integer data type

_ERROR_INVALIDMSGTYPE = const(0x8021) # SDEP: Unexpected SDEP MsgType
_ERROR_INVALIDCMDID = const(0x8022) # SDEP: Unknown command ID
_ERROR_INVALIDPAYLOAD = const(0x8023) # SDEP: Payload problem
_ERROR_INVALIDLEN = const(0x8024) # SDEP: Indicated len too large
_ERROR_INVALIDINPUT = const(0x8060) # AT: Invalid data
_ERROR_UNKNOWNCMD = const(0x8061) # AT: Unknown command name
_ERROR_INVALIDPARAM = const(0x8062) # AT: Invalid param value
_ERROR_UNSUPPORTED = const(0x8063) # AT: Unsupported command
_MSG_COMMAND = const(0x10) # Command message
_MSG_RESPONSE = const(0x20) # Response message
_MSG_ALERT = const(0x40) # Alert message
_MSG_ERROR = const(0x80) # Error message

_SDEP_INITIALIZE = const(0xBEEF) # Resets the Bluefruit device
_SDEP_ATCOMMAND = const(0x0A00) # AT command wrapper
_SDEP_BLEUART_TX = const(0x0A01) # BLE UART transmit data
_SDEP_BLEUART_RX = const(0x0A02) # BLE UART read data

_ARG_STRING = const(0x0100) # String data type
_ARG_BYTEARRAY = const(0x0200) # Byte array data type
_ARG_INT32 = const(0x0300) # Signed 32-bit integer data type
_ARG_UINT32 = const(0x0400) # Unsigned 32-bit integer data type
_ARG_INT16 = const(0x0500) # Signed 16-bit integer data type
_ARG_UINT16 = const(0x0600) # Unsigned 16-bit integer data type
_ARG_INT8 = const(0x0700) # Signed 8-bit integer data type
_ARG_UINT8 = const(0x0800) # Unsigned 8-bit integer data type

_ERROR_INVALIDMSGTYPE = const(0x8021) # SDEP: Unexpected SDEP MsgType
_ERROR_INVALIDCMDID = const(0x8022) # SDEP: Unknown command ID
_ERROR_INVALIDPAYLOAD = const(0x8023) # SDEP: Payload problem
_ERROR_INVALIDLEN = const(0x8024) # SDEP: Indicated len too large
_ERROR_INVALIDINPUT = const(0x8060) # AT: Invalid data
_ERROR_UNKNOWNCMD = const(0x8061) # AT: Unknown command name
_ERROR_INVALIDPARAM = const(0x8062) # AT: Invalid param value
_ERROR_UNSUPPORTED = const(0x8063) # AT: Unsupported command

# For the Bluefruit Connect packets
_PACKET_BUTTON_LEN = const(5)
_PACKET_COLOR_LEN = const(6)
_PACKET_BUTTON_LEN = const(5)
_PACKET_COLOR_LEN = const(6)

# pylint: enable=bad-whitespace


class BluefruitSPI:
"""Helper for the Bluefruit LE SPI Friend"""

def __init__(self, spi, cs, irq, reset, debug=False): # pylint: disable=too-many-arguments
def __init__(
self, spi, cs, irq, reset, debug=False
): # pylint: disable=too-many-arguments
self._irq = irq
self._buf_tx = bytearray(20)
self._buf_rx = bytearray(20)
Expand All @@ -114,8 +116,7 @@ def __init__(self, spi, cs, irq, reset, debug=False): # pylint: disable=too-many
self._irq.direction = Direction.INPUT
self._irq.pull = Pull.DOWN

self._spi_device = SPIDevice(spi, cs,
baudrate=4000000, phase=0, polarity=0)
self._spi_device = SPIDevice(spi, cs, baudrate=4000000, phase=0, polarity=0)

def _cmd(self, cmd): # pylint: disable=too-many-branches
"""
Expand All @@ -129,9 +130,9 @@ def _cmd(self, cmd): # pylint: disable=too-many-branches
if len(cmd) > 127:
if self._debug:
print("ERROR: Command too long.")
raise ValueError('Command too long.')
raise ValueError("Command too long.")

more = 0x80 # More bit is in pos 8, 1 = more data available
more = 0x80 # More bit is in pos 8, 1 = more data available
pos = 0
while len(cmd) - pos:
# Construct the SDEP packet
Expand All @@ -142,9 +143,15 @@ def _cmd(self, cmd): # pylint: disable=too-many-branches
if plen > 16:
plen = 16
# Note the 'more' value in bit 8 of the packet len
struct.pack_into("<BHB16s", self._buf_tx, 0,
_MSG_COMMAND, _SDEP_ATCOMMAND,
plen | more, cmd[pos:pos+plen])
struct.pack_into(
"<BHB16s",
self._buf_tx,
0,
_MSG_COMMAND,
_SDEP_ATCOMMAND,
plen | more,
cmd[pos : pos + plen],
)
if self._debug:
print("Writing: ", [hex(b) for b in self._buf_tx])
else:
Expand All @@ -155,7 +162,7 @@ def _cmd(self, cmd): # pylint: disable=too-many-branches

# Send out the SPI bus
with self._spi_device as spi:
spi.write(self._buf_tx, end=len(cmd) + 4) # pylint: disable=no-member
spi.write(self._buf_tx, end=len(cmd) + 4) # pylint: disable=no-member

# Wait up to 200ms for a response
timeout = 0.2
Expand All @@ -165,7 +172,7 @@ def _cmd(self, cmd): # pylint: disable=too-many-branches
if timeout <= 0:
if self._debug:
print("ERROR: Timed out waiting for a response.")
raise RuntimeError('Timed out waiting for a response.')
raise RuntimeError("Timed out waiting for a response.")

# Retrieve the response message
msgtype = 0
Expand All @@ -179,11 +186,11 @@ def _cmd(self, cmd): # pylint: disable=too-many-branches
spi.readinto(self._buf_rx)

# Read the message envelope and contents
msgtype, rspid, rsplen = struct.unpack('>BHB', self._buf_rx[0:4])
msgtype, rspid, rsplen = struct.unpack(">BHB", self._buf_rx[0:4])
if rsplen >= 16:
rsp += self._buf_rx[4:20]
else:
rsp += self._buf_rx[4:rsplen+4]
rsp += self._buf_rx[4 : rsplen + 4]
if self._debug:
print("Reading: ", [hex(b) for b in self._buf_rx])
else:
Expand All @@ -200,35 +207,34 @@ def init(self):
This command should complete in under 1s.
"""
# Construct the SDEP packet
struct.pack_into("<BHB", self._buf_tx, 0,
_MSG_COMMAND, _SDEP_INITIALIZE, 0)
struct.pack_into("<BHB", self._buf_tx, 0, _MSG_COMMAND, _SDEP_INITIALIZE, 0)
if self._debug:
print("Writing: ", [hex(b) for b in self._buf_tx])

# Send out the SPI bus
with self._spi_device as spi:
spi.write(self._buf_tx, end=4) # pylint: disable=no-member
spi.write(self._buf_tx, end=4) # pylint: disable=no-member

# Wait 1 second for the command to complete.
time.sleep(1)

@property
def connected(self):
"""Whether the Bluefruit module is connected to the central"""
return int(self.command_check_OK(b'AT+GAPGETCONN')) == 1
return int(self.command_check_OK(b"AT+GAPGETCONN")) == 1

def uart_tx(self, data):
"""
Sends the specific bytestring out over BLE UART.
:param data: The bytestring to send.
"""
return self._cmd(b'AT+BLEUARTTX='+data+b'\r\n')
return self._cmd(b"AT+BLEUARTTX=" + data + b"\r\n")

def uart_rx(self):
"""
Reads byte data from the BLE UART FIFO.
"""
data = self.command_check_OK(b'AT+BLEUARTRX')
data = self.command_check_OK(b"AT+BLEUARTRX")
if data:
# remove \r\n from ending
return data[:-2]
Expand All @@ -237,7 +243,7 @@ def uart_rx(self):
def command(self, string):
"""Send a command and check response code"""
try:
msgtype, msgid, rsp = self._cmd(string+"\n")
msgtype, msgid, rsp = self._cmd(string + "\n")
if msgtype == _MSG_ERROR:
raise RuntimeError("Error (id:{0})".format(hex(msgid)))
if msgtype == _MSG_RESPONSE:
Expand All @@ -246,20 +252,20 @@ def command(self, string):
except RuntimeError as error:
raise RuntimeError("AT command failure: " + repr(error))

def command_check_OK(self, command, delay=0.0): # pylint: disable=invalid-name
def command_check_OK(self, command, delay=0.0): # pylint: disable=invalid-name
"""Send a fully formed bytestring AT command, and check
whether we got an 'OK' back. Returns payload bytes if there is any"""
ret = self.command(command)
time.sleep(delay)
if not ret or not ret[-4:]:
raise RuntimeError("Not OK")
if ret[-4:] != b'OK\r\n':
if ret[-4:] != b"OK\r\n":
raise RuntimeError("Not OK")
if ret[:-4]:
return ret[:-4]
return None

def read_packet(self): # pylint: disable=too-many-return-statements
def read_packet(self): # pylint: disable=too-many-return-statements
"""
Will read a Bluefruit Connect packet and return it in a parsed format.
Currently supports Button and Color packets only
Expand All @@ -270,16 +276,16 @@ def read_packet(self): # pylint: disable=too-many-return-statements
# convert to an array of character bytes
self._buffer += [chr(b) for b in data]
# Find beginning of new packet, starts with a '!'
while self._buffer and self._buffer[0] != '!':
while self._buffer and self._buffer[0] != "!":
self._buffer.pop(0)
# we need at least 2 bytes in the buffer
if len(self._buffer) < 2:
return None

# Packet beginning found
if self._buffer[1] == 'B':
if self._buffer[1] == "B":
plen = _PACKET_BUTTON_LEN
elif self._buffer[1] == 'C':
elif self._buffer[1] == "C":
plen = _PACKET_COLOR_LEN
else:
# unknown packet type
Expand All @@ -289,16 +295,16 @@ def read_packet(self): # pylint: disable=too-many-return-statements
# split packet off of buffer cache
packet = self._buffer[0:plen]

self._buffer = self._buffer[plen:] # remove packet from buffer
if sum([ord(x) for x in packet]) % 256 != 255: # check sum
self._buffer = self._buffer[plen:] # remove packet from buffer
if sum([ord(x) for x in packet]) % 256 != 255: # check sum
return None

# OK packet's good!
if packet[1] == 'B': # buttons have 2 int args to parse
if packet[1] == "B": # buttons have 2 int args to parse
# button number & True/False press
return ('B', int(packet[2]), packet[3] == '1')
if packet[1] == 'C': # colorpick has 3 int args to parse
return ("B", int(packet[2]), packet[3] == "1")
if packet[1] == "C": # colorpick has 3 int args to parse
# red, green and blue
return ('C', ord(packet[2]), ord(packet[3]), ord(packet[4]))
return ("C", ord(packet[2]), ord(packet[3]), ord(packet[4]))
# We don't nicely parse this yet
return packet[1:-1]
Loading