mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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