Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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