Skip to content

Commit 66360d2

Browse files
authored
Merge pull request #16 from adafruit/pylint-update
Ran black, updated to pylint 2.x
2 parents f5d9659 + 1c8f6ca commit 66360d2

7 files changed

+188
-156
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
source actions-ci/install.sh
4141
- name: Pip install pylint, black, & Sphinx
4242
run: |
43-
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
43+
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
4444
- name: Library version
4545
run: git describe --dirty --always --tags
4646
- name: PyLint

adafruit_bluefruitspi.py

+66-60
Original file line numberDiff line numberDiff line change
@@ -52,45 +52,47 @@
5252
from micropython import const
5353

5454
# pylint: disable=bad-whitespace
55-
_MSG_COMMAND = const(0x10) # Command message
56-
_MSG_RESPONSE = const(0x20) # Response message
57-
_MSG_ALERT = const(0x40) # Alert message
58-
_MSG_ERROR = const(0x80) # Error message
59-
60-
_SDEP_INITIALIZE = const(0xBEEF) # Resets the Bluefruit device
61-
_SDEP_ATCOMMAND = const(0x0A00) # AT command wrapper
62-
_SDEP_BLEUART_TX = const(0x0A01) # BLE UART transmit data
63-
_SDEP_BLEUART_RX = const(0x0A02) # BLE UART read data
64-
65-
_ARG_STRING = const(0x0100) # String data type
66-
_ARG_BYTEARRAY = const(0x0200) # Byte array data type
67-
_ARG_INT32 = const(0x0300) # Signed 32-bit integer data type
68-
_ARG_UINT32 = const(0x0400) # Unsigned 32-bit integer data type
69-
_ARG_INT16 = const(0x0500) # Signed 16-bit integer data type
70-
_ARG_UINT16 = const(0x0600) # Unsigned 16-bit integer data type
71-
_ARG_INT8 = const(0x0700) # Signed 8-bit integer data type
72-
_ARG_UINT8 = const(0x0800) # Unsigned 8-bit integer data type
73-
74-
_ERROR_INVALIDMSGTYPE = const(0x8021) # SDEP: Unexpected SDEP MsgType
75-
_ERROR_INVALIDCMDID = const(0x8022) # SDEP: Unknown command ID
76-
_ERROR_INVALIDPAYLOAD = const(0x8023) # SDEP: Payload problem
77-
_ERROR_INVALIDLEN = const(0x8024) # SDEP: Indicated len too large
78-
_ERROR_INVALIDINPUT = const(0x8060) # AT: Invalid data
79-
_ERROR_UNKNOWNCMD = const(0x8061) # AT: Unknown command name
80-
_ERROR_INVALIDPARAM = const(0x8062) # AT: Invalid param value
81-
_ERROR_UNSUPPORTED = const(0x8063) # AT: Unsupported command
55+
_MSG_COMMAND = const(0x10) # Command message
56+
_MSG_RESPONSE = const(0x20) # Response message
57+
_MSG_ALERT = const(0x40) # Alert message
58+
_MSG_ERROR = const(0x80) # Error message
59+
60+
_SDEP_INITIALIZE = const(0xBEEF) # Resets the Bluefruit device
61+
_SDEP_ATCOMMAND = const(0x0A00) # AT command wrapper
62+
_SDEP_BLEUART_TX = const(0x0A01) # BLE UART transmit data
63+
_SDEP_BLEUART_RX = const(0x0A02) # BLE UART read data
64+
65+
_ARG_STRING = const(0x0100) # String data type
66+
_ARG_BYTEARRAY = const(0x0200) # Byte array data type
67+
_ARG_INT32 = const(0x0300) # Signed 32-bit integer data type
68+
_ARG_UINT32 = const(0x0400) # Unsigned 32-bit integer data type
69+
_ARG_INT16 = const(0x0500) # Signed 16-bit integer data type
70+
_ARG_UINT16 = const(0x0600) # Unsigned 16-bit integer data type
71+
_ARG_INT8 = const(0x0700) # Signed 8-bit integer data type
72+
_ARG_UINT8 = const(0x0800) # Unsigned 8-bit integer data type
73+
74+
_ERROR_INVALIDMSGTYPE = const(0x8021) # SDEP: Unexpected SDEP MsgType
75+
_ERROR_INVALIDCMDID = const(0x8022) # SDEP: Unknown command ID
76+
_ERROR_INVALIDPAYLOAD = const(0x8023) # SDEP: Payload problem
77+
_ERROR_INVALIDLEN = const(0x8024) # SDEP: Indicated len too large
78+
_ERROR_INVALIDINPUT = const(0x8060) # AT: Invalid data
79+
_ERROR_UNKNOWNCMD = const(0x8061) # AT: Unknown command name
80+
_ERROR_INVALIDPARAM = const(0x8062) # AT: Invalid param value
81+
_ERROR_UNSUPPORTED = const(0x8063) # AT: Unsupported command
8282

8383
# For the Bluefruit Connect packets
84-
_PACKET_BUTTON_LEN = const(5)
85-
_PACKET_COLOR_LEN = const(6)
84+
_PACKET_BUTTON_LEN = const(5)
85+
_PACKET_COLOR_LEN = const(6)
8686

8787
# pylint: enable=bad-whitespace
8888

8989

9090
class BluefruitSPI:
9191
"""Helper for the Bluefruit LE SPI Friend"""
9292

93-
def __init__(self, spi, cs, irq, reset, debug=False): # pylint: disable=too-many-arguments
93+
def __init__(
94+
self, spi, cs, irq, reset, debug=False
95+
): # pylint: disable=too-many-arguments
9496
self._irq = irq
9597
self._buf_tx = bytearray(20)
9698
self._buf_rx = bytearray(20)
@@ -114,8 +116,7 @@ def __init__(self, spi, cs, irq, reset, debug=False): # pylint: disable=too-many
114116
self._irq.direction = Direction.INPUT
115117
self._irq.pull = Pull.DOWN
116118

117-
self._spi_device = SPIDevice(spi, cs,
118-
baudrate=4000000, phase=0, polarity=0)
119+
self._spi_device = SPIDevice(spi, cs, baudrate=4000000, phase=0, polarity=0)
119120

120121
def _cmd(self, cmd): # pylint: disable=too-many-branches
121122
"""
@@ -129,9 +130,9 @@ def _cmd(self, cmd): # pylint: disable=too-many-branches
129130
if len(cmd) > 127:
130131
if self._debug:
131132
print("ERROR: Command too long.")
132-
raise ValueError('Command too long.')
133+
raise ValueError("Command too long.")
133134

134-
more = 0x80 # More bit is in pos 8, 1 = more data available
135+
more = 0x80 # More bit is in pos 8, 1 = more data available
135136
pos = 0
136137
while len(cmd) - pos:
137138
# Construct the SDEP packet
@@ -142,9 +143,15 @@ def _cmd(self, cmd): # pylint: disable=too-many-branches
142143
if plen > 16:
143144
plen = 16
144145
# Note the 'more' value in bit 8 of the packet len
145-
struct.pack_into("<BHB16s", self._buf_tx, 0,
146-
_MSG_COMMAND, _SDEP_ATCOMMAND,
147-
plen | more, cmd[pos:pos+plen])
146+
struct.pack_into(
147+
"<BHB16s",
148+
self._buf_tx,
149+
0,
150+
_MSG_COMMAND,
151+
_SDEP_ATCOMMAND,
152+
plen | more,
153+
cmd[pos : pos + plen],
154+
)
148155
if self._debug:
149156
print("Writing: ", [hex(b) for b in self._buf_tx])
150157
else:
@@ -155,7 +162,7 @@ def _cmd(self, cmd): # pylint: disable=too-many-branches
155162

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

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

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

181188
# Read the message envelope and contents
182-
msgtype, rspid, rsplen = struct.unpack('>BHB', self._buf_rx[0:4])
189+
msgtype, rspid, rsplen = struct.unpack(">BHB", self._buf_rx[0:4])
183190
if rsplen >= 16:
184191
rsp += self._buf_rx[4:20]
185192
else:
186-
rsp += self._buf_rx[4:rsplen+4]
193+
rsp += self._buf_rx[4 : rsplen + 4]
187194
if self._debug:
188195
print("Reading: ", [hex(b) for b in self._buf_rx])
189196
else:
@@ -200,35 +207,34 @@ def init(self):
200207
This command should complete in under 1s.
201208
"""
202209
# Construct the SDEP packet
203-
struct.pack_into("<BHB", self._buf_tx, 0,
204-
_MSG_COMMAND, _SDEP_INITIALIZE, 0)
210+
struct.pack_into("<BHB", self._buf_tx, 0, _MSG_COMMAND, _SDEP_INITIALIZE, 0)
205211
if self._debug:
206212
print("Writing: ", [hex(b) for b in self._buf_tx])
207213

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

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

215221
@property
216222
def connected(self):
217223
"""Whether the Bluefruit module is connected to the central"""
218-
return int(self.command_check_OK(b'AT+GAPGETCONN')) == 1
224+
return int(self.command_check_OK(b"AT+GAPGETCONN")) == 1
219225

220226
def uart_tx(self, data):
221227
"""
222228
Sends the specific bytestring out over BLE UART.
223229
:param data: The bytestring to send.
224230
"""
225-
return self._cmd(b'AT+BLEUARTTX='+data+b'\r\n')
231+
return self._cmd(b"AT+BLEUARTTX=" + data + b"\r\n")
226232

227233
def uart_rx(self):
228234
"""
229235
Reads byte data from the BLE UART FIFO.
230236
"""
231-
data = self.command_check_OK(b'AT+BLEUARTRX')
237+
data = self.command_check_OK(b"AT+BLEUARTRX")
232238
if data:
233239
# remove \r\n from ending
234240
return data[:-2]
@@ -237,7 +243,7 @@ def uart_rx(self):
237243
def command(self, string):
238244
"""Send a command and check response code"""
239245
try:
240-
msgtype, msgid, rsp = self._cmd(string+"\n")
246+
msgtype, msgid, rsp = self._cmd(string + "\n")
241247
if msgtype == _MSG_ERROR:
242248
raise RuntimeError("Error (id:{0})".format(hex(msgid)))
243249
if msgtype == _MSG_RESPONSE:
@@ -246,20 +252,20 @@ def command(self, string):
246252
except RuntimeError as error:
247253
raise RuntimeError("AT command failure: " + repr(error))
248254

249-
def command_check_OK(self, command, delay=0.0): # pylint: disable=invalid-name
255+
def command_check_OK(self, command, delay=0.0): # pylint: disable=invalid-name
250256
"""Send a fully formed bytestring AT command, and check
251257
whether we got an 'OK' back. Returns payload bytes if there is any"""
252258
ret = self.command(command)
253259
time.sleep(delay)
254260
if not ret or not ret[-4:]:
255261
raise RuntimeError("Not OK")
256-
if ret[-4:] != b'OK\r\n':
262+
if ret[-4:] != b"OK\r\n":
257263
raise RuntimeError("Not OK")
258264
if ret[:-4]:
259265
return ret[:-4]
260266
return None
261267

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

279285
# Packet beginning found
280-
if self._buffer[1] == 'B':
286+
if self._buffer[1] == "B":
281287
plen = _PACKET_BUTTON_LEN
282-
elif self._buffer[1] == 'C':
288+
elif self._buffer[1] == "C":
283289
plen = _PACKET_COLOR_LEN
284290
else:
285291
# unknown packet type
@@ -289,16 +295,16 @@ def read_packet(self): # pylint: disable=too-many-return-statements
289295
# split packet off of buffer cache
290296
packet = self._buffer[0:plen]
291297

292-
self._buffer = self._buffer[plen:] # remove packet from buffer
293-
if sum([ord(x) for x in packet]) % 256 != 255: # check sum
298+
self._buffer = self._buffer[plen:] # remove packet from buffer
299+
if sum([ord(x) for x in packet]) % 256 != 255: # check sum
294300
return None
295301

296302
# OK packet's good!
297-
if packet[1] == 'B': # buttons have 2 int args to parse
303+
if packet[1] == "B": # buttons have 2 int args to parse
298304
# button number & True/False press
299-
return ('B', int(packet[2]), packet[3] == '1')
300-
if packet[1] == 'C': # colorpick has 3 int args to parse
305+
return ("B", int(packet[2]), packet[3] == "1")
306+
if packet[1] == "C": # colorpick has 3 int args to parse
301307
# red, green and blue
302-
return ('C', ord(packet[2]), ord(packet[3]), ord(packet[4]))
308+
return ("C", ord(packet[2]), ord(packet[3]), ord(packet[4]))
303309
# We don't nicely parse this yet
304310
return packet[1:-1]

0 commit comments

Comments
 (0)