feat: add SDK-free 3DoF tracking for VITURE Pro 2 (#1115)

* feat: itch.io store, library and installs for DRM-free Windows builds (#1064)

* feat: add experimental SDK-free VITURE Pro 2 tracking

* docs: record full-app Pro 2 hardware validation

* docs: record successful Pro 2 user game test

* fix: translate Pro 2 tracking notice for all supported locales

* fix: respect model capabilities for glasses controls

* fix: handle fold configuration during glasses probe

---------

Co-authored-by: kingokksa <47888451+kingokksa@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-10-22 10:28:14 -04:00
1 parent 7482f797c7
commit 3d9f200a7e
66 files changed
+2974 -19

No files matched your search

@@ -43,6 +43,7 @@ data class GlassesState(
val volumeMax: Int = 8,
val sunblock: Boolean = false,
val hardwareControls: Boolean = true,
val supportsDisplayModeControl: Boolean = true,
val hasSunblock: Boolean = true,
val controlError: String? = null,
val tracking: Boolean = false,
@@ -64,7 +65,7 @@ data class GlassesState(
val spatialConnected: Boolean get() = canControl && supportsSpatial
fun usesSpatialControllers(settings: GlassesSettings) = spatialConnected && settings.positionalTracking &&
(settings.handControllers || settings.joyConTracking)
val can3d: Boolean get() = canControl && !noSignal && sbsSupport != SBS_FAILS
val can3d: Boolean get() = canControl && supportsDisplayModeControl && !noSignal && sbsSupport != SBS_FAILS
val verticalFovDeg: Float get() {
val half = Math.toRadians(diagonalFovDeg / 2.0)
val aspect = if (width > 0 && height > 0)
@@ -213,7 +214,7 @@ object Glasses {
}
fun sendMode(mode: Int) = post {
if (!_state.value.canControl) return@post
if (!_state.value.canControl || !_state.value.supportsDisplayModeControl) return@post
if (link?.send(VitureProtocol.MSG_DISPLAY, byteArrayOf(mode.toByte())) == true) lastModeSentAt = SystemClock.uptimeMillis()
}
@@ -470,6 +471,7 @@ object Glasses {
.put("controlError", s.controlError ?: JSONObject.NULL)
.put("experimental", s.experimental)
.put("supportsScreen3dof", s.supportsScreen3dof)
.put("supportsDisplayModeControl", s.supportsDisplayModeControl)
.put("supportsSpatial", s.supportsSpatial)
.put("firmware", (link as? VitureSdkLink)?.firmware ?: JSONObject.NULL)
.put("spatialSdk", (link as? VitureSdkLink)?.spatialDiagnostics()?.let {
@@ -614,14 +616,16 @@ object Glasses {
switching3d = modeState.pendingStereo,
brightnessMax = m?.brightnessMax ?: 8, volumeMax = m?.volumeMax ?: 8,
sunblock = if (m === XrealModel) it.sunblock else _settings.value.sunblock ?: it.sunblock,
hardwareControls = device?.vendorId == VitureProtocol.VENDOR_ID || m === XrealModel || m is RokidModel,
hardwareControls = m?.hardwareControls == true &&
(device?.vendorId == VitureProtocol.VENDOR_ID || m === XrealModel || m is RokidModel),
supportsDisplayModeControl = m?.supportsDisplayModeControl == true,
hasSunblock = m?.hasSunblock == true,
diagonalFovDeg = m?.diagonalFovDeg ?: 46f,
tracking = it.tracking && link != null,
noSignal = noSignal,
sbs90 = m?.sbs90 == true,
rates2d = m?.rates2d ?: listOf(60, 90, 120),
experimental = m === XrealModel,
experimental = m === XrealModel || (m is VitureProtocol.Model && m.family == VitureProtocol.Family.PRO2 && !m.needsSdk),
supportsScreen3dof = link?.hasImu == true,
supports30Hz = display?.let(::supports30Hz) == true,
supportsSpatial = (link as? VitureSdkLink)?.isCarina == true,
@@ -653,7 +657,8 @@ object Glasses {
internal fun deviceModel(device: UsbDevice): GlassesModel? {
val name = runCatching { device.productName }.getOrNull()
return if (device.vendorId == VitureProtocol.VENDOR_ID) {
VitureSdk.marketName(device.productId)?.let(VitureProtocol::sdkModel)
if (device.productId == VitureProtocol.PRO2_PRODUCT_ID) VitureProtocol.model(device.productId, name)
else VitureSdk.marketName(device.productId)?.let(VitureProtocol::sdkModel)
?: VitureProtocol.model(device.productId, name)
}
else if (XrealModel.matches(device.vendorId, device.productId)) XrealModel
@@ -720,7 +725,9 @@ object Glasses {
calibrationKey = calibrationStore.key(device.vendorId, device.productId,
runCatching { device.serialNumber }.getOrNull(), model?.name.orEmpty())
val l = if (device.vendorId == VitureProtocol.VENDOR_ID) {
if ((model as? VitureProtocol.Model)?.needsSdk == true) {
if (device.productId == VitureProtocol.PRO2_PRODUCT_ID) {
VitureV2Link.open(usb, device, v2Listener(generation))
} else if ((model as? VitureProtocol.Model)?.needsSdk == true) {
// Android can evict cacheDir while the SDK is running.
Log.i(TAG, "opening VITURE SDK pid=0x%04X spatial=%s".format(device.productId, spatial))
VitureSdkLink.open(usb, device, File(app.noBackupFilesDir, "viture"), sdkListener(generation),
@@ -851,6 +858,40 @@ object Glasses {
}
}
private fun v2Listener(generation: Long) = object : VitureV2Link.Listener {
override fun onReady() = post {
if (generation != linkGeneration) return@post
val current = link as? VitureV2Link ?: return@post
publish { it.copy(connected = current.ready, supportsScreen3dof = current.hasImu,
tracking = current.trackingActive, controlError = null) }
updateImu()
}
override fun onValue(msgId: Int, value: Int) = post {
if (generation != linkGeneration) return@post
when (msgId) {
VitureProtocol.MSG_GET_DISPLAY -> glassesMode = value
VitureProtocol.MSG_GET_BRIGHTNESS -> publish { it.copy(brightness = value) }
VitureProtocol.MSG_GET_VOLUME -> publish { it.copy(volume = value) }
}
}
override fun onPose(raw: ByteArray, q: FloatArray, timeNs: Long) {
if (generation != linkGeneration) return
imuPackets++
probe?.invoke(raw, q)
if (simulating.get() == null) deliver(q, timeNs)
}
override fun onTracking(active: Boolean) = post {
if (generation != linkGeneration) return@post
val current = link as? VitureV2Link ?: return@post
publish { it.copy(tracking = active && current.trackingActive) }
}
override fun onLost() = post {
if (generation != linkGeneration) return@post
closeLink()
publish { it.copy(controlError = app.getString(R.string.glasses_connection_failed)) }
}
}
private fun rayNeoListener(generation: Long) = object : RayNeoLink.Listener {
override fun onDeviceInfo(boardId: Int, calibrationSaved: Boolean) = post {
if (generation != linkGeneration) return@post
@@ -926,7 +967,7 @@ object Glasses {
private fun applyMode(force: Boolean) {
val s = _state.value
val m = model ?: return
if (!s.canControl) return
if (!s.canControl || !s.supportsDisplayModeControl) return
if (modeState.restorePending) {
sendMode(false)
return
@@ -948,6 +989,7 @@ object Glasses {
private fun sendMode(sbs: Boolean): Boolean {
val m = model ?: return false
if (!m.supportsDisplayModeControl) return false
if (sbs && !_settings.value.gameOnGlasses) update { it.copy(gameOnGlasses = true) }
val g = _settings.value
val resolution = GlassesDisplayModes.selected(g, _state.value, sbs)
@@ -980,11 +1022,11 @@ object Glasses {
val l = link ?: return
if (!l.ready) return
val want = (sink != null || poseObservers.isNotEmpty() || probe != null || _calibration.value.running || SpatialTracking.enabled) && l.hasImu
if (want == _state.value.tracking) return
if (l !is VitureV2Link && want == _state.value.tracking) return
val accepted = l.send(VitureProtocol.MSG_IMU, byteArrayOf(if (want) 1 else 0))
if (want && !accepted) return
if (want) l.send(VitureProtocol.MSG_IMU_RATE, byteArrayOf(VitureProtocol.IMU_RATE_240.toByte()))
publish { it.copy(tracking = want) }
publish { it.copy(tracking = if (l is VitureV2Link) l.trackingActive else want) }
}
private fun linkListener(generation: Long) = object : VitureLink.Listener {
@@ -4,6 +4,8 @@ package com.droiddeck.launcher.glasses
interface GlassesModel {
val name: String
val controllable: Boolean
val hardwareControls: Boolean get() = true
val supportsDisplayModeControl: Boolean get() = true
val brightnessMax: Int
val volumeMax: Int
val diagonalFovDeg: Float
@@ -6,6 +6,7 @@ import kotlin.math.sqrt
object VitureProtocol {
const val VENDOR_ID = 0x35CA
const val PRO2_PRODUCT_ID = 0x1301
const val PACKET_SIZE = 64
private const val HEADER = 0x12
@@ -33,13 +34,19 @@ object VitureProtocol {
enum class Family { ONE, PRO, LUMA, ULTRA, BEAST, PRO2 }
class Model(override val name: String, val family: Family) : GlassesModel {
class Model(
override val name: String,
val family: Family,
val needsSdk: Boolean = family == Family.ULTRA || family == Family.BEAST || family == Family.PRO2,
) : GlassesModel {
override val controllable: Boolean get() = true
val needsSdk: Boolean get() = family == Family.ULTRA || family == Family.BEAST || family == Family.PRO2
private val directPro2: Boolean get() = family == Family.PRO2 && !needsSdk
override val hardwareControls: Boolean get() = !directPro2
override val supportsDisplayModeControl: Boolean get() = !directPro2
val tall: Boolean get() = family == Family.LUMA || family == Family.ULTRA || family == Family.BEAST
override val sbs90: Boolean get() = family == Family.ULTRA
override val brightnessMax: Int get() = 8
override val volumeMax: Int get() = if (family == Family.BEAST) 15 else 8
override val brightnessMax: Int get() = if (directPro2) 0 else 8
override val volumeMax: Int get() = if (directPro2) 0 else if (family == Family.BEAST) 15 else 8
override val hasSunblock: Boolean get() = family != Family.PRO2
override val diagonalFovDeg: Float get() = when (family) {
Family.ONE -> 43f
@@ -89,6 +96,7 @@ object VitureProtocol {
}
fun model(productId: Int, productName: String?): Model? {
if (productId == PRO2_PRODUCT_ID) return Model("VITURE Pro 2", Family.PRO2, needsSdk = false)
val family = when (productId) {
0x1011, 0x1013, 0x1015, 0x1017, 0x101b -> Family.ONE
0x1019, 0x101d -> Family.PRO
@@ -0,0 +1,288 @@
package com.droiddeck.launcher.glasses
import android.hardware.usb.UsbConstants
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbDeviceConnection
import android.hardware.usb.UsbEndpoint
import android.hardware.usb.UsbInterface
import android.hardware.usb.UsbManager
import android.hardware.usb.UsbRequest
import android.util.Log
import java.io.IOException
import java.nio.ByteBuffer
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.TimeoutException
import java.util.concurrent.atomic.AtomicLong
internal class VitureV2Link internal constructor(
private val transport: Transport,
private val listener: Listener,
) : GlassesLink {
interface Listener {
fun onReady()
fun onValue(msgId: Int, value: Int)
fun onPose(raw: ByteArray, q: FloatArray, timeNs: Long)
fun onTracking(active: Boolean)
fun onLost()
}
internal data class Received(val bytes: ByteArray, val timeNs: Long)
internal interface Transport {
fun write(packet: ByteArray): Boolean
fun read(timeoutMs: Long): Received?
fun close()
}
private data class Command(val packet: ByteArray, val stream: Boolean? = null)
private val commands = ArrayBlockingQueue<Command>(16)
private val stateLock = Any()
private val bad = AtomicLong()
@Volatile private var running = true
@Volatile private var wanted = true
@Volatile override var ready = false
private set
@Volatile var trackingActive = false
private set
override val hasImu: Boolean get() = ready
override val badFrames: Long get() = bad.get()
@Volatile private var streamOn = false
private var lastPoseAt = 0L
private var lastTick: Long? = null
private var poseNotBefore = 0L
private val worker = Thread(::run, "viture-v2").apply { isDaemon = true; start() }
override fun send(msgId: Int, payload: ByteArray): Boolean {
synchronized(stateLock) {
if (!running || !ready) return false
if (msgId == VitureProtocol.MSG_IMU) {
if (payload.size != 1 || payload[0].toInt() !in 0..1) return false
val enabled = payload[0].toInt() == 1
if (wanted == enabled) return true
val command = Command(VitureV2Protocol.imuControl(
if (enabled) VitureV2Protocol.STREAM_POSE else VitureV2Protocol.STREAM_OFF), enabled)
if (!commands.offer(command)) return false
wanted = enabled
if (!enabled) {
streamOn = false
tracking(false)
}
return true
}
if (msgId == VitureProtocol.MSG_GET_IMU) return true
// Keep the measured 120 Hz mode; the generic controller requests 240 Hz.
if (msgId == VitureProtocol.MSG_IMU_RATE) return false
val query = when (msgId) {
VitureProtocol.MSG_GET_BRIGHTNESS -> VitureV2Protocol.MSG_GET_BRIGHTNESS
VitureProtocol.MSG_GET_VOLUME -> VitureV2Protocol.MSG_GET_VOLUME
VitureProtocol.MSG_GET_DISPLAY -> VitureV2Protocol.MSG_GET_DISPLAY
else -> return false
}
return payload.isEmpty() && commands.offer(Command(VitureV2Protocol.packet(query)))
}
}
private fun tracking(active: Boolean) {
if (trackingActive != active) {
trackingActive = active
listener.onTracking(active)
}
}
private fun run() {
try {
if (!transport.write(VitureV2Protocol.imuControl(VitureV2Protocol.STREAM_POSE)))
throw IOException("Could not start pose stream")
poseNotBefore = System.nanoTime()
streamOn = true
lastPoseAt = System.nanoTime()
while (running) {
commands.poll()?.let { command ->
if (!transport.write(command.packet)) throw IOException("USB command failed")
val completedAt = System.nanoTime()
synchronized(stateLock) {
command.stream?.let {
streamOn = it
lastTick = null
poseNotBefore = completedAt
lastPoseAt = System.nanoTime()
}
}
}
transport.read(100)?.let(::receive)
if (streamOn && System.nanoTime() - lastPoseAt > POSE_TIMEOUT_NS)
throw IOException("No fresh valid pose samples")
}
} catch (e: Exception) {
if (running) {
ready = false
tracking(false)
Log.w(TAG, "V2 connection lost: ${e.message}")
listener.onLost()
}
} finally {
running = false
ready = false
tracking(false)
runCatching { transport.write(VitureV2Protocol.imuControl(VitureV2Protocol.STREAM_OFF)) }
transport.close()
}
}
private fun receive(received: Received) = synchronized(stateLock) {
if (!running) return
val frame = VitureV2Protocol.parse(received.bytes) ?: run { bad.incrementAndGet(); return }
if (frame.msgId == VitureV2Protocol.EVENT_POSE) {
val pose = VitureV2Protocol.pose(frame) ?: run { bad.incrementAndGet(); return }
if (!streamOn || !wanted || received.timeNs < poseNotBefore) return
val previous = lastTick
if (previous != null) {
val delta = (pose.deviceTick - previous) and 0xffffffffL
if (delta == 0L || delta >= 0x80000000L) return
}
lastTick = pose.deviceTick
lastPoseAt = System.nanoTime()
val becameReady = !ready
ready = true
tracking(true)
if (becameReady) listener.onReady()
val q = pose.quaternion
// Upstream right/up/back pose converted to the shared VITURE legacy basis.
val shared = floatArrayOf(-q[2], -q[0], q[1], q[3])
// Upstream does not establish device tick units; use host monotonic receipt time.
if (running && wanted && streamOn) listener.onPose(received.bytes, shared, received.timeNs)
return
}
VitureV2Protocol.reply(frame, VitureV2Protocol.MSG_IMU_CONTROL)?.let {
if (!it.success) throw IOException("Pose stream rejected with status ${it.status}")
}
val queries = arrayOf(
VitureV2Protocol.MSG_GET_BRIGHTNESS to VitureProtocol.MSG_GET_BRIGHTNESS,
VitureV2Protocol.MSG_GET_VOLUME to VitureProtocol.MSG_GET_VOLUME,
VitureV2Protocol.MSG_GET_DISPLAY to VitureProtocol.MSG_GET_DISPLAY,
)
for ((query, internalId) in queries) {
val reply = VitureV2Protocol.reply(frame, query) ?: continue
if (reply.success && reply.body.size == 1) listener.onValue(internalId, reply.body[0].toInt() and 255)
}
}
override fun close() {
synchronized(stateLock) {
running = false
ready = false
wanted = false
commands.clear()
}
if (Thread.currentThread() !== worker) worker.join(1500)
}
companion object {
private const val TAG = "VitureV2Link"
private const val POSE_TIMEOUT_NS = 3_000_000_000L
const val PRODUCT_ID = 0x1301
fun open(usb: UsbManager, device: UsbDevice, listener: Listener): VitureV2Link? {
if (device.vendorId != VitureProtocol.VENDOR_ID || device.productId != PRODUCT_ID) return null
val candidates = (0 until device.interfaceCount).map(device::getInterface).filter {
it.interfaceClass == UsbConstants.USB_CLASS_HID && it.alternateSetting == 0 && it.endpointCount == 2
}.mapNotNull { intf ->
val endpoints = (0 until intf.endpointCount).map(intf::getEndpoint)
val input = endpoints.singleOrNull { it.direction == UsbConstants.USB_DIR_IN &&
it.type == UsbConstants.USB_ENDPOINT_XFER_INT && it.maxPacketSize == 64 }
val output = endpoints.singleOrNull { it.direction == UsbConstants.USB_DIR_OUT &&
it.type == UsbConstants.USB_ENDPOINT_XFER_INT && it.maxPacketSize == 64 }
if (input != null && output != null) Triple(intf, input, output) else null
}
val (intf, input, output) = candidates.singleOrNull() ?: return null
val connection = usb.openDevice(device) ?: return null
if (!connection.claimInterface(intf, true)) { connection.close(); return null }
return try {
VitureV2Link(AndroidTransport(connection, intf, input, output), listener)
} catch (e: Exception) {
connection.releaseInterface(intf)
connection.close()
Log.w(TAG, "Could not initialize USB requests", e)
null
}
}
}
private class AndroidTransport(
private val connection: UsbDeviceConnection,
private val intf: UsbInterface,
input: UsbEndpoint,
output: UsbEndpoint,
) : Transport {
private val inputRequest = UsbRequest()
private val outputRequest = UsbRequest()
private val inputBuffer = ByteBuffer.allocateDirect(64)
private var outputBuffer: ByteBuffer? = null
private var outputPending = false
private val received = ArrayDeque<Received>()
init {
try {
if (!inputRequest.initialize(connection, input) || !outputRequest.initialize(connection, output))
throw IOException("USB request initialization failed")
queueInput()
} catch (e: Exception) {
inputRequest.close()
outputRequest.close()
throw e
}
}
private fun queueInput() {
inputBuffer.clear()
if (!inputRequest.queue(inputBuffer)) throw IOException("USB input queue failed")
}
private fun await(timeoutMs: Long) {
val completed = try { connection.requestWait(timeoutMs) } catch (_: TimeoutException) { return }
?: throw IOException("USB device disconnected")
if (completed === outputRequest) {
outputPending = false
} else if (completed === inputRequest) {
val count = inputBuffer.position()
if (count > 0) {
val bytes = ByteArray(count)
inputBuffer.flip()
inputBuffer.get(bytes)
if (received.size >= 512) throw IOException("USB input backlog overflow")
received.addLast(Received(bytes, System.nanoTime()))
}
queueInput()
}
}
override fun write(packet: ByteArray): Boolean {
if (outputPending) throw IOException("Previous USB output has not completed")
val buffer = ByteBuffer.allocateDirect(packet.size).apply { put(packet); flip() }
outputBuffer = buffer
outputPending = outputRequest.queue(buffer)
if (!outputPending) return false
val deadline = System.nanoTime() + 250_000_000L
while (outputPending && System.nanoTime() < deadline) await(25)
if (outputPending) {
outputRequest.cancel()
throw IOException("USB output timed out")
}
return buffer.position() == packet.size
}
override fun read(timeoutMs: Long): Received? {
if (received.isEmpty()) await(timeoutMs)
return received.removeFirstOrNull()
}
override fun close() {
inputRequest.cancel()
if (outputPending) outputRequest.cancel()
connection.releaseInterface(intf)
connection.close()
inputRequest.close()
outputRequest.close()
outputBuffer = null
}
}
}
@@ -0,0 +1,132 @@
// Adapted from elasticjava/viture-v2, MIT, Copyright (c) 2026 Holger Bartnick.
// Source: ece8c30485f7d357fefa362ae74f3eabac9b3899; see docs/third-party/viture-v2/.
package com.droiddeck.launcher.glasses
import kotlin.math.sqrt
object VitureV2Protocol {
const val PACKET_SIZE = 64
const val HEADER_SIZE = 8
const val RESPONSE_OFFSET = 0x2000
private const val PREAMBLE = 0x0010
private const val MAX_PAYLOAD_SIZE = PACKET_SIZE - HEADER_SIZE
const val MSG_IMU_CONTROL = 0x0301
const val MSG_GET_SERIAL = 0x3002
const val MSG_GET_VERSION = 0x3003
const val MSG_GET_BRIGHTNESS = 0x3122
const val MSG_GET_DUTY_CYCLE = 0x3125
const val MSG_GET_DISPLAY = 0x3141
const val MSG_GET_VOLUME = 0x3201
const val MSG_GET_WEAR = 0x3321
const val EVENT_POSE = 0x7308
const val EVENT_RAW = 0x7309
const val STREAM_OFF = 0
const val STREAM_POSE = 1
const val STREAM_RAW = 2
enum class Rate(val wireValue: Int, val hz: Int) {
HZ_60(0, 60), HZ_90(1, 90), HZ_120(2, 120),
HZ_240(3, 240), HZ_500(4, 500), HZ_1000(5, 1000),
}
class Frame internal constructor(val msgId: Int, val payload: ByteArray)
class Reply internal constructor(val status: Int, val body: ByteArray) {
val success: Boolean get() = status == 0
}
// Device ticks have no established time unit; quaternion is xyzw with no axis remapping.
class Pose internal constructor(
val deviceTick: Long,
val quaternion: FloatArray,
val unknown: Long,
)
class RawSample internal constructor(
val deviceTick: Long,
val gyroRadPerSec: FloatArray,
val accelG: FloatArray,
val payload: ByteArray,
)
fun packet(msgId: Int, payload: ByteArray = ByteArray(0)): ByteArray {
require(msgId in 0..0xFFFF)
require(payload.size <= MAX_PAYLOAD_SIZE)
return ByteArray(HEADER_SIZE + payload.size).also { buffer ->
putU16(buffer, 0, PREAMBLE)
putU16(buffer, 2, msgId)
putU16(buffer, 4, payload.size)
putU16(buffer, 6, checksum(payload, 0, payload.size))
payload.copyInto(buffer, HEADER_SIZE)
}
}
fun imuControl(streams: Int, rate: Rate = Rate.HZ_120): ByteArray {
// Upstream measured that stream mask 3 ACKs but produces no events on Pro 2.
require(streams == STREAM_OFF || streams == STREAM_POSE || streams == STREAM_RAW)
require(streams != STREAM_POSE || rate.hz <= 240)
val wireRate = if (streams == STREAM_OFF) 0 else rate.wireValue
return packet(MSG_IMU_CONTROL, byteArrayOf(streams.toByte(), wireRate.toByte()))
}
fun parse(buffer: ByteArray, size: Int = buffer.size): Frame? {
if (size < HEADER_SIZE || size > PACKET_SIZE || size > buffer.size) return null
if (u16(buffer, 0) != PREAMBLE) return null
val payloadSize = u16(buffer, 4)
if (payloadSize > MAX_PAYLOAD_SIZE || HEADER_SIZE + payloadSize > size) return null
if (checksum(buffer, HEADER_SIZE, payloadSize) != u16(buffer, 6)) return null
return Frame(u16(buffer, 2), buffer.copyOfRange(HEADER_SIZE, HEADER_SIZE + payloadSize))
}
fun reply(frame: Frame, requestMsgId: Int): Reply? {
if (requestMsgId !in 0..0xFFFF || frame.msgId != ((requestMsgId + RESPONSE_OFFSET) and 0xFFFF)) return null
if (frame.payload.isEmpty()) return null
return Reply(u8(frame.payload[0]), frame.payload.copyOfRange(1, frame.payload.size))
}
fun pose(frame: Frame): Pose? {
if (frame.msgId != EVENT_POSE || frame.payload.size != 24) return null
val payload = frame.payload
val w = float(payload, 8)
val x = float(payload, 12)
val y = float(payload, 16)
val z = float(payload, 20)
if (!w.isFinite() || !x.isFinite() || !y.isFinite() || !z.isFinite()) return null
val norm = sqrt(w.toDouble() * w + x.toDouble() * x + y.toDouble() * y + z.toDouble() * z)
if (norm !in 0.9..1.1) return null
val n = norm.toFloat()
return Pose(u32(payload, 4), floatArrayOf(x / n, y / n, z / n, w / n), u32(payload, 0))
}
fun raw(frame: Frame): RawSample? {
if (frame.msgId != EVENT_RAW || frame.payload.size != 56) return null
val payload = frame.payload
val gyro = floatArrayOf(float(payload, 10), float(payload, 14), float(payload, 18))
val accel = floatArrayOf(float(payload, 22), float(payload, 26), float(payload, 30))
if (gyro.any { !it.isFinite() } || accel.any { !it.isFinite() }) return null
return RawSample(u32(payload, 4), gyro, accel, payload.copyOf())
}
private fun checksum(buffer: ByteArray, offset: Int, size: Int): Int {
var sum = 0
for (i in offset until offset + size) sum += u8(buffer[i])
return sum and 0xFFFF
}
private fun u8(value: Byte) = value.toInt() and 0xFF
private fun u16(buffer: ByteArray, offset: Int) =
u8(buffer[offset]) or (u8(buffer[offset + 1]) shl 8)
private fun u32(buffer: ByteArray, offset: Int): Long =
u16(buffer, offset).toLong() or (u16(buffer, offset + 2).toLong() shl 16)
private fun float(buffer: ByteArray, offset: Int): Float = Float.fromBits(u32(buffer, offset).toInt())
private fun putU16(buffer: ByteArray, offset: Int, value: Int) {
buffer[offset] = value.toByte()
buffer[offset + 1] = (value ushr 8).toByte()
}
}
@@ -149,7 +149,8 @@ internal fun GlassesGroups(
}
}
if (display) SettingsGroup(g.model) {
if (g.experimental) SettingsRow(stringResource(R.string.glasses_control), stringResource(R.string.glasses_xreal_experimental)) {}
if (g.experimental) SettingsRow(stringResource(R.string.glasses_control), stringResource(
if (g.supportsDisplayModeControl) R.string.glasses_xreal_experimental else R.string.glasses_pro2_experimental)) {}
val status = if (g.hasDisplay) stringResource(R.string.glasses_status, g.width, g.height, kotlin.math.round(g.hz).toInt(),
stringResource(if (g.sbs) R.string.glasses_mode_3d else R.string.glasses_mode_2d)) else null
when {
@@ -168,7 +169,7 @@ internal fun GlassesGroups(
ToggleRow(host, "glasses-game", stringResource(R.string.glasses_game_on), stringResource(R.string.glasses_game_on_hint),
s.gameOnGlasses, chipModifier = track("glasses-game")) { Glasses.setGameOnGlasses(it) }
}
if (display && g.canControl && (inSession || g.sbs || g.sbsSupport == GlassesState.SBS_FAILS)) {
if (display && g.canControl && g.supportsDisplayModeControl && (inSession || g.sbs || g.sbsSupport == GlassesState.SBS_FAILS)) {
SettingsGroup(stringResource(R.string.glasses_display_mode)) {
if (g.sbsSupport == GlassesState.SBS_FAILS && !g.sbs) {
ActionRow(stringResource(R.string.glasses_3d), stringResource(R.string.glasses_3d_failed),
@@ -177,7 +178,7 @@ internal fun GlassesGroups(
g.sbs, chipModifier = track("glasses-3d")) { Glasses.set3d(it) }
}
}
if (display && g.canControl) {
if (display && g.canControl && g.supportsDisplayModeControl) {
val profiles = if (inSession) listOf(GlassesDisplayModes.profile(g.sbs, false))
else listOf(GlassesDisplayProfile.TWO_D, GlassesDisplayProfile.THREE_D)
for (profile in profiles) SettingsGroup(stringResource(if (profile == GlassesDisplayProfile.TWO_D)
@@ -214,9 +215,9 @@ internal fun GlassesGroups(
}
if (vr && (g.canControl || g.hasDisplay)) SettingsGroup(stringResource(R.string.cardboard_steamvr)) {
steamVr()
ToggleRow(host, "glasses-auto3d", stringResource(R.string.glasses_auto3d), stringResource(R.string.glasses_auto3d_hint),
s.auto3d, chipModifier = track("glasses-auto3d")) { Glasses.setAuto3d(it) }
if (g.canControl) {
if (g.supportsDisplayModeControl) ToggleRow(host, "glasses-auto3d", stringResource(R.string.glasses_auto3d),
stringResource(R.string.glasses_auto3d_hint), s.auto3d, chipModifier = track("glasses-auto3d")) { Glasses.setAuto3d(it) }
if (g.canControl && g.supportsDisplayModeControl) {
GlassesResolutionRow(host, g, s, GlassesDisplayProfile.VR, track)
if (steamVrRunning && tab != null) GlassesRefreshRow(host, g, s, track)
}