Skip to content

Commit 7b8a853

Browse files
committed
pylint and black fixes
1 parent b4d4acf commit 7b8a853

3 files changed

Lines changed: 54 additions & 13 deletions

File tree

adafruit_usb_host_midi.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@
2020

2121
DIR_IN = 0x80
2222

23+
2324
class MIDI:
25+
"""
26+
Stream-like MIDI device for use with `adafruit_midi` and similar upstream
27+
MIDI parser libraries.
28+
29+
:param device: a ``usb.core.Device` object which implements
30+
``read(endpoint, buffer)`` and ``write(endpoint,buffer)``
31+
:param float timeout: timeout in seconds to wait for read or write operation
32+
to succeeds. Default to None, i.e. reads and writes will block.
33+
"""
34+
2435
def __init__(self, device, timeout=None):
2536
self.interface_number = 0
2637
self.in_ep = 0
@@ -42,14 +53,16 @@ def __init__(self, device, timeout=None):
4253
descriptor_len = config_descriptor[i]
4354
descriptor_type = config_descriptor[i + 1]
4455
if descriptor_type == adafruit_usb_host_descriptors.DESC_CONFIGURATION:
56+
# pylint: disable=unused-variable
4557
config_value = config_descriptor[i + 5]
58+
# pylint: enable=unused-variable
4659
elif descriptor_type == adafruit_usb_host_descriptors.DESC_INTERFACE:
4760
interface_number = config_descriptor[i + 2]
4861
interface_class = config_descriptor[i + 5]
4962
interface_subclass = config_descriptor[i + 6]
5063
midi_interface = interface_class == 0x1 and interface_subclass == 0x3
5164
if midi_interface:
52-
self.interface_number= interface_number
65+
self.interface_number = interface_number
5366

5467
elif descriptor_type == adafruit_usb_host_descriptors.DESC_ENDPOINT:
5568
endpoint_address = config_descriptor[i + 2]
@@ -65,20 +78,39 @@ def __init__(self, device, timeout=None):
6578
device.detach_kernel_driver(self.interface_number)
6679

6780
def read(self, size):
81+
"""
82+
Read bytes. If ``nbytes`` is specified then read at most that many
83+
bytes. Otherwise, read everything that arrives until the connection
84+
times out. Providing the number of bytes expected is highly recommended
85+
because it will be faster. If no bytes are read, return ``None``.
86+
87+
.. note:: When no bytes are read due to a timeout, this function returns ``None``.
88+
This matches the behavior of `io.RawIOBase.read` in Python 3, but
89+
differs from pyserial which returns ``b''`` in that situation.
90+
91+
:return: Data read
92+
:rtype: bytes or None
93+
"""
94+
6895
if self._remaining == 0:
6996
try:
7097
n = self.device.read(self.in_ep, self.buf, self.timeout_ms)
7198
self._remaining = n - 1
7299
self.start = 1
73-
except usb.core.USBTimeoutError as e:
100+
except usb.core.USBTimeoutError:
74101
pass
75102
size = min(size, self._remaining)
76-
b = self.buf[self.start:self.start + size]
103+
b = self.buf[self.start : self.start + size]
77104
self.start += size
78105
self._remaining -= size
79106
return b
80107

81108
def readinto(self, buf):
109+
"""Read bytes into the ``buf``. Read at most ``len(buf)`` bytes.
110+
111+
:return: number of bytes read and stored into ``buf``
112+
:rtype: int or None (on a non-blocking error)
113+
"""
82114
b = self.read(len(buf))
83115
n = len(b)
84116
if n:
@@ -87,4 +119,9 @@ def readinto(self, buf):
87119

88120
def __repr__(self):
89121
# also idProduct/idVendor for vid/pid
90-
return "MIDI Device " + str(self.device.manufacturer) + "/" + str(self.device.product)
122+
return (
123+
"MIDI Device "
124+
+ str(self.device.manufacturer)
125+
+ "/"
126+
+ str(self.device.product)
127+
)

examples/usb_host_midi_simpletest.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import audiobusio
66
import board
77
import synthio
8-
import adafruit_midi
9-
import adafruit_usb_host_midi
108
import usb.core
9+
import wm8960
10+
import adafruit_midi
1111
from adafruit_midi.note_on import NoteOn
1212
from adafruit_midi.note_off import NoteOff
13-
import wm8960
13+
import adafruit_usb_host_midi
1414

1515
print("Looking for midi device")
1616
raw_midi = None
@@ -26,7 +26,9 @@
2626
# This setup is for the headphone output on the iMX RT 1060 EVK.
2727
dac = wm8960.WM8960(board.I2C())
2828
dac.start_i2s_out()
29-
audio = audiobusio.I2SOut(board.AUDIO_BCLK, board.AUDIO_SYNC, board.AUDIO_TXD, main_clock=board.AUDIO_MCLK)
29+
audio = audiobusio.I2SOut(
30+
board.AUDIO_BCLK, board.AUDIO_SYNC, board.AUDIO_TXD, main_clock=board.AUDIO_MCLK
31+
)
3032
synth = synthio.Synthesizer(sample_rate=44100)
3133
audio.play(synth)
3234

@@ -41,7 +43,9 @@
4143
print("noteOn: ", msg.note, "vel:", msg.velocity)
4244
synth.press(note)
4345
pressed[msg.note] = note
44-
elif (isinstance(msg,NoteOff) or (isinstance(msg,NoteOn) and msg.velocity==0)) and msg.note in pressed:
46+
elif (
47+
isinstance(msg, NoteOff) or (isinstance(msg, NoteOn) and msg.velocity == 0)
48+
) and msg.note in pressed:
4549
print("noteOff:", msg.note, "vel:", msg.velocity)
4650
note = pressed[msg.note]
4751
synth.release(note)

examples/usb_host_midi_simpletest_rp2040usbfeather.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2023 Scott Shawcroft for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
4+
# pylint: disable=unused-import
45

56
import board
67
import busio
7-
import synthio
8-
import adafruit_midi
9-
import adafruit_usb_host_midi
108
import usb.core
9+
import adafruit_midi
1110
from adafruit_midi.note_on import NoteOn
1211
from adafruit_midi.note_off import NoteOff
1312
from adafruit_midi.control_change import ControlChange
1413
from adafruit_midi.pitch_bend import PitchBend
14+
import adafruit_usb_host_midi
1515

1616
print("Looking for midi device")
1717
raw_midi = None
@@ -35,5 +35,5 @@
3535
while True:
3636
msg = midi_device.receive()
3737
if msg:
38-
print("midi msg:",msg)
38+
print("midi msg:", msg)
3939
midi_uart.send(msg)

0 commit comments

Comments
 (0)