ESP32-S3-Firmware mit 18 Effekten, Mikrofon (Klatschen, Beat, Spektrum) und REST-API, Testprogramme, SwiftUI-Fernbedienung und OpenSCAD-Sockel. Die WLAN-Zugangsdaten (secrets.h) sind nicht enthalten. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
211 lines
7.6 KiB
Swift
211 lines
7.6 KiB
Swift
import SwiftUI
|
|
|
|
struct ContentView: View {
|
|
@State private var model = CubeModel()
|
|
@State private var showSettings = false
|
|
@State private var flashes = 0
|
|
@Environment(\.scenePhase) private var scenePhase
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
// Kein Scrollen: die Effekt-Kacheln teilen sich die restliche Höhe
|
|
VStack(spacing: 12) {
|
|
status
|
|
specials
|
|
brightness
|
|
autoToggle
|
|
effectGrid
|
|
}
|
|
.padding(.horizontal)
|
|
.padding(.vertical, 8)
|
|
.navigationTitle("LED-Würfel")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .topBarTrailing) {
|
|
Button("Einstellungen", systemImage: "gearshape") { showSettings = true }
|
|
}
|
|
}
|
|
.sheet(isPresented: $showSettings) {
|
|
SettingsView(host: model.host) { model.host = CubeModel.cleanHost($0) }
|
|
}
|
|
// Nur abfragen, solange die App im Vordergrund ist
|
|
.task(id: scenePhase) {
|
|
if scenePhase == .active { await model.poll() }
|
|
}
|
|
}
|
|
}
|
|
|
|
private var status: some View {
|
|
HStack(spacing: 6) {
|
|
Circle()
|
|
.fill(model.isConnected ? .green : model.error == nil ? .gray : .red)
|
|
.frame(width: 8, height: 8)
|
|
Text(model.error ?? (model.isConnected ? "Verbunden mit \(model.host)" : "Verbinde mit \(model.host) …"))
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
|
|
/// Spezialeffekte, die kurz über dem laufenden Effekt abgespielt werden
|
|
private var specials: some View {
|
|
HStack(spacing: 8) {
|
|
specialButton("blitz", "Blitz", "bolt.fill", .yellow)
|
|
specialButton("explosion", "Explosion", "burst.fill", .orange)
|
|
specialButton("regenbogen", "Regenbogen", "rainbow", .pink)
|
|
}
|
|
.sensoryFeedback(.impact(weight: .heavy), trigger: flashes)
|
|
.disabled(!model.isConnected)
|
|
}
|
|
|
|
private func specialButton(_ id: String, _ title: String, _ symbol: String, _ color: Color) -> some View {
|
|
Button {
|
|
flashes += 1
|
|
Task { await model.special(id) }
|
|
} label: {
|
|
VStack(spacing: 4) {
|
|
Image(systemName: symbol).font(.title2)
|
|
Text(title)
|
|
.font(.caption.weight(.semibold))
|
|
.lineLimit(1)
|
|
.minimumScaleFactor(0.7)
|
|
}
|
|
.foregroundStyle(.black)
|
|
.frame(maxWidth: .infinity, minHeight: 56)
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
.buttonBorderShape(.roundedRectangle(radius: 14))
|
|
.tint(color)
|
|
}
|
|
|
|
private var brightness: some View {
|
|
HStack(spacing: 12) {
|
|
Image(systemName: "sun.min.fill")
|
|
.foregroundStyle(.secondary)
|
|
Slider(value: $model.brightness, in: 0...255, step: 1) { editing in
|
|
model.isDraggingBrightness = editing
|
|
if !editing { Task { await model.commitBrightness() } }
|
|
}
|
|
.accessibilityLabel("Helligkeit")
|
|
Image(systemName: "sun.max.fill")
|
|
.foregroundStyle(.secondary)
|
|
Text("\(Int(model.brightness))")
|
|
.monospacedDigit()
|
|
.foregroundStyle(.secondary)
|
|
.frame(width: 36, alignment: .trailing)
|
|
}
|
|
.disabled(!model.isConnected)
|
|
}
|
|
|
|
private var autoToggle: some View {
|
|
Toggle("Automatisch wechseln", isOn: Binding(
|
|
get: { model.state?.auto ?? true },
|
|
set: { on in Task { await model.setAuto(on) } }
|
|
))
|
|
.disabled(!model.isConnected)
|
|
}
|
|
|
|
/// Links zwei Spalten normale Effekte, rechts eine Spalte Musik-Effekte.
|
|
/// Die Zeilen wachsen mit dem Platz (32 bis 56 Punkte hoch).
|
|
private var effectGrid: some View {
|
|
let normal = model.effects.filter { !$0.isMusic }
|
|
let music = model.effects.filter(\.isMusic)
|
|
let rowCount = max((normal.count + 1) / 2, music.count)
|
|
return VStack(spacing: 8) {
|
|
HStack(spacing: 8) {
|
|
Color.clear.frame(maxWidth: .infinity)
|
|
Color.clear.frame(maxWidth: .infinity)
|
|
Label("Musik", systemImage: "music.note")
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
.font(.caption.weight(.semibold))
|
|
.foregroundStyle(.secondary)
|
|
.opacity(model.effects.isEmpty ? 0 : 1)
|
|
ForEach(0..<rowCount, id: \.self) { row in
|
|
HStack(spacing: 8) {
|
|
cell(normal, 2 * row)
|
|
cell(normal, 2 * row + 1)
|
|
cell(music, row)
|
|
}
|
|
.frame(minHeight: 32, maxHeight: 56)
|
|
}
|
|
}
|
|
.frame(maxHeight: .infinity, alignment: .top)
|
|
.disabled(!model.isConnected)
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func cell(_ effects: [CubeEffect], _ index: Int) -> some View {
|
|
if index < effects.count {
|
|
tile(effects[index])
|
|
} else {
|
|
Color.clear.frame(maxWidth: .infinity)
|
|
}
|
|
}
|
|
|
|
private func tile(_ effect: CubeEffect) -> some View {
|
|
let active = effect.id == model.state?.effect
|
|
let idle = effect.isMusic ? Color.indigo.opacity(0.35) : Color(.secondarySystemBackground)
|
|
return Button {
|
|
Task { await model.select(effect) }
|
|
} label: {
|
|
Text(effect.name)
|
|
.font(.subheadline)
|
|
.fontWeight(active ? .semibold : .regular)
|
|
.multilineTextAlignment(.center)
|
|
// Nur an Leerzeichen umbrechen; einzelne Wörter lieber verkleinern als trennen
|
|
.lineLimit(effect.name.contains(" ") ? 2 : 1)
|
|
.minimumScaleFactor(0.6)
|
|
.foregroundStyle(active ? .black : .primary)
|
|
.padding(.horizontal, 4)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
.background(active ? Color.green : idle, in: .rect(cornerRadius: 10))
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
}
|
|
|
|
struct SettingsView: View {
|
|
let host: String
|
|
let save: (String) -> Void
|
|
@State private var draft = ""
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Form {
|
|
Section {
|
|
TextField("cube.local", text: $draft)
|
|
.keyboardType(.URL)
|
|
.textInputAutocapitalization(.never)
|
|
.autocorrectionDisabled()
|
|
} header: {
|
|
Text("Adresse des Würfels")
|
|
} footer: {
|
|
Text("Name oder IP-Adresse im WLAN, z. B. cube.local oder 10.0.1.8")
|
|
}
|
|
}
|
|
.navigationTitle("Einstellungen")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Abbrechen") { dismiss() }
|
|
}
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button("Fertig") {
|
|
save(draft)
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
.onAppear { draft = host }
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
ContentView()
|
|
.preferredColorScheme(.dark)
|
|
}
|