|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""A EuroPi re-imagining of Traffic by Jasmine & Olive Trees |
| 3 | +
|
| 4 | +Two gate inputs are sent to AIN and DIN, their values multiplied by gains controlled by K1 and K2, |
| 5 | +and the summed & differenced outputs sent to the outputs |
| 6 | +""" |
| 7 | + |
| 8 | +from europi import * |
| 9 | +from europi_script import EuroPiScript |
| 10 | + |
| 11 | +from experimental.a_to_d import AnalogReaderDigitalWrapper |
| 12 | +from experimental.knobs import KnobBank |
| 13 | +from experimental.screensaver import OledWithScreensaver |
| 14 | + |
| 15 | +ssoled = OledWithScreensaver() |
| 16 | + |
| 17 | +class Traffic(EuroPiScript): |
| 18 | + def __init__(self): |
| 19 | + super().__init__() |
| 20 | + |
| 21 | + state = self.load_state_json() |
| 22 | + |
| 23 | + # Button handlers to change the active channel for setting gains |
| 24 | + self.channel_markers = ['>', ' ', ' '] # used to indicate the editable channel on the screen |
| 25 | + @b1.handler |
| 26 | + def b1_rising(): |
| 27 | + """Activate channel b controls while b1 is held |
| 28 | + """ |
| 29 | + ssoled.notify_user_interaction() |
| 30 | + self.k1_bank.set_current("channel_b") |
| 31 | + self.k2_bank.set_current("channel_b") |
| 32 | + self.channel_markers[0] = ' ' |
| 33 | + self.channel_markers[1] = '>' |
| 34 | + self.channel_markers[2] = ' ' |
| 35 | + |
| 36 | + @b2.handler |
| 37 | + def b2_rising(): |
| 38 | + """Activate channel c controls while b1 is held |
| 39 | + """ |
| 40 | + ssoled.notify_user_interaction() |
| 41 | + self.k1_bank.set_current("channel_c") |
| 42 | + self.k2_bank.set_current("channel_c") |
| 43 | + self.channel_markers[0] = ' ' |
| 44 | + self.channel_markers[1] = ' ' |
| 45 | + self.channel_markers[2] = '>' |
| 46 | + |
| 47 | + def either_button_falling(): |
| 48 | + """Revert to channel a when the button is released |
| 49 | + """ |
| 50 | + self.k1_bank.set_current("channel_a") |
| 51 | + self.k2_bank.set_current("channel_a") |
| 52 | + self.channel_markers[0] = '>' |
| 53 | + self.channel_markers[1] = ' ' |
| 54 | + self.channel_markers[2] = ' ' |
| 55 | + self.save_state() |
| 56 | + b1.handler_falling(either_button_falling) |
| 57 | + b2.handler_falling(either_button_falling) |
| 58 | + |
| 59 | + |
| 60 | + # Input trigger handlers |
| 61 | + self.input1_trigger_at = time.ticks_ms() |
| 62 | + self.input2_trigger_at = self.input1_trigger_at |
| 63 | + @din.handler |
| 64 | + def din1_rising(): |
| 65 | + self.input1_trigger_at = time.ticks_ms() |
| 66 | + |
| 67 | + # Set the all-triggers output high |
| 68 | + self.last_output_trigger_at = self.input1_trigger_at |
| 69 | + cv6.on() |
| 70 | + |
| 71 | + def din2_rising(): |
| 72 | + self.input2_trigger_at = time.ticks_ms() |
| 73 | + |
| 74 | + # Set the all-triggers output high |
| 75 | + self.last_output_trigger_at = self.input2_trigger_at |
| 76 | + cv6.on() |
| 77 | + |
| 78 | + self.k1_bank = ( |
| 79 | + KnobBank.builder(k1) \ |
| 80 | + .with_unlocked_knob("channel_a") \ |
| 81 | + .with_locked_knob("channel_b", initial_percentage_value=state.get("gain_b1", 0.5)) \ |
| 82 | + .with_locked_knob("channel_c", initial_percentage_value=state.get("gain_c1", 0.5)) \ |
| 83 | + .build() |
| 84 | + ) |
| 85 | + |
| 86 | + self.k2_bank = ( |
| 87 | + KnobBank.builder(k2) \ |
| 88 | + .with_unlocked_knob("channel_a") \ |
| 89 | + .with_locked_knob("channel_b", initial_percentage_value=state.get("gain_b2", 0.5)) \ |
| 90 | + .with_locked_knob("channel_c", initial_percentage_value=state.get("gain_c2", 0.5)) \ |
| 91 | + .build() |
| 92 | + ) |
| 93 | + |
| 94 | + self.din1 = din |
| 95 | + self.din2 = AnalogReaderDigitalWrapper(ain, cb_rising=din2_rising) |
| 96 | + |
| 97 | + # we fire a trigger on CV6, so keep track of the previous outputs so we know when something's changed |
| 98 | + self.last_output_trigger_at = time.ticks_ms() |
| 99 | + |
| 100 | + def save_state(self): |
| 101 | + state = { |
| 102 | + "gain_b1": self.k1_bank["channel_b"].percent(samples=1024), |
| 103 | + "gain_b2": self.k2_bank["channel_b"].percent(samples=1024), |
| 104 | + "gain_c1": self.k1_bank["channel_c"].percent(samples=1024), |
| 105 | + "gain_c2": self.k2_bank["channel_c"].percent(samples=1024) |
| 106 | + } |
| 107 | + self.save_state_json(state) |
| 108 | + |
| 109 | + def main(self): |
| 110 | + TRIGGER_DURATION = 10 # 10ms triggers every time we get a rising edge on either input channel |
| 111 | + |
| 112 | + while True: |
| 113 | + self.din2.update() |
| 114 | + |
| 115 | + gain_a1 = round(self.k1_bank["channel_a"].percent(samples=1024), 3) |
| 116 | + gain_a2 = round(self.k2_bank["channel_a"].percent(samples=1024), 3) |
| 117 | + |
| 118 | + gain_b1 = round(self.k1_bank["channel_b"].percent(samples=1024), 3) |
| 119 | + gain_b2 = round(self.k2_bank["channel_b"].percent(samples=1024), 3) |
| 120 | + |
| 121 | + gain_c1 = round(self.k1_bank["channel_c"].percent(samples=1024), 3) |
| 122 | + gain_c2 = round(self.k2_bank["channel_c"].percent(samples=1024), 3) |
| 123 | + |
| 124 | + # calculate the outputs |
| 125 | + delta_t = time.ticks_diff(self.input1_trigger_at, self.input2_trigger_at) |
| 126 | + if delta_t > 0 or abs(delta_t) <= 50: # assume triggers within 50ms are simultaneous |
| 127 | + # din received a trigger more recently than ain |
| 128 | + out_a = MAX_OUTPUT_VOLTAGE * gain_a1 |
| 129 | + out_b = MAX_OUTPUT_VOLTAGE * gain_b1 |
| 130 | + out_c = MAX_OUTPUT_VOLTAGE * gain_c1 |
| 131 | + else: |
| 132 | + # ain received a trigger more recently than din |
| 133 | + out_a = MAX_OUTPUT_VOLTAGE * gain_a2 |
| 134 | + out_b = MAX_OUTPUT_VOLTAGE * gain_b2 |
| 135 | + out_c = MAX_OUTPUT_VOLTAGE * gain_c2 |
| 136 | + |
| 137 | + cv1.voltage(out_a) |
| 138 | + cv2.voltage(out_b) |
| 139 | + cv3.voltage(out_c) |
| 140 | + cv4.voltage(abs(out_a - out_b)) |
| 141 | + cv5.voltage(abs(out_a - out_c)) |
| 142 | + |
| 143 | + now = time.ticks_ms() |
| 144 | + if time.ticks_diff(now, self.last_output_trigger_at) > TRIGGER_DURATION: |
| 145 | + cv6.off() |
| 146 | + |
| 147 | + # show the current gains * outputs, marking the channel we're controlling via the knobs & buttons |
| 148 | + ssoled.centre_text(f"{self.channel_markers[0]} A {gain_a1:0.3f} {gain_a2:0.3f}\n{self.channel_markers[1]} B {gain_b1:0.3f} {gain_b2:0.3f}\n{self.channel_markers[2]} C {gain_c1:0.3f} {gain_c2:0.3f}") |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + Traffic().main() |
0 commit comments