Firmware, iOS-App und Gehäuse für den 5x5x5-LED-Würfel
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>
This commit is contained in:
commit
55b94475a5
22 files changed
+52104
No files matched your search
@@ -0,0 +1,94 @@
|
||||
import Foundation
|
||||
import Observation
|
||||
|
||||
/// Hält den Zustand des Würfels und gleicht ihn regelmäßig mit der API ab.
|
||||
@MainActor @Observable
|
||||
final class CubeModel {
|
||||
/// Name oder IP-Adresse des Würfels, wird gespeichert.
|
||||
var host: String {
|
||||
didSet {
|
||||
UserDefaults.standard.set(host, forKey: "host")
|
||||
state = nil
|
||||
effects = []
|
||||
error = nil
|
||||
}
|
||||
}
|
||||
|
||||
private(set) var state: CubeState?
|
||||
private(set) var effects: [CubeEffect] = []
|
||||
private(set) var error: String?
|
||||
|
||||
/// Wert des Helligkeitsreglers; folgt dem Würfel, solange nicht gezogen wird.
|
||||
var brightness: Double = 32
|
||||
var isDraggingBrightness = false
|
||||
|
||||
var isConnected: Bool { state != nil && error == nil }
|
||||
|
||||
private var api: CubeAPI { CubeAPI(host: host) }
|
||||
|
||||
init() {
|
||||
host = UserDefaults.standard.string(forKey: "host") ?? "cube.local"
|
||||
}
|
||||
|
||||
/// Fragt den Zustand alle 2 Sekunden ab, bis die Aufgabe abgebrochen wird.
|
||||
func poll() async {
|
||||
while !Task.isCancelled {
|
||||
await refresh()
|
||||
try? await Task.sleep(for: .seconds(2))
|
||||
}
|
||||
}
|
||||
|
||||
func refresh() async {
|
||||
do {
|
||||
if effects.isEmpty { effects = try await api.effects() }
|
||||
apply(try await api.state())
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
func select(_ effect: CubeEffect) async {
|
||||
await update(CubeStatePatch(effect: effect.id))
|
||||
}
|
||||
|
||||
func setAuto(_ on: Bool) async {
|
||||
await update(CubeStatePatch(auto: on))
|
||||
}
|
||||
|
||||
func commitBrightness() async {
|
||||
await update(CubeStatePatch(brightness: Int(brightness)))
|
||||
}
|
||||
|
||||
func special(_ id: String) async {
|
||||
do {
|
||||
try await api.special(id)
|
||||
error = nil
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
private func update(_ patch: CubeStatePatch) async {
|
||||
do {
|
||||
apply(try await api.update(patch))
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
private func apply(_ newState: CubeState) {
|
||||
state = newState
|
||||
error = nil
|
||||
if !isDraggingBrightness { brightness = Double(newState.brightness) }
|
||||
}
|
||||
|
||||
/// Macht aus Eingaben wie "http://10.0.1.8/" ein "10.0.1.8".
|
||||
static func cleanHost(_ input: String) -> String {
|
||||
var host = input.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
for prefix in ["http://", "https://"] where host.lowercased().hasPrefix(prefix) {
|
||||
host.removeFirst(prefix.count)
|
||||
}
|
||||
while host.hasSuffix("/") { host.removeLast() }
|
||||
return host.isEmpty ? "cube.local" : host
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user