mirror of
https://github.com/ashstrahle/Pi-Pico-ExpressionPedal2Midi.git
synced 2025-12-31 19:49:51 +10:00
Logarithmic or linear selectable
This commit is contained in:
94
code.py
94
code.py
@@ -2,8 +2,7 @@
|
||||
#
|
||||
# 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.
|
||||
# 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
|
||||
@@ -21,6 +20,7 @@ import board
|
||||
import busio
|
||||
import analogio
|
||||
import digitalio
|
||||
import math
|
||||
import usb_midi
|
||||
import adafruit_midi
|
||||
from adafruit_midi.timing_clock import TimingClock
|
||||
@@ -29,55 +29,69 @@ 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)
|
||||
# Midi output settings
|
||||
midi_channel = 1 # Target midi channel to write to
|
||||
cc = 1 # Target Control Change number - Expression Pedal
|
||||
cc_min = 0 # Minimum desired CC output - 0 - 127
|
||||
cc_max = 127 # Maximum desired CC output - 0 - 127
|
||||
|
||||
exp_pedal_calibration_percent = 80 # Required percentage of expression pedal movement for calibration
|
||||
# Expression pedal settings
|
||||
logarithmic = True # Expression pedal logarithmic or linear. Set to False for linear
|
||||
log_base = 100 # This value changes the feel of the log curve
|
||||
|
||||
# Required percentage of expression pedal movement for calibration
|
||||
exp_pedal_calibration_percent = 80 # 0 - 100
|
||||
|
||||
# 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
|
||||
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
|
||||
usb_midi = adafruit_midi.MIDI(
|
||||
midi_out=usb_midi.ports[1], out_channel=midi_channel - 1)
|
||||
|
||||
# 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
|
||||
if logarithmic:
|
||||
scaled_val = math.log(exp_val, log_base) # Apply logarithmic scaling
|
||||
return int((((scaled_val - math.log(exp_min, log_base)) * (cc_max - cc_min)) / (math.log(exp_max, log_base) - math.log(exp_min, log_base))) + cc_min)
|
||||
else:
|
||||
return int((((exp_val - exp_min) * (cc_max - cc_min)) / (exp_max - exp_min)) + cc_min)
|
||||
|
||||
# Initialise variables
|
||||
offset = 1e-6 # Small offset to avoid log(0) error
|
||||
|
||||
# Set these to reverse thresholds to enable calibration
|
||||
exp_min = 65535
|
||||
exp_max = offset
|
||||
|
||||
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
|
||||
exp_previous = exp.value
|
||||
if exp_previous == 0:
|
||||
exp_previous = offset
|
||||
|
||||
# main loop
|
||||
while True:
|
||||
exp_current = exp.value
|
||||
exp_current = exp.value
|
||||
if exp_current == 0:
|
||||
exp_current = offset
|
||||
|
||||
# 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 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))
|
||||
# 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))
|
||||
Reference in New Issue
Block a user