|
| 1 | +# Simple example of sending MIDI via UART to classic DIN-5 (not USB) synth |
| 2 | + |
| 3 | +import adafruit_trellism4 |
| 4 | + |
| 5 | +import board |
| 6 | +import busio |
| 7 | +midiuart = busio.UART(board.SDA, board.SCL, baudrate=31250) |
| 8 | +print("MIDI UART EXAMPLE") |
| 9 | + |
| 10 | +trellis = adafruit_trellism4.TrellisM4Express() |
| 11 | + |
| 12 | +def wheel(pos): |
| 13 | + if pos < 0 or pos > 255: |
| 14 | + return 0, 0, 0 |
| 15 | + if pos < 85: |
| 16 | + return int(255 - pos * 3), int(pos * 3), 0 |
| 17 | + if pos < 170: |
| 18 | + pos -= 85 |
| 19 | + return 0, int(255 - pos * 3), int(pos * 3) |
| 20 | + pos -= 170 |
| 21 | + return int(pos * 3), 0, int(255 - (pos * 3)) |
| 22 | + |
| 23 | + |
| 24 | +for x in range(trellis.pixels.width): |
| 25 | + for y in range(trellis.pixels.height): |
| 26 | + pixel_index = (((y * 8) + x) * 256 // 2) |
| 27 | + trellis.pixels[x, y] = wheel(pixel_index & 255) |
| 28 | + |
| 29 | +current_press = set() |
| 30 | + |
| 31 | +while True: |
| 32 | + pressed = set(trellis.pressed_keys) |
| 33 | + |
| 34 | + for press in pressed - current_press: |
| 35 | + x, y = press |
| 36 | + print("Pressed:", press) |
| 37 | + noteval = 36 + x + (y * 8) |
| 38 | + midiuart.write(bytes([0x90, noteval, 100])) |
| 39 | + |
| 40 | + for release in current_press - pressed: |
| 41 | + x, y = release |
| 42 | + print("Released:", release) |
| 43 | + noteval = 36 + x + (y * 8) |
| 44 | + midiuart.write(bytes([0x90, noteval, 0])) # note off |
| 45 | + |
| 46 | + current_press = pressed |
0 commit comments