Skip to content

Commit e8aeb9e

Browse files
committed
Black and lint
1 parent 67e1313 commit e8aeb9e

File tree

5 files changed

+144
-107
lines changed

5 files changed

+144
-107
lines changed

adafruit_ble_midi.py

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,69 @@
3939
__version__ = "0.0.0-auto.0"
4040
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_MIDI.git"
4141

42+
4243
class _MidiCharacteristic(ComplexCharacteristic):
4344
"""Endpoint for sending commands to a media player. The value read will list all available
4445
4546
commands."""
47+
4648
uuid = VendorUUID("7772E5DB-3868-4112-A1A9-F2669D106BF3")
4749

4850
def __init__(self):
49-
super().__init__(properties=Characteristic.WRITE_NO_RESPONSE | Characteristic.READ | Characteristic.NOTIFY,
50-
read_perm=Attribute.ENCRYPT_NO_MITM, write_perm=Attribute.ENCRYPT_NO_MITM,
51-
max_length=512,
52-
fixed_length=False)
51+
super().__init__(
52+
properties=Characteristic.WRITE_NO_RESPONSE
53+
| Characteristic.READ
54+
| Characteristic.NOTIFY,
55+
read_perm=Attribute.ENCRYPT_NO_MITM,
56+
write_perm=Attribute.ENCRYPT_NO_MITM,
57+
max_length=512,
58+
fixed_length=False,
59+
)
5360

5461
def bind(self, service):
5562
"""Binds the characteristic to the given Service."""
5663
bound_characteristic = super().bind(service)
57-
return _bleio.PacketBuffer(bound_characteristic,
58-
buffer_size=4)
64+
return _bleio.PacketBuffer(bound_characteristic, buffer_size=4)
65+
5966

6067
class MIDIService(Service):
68+
"""BLE MIDI service. It acts just like a USB MIDI PortIn and PortOut and can be used as a drop
69+
in replacement.
70+
71+
BLE MIDI's protocol includes timestamps for MIDI messages. This class automatically adds them
72+
to MIDI data written out and strips them from MIDI data read in."""
73+
6174
uuid = VendorUUID("03B80E5A-EDE8-4B33-A751-6CE34EC4C700")
6275
_raw = _MidiCharacteristic()
76+
# _raw gets shadowed for each MIDIService instance by a PacketBuffer. PyLint doesn't know this
77+
# so it complains about missing members.
78+
# pylint: disable=no-member
6379

6480
def __init__(self, **kwargs):
6581
super().__init__(**kwargs)
6682
self._in_buffer = bytearray(self._raw.packet_size)
6783
self._out_buffer = None
6884
shared_buffer = memoryview(bytearray(4))
69-
self._buffers = [None, shared_buffer[:1], shared_buffer[:2], shared_buffer[:3], shared_buffer[:4]]
85+
self._buffers = [
86+
None,
87+
shared_buffer[:1],
88+
shared_buffer[:2],
89+
shared_buffer[:3],
90+
shared_buffer[:4],
91+
]
7092
self._header = bytearray(1)
7193
self._in_sysex = False
7294
self._message_target_length = None
7395
self._message_length = 0
7496
self._pending_realtime = None
7597
self._in_length = 0
7698
self._in_index = 1
77-
self._last_read = time.monotonic_ns() // 1000000
78-
self._last_low_ms = None
7999
self._last_data = True
80100

81101
def readinto(self, buf, length):
102+
"""Reads up to ``length`` bytes into ``buf`` starting at index 0.
103+
104+
Returns the number of bytes written into ``buf``."""
82105
i = 0
83106
while i < length:
84107
if self._in_index < self._in_length:
@@ -109,45 +132,53 @@ def readinto(self, buf, length):
109132
return i
110133

111134
def read(self, length):
135+
"""Reads up to ``length`` bytes and returns them."""
112136
result = bytearray(length)
113137
i = self.readinto(result, length)
114138
return result[:i]
115139

116140
def write(self, buf, length):
141+
"""Writes ``length`` bytes out."""
142+
# pylint: disable=too-many-branches
117143
timestamp_ms = time.monotonic_ns() // 1000000
118-
self._header[0] = (timestamp_ms >> 7 & 0x3f) | 0x80
144+
self._header[0] = (timestamp_ms >> 7 & 0x3F) | 0x80
119145
i = 0
120146
while i < length:
121147
data = buf[i]
122148
command = data & 0x80 != 0
123149
if self._in_sysex:
124-
if command: # End of sysex or real time
150+
if command: # End of sysex or real time
125151
b = self._buffers[2]
126-
b[0] = 0x80 | (timestamp_ms & 0x7f)
127-
b[1] = 0xf7
152+
b[0] = 0x80 | (timestamp_ms & 0x7F)
153+
b[1] = 0xF7
128154
self._raw.write(b, header=self._header)
129-
self._in_sysex = data == 0xf7
155+
self._in_sysex = data == 0xF7
130156
else:
131157
b = self._buffers[1]
132158
b[0] = data
133159
self._raw.write(b, header=self._header)
134160
elif command:
135-
self._in_sysex = data == 0xf0
161+
self._in_sysex = data == 0xF0
136162
b = self._buffers[2]
137-
b[0] = 0x80 | (timestamp_ms & 0x7f)
163+
b[0] = 0x80 | (timestamp_ms & 0x7F)
138164
b[1] = data
139-
if 0xf6 <= data <= 0xff or self._in_sysex: # Real time, command only or start sysex
165+
if (
166+
0xF6 <= data <= 0xFF or self._in_sysex
167+
): # Real time, command only or start sysex
140168
if self._message_target_length:
141169
self._pending_realtime = b
142170
else:
143171
self._raw.write(b, self._header)
144172
else:
145-
if 0x80 <= data <= 0xbf or 0xe0 <= data <= 0xef or data == 0xf2: # Two following bytes
173+
if (
174+
0x80 <= data <= 0xBF or 0xE0 <= data <= 0xEF or data == 0xF2
175+
): # Two following bytes
146176
self._message_target_length = 4
147177
else:
148178
self._message_target_length = 3
149179
b = self._buffers[self._message_target_length]
150-
# All of the buffers share memory so the timestamp and data have already been set.
180+
# All of the buffers share memory so the timestamp and data have already been
181+
# set.
151182
self._message_length = 2
152183
self._out_buffer = b
153184
else:

docs/conf.py

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
import os
44
import sys
5-
sys.path.insert(0, os.path.abspath('..'))
5+
6+
sys.path.insert(0, os.path.abspath(".."))
67

78
# -- General configuration ------------------------------------------------
89

910
# Add any Sphinx extension module names here, as strings. They can be
1011
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
1112
# ones.
1213
extensions = [
13-
'sphinx.ext.autodoc',
14-
'sphinx.ext.intersphinx',
15-
'sphinx.ext.napoleon',
16-
'sphinx.ext.todo',
14+
"sphinx.ext.autodoc",
15+
"sphinx.ext.intersphinx",
16+
"sphinx.ext.napoleon",
17+
"sphinx.ext.todo",
1718
]
1819

1920
# TODO: Please Read!
@@ -23,29 +24,32 @@
2324
# autodoc_mock_imports = ["digitalio", "busio"]
2425

2526

26-
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}
27+
intersphinx_mapping = {
28+
"python": ("https://docs.python.org/3.4", None),
29+
"CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None),
30+
}
2731

2832
# Add any paths that contain templates here, relative to this directory.
29-
templates_path = ['_templates']
33+
templates_path = ["_templates"]
3034

31-
source_suffix = '.rst'
35+
source_suffix = ".rst"
3236

3337
# The master toctree document.
34-
master_doc = 'index'
38+
master_doc = "index"
3539

3640
# General information about the project.
37-
project = u'Adafruit BLE_MIDI Library'
38-
copyright = u'2020 Scott Shawcroft'
39-
author = u'Scott Shawcroft'
41+
project = "Adafruit BLE_MIDI Library"
42+
copyright = "2020 Scott Shawcroft"
43+
author = "Scott Shawcroft"
4044

4145
# The version info for the project you're documenting, acts as replacement for
4246
# |version| and |release|, also used in various other places throughout the
4347
# built documents.
4448
#
4549
# The short X.Y version.
46-
version = u'1.0'
50+
version = "1.0"
4751
# The full version, including alpha/beta/rc tags.
48-
release = u'1.0'
52+
release = "1.0"
4953

5054
# The language for content autogenerated by Sphinx. Refer to documentation
5155
# for a list of supported languages.
@@ -57,7 +61,7 @@
5761
# List of patterns, relative to source directory, that match files and
5862
# directories to ignore when looking for source files.
5963
# This patterns also effect to html_static_path and html_extra_path
60-
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', '.env', 'CODE_OF_CONDUCT.md']
64+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", ".env", "CODE_OF_CONDUCT.md"]
6165

6266
# The reST default role (used for this markup: `text`) to use for all
6367
# documents.
@@ -69,7 +73,7 @@
6973
add_function_parentheses = True
7074

7175
# The name of the Pygments (syntax highlighting) style to use.
72-
pygments_style = 'sphinx'
76+
pygments_style = "sphinx"
7377

7478
# If true, `todo` and `todoList` produce output, else they produce nothing.
7579
todo_include_todos = False
@@ -84,68 +88,76 @@
8488
# The theme to use for HTML and HTML Help pages. See the documentation for
8589
# a list of builtin themes.
8690
#
87-
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
91+
on_rtd = os.environ.get("READTHEDOCS", None) == "True"
8892

8993
if not on_rtd: # only import and set the theme if we're building docs locally
9094
try:
9195
import sphinx_rtd_theme
92-
html_theme = 'sphinx_rtd_theme'
93-
html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), '.']
96+
97+
html_theme = "sphinx_rtd_theme"
98+
html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), "."]
9499
except:
95-
html_theme = 'default'
96-
html_theme_path = ['.']
100+
html_theme = "default"
101+
html_theme_path = ["."]
97102
else:
98-
html_theme_path = ['.']
103+
html_theme_path = ["."]
99104

100105
# Add any paths that contain custom static files (such as style sheets) here,
101106
# relative to this directory. They are copied after the builtin static files,
102107
# so a file named "default.css" will overwrite the builtin "default.css".
103-
html_static_path = ['_static']
108+
html_static_path = ["_static"]
104109

105110
# The name of an image file (relative to this directory) to use as a favicon of
106111
# the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
107112
# pixels large.
108113
#
109-
html_favicon = '_static/favicon.ico'
114+
html_favicon = "_static/favicon.ico"
110115

111116
# Output file base name for HTML help builder.
112-
htmlhelp_basename = 'AdafruitBle_midiLibrarydoc'
117+
htmlhelp_basename = "AdafruitBle_midiLibrarydoc"
113118

114119
# -- Options for LaTeX output ---------------------------------------------
115120

116121
latex_elements = {
117-
# The paper size ('letterpaper' or 'a4paper').
118-
#
119-
# 'papersize': 'letterpaper',
120-
121-
# The font size ('10pt', '11pt' or '12pt').
122-
#
123-
# 'pointsize': '10pt',
124-
125-
# Additional stuff for the LaTeX preamble.
126-
#
127-
# 'preamble': '',
128-
129-
# Latex figure (float) alignment
130-
#
131-
# 'figure_align': 'htbp',
122+
# The paper size ('letterpaper' or 'a4paper').
123+
#
124+
# 'papersize': 'letterpaper',
125+
# The font size ('10pt', '11pt' or '12pt').
126+
#
127+
# 'pointsize': '10pt',
128+
# Additional stuff for the LaTeX preamble.
129+
#
130+
# 'preamble': '',
131+
# Latex figure (float) alignment
132+
#
133+
# 'figure_align': 'htbp',
132134
}
133135

134136
# Grouping the document tree into LaTeX files. List of tuples
135137
# (source start file, target name, title,
136138
# author, documentclass [howto, manual, or own class]).
137139
latex_documents = [
138-
(master_doc, 'AdafruitBLE_MIDILibrary.tex', u'AdafruitBLE_MIDI Library Documentation',
139-
author, 'manual'),
140+
(
141+
master_doc,
142+
"AdafruitBLE_MIDILibrary.tex",
143+
"AdafruitBLE_MIDI Library Documentation",
144+
author,
145+
"manual",
146+
),
140147
]
141148

142149
# -- Options for manual page output ---------------------------------------
143150

144151
# One entry per manual page. List of tuples
145152
# (source start file, name, description, authors, manual section).
146153
man_pages = [
147-
(master_doc, 'AdafruitBLE_MIDIlibrary', u'Adafruit BLE_MIDI Library Documentation',
148-
[author], 1)
154+
(
155+
master_doc,
156+
"AdafruitBLE_MIDIlibrary",
157+
"Adafruit BLE_MIDI Library Documentation",
158+
[author],
159+
1,
160+
)
149161
]
150162

151163
# -- Options for Texinfo output -------------------------------------------
@@ -154,7 +166,13 @@
154166
# (source start file, target name, title, author,
155167
# dir menu entry, description, category)
156168
texinfo_documents = [
157-
(master_doc, 'AdafruitBLE_MIDILibrary', u'Adafruit BLE_MIDI Library Documentation',
158-
author, 'AdafruitBLE_MIDILibrary', 'One line description of project.',
159-
'Miscellaneous'),
169+
(
170+
master_doc,
171+
"AdafruitBLE_MIDILibrary",
172+
"Adafruit BLE_MIDI Library Documentation",
173+
author,
174+
"AdafruitBLE_MIDILibrary",
175+
"One line description of project.",
176+
"Miscellaneous",
177+
),
160178
]

examples/ble_midi_simplein.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
import time
66
import adafruit_ble
7+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
78
import adafruit_ble_midi
89
import adafruit_midi
9-
from adafruit_ble.advertising import Advertisement
10-
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
10+
11+
# These import auto-register the message type with the MIDI machinery.
12+
# pylint: disable=unused-import
1113
from adafruit_midi.control_change import ControlChange
1214
from adafruit_midi.midi_message import MIDIUnknownEvent
1315
from adafruit_midi.note_off import NoteOff

0 commit comments

Comments
 (0)