nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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