This commit is contained in:
2021-01-30 20:53:17 +10:00
parent fc4584fdce
commit f2fdcd1e67

36
main.py
View File

@@ -12,6 +12,8 @@ cc = 68 # Target Control Change number - this is for Behringer X32 Matrix 5
cc_min = 0 # Minimum desired CC output cc_min = 0 # Minimum desired CC output
cc_max = 97 # Maximum desired CC output (only want fader to go to unity gain - hence not 127) cc_max = 97 # Maximum desired CC output (only want fader to go to unity gain - hence not 127)
exp_calibration_threshold = 40000 # This is the minimum amount of change required successful for calibration
# Devices # Devices
exp = machine.ADC(Pin(26)) # Expression pedal device on pin 31 exp = machine.ADC(Pin(26)) # Expression pedal device on pin 31
uart = machine.UART(1, 31250) # UART Midi device on pin 6 uart = machine.UART(1, 31250) # UART Midi device on pin 6
@@ -20,20 +22,11 @@ led = machine.Pin(25, Pin.OUT) # Pico onboard led
# Initialise variables # Initialise variables
# Set these to reverse thresholds to enable self calibration # Set these to reverse thresholds to enable self calibration
exp_min = 65535 exp_min = 65535
exp_max = 0 exp_max = 1
exp_calibration_threshold = 40000 # This is the minimum amount of change required successful for calibration
cc_previous = 0
cc_current = 0
# This function translates the expression pedal value to the equivalent CC value # This function translates the expression pedal value to the equivalent CC value
def translate(exp_val): def translate(exp_val):
if exp_max - exp_min > exp_calibration_threshold: ret = int((((exp_val - exp_min) * (cc_max - cc_min)) / (exp_max - exp_min)) + cc_min)
ret = int((((exp_val - exp_min) * (cc_max - cc_min)) / (exp_max - exp_min)) + cc_min)
else:
return 0
if ret > 0: if ret > 0:
return ret return ret
else: else:
@@ -42,18 +35,19 @@ def translate(exp_val):
exp_previous = exp.read_u16() exp_previous = exp.read_u16()
while True: while True:
exp_current = exp.read_u16() exp_current = exp.read_u16()
if abs(exp_current - exp_previous) > 800:
exp_previous = exp_current # Only process if the change ratio is greater than the possible number of CC values
cc_current = translate(exp_current) if abs(exp_current - exp_previous)/exp_max > 1/(cc_max - cc_min):
if exp_current > exp_max: if exp_current > exp_max:
exp_max = exp_current exp_max = exp_current
elif exp_current < exp_min: elif exp_current < exp_min:
exp_min = exp_current exp_min = exp_current
exp_previous = exp_current
if cc_current != cc_previous: # Only send midi when calibration threshold has been reached
led.value(1) # Turn led on if exp_max - exp_min > exp_calibration_threshold:
uart.write(ustruct.pack("bbb",ControlChange + midi_channel - 1,cc,cc_current)) cc_val = translate(exp_current)
led.value(0) # Turn led off led.value(1) # Turn led on
cc_previous = cc_current uart.write(ustruct.pack("bbb",ControlChange + midi_channel - 1,cc,cc_val))
print("Writing Midi Channel: {}, ControlChange: {}, Value {}. Exp Pedal: cur: {}, min: {}, max: {}".format(midi_channel, cc, cc_current, exp_current, exp_min, exp_max)) led.value(0) # Turn led off
time.sleep(0.05) print("Writing Midi Channel: {}, ControlChange: {}, Value {}. Exp Pedal: cur: {}, min: {}, max: {}".format(midi_channel, cc, cc_val, exp_current, exp_min, exp_max))