Skip to content

Commit 5cd820d

Browse files
author
Kevin J Walters
committed
Removing default values in MIDI constructor for midi_in and midi_out based on @tannewt advice.
Updating all the examples to explicitly set midi_in or midi_out to the USB ports. Adding test case as constructor now raises an error if midi_in and midi_out are both omitted. This will make sphinx-build work but that appears to be a separate bug. adafruit#13
1 parent d78b0f0 commit 5cd820d

File tree

10 files changed

+37
-24
lines changed

10 files changed

+37
-24
lines changed

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ Usage Example
5858
5959
import time
6060
import random
61+
import usb_midi
6162
import adafruit_midi
6263
63-
midi = adafruit_midi.MIDI(out_channel=0)
64+
midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0)
6465
6566
print("Midi test")
6667
67-
print("Default output channel:", midi.out_channel)
68-
print("Listening on input channel:", midi.in_channel)
68+
print("Default output MIDI channel:", midi.out_channel + 1)
6969
7070
while True:
7171
midi.note_on(44, 120)

adafruit_midi/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,19 @@
4242
4343
"""
4444

45-
import usb_midi
46-
4745
from .midi_message import MIDIMessage, ALL_CHANNELS
4846

4947
__version__ = "0.0.0-auto.0"
5048
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MIDI.git"
5149

5250

5351
class MIDI:
54-
"""MIDI helper class.
52+
"""MIDI helper class. ``midi_in`` or ``midi_out`` *must* be set or both together.
5553
5654
:param midi_in: an object which implements ``read(length)``,
57-
defaults to ``usb_midi.ports[0]``.
55+
set to ``usb_midi.ports[0]`` for USB MIDI, default None.
5856
:param midi_out: an object which implements ``write(buffer, length)``,
59-
defaults to ``usb_midi.ports[1]``.
57+
set to ``usb_midi.ports[1]`` for USB MIDI, default None.
6058
:param in_channel: The input channel(s).
6159
This is used by ``receive`` to filter data.
6260
This can either be an ``int`` for the wire protocol channel number (0-15)
@@ -65,7 +63,7 @@ class MIDI:
6563
:param int out_channel: The wire protocol output channel number (0-15)
6664
used by ``send`` if no channel is specified,
6765
defaults to 0 (MIDI Channel 1).
68-
:param int in_buf_size: Size of input buffer in bytes, default 30.
66+
:param int in_buf_size: Maximum size of input buffer in bytes, default 30.
6967
:param bool debug: Debug mode, default False.
7068
7169
"""
@@ -75,8 +73,10 @@ class MIDI:
7573
PITCH_BEND = 0xE0
7674
CONTROL_CHANGE = 0xB0
7775

78-
def __init__(self, midi_in=usb_midi.ports[0], midi_out=usb_midi.ports[1], *,
76+
def __init__(self, midi_in=None, midi_out=None, *,
7977
in_channel=None, out_channel=0, in_buf_size=30, debug=False):
78+
if midi_in is None and midi_out is None:
79+
raise ValueError("No midi_in or midi_out provided")
8080
self._midi_in = midi_in
8181
self._midi_out = midi_out
8282
self._in_channel = in_channel # dealing with pylint inadequacy

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# Uncomment the below if you use native CircuitPython modules such as
2121
# digitalio, micropython and busio. List the modules you use. Without it, the
2222
# autodoc module docs will fail to generate with a warning.
23-
autodoc_mock_imports = ["usb_midi"]
23+
autodoc_mock_imports = []
2424

2525

2626
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

examples/midi_inoutdemo.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# midi_inoutdemo - demonstrates receiving and sending MIDI events
22

3+
import usb_midi
34
import adafruit_midi
45

56
# TimingClock is worth importing first if present as it
@@ -21,7 +22,9 @@
2122

2223
from adafruit_midi.midi_message import MIDIUnknownEvent
2324

24-
midi = adafruit_midi.MIDI(in_channel=(1, 2, 3), out_channel=0)
25+
midi = adafruit_midi.MIDI(midi_in=usb_midi.ports[0],
26+
midi_out=usb_midi.ports[1],
27+
in_channel=(1, 2, 3), out_channel=0)
2528

2629
print("Midi Demo in and out")
2730

examples/midi_intest1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import time
2+
import usb_midi
23
import adafruit_midi
34

45
### 0 is MIDI channel 1
5-
midi = adafruit_midi.MIDI(in_channel=0)
6+
midi = adafruit_midi.MIDI(midi_in=usb_midi.ports[0], in_channel=0)
67

78
print("Midi test II")
89

examples/midi_simpletest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import time
22
import random
3+
import usb_midi
34
import adafruit_midi
45

5-
midi = adafruit_midi.MIDI(out_channel=0)
6+
midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0)
67

78
print("Midi test")
89

examples/midi_simpletest2.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# simple_test demonstrating both interfaces
22
import time
33
import random
4+
import usb_midi
45
import adafruit_midi
5-
from adafruit_midi.control_change import ControlChange
6-
from adafruit_midi.note_off import NoteOff
7-
from adafruit_midi.note_on import NoteOn
6+
from adafruit_midi.control_change import ControlChange
7+
from adafruit_midi.note_off import NoteOff
8+
from adafruit_midi.note_on import NoteOn
89
from adafruit_midi.pitch_bend import PitchBend
910

10-
midi = adafruit_midi.MIDI(out_channel=0)
11+
midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0)
1112

1213
print("Midi test")
1314

@@ -25,6 +26,7 @@
2526
midi.note_off(44, 120)
2627
midi.control_change(3, 44)
2728
time.sleep(0.5)
29+
2830
# send message(s) interface
2931
midi.send(NoteOn(44, 120))
3032
time.sleep(0.25)

tests/test_MIDIMessage_unittests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import os
2828
verbose = int(os.getenv('TESTVERBOSE', '2'))
2929

30-
# adafruit_midi has an import usb_midi
30+
# adafruit_midi had an import usb_midi
3131
import sys
32-
sys.modules['usb_midi'] = MagicMock()
32+
#sys.modules['usb_midi'] = MagicMock()
3333

3434
# Borrowing the dhalbert/tannewt technique from adafruit/Adafruit_CircuitPython_Motor
3535
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

tests/test_MIDI_unittests.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import os
2828
verbose = int(os.getenv('TESTVERBOSE', '2'))
2929

30-
# adafruit_midi has an import usb_midi
30+
# adafruit_midi had an import usb_midi
3131
import sys
32-
sys.modules['usb_midi'] = MagicMock()
32+
#sys.modules['usb_midi'] = MagicMock()
3333

3434
# Borrowing the dhalbert/tannewt technique from adafruit/Adafruit_CircuitPython_Motor
3535
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
@@ -99,6 +99,12 @@ def read(length):
9999
out_channel=in_c, in_channel=in_c)
100100
return m
101101

102+
103+
class Test_MIDI_constructor(unittest.TestCase):
104+
def test_no_inout(self):
105+
# constructor likes a bit of in out
106+
with self.assertRaises(ValueError):
107+
adafruit_midi.MIDI()
102108

103109
class Test_MIDI(unittest.TestCase):
104110
# pylint: disable=too-many-branches

tests/test_note_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import os
2828
verbose = int(os.getenv('TESTVERBOSE', '2'))
2929

30-
# adafruit_midi has an import usb_midi
30+
# adafruit_midi had an import usb_midi
3131
import sys
32-
sys.modules['usb_midi'] = MagicMock()
32+
#sys.modules['usb_midi'] = MagicMock()
3333

3434
# Borrowing the dhalbert/tannewt technique from adafruit/Adafruit_CircuitPython_Motor
3535
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

0 commit comments

Comments
 (0)