‹ projects

reverb-m1

Native wired VR on Apple silicon: Reverb G1 display bring-up and stereo demo
git clone https://umhau.org/code/reverb-m1.git
Log | Files | Refs

main.swift (2846B)


      1 import Foundation
      2 import Metal
      3 import simd
      4 setvbuf(stdout, nil, _IOLBF, 0)
      5 // Usage: VRWorkstation | --observe <seconds> | --snapshot out.png [--snapshot-size WxH] [--snapshot-yaw deg]
      6 // Options: --snapshot-size WxH (default 4320x2160), --snapshot-yaw <degrees> (head turn, positive = left).
      7 func option(_ name: String) -> String? {
      8     guard let i = CommandLine.arguments.firstIndex(of: name), CommandLine.arguments.count > i + 1 else { return nil }
      9     return CommandLine.arguments[i + 1]
     10 }
     11 if let path = option("--read-calibration") {
     12     let hmd = HMD()
     13     do {
     14         try hmd.start(mode: .observe)
     15         defer { hmd.stop() }
     16         let data = try hmd.readFactoryConfiguration()
     17         let block = try WMRConfigBlock(raw: [UInt8](data))
     18         let calibration = try HeadsetCalibration(json: block.json)
     19         let url = URL(fileURLWithPath: path)
     20         try data.write(to: url, options: .atomic)
     21         let jsonURL = url.deletingPathExtension().appendingPathExtension("json")
     22         try block.json.write(to: jsonURL, options: .atomic)
     23         print("saved factory configuration to", path, "and", jsonURL.path)
     24         print("validated factory optical calibration (unit-specific geometry omitted)")
     25     } catch { print("factory calibration read failed:", error); exit(1) }
     26     exit(0)
     27 }
     28 if CommandLine.arguments.contains("--observe") {
     29     guard let value = option("--observe"), let seconds = Double(value), seconds > 0, seconds.isFinite else {
     30         print("usage: VRWorkstation --observe <positive seconds>"); exit(2)
     31     }
     32     let hmd = HMD()
     33     hmd.onStatus = { log("[observe] \($0)") }
     34     hmd.onDisconnected = { log("[observe] USB device removed") }
     35     do {
     36         try hmd.start(mode: .observe)
     37         log("[observe] listening for \(seconds)s; no HID commands or display capture")
     38         RunLoop.main.run(until: Date().addingTimeInterval(seconds))
     39         log("[observe] IMU samples=\(hmd.imuSamples), status reports=\(hmd.statusReports), display=\(onlineHeadsetDisplay().map(String.init) ?? "absent")")
     40         hmd.stop()
     41     } catch { print("observation failed:", error); exit(1) }
     42     exit(0)
     43 }
     44 if let i = CommandLine.arguments.firstIndex(of: "--snapshot") {
     45     let path = CommandLine.arguments.count > i + 1 ? CommandLine.arguments[i + 1] : "snapshot.png"
     46     let dims = (option("--snapshot-size") ?? "4320x2160").split(separator: "x").compactMap { Int($0) }
     47     let (w, h) = dims.count == 2 ? (dims[0], dims[1]) : (4320, 2160)
     48     let yaw = Float(option("--snapshot-yaw") ?? "0") ?? 0
     49     do {
     50         let r = try Renderer(device: MTLCreateSystemDefaultDevice())
     51         r.headOrientation = { simd_quatf(angle: yaw * .pi / 180, axis: [0, 1, 0]) }
     52         try r.snapshot(width: w, height: h, time: 1.0, to: path)
     53         print("wrote", path)
     54     } catch { print("snapshot failed:", error) }
     55     exit(0)
     56 }
     57 runApp()