Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Show ESR, etc. in circuit view #195

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions Impedance Converter/Impedance Converter/CircuitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ struct InductanceView: View {
}
}

extension Binding where Value: BinaryFloatingPoint {
static func reciprocal(_ source: Binding<Value>) -> Binding<Value> {
Binding(
get: { 1 / source.wrappedValue },
set: { source.wrappedValue = 1 / $0 }
)
}
}

struct ResistorView: View {
@ObservedObject var viewModel: ViewModel

var body: some View {
switch (viewModel.circuitMode, viewModel.displayMode) {
case (.series, .impedance), (.series, .reflectionCoefficient):
UnitInputView(value: $viewModel.resistance, unit: ResistanceUnit.Ω, label: "ESR", description: "eq. series res.")
case (.parallel, .impedance), (.parallel, .reflectionCoefficient):
UnitInputView(value: .reciprocal($viewModel.conductance), unit: ResistanceUnit.Ω, label: "Rₚ", description: "eq. parallel res.")
case (.series, .admittance):
UnitInputView(value: .reciprocal($viewModel.resistance), unit: ConductanceUnit.S, label: "Gₛ", description: "eq. series cond.")
case (.parallel, .admittance):
UnitInputView(value: $viewModel.conductance, unit: ConductanceUnit.S, label: "Gₚ", description: "eq. parallel cond.")
}
}
}

struct CircuitView: View {

@AppStorage("showLCQD") private var showLCQD = true
Expand All @@ -55,14 +81,15 @@ struct CircuitView: View {

var body: some View {
if (showLCQD) {
HStack(alignment: .bottom, spacing: 0) {
CircuitPickerView(viewModel: viewModel)
DisplayView {
CircuitPickerView(viewModel: viewModel)
InductanceView(viewModel: viewModel)
CapacitanceView(viewModel: viewModel)
DisplayView {
HStack {
ResistorView(viewModel: viewModel)
FrequencyView(viewModel: viewModel)
}
}
InductanceView(viewModel: viewModel)
CapacitanceView(viewModel: viewModel)
}
}
}