|
| 1 | +# quick-n-dirty MIDI controller with Circuit Playground Express |
| 2 | +# 30 Apr 2024 - @todbot / Tod Kurt |
| 3 | +# Works on Circuit Playground Express in CircuitPython 9, |
| 4 | +# but will also work on also any board with touchio, like Trinket M0 or QTPy M0 |
| 5 | +# Uses the minimal number of libraries to save RAM on the tiny M0 in the CPX |
| 6 | +# (e.g. cannot load `adafruit_circuitpython`, `adafruit_midi`, and `adafruit_debouncer` on CPX) |
| 7 | + |
| 8 | +import board |
| 9 | +import touchio |
| 10 | +import usb_midi |
| 11 | +import neopixel |
| 12 | + |
| 13 | +# which MIDI notes to send |
| 14 | +midi_notenums = (40, 41, 42, 43) |
| 15 | + |
| 16 | +# which pins for each of the above notes |
| 17 | +touch_pins = (board.A1, board.A2, board.A3, board.A4) |
| 18 | + |
| 19 | +# which MIDI channel to transmit on |
| 20 | +midi_out_channel = 1 |
| 21 | + |
| 22 | +# set up the status byte constants for sending MIDI later |
| 23 | +note_on_status = (0x90 | (midi_out_channel-1)) |
| 24 | +note_off_status = (0x80 | (midi_out_channel-1)) |
| 25 | + |
| 26 | +# get a MIDI out port |
| 27 | +midi_out = usb_midi.ports[1] |
| 28 | +# make ready the neopixels so we can blink when a pad is touched |
| 29 | +leds = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1) |
| 30 | + |
| 31 | +# set up the touch pins |
| 32 | +touchins = [] |
| 33 | +for i, pin in enumerate(touch_pins): |
| 34 | + touchins.append( touchio.TouchIn(pin) ) |
| 35 | +last_touch = [False] * len(touchins) |
| 36 | + |
| 37 | +print("Welcome! Touch a pad!") |
| 38 | +while True: |
| 39 | + for i, touchin in enumerate(touchins): |
| 40 | + touch = touchin.value # get current touch value |
| 41 | + if touch != last_touch[i]: # was there a change in touch? |
| 42 | + last_touch[i] = touch # save for next time |
| 43 | + notenum = midi_notenums[i] # get MIDI note for this pad |
| 44 | + if touch : # pressed! |
| 45 | + print("touch!", i) |
| 46 | + midi_out.write(bytearray([note_on_status, notenum, 127])) |
| 47 | + leds.fill(0x330044) |
| 48 | + else: |
| 49 | + print("release!",i) |
| 50 | + midi_out.write(bytearray([note_off_status, notenum, 0])) |
| 51 | + leds.fill(0) |
0 commit comments