Skip to content

Commit 3247471

Browse files
committed
More code cleanup
More idiomatic Python 3 Inherited method declaration compatible with parent
1 parent 8fb90b9 commit 3247471

File tree

10 files changed

+140
-142
lines changed

10 files changed

+140
-142
lines changed

TODO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
- [x] [mzuther] implement basic functionality
22
- [x] [rdoursenaud] port to python 3 and pyside 2
33
- [x] [rdoursenaud] cleanup code and make it PEP008 compliant
4+
- [ ] [rdoursenaud] write pytest unit tests only looking at the protocol docs
45
- [ ] [rdoursenaud] properly handle invalid config file
56
- [ ] [rdoursenaud] disallow start if MIDI ports are not set in the GUI
67
- [ ] [rdoursenaud] don't freeze GUI while "Waiting for MIDI input from host..."

src/PythonMcu.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@
5050
class PythonMcu(QFrame):
5151
# noinspection PyUnresolvedReferences
5252
def __init__(self, parent=None):
53-
super(PythonMcu, self).__init__(parent)
53+
super().__init__(parent)
5454

55-
self._mcu_emulated_model = None
56-
self._mcu_midi_input = None
57-
self._mcu_midi_output = None
58-
self._hardware_controller = None
5955
self._controller_midi_input = None
6056
self._controller_midi_output = None
57+
self._hardware_controller = None
58+
self._hardware_controller_class = None
6159
self._mcu_connection = None
60+
self._mcu_emulated_model = None
61+
self._mcu_midi_input = None
62+
self._mcu_midi_output = None
63+
self._mcu_model_id = None
6264

6365
font = QFont()
6466
font.setStyleHint(QFont.TypeWriter, QFont.PreferAntialias)

src/PythonMcu/Hardware/MidiControllerTemplate.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
from PythonMcu.Midi.MidiConnection import MidiConnection
3434

3535

36-
class MidiControllerTemplate(object):
37-
36+
class MidiControllerTemplate:
3837
MIDI_MANUFACTURER_ID = None
3938
MIDI_DEVICE_ID = None
4039

@@ -47,7 +46,7 @@ class MidiControllerTemplate(object):
4746
0x00: 'off',
4847
0x01: 'flashing',
4948
0x7F: 'on'
50-
}
49+
}
5150

5251
def __init__(self, midi_input_name, midi_output_name, callback_log):
5352
self.callback_log = callback_log
@@ -79,11 +78,11 @@ def __init__(self, midi_input_name, midi_output_name, callback_log):
7978
self.meter_bridge_available = True
8079

8180
self.display_7seg_characters = []
82-
for counter in range(4):
81+
for _ in range(4):
8382
self.display_7seg_characters.append(' ')
8483

8584
self.display_timecode_characters = []
86-
for counter in range(20):
85+
for _ in range(20):
8786
self.display_timecode_characters.append(' ')
8887

8988
@staticmethod
@@ -145,7 +144,7 @@ def send_midi_control_change(self, channel, cc_number, cc_value):
145144
self.midi.send_control_change(channel, cc_number, cc_value)
146145

147146
def send_midi_sysex(self, data):
148-
assert(type(data) is list)
147+
assert isinstance(data, list)
149148

150149
header = []
151150
header.extend(self.MIDI_MANUFACTURER_ID)
@@ -215,8 +214,8 @@ def _decode_7seg_character(character_code):
215214

216215
if character_code < 0x20:
217216
return chr(character_code + 0x40), dot
218-
else:
219-
return chr(character_code), dot
217+
218+
return chr(character_code), dot
220219

221220
def set_display_timecode(self, position, character_code):
222221
character = self._decode_7seg_character(character_code)
@@ -262,11 +261,11 @@ def get_lcd_characters(self, line):
262261

263262
if self._show_overlay[line]:
264263
return self._lcd_overlay_characters[line]
265-
else:
266-
return self._lcd_characters[line]
264+
265+
return self._lcd_characters[line]
267266

268267
def show_menu(self, line, menu_strings):
269-
assert(len(menu_strings) == 8)
268+
assert len(menu_strings) == 8
270269

271270
menu_string_temp = ''
272271
for menu_string in menu_strings:
@@ -280,7 +279,7 @@ def hide_menu(self, line):
280279

281280
def show_overlay(self, line, overlay_characters):
282281
line %= 2
283-
assert(len(overlay_characters) == 56)
282+
assert len(overlay_characters) == 56
284283

285284
self._show_overlay[line] = True
286285
self._lcd_overlay_characters[line] = overlay_characters

src/PythonMcu/Hardware/NovationZeROSLMkII.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ def _enter_ableton_mode(self):
170170
self.send_midi_sysex([0x01, 0x01])
171171

172172
# clear all LEDs and switch off "transport" mode
173-
self.send_midi_control_change(self._MIDI_CC_CLEAR_ALL_LEDS, 0x00)
174-
self.send_midi_control_change(self._MIDI_CC_BUTTON_MODE_TRANSPORT, 0x00)
173+
self.send_midi_control_change(cc_number=self._MIDI_CC_CLEAR_ALL_LEDS, cc_value=0x00)
174+
self.send_midi_control_change(cc_number=self._MIDI_CC_BUTTON_MODE_TRANSPORT, cc_value=0x00)
175175

176176
def _leave_ableton_mode(self):
177177
self._log('Leaving "Ableton" mode...', True)
@@ -180,8 +180,8 @@ def _leave_ableton_mode(self):
180180
self.send_midi_sysex([0x01, 0x00])
181181

182182
# clear all LEDs and switch off "transport" mode
183-
self.send_midi_control_change(self._MIDI_CC_CLEAR_ALL_LEDS, 0x00)
184-
self.send_midi_control_change(self._MIDI_CC_BUTTON_MODE_TRANSPORT, 0x00)
183+
self.send_midi_control_change(cc_number=self._MIDI_CC_CLEAR_ALL_LEDS, cc_value=0x00)
184+
self.send_midi_control_change(cc_number=self._MIDI_CC_BUTTON_MODE_TRANSPORT, cc_value=0x00)
185185

186186
# --- MIDI processing ---
187187
def receive_midi(self, status, message):
@@ -274,25 +274,28 @@ def receive_midi(self, status, message):
274274
message_string.append('%02X' % byte)
275275
self._log(' '.join(message_string))
276276

277-
def send_midi_control_change(self, cc_number, cc_value, channel=None):
277+
def send_midi_control_change(self, channel=None, cc_number=None, cc_value=None):
278278
if not self._is_connected:
279279
return
280280

281+
if channel:
282+
raise ValueError("The channel is fixed for this device!")
283+
281284
MidiControllerTemplate.send_midi_control_change(self, self._MIDI_DEVICE_CHANNEL, cc_number, cc_value)
282285

283286
@staticmethod
284287
def get_preferred_midi_input():
285288
if os.name == 'nt':
286289
return 'ZeRO MkII: Port 2'
287-
else:
288-
return 'ZeRO MkII MIDI 2'
290+
291+
return 'ZeRO MkII MIDI 2'
289292

290293
@staticmethod
291294
def get_preferred_midi_output():
292295
if os.name == 'nt':
293296
return 'ZeRO MkII: Port 2'
294-
else:
295-
return 'ZeRO MkII MIDI 2'
297+
298+
return 'ZeRO MkII MIDI 2'
296299

297300
# --- registration of MIDI controls ---
298301
def register_control(self, mcu_command, midi_switch, midi_led=None):
@@ -319,8 +322,8 @@ def set_lcd_directly(self, line, lcd_string):
319322
lcd_string = lcd_string.ljust(72)[:72]
320323

321324
lcd_characters = []
322-
for n in range(len(lcd_string)):
323-
lcd_characters.append(ord(lcd_string[n]))
325+
for index, string in enumerate(lcd_string):
326+
lcd_characters.append(ord(string))
324327

325328
self._update_lcd_raw(line, lcd_characters)
326329

@@ -332,9 +335,9 @@ def update_lcd(self):
332335
self._lcd_strings[line] = new_string
333336
hex_codes = []
334337

335-
for n in range(len(new_string)):
336-
hex_codes.append(ord(new_string[n]))
337-
if (n % 7) == 6:
338+
for index, string in enumerate(new_string):
339+
hex_codes.append(ord(string))
340+
if index % 7 == 6:
338341
hex_codes.append(0x20)
339342
hex_codes.append(0x20)
340343

@@ -350,7 +353,7 @@ def _update_lcd_raw(self, line, hex_codes):
350353
if not self._is_connected:
351354
return
352355

353-
assert (len(hex_codes) == 72)
356+
assert len(hex_codes) == 72
354357

355358
line %= 2
356359
if line == 0:
@@ -395,25 +398,25 @@ def _set_led(self, led_id, led_status):
395398

396399
MidiControllerTemplate.send_midi_control_change(self, self._MIDI_DEVICE_CHANNEL, led_id, led_status)
397400

398-
def set_vpot_led_ring(self, vpot_id, center_led, mode, position):
399-
vpot_mode = None
400-
if mode == self.VPOT_MODE_WRAP:
401-
vpot_mode = 0x00
402-
elif mode == self.VPOT_MODE_BOOST_CUT:
403-
vpot_mode = 0x20
404-
elif mode == self.VPOT_MODE_SPREAD:
405-
vpot_mode = 0x30
406-
elif mode == self.VPOT_MODE_SINGLE_DOT:
407-
vpot_mode = 0x40
401+
def set_vpot_led_ring(self, vpot_id, vpot_center_led, vpot_mode, vpot_position):
402+
mode = None
403+
if vpot_mode == self.VPOT_MODE_WRAP:
404+
mode = 0x00
405+
elif vpot_mode == self.VPOT_MODE_BOOST_CUT:
406+
mode = 0x20
407+
elif vpot_mode == self.VPOT_MODE_SPREAD:
408+
mode = 0x30
409+
elif vpot_mode == self.VPOT_MODE_SINGLE_DOT:
410+
mode = 0x40
408411

409-
self._vpot_modes[vpot_id] = vpot_mode
410-
self._vpot_positions[vpot_id] = position
412+
self._vpot_modes[vpot_id] = mode
413+
self._vpot_positions[vpot_id] = vpot_position
411414

412-
self._set_led(self._MIDI_CC_ENCODER_MODE + vpot_id, vpot_mode)
413-
self._set_led(self._MIDI_CC_ENCODER_LIGHTS + vpot_id, position)
415+
self._set_led(self._MIDI_CC_ENCODER_MODE + vpot_id, mode)
416+
self._set_led(self._MIDI_CC_ENCODER_LIGHTS + vpot_id, vpot_position)
414417

415418
def all_leds_off(self):
416-
self.send_midi_control_change(self._MIDI_CC_CLEAR_ALL_LEDS, 0x00)
419+
self.send_midi_control_change(cc_number=self._MIDI_CC_CLEAR_ALL_LEDS, cc_value=0x00)
417420

418421
# --- pedal handling ---
419422
def on_control_pedal(self, status):

src/PythonMcu/MackieControl/MackieHostControl.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,19 @@ def connect(self):
208208
self.send_midi_sysex(sysex_message)
209209

210210
return
211-
else:
212-
# let's make sure the MIDI input buffer is empty
213-
self._midi.process_input_buffer(use_callback=False)
214211

215-
if self._mcu_connection == self.WAIT_FOR_MIDI_DATA:
216-
self._log('Waiting for MIDI input from host...', True)
212+
# let's make sure the MIDI input buffer is empty
213+
self._midi.process_input_buffer(use_callback=False)
214+
215+
if self._mcu_connection == self.WAIT_FOR_MIDI_DATA:
216+
self._log('Waiting for MIDI input from host...', True)
217217

218-
# wait for some MIDI input from the host (all data are
219-
# left in the MIDI input buffer!)
220-
while self._midi.buffer_is_empty():
221-
time.sleep(0.1)
218+
# wait for some MIDI input from the host (all data are
219+
# left in the MIDI input buffer!)
220+
while self._midi.buffer_is_empty():
221+
time.sleep(0.1)
222222

223-
self.go_online()
223+
self.go_online()
224224

225225
def disconnect(self):
226226
self._log('Disconnecting...', True)
@@ -271,27 +271,25 @@ def _calculate_response_from_challenge(challenge_bytes):
271271
def get_mcu_model_from_id(model_id):
272272
if model_id == 0x10:
273273
return 'Logic Control'
274-
elif model_id == 0x11:
274+
if model_id == 0x11:
275275
return 'Logic Control XT'
276-
elif model_id == 0x14:
276+
if model_id == 0x14:
277277
return 'Mackie Control'
278-
elif model_id == 0x15:
278+
if model_id == 0x15:
279279
return 'Mackie Control XT'
280-
else:
281-
return None
280+
return None
282281

283282
@staticmethod
284283
def get_mcu_id_from_model(model):
285284
if model == 'Logic Control':
286285
return 0x10
287-
elif model == 'Logic Control XT':
286+
if model == 'Logic Control XT':
288287
return 0x11
289-
elif model == 'Mackie Control':
288+
if model == 'Mackie Control':
290289
return 0x14
291-
elif model == 'Mackie Control XT':
290+
if model == 'Mackie Control XT':
292291
return 0x15
293-
else:
294-
return None
292+
return None
295293

296294
@staticmethod
297295
def get_preferred_mcu_model():
@@ -305,15 +303,15 @@ def get_preferred_mcu_model_id():
305303
def get_preferred_midi_input():
306304
if os.name == 'nt':
307305
return 'In From MIDI Yoke: 2'
308-
else:
309-
return 'mcu'
306+
307+
return 'mcu'
310308

311309
@staticmethod
312310
def get_preferred_midi_output():
313311
if os.name == 'nt':
314312
return 'Out To MIDI Yoke: 1'
315-
else:
316-
return 'mcu'
313+
314+
return 'mcu'
317315

318316
# --- MIDI processing ---
319317
def process_midi_input(self):
@@ -331,7 +329,7 @@ def receive_midi(self, status, message):
331329
self.send_midi_sysex(sysex_message)
332330

333331
return
334-
elif message[5] == 0x02:
332+
if message[5] == 0x02:
335333
self._log('Received "Host Connection Reply".')
336334
if (message[6:13] == self._serial_number_bytes) and (message[13:17] == self._response_bytes):
337335
self._log('Sending "Host Connection Confirmation"...', True)
@@ -356,7 +354,7 @@ def receive_midi(self, status, message):
356354
self.send_midi_sysex(sysex_message)
357355

358356
return
359-
elif message[5:] == [0x13, 0x00, 0x0F7]:
357+
if message[5:] == [0x13, 0x00, 0x0F7]:
360358
self._log('Received "Version Request".')
361359
self._log('Sending "Version Reply"...', True)
362360

@@ -365,22 +363,22 @@ def receive_midi(self, status, message):
365363
self.send_midi_sysex(sysex_message)
366364

367365
return
368-
elif message[5:] == [0x0F, 0x7F, 0x0F7]:
366+
if message[5:] == [0x0F, 0x7F, 0x0F7]:
369367
self._log('Received "Go Offline".', True)
370368
self.go_offline()
371369

372370
return
373-
elif message[5:] == [0x61, 0x0F7]:
371+
if message[5:] == [0x61, 0x0F7]:
374372
self._log('Received "Faders To Minimum".', True)
375373
self.faders_to_minimum()
376374

377375
return
378-
elif message[5:] == [0x62, 0x0F7]:
376+
if message[5:] == [0x62, 0x0F7]:
379377
self._log('Received "All LEDs Off".', True)
380378
self.all_leds_off()
381379

382380
return
383-
elif message[5:] == [0x63, 0x0F7]:
381+
if message[5:] == [0x63, 0x0F7]:
384382
self._log('Received "Reset".', True)
385383
self.reset()
386384

@@ -445,7 +443,7 @@ def receive_midi(self, status, message):
445443
self._log(output.strip())
446444

447445
def send_midi_sysex(self, data):
448-
assert(type(data) is list)
446+
assert isinstance(data, list)
449447

450448
header = [0x00, 0x00, 0x66, self._mcu_model_id]
451449

0 commit comments

Comments
 (0)