From f676c50ba91ca6d4d0da9f77aeadc8b3fb16dc26 Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Sat, 9 Nov 2024 23:22:55 +0100 Subject: CLI improvements --- .../solo_tool/midi_controller_launchpad_mini.py | 7 +++-- .../src/solo_tool/midi_wrapper_mido.py | 19 ++++++++----- .../test/midi_launchpad_mini_integrationtest.py | 33 ++++++++++++---------- 3 files changed, 35 insertions(+), 24 deletions(-) (limited to 'solo-tool-project') diff --git a/solo-tool-project/src/solo_tool/midi_controller_launchpad_mini.py b/solo-tool-project/src/solo_tool/midi_controller_launchpad_mini.py index 961127c..8f32650 100644 --- a/solo-tool-project/src/solo_tool/midi_controller_launchpad_mini.py +++ b/solo-tool-project/src/solo_tool/midi_controller_launchpad_mini.py @@ -51,10 +51,13 @@ class MidiController: self._handlers[button] = self._createSetPlaybackRateCallback(rate) def connect(self): - self._midiWrapper.setCallback(self._callback) - self._midiWrapper.connect(MidiController.DEVICE_NAME) + self._midiWrapper.connect(MidiController.DEVICE_NAME, self._callback) self._initialiseButtonLEDs() + def disconnect(self): + self._allLEDsOff() + self._midiWrapper.disconnect() + def _callback(self, msg): if msg.type != "note_on" or msg.velocity < 127: return diff --git a/solo-tool-project/src/solo_tool/midi_wrapper_mido.py b/solo-tool-project/src/solo_tool/midi_wrapper_mido.py index bf3aa85..4cfc9c3 100644 --- a/solo-tool-project/src/solo_tool/midi_wrapper_mido.py +++ b/solo-tool-project/src/solo_tool/midi_wrapper_mido.py @@ -4,15 +4,20 @@ class MidiWrapper: def __init__(self): self._inPort = None self._outPort = None - self._callback = None - def setCallback(self, callback): - self._callback = callback + def connect(self, deviceName, callback): + if self._inPort is None and self._outPort is None: + self._inPort = mido.open_input(deviceName) + self._inPort.callback = callback + self._outPort = mido.open_output(deviceName) - def connect(self, deviceName): - self._inPort = mido.open_input(deviceName) - self._inPort.callback = self._callback - self._outPort = mido.open_output(deviceName) + def disconnect(self): + self._inPort.close() + self._inPort = None + + self._outPort.reset() + self._outPort.close() + self._outPort = None def sendMessage(self, note, velocity, channel): if self._outPort is not None: diff --git a/solo-tool-project/test/midi_launchpad_mini_integrationtest.py b/solo-tool-project/test/midi_launchpad_mini_integrationtest.py index 8542aae..17649d4 100644 --- a/solo-tool-project/test/midi_launchpad_mini_integrationtest.py +++ b/solo-tool-project/test/midi_launchpad_mini_integrationtest.py @@ -24,11 +24,12 @@ class MidiWrapperMock: self.connectedDevice = None self.sentMessages = list() - def setCallback(self, callback): + def connect(self, deviceName, callback): + self.connectedDevice = deviceName self.callback = callback - def connect(self, deviceName): - self.connectedDevice = deviceName + def disconnect(self): + self.connectedDevice = None def sendMessage(self, note, velocity, channel): self.sentMessages.append((note, velocity, channel)) @@ -58,12 +59,6 @@ def midiWrapperMock(): def uut(soloTool, midiWrapperMock): return MidiController(soloTool, midiWrapperMock) -def test_connect(uut, midiWrapperMock): - expectedDevice = "Launchpad Mini MIDI 1" - uut.connect() - - assert midiWrapperMock.connectedDevice == expectedDevice - def test_startStopAndPauseButtons(uut, midiWrapperMock, playerMock): uut.connect() @@ -345,8 +340,8 @@ def test_unassignedButton(uut, midiWrapperMock): midiWrapperMock.simulateInput(unassignedButton) # XXX would be better to assert that nothing changed in the solo tool -def test_initializationMessages(uut, midiWrapperMock): - expectedMessages = set( +def test_connectDisconnect(uut, midiWrapperMock): + startupMessages = list( [(int(i / 8) * 16 + (i % 8), LED_OFF, 0) for i in range(0, 64)] + # clear all [(i, LED_GREEN, 0) for i in range(0, 6)] + # volume row [(i, LED_YELLOW, 0) for i in range(16, 22)] + # playback rate row @@ -359,13 +354,21 @@ def test_initializationMessages(uut, midiWrapperMock): (nextLimitButton, LED_GREEN, 0), (previousSongButton, LED_RED, 0), (nextSongButton, LED_GREEN, 0) - ] - ) + ]) + + teardownMessages = [(int(i / 8) * 16 + (i % 8), LED_OFF, 0) for i in range(0, 64)] # clear all + expectedDevice = "Launchpad Mini MIDI 1" uut.connect() - sentMessagesSet = set(midiWrapperMock.sentMessages) - assert sentMessagesSet == expectedMessages + assert midiWrapperMock.connectedDevice == expectedDevice + assert set(midiWrapperMock.sentMessages) == set(startupMessages) + + midiWrapperMock.sentMessages.clear() + + uut.disconnect() + + assert set(midiWrapperMock.sentMessages) == set(teardownMessages) def test_playingFeedbackWhenChangingSong(uut, midiWrapperMock, soloTool, playerMock): songs = [ -- cgit v1.2.3