diff --git a/code.py b/code.py new file mode 100755 index 0000000..7f8ab20 --- /dev/null +++ b/code.py @@ -0,0 +1,83 @@ +################################################################################ +# +# Pi-Pico-ExpressionPedal2Midi +# +# Connect an expression pedal to ADC0, a midi out socket/cable to UART1 if not +# using USB midi. Midi messages are sent simultaneoulsy to UART1 and USB. +# Set desired midi channel, change control, and maximum and minimum values +# +# Upon run/power on, move expresson pedal from maximum to minimum to calibrate +# your pedal. CC commands will immediately start sending once calibrated. +# +# Ashley Strahle +# https://github.com/ashstrahle +# +################################################################################ + +import sys +import struct +import time +import board +import busio +import analogio +import digitalio +import usb_midi +import adafruit_midi +from adafruit_midi.timing_clock import TimingClock +from adafruit_midi.note_on import NoteOn +from adafruit_midi.note_off import NoteOff +from adafruit_midi.pitch_bend import PitchBend +from adafruit_midi.control_change import ControlChange + +# Midi settings +midi_channel = 1 # Target midi channel to write to +cc = 68 # Target Control Change number - this is for Behringer X32 Matrix 5 +cc_min = 20 # Minimum desired CC output +cc_max = 97 # Maximum desired CC output (only want fader to go to unity gain - hence not 127) + +exp_pedal_calibration_percent = 80 # Required percentage of expression pedal movement for calibration + +# Devices +led = digitalio.DigitalInOut(board.LED) +led.direction = digitalio.Direction.OUTPUT +exp = analogio.AnalogIn(board.GP26) # Expression pedal device on pin 31 +uart = busio.UART(tx=board.GP4, rx=board.GP5, baudrate=31250, timeout=0.001) # UART Midi device on pin 6 +uart_midi = adafruit_midi.MIDI(midi_out=uart, out_channel=midi_channel - 1) +usb_midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=midi_channel - 1) + +# Initialise variables +# Set these to reverse thresholds to enable calibration +exp_min = 65535 +exp_max = 1 + +exp_calibration_threshold = int(abs(exp_max - exp_min) * exp_pedal_calibration_percent / 100) +cc_ratio = 1/(cc_max - cc_min) # Calculate number of possible CC values + +# This function translates the expression pedal value to the equivalent CC value +def translate(exp_val): + ret = int((((exp_val - exp_min) * (cc_max - cc_min)) / (exp_max - exp_min)) + cc_min) + if ret > 0: + return ret + else: + return 0 + +exp_previous = exp.value +while True: + exp_current = exp.value + + # Only process if the change ratio is greater than the possible number of CC values + if abs(exp_current - exp_previous) / exp_max > cc_ratio: + if exp_current > exp_max: + exp_max = exp_current + elif exp_current < exp_min: + exp_min = exp_current + exp_previous = exp_current + + # Only send midi when calibration threshold has been reached + if exp_max - exp_min > exp_calibration_threshold: + led.value = True # Turn led on + cc_val = translate(exp_current) + uart_midi.send(ControlChange(cc, cc_val)) + usb_midi.send(ControlChange(cc, cc_val)) + led.value = False # Turn led off + print("Writing Midi Channel: {}, ControlChange: {}, Value {}. Exp Pedal: cur: {}, min: {}, max: {}".format(midi_channel, cc, cc_val, exp_current, exp_min, exp_max)) \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index a49a6a9..25817ed 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,10 +1,10 @@ -# Pi-Pico-ExpressionPedal2Midi +# [Pi-Pico-ExpressionPedal2Midi](https://github.com/ashstrahle/Pi-Pico-ExpressionPedal2Midi) ![](/docs/Pi-Pico-ExpressionPedal2Midi.gif) ![](/docs/Pi-Pico-ExpressionPedal2Midi2.jpeg) -This is a Raspberry Pi Pico MicroPython project that takes an expression pedal input via a TRS 1/4" jack connected to ADC pins on the Pico, and outputs respective Midi CC messages via UART. +This is a Raspberry Pi Pico CircuitPython project that takes an expression pedal input via a TRS 1/4" jack connected to ADC pins on the Pico, and outputs respective Midi CC messages simultaneously to UART and USB. Midi channel, CC number, and maximum and minimum values are customisable. @@ -16,43 +16,48 @@ The expression pedal is automatically calibrated. At startup, simply move your p - Raspberry Pi Pico (loaded with MicroPython) - ¼” jack TRS socket -- 5 pin DIN midi socket -- 10Ω resistor -- 33Ω resistor +- 5 pin DIN midi socket1 +- 10Ω resistor1 +- 33Ω resistor1 -Optional: breadboard, 40 pin male headers, pin cables, scotch +1 Required only if using midi port (non-usb) + +Optional: breadboard, 40 pin male headers, pin cables, scotch whisky Power source: Either USB or 3xAA battery holder ### Method -1. Customise the midi settings in main.py. Season to taste -2. Upload to your board using Thonny, or your favorite IDE. +1. Download [CircuitPython](https://circuitpython.org/board/raspberry_pi_pico/) and install on your Pico. +2. Download [CircuitPython Libraries](https://circuitpython.org/libraries) and copy ```adafruit_midi``` folder to ```lib``` folder on your Pico. +3. Customise the midi settings in ```code.py```. Season to taste +4. Copy ```code.py``` to your Pico. -Now for the stuffing: +Now for the stuffing: -#### Midi port - -![](/docs/Pi-Pico-ExpressionPedal2Midi4.jpeg) - -Midi messages are sent via UART1. Here’s where we need the resistors to protect the board and your midi device. - -3. Connect a 10Ω resistor to pin 6 on the Pico (UART1 TX). T’other end of the resistor to pin 4 of your midi socket -4. Connect a 33Ω resistor to pin 36 on the Pico (3V3 OUT). T’other end of the resistor to pin 5 of your midi socket -5. Connect a Pico ground pin (any of 3, 8, 13, 18, 23, 28, or 33) to pin 2 of your midi socket - -You’re done here, next… - -#### Expression pedal jack +#### Expression Pedal Jack ![](/docs/Pi-Pico-ExpressionPedal2Midi3.jpeg) The expression pedal is connected to ADC0 on the Pico. -6. Connect the jack sleeve to Pico ground pin (any of 3, 8, 13, 18, 23, 28, or 33) -7. Connect the jack ring to Pico pin 31 (ADC0) -8. Connect the jack tip to Pico pin 36 (3V3 OUT) +5. Connect the jack sleeve to Pico ground pin (any of 3, 8, 13, 18, 23, 28, or 33) +6. Connect the jack ring to Pico pin 31 (ADC0) +7. Connect the jack tip to Pico pin 36 (3V3 OUT) + +If you're only using USB, you're done! + +Otherwise... +#### Midi Port + +![](/docs/Pi-Pico-ExpressionPedal2Midi4.jpeg) + +Midi messages are sent simultaneously to USB and UART1. If you wish to connect your Pico to a Midi port, proeceed with the following. + +8. Connect a 10Ω resistor to pin 6 on the Pico (UART1 TX). T’other end of the resistor to pin 4 of your midi socket +9. Connect a 33Ω resistor to pin 36 on the Pico (3V3 OUT). T’other end of the resistor to pin 5 of your midi socket +10. Connect a Pico ground pin (any of 3, 8, 13, 18, 23, 28, or 33) to pin 2 of your midi socket Jubilations, you’re done. diff --git a/legacy/README.md b/legacy/README.md new file mode 100644 index 0000000..b60b915 --- /dev/null +++ b/legacy/README.md @@ -0,0 +1,4 @@ +# Legacy Notes + +This is the original MicroPython version of Pi-PicoExpressionPedal2Midi, and only outputs to UART1 (no USB). +Kept here for reference. \ No newline at end of file diff --git a/main.py b/legacy/main.py similarity index 100% rename from main.py rename to legacy/main.py