Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:0e018d759a2a 1 from __future__ import print_function
switches 0:0e018d759a2a 2 import sys
switches 0:0e018d759a2a 3 import re
switches 0:0e018d759a2a 4 import time
switches 0:0e018d759a2a 5 import mido
switches 0:0e018d759a2a 6 from mido import Message
switches 0:0e018d759a2a 7
switches 0:0e018d759a2a 8
switches 0:0e018d759a2a 9 def test_midi_in(port):
switches 0:0e018d759a2a 10 expected_messages_count=0
switches 0:0e018d759a2a 11 while expected_messages_count < 7:
switches 0:0e018d759a2a 12 for message in port.iter_pending():
switches 0:0e018d759a2a 13 if message.type in ('note_on', 'note_off', 'program_change', 'sysex'):
switches 0:0e018d759a2a 14 yield message
switches 0:0e018d759a2a 15 expected_messages_count+=1
switches 0:0e018d759a2a 16 time.sleep(0.1)
switches 0:0e018d759a2a 17
switches 0:0e018d759a2a 18 def test_midi_loopback(input_port):
switches 0:0e018d759a2a 19 expected_messages_count=0
switches 0:0e018d759a2a 20 while expected_messages_count < 1:
switches 0:0e018d759a2a 21 for message in input_port.iter_pending():
switches 0:0e018d759a2a 22 print('Test MIDI OUT loopback received {}'.format(message.hex()))
switches 0:0e018d759a2a 23 expected_messages_count+=1
switches 0:0e018d759a2a 24
switches 0:0e018d759a2a 25 def test_midi_out_loopback(output_port,input_port):
switches 0:0e018d759a2a 26 print("Test MIDI OUT loopback")
switches 0:0e018d759a2a 27 output_port.send(Message('program_change', program=1))
switches 0:0e018d759a2a 28 test_midi_loopback(input_port)
switches 0:0e018d759a2a 29
switches 0:0e018d759a2a 30 output_port.send(Message('note_on', note=21))
switches 0:0e018d759a2a 31 test_midi_loopback(input_port)
switches 0:0e018d759a2a 32
switches 0:0e018d759a2a 33 output_port.send(Message('note_off', note=21))
switches 0:0e018d759a2a 34 test_midi_loopback(input_port)
switches 0:0e018d759a2a 35
switches 0:0e018d759a2a 36 output_port.send(Message('sysex', data=[0x7E,0x7F,0x09,0x01]))
switches 0:0e018d759a2a 37 test_midi_loopback(input_port)
switches 0:0e018d759a2a 38
switches 0:0e018d759a2a 39 output_port.send(Message('sysex', data=[0x7F,0x7F,0x04,0x01,0x7F,0x7F]))
switches 0:0e018d759a2a 40 test_midi_loopback(input_port)
switches 0:0e018d759a2a 41
switches 0:0e018d759a2a 42 output_port.send(Message('sysex', data=[0x41,0x10,0x42,0x12,0x40,0x00,0x7F,0x00,0x41]))
switches 0:0e018d759a2a 43 test_midi_loopback(input_port)
switches 0:0e018d759a2a 44
switches 0:0e018d759a2a 45 output_port.send(Message('sysex', data=[0x41,0x10,0x42,0x12,0x40,0x00,0x04,0x7F,0x3D]))
switches 0:0e018d759a2a 46 test_midi_loopback(input_port)
switches 0:0e018d759a2a 47
switches 0:0e018d759a2a 48 portname=""
switches 0:0e018d759a2a 49
switches 0:0e018d759a2a 50 while portname=="":
switches 0:0e018d759a2a 51 print("Wait for MIDI IN plug ...")
switches 0:0e018d759a2a 52 for name in mido.get_input_names():
switches 0:0e018d759a2a 53 matchObj = re.match( r'Mbed', name)
switches 0:0e018d759a2a 54
switches 0:0e018d759a2a 55 if matchObj:
switches 0:0e018d759a2a 56 portname=name
switches 0:0e018d759a2a 57 time.sleep( 1 )
switches 0:0e018d759a2a 58
switches 0:0e018d759a2a 59 try:
switches 0:0e018d759a2a 60 input_port = mido.open_input(portname)
switches 0:0e018d759a2a 61 output_port = mido.open_output(portname)
switches 0:0e018d759a2a 62
switches 0:0e018d759a2a 63 print('Using {}'.format(input_port))
switches 0:0e018d759a2a 64
switches 0:0e018d759a2a 65 print("Test MIDI IN")
switches 0:0e018d759a2a 66
switches 0:0e018d759a2a 67 for message in test_midi_in(input_port):
switches 0:0e018d759a2a 68 print('Test MIDI IN received {}'.format(message.hex()))
switches 0:0e018d759a2a 69
switches 0:0e018d759a2a 70 test_midi_out_loopback(output_port,input_port)
switches 0:0e018d759a2a 71 except KeyboardInterrupt:
switches 0:0e018d759a2a 72 pass