From 4d8464282ee470fe1e830b8fc779bcf39b71d2ec Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Wed, 1 Jul 2026 13:09:18 -0700 Subject: [PATCH] Add per-binding CC invert toggle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each CC binding gains an "Invert" toggle in the mapping sheet (shown once the control is bound). Inverting swaps the sweep — CC 0 drives the control to its max, 127 to its min — so a single expression pedal can crossfade two controls: map both to the same CC and invert one. The MIDICCBinding.inverted field and applyCC already honored inversion; this adds isCCInverted / setCCInverted plus the sheet toggle, and the binding label now notes "· inverted". Test: an inverted binding reverses the mapped output. 321 green; both app targets build. Co-Authored-By: Claude Opus 4.8 --- .../LyrebirdShoeGaze/MIDI/MIDIController.swift | 16 ++++++++++++++++ .../LyrebirdShoeGazeUI/MIDIMappingSheet.swift | 9 ++++++++- .../MIDIControllerCCTests.swift | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift b/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift index 0f5a7c6..16dbdef 100644 --- a/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift +++ b/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift @@ -176,6 +176,22 @@ public final class MIDIController { shoeGaze?.midiMap?.ccs.first { $0.target.addresses(target) } } + /// Whether the CC binding on a control is inverted (CC 0 → control max), or `false` if unbound. + public func isCCInverted(for target: MIDIMap.CCTarget) -> Bool { + ccBinding(for: target)?.inverted ?? false + } + + /// Sets the invert flag on the CC binding assigned to a control. Inverting swaps the sweep so a + /// single expression pedal can crossfade — e.g. fade one control down as it moves, another up. + public func setCCInverted(for target: MIDIMap.CCTarget, _ inverted: Bool) { + guard let shoeGaze: ShoeGaze = shoeGaze, var map: MIDIMap = shoeGaze.midiMap, + let index: Int = map.ccs.firstIndex(where: { $0.target.addresses(target) }), + map.ccs[index].inverted != inverted else { return } + map.ccs[index].inverted = inverted + shoeGaze.midiMap = map + onMapChanged?() + } + /// Removes any CC binding assigned to a control. public func clearCCBinding(for target: MIDIMap.CCTarget) { guard let shoeGaze: ShoeGaze = shoeGaze, var map: MIDIMap = shoeGaze.midiMap else { return } diff --git a/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift b/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift index e355a5d..b11060a 100644 --- a/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift +++ b/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift @@ -223,6 +223,12 @@ public struct MIDIMappingSheet: View { } Spacer() if binding != nil { + Toggle("Invert", isOn: Binding( + get: { controller.isCCInverted(for: row.target) }, + set: { controller.setCCInverted(for: row.target, $0) } + )) + .toggleStyle(.button) + .tint(.orange) Button("Clear") { controller.clearCCBinding(for: row.target) } .buttonStyle(.borderless) } @@ -243,6 +249,7 @@ public struct MIDIMappingSheet: View { if isArmed { return "Listening — move an expression pedal…" } guard let binding: MIDIMap.CCBinding = binding else { return "Unassigned" } let channelText: String = binding.channel.map { " · ch \($0 + 1)" } ?? "" - return "CC \(binding.controller)\(channelText)" + let invertText: String = binding.inverted ? " · inverted" : "" + return "CC \(binding.controller)\(channelText)\(invertText)" } } diff --git a/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift b/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift index 7bdf1d9..2291178 100644 --- a/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift +++ b/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift @@ -121,6 +121,24 @@ struct MIDIControllerCCTests { } } + @Test func invertedCCReversesTheSweep() throws { + let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig() + defer { try? FileManager.default.removeItem(at: url) } + + withExtendedLifetime(sg) { + controller.armCCLearn(pedalControl(0, 0, 0)) + controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0)) + controller.setCCInverted(for: pedalControl(0, 0, 0), true) + #expect(controller.isCCInverted(for: pedalControl(0, 0, 0))) + + let node: UIParamNode = board.pedals[0].ccMappableControls[0].node + controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 127)) + #expect(abs(node.value - 0.0) < 1e-9, "inverted CC 127 should map to the control min") + controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0)) + #expect(abs(node.value - 1.0) < 1e-9, "inverted CC 0 should map to the control max") + } + } + @Test func unboundCCIsIgnored() throws { let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) }