Simple test

Ensure your device works with this simple test.

examples/ble_midi_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This example sends MIDI out. It sends NoteOn and then NoteOff with a random pitch bend.
"""

import time
import random
import adafruit_ble
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
import adafruit_midi
from adafruit_midi.control_change import ControlChange
from adafruit_midi.note_off import NoteOff
from adafruit_midi.note_on import NoteOn
from adafruit_midi.pitch_bend import PitchBend
import adafruit_ble_midi

# Use default HID descriptor
midi_service = adafruit_ble_midi.MIDIService()
advertisement = ProvideServicesAdvertisement(midi_service)
# advertisement.appearance = 961

ble = adafruit_ble.BLERadio()
if ble.connected:
    for c in ble.connections:
        c.disconnect()

midi = adafruit_midi.MIDI(midi_out=midi_service, out_channel=0)

print("advertising")
ble.start_advertising(advertisement)

while True:
    print("Waiting for connection")
    while not ble.connected:
        pass
    print("Connected")
    # Sleep briefly so client can get ready and send setup
    # writes to the MIDIService. 0.5secs was insufficient.
    time.sleep(1.0)
    # Send one unique NoteOn/Off at the beginning to check that the
    # delay is sufficient.
    midi.send(NoteOn(20, 99))
    midi.send(NoteOff(20, 99))
    while ble.connected:
        midi.send(NoteOn(44, 120))  # G sharp 2nd octave
        time.sleep(0.25)
        a_pitch_bend = PitchBend(random.randint(0, 16383))
        midi.send(a_pitch_bend)
        time.sleep(0.25)
        # note how a list of messages can be used
        midi.send([NoteOff("G#2", 120), ControlChange(3, 44)])
        time.sleep(0.5)
    print("Disconnected")
    print()
    ble.start_advertising(advertisement)