From 02df4118198225f3c900e48489fc69fd7cb6b369 Mon Sep 17 00:00:00 2001 From: Sapphire Date: Wed, 28 Oct 2026 18:55:35 -0500 Subject: [PATCH] Allow performing resets with button press Maybe we'd want some UI changes for the resets section, not sure. Sorry if this code sucks :sob: --- gui/app/public/i18n/en/translation.ftl | 4 +- .../slimevr/hid/behaviours/legacy/button.kt | 8 +- .../dev/slimevr/hid/behaviours/v3/button.kt | 8 +- .../src/main/java/dev/slimevr/hid/module.kt | 10 +- .../dev/slimevr/solarxr/rpc/tap-detection.kt | 2 +- .../dev/slimevr/tapdetection/behaviours.kt | 138 ++++++++---------- .../java/dev/slimevr/tapdetection/module.kt | 44 +++++- .../java/dev/slimevr/tapdetection/reducer.kt | 1 + 8 files changed, 114 insertions(+), 101 deletions(-) diff --git a/gui/app/public/i18n/en/translation.ftl b/gui/app/public/i18n/en/translation.ftl index 9c424647e..c67f2fb7e 100644 --- a/gui/app/public/i18n/en/translation.ftl +++ b/gui/app/public/i18n/en/translation.ftl @@ -778,8 +778,8 @@ settings-general-fk_settings-self_localization-title = Mocap mode settings-general-fk_settings-self_localization-description = Mocap Mode allows the skeleton to roughly track its own position without any positional tracker. ## Gesture control settings (tracker tapping) -settings-general-gesture_control-subtitle = Tap-based resets -settings-general-gesture_control-description = Allows for resets to be triggered by tapping a tracker. Taps must occur within the time limit of 0.3 seconds times the number of taps to be recognized. +settings-general-gesture_control-subtitle = Tap/button based resets +settings-general-gesture_control-description = Allows for resets to be triggered by tapping a tracker or pressing its button. Taps must occur within the time limit of 0.3 seconds times the number of taps to be recognized. # This is a unit: 3 taps, 2 taps, 1 tap # $amount (Number) - Amount of taps (touches to the tracker's case) settings-general-gesture_control-taps = { $amount -> diff --git a/server/core/src/main/java/dev/slimevr/hid/behaviours/legacy/button.kt b/server/core/src/main/java/dev/slimevr/hid/behaviours/legacy/button.kt index da2b018b9..2504c2230 100644 --- a/server/core/src/main/java/dev/slimevr/hid/behaviours/legacy/button.kt +++ b/server/core/src/main/java/dev/slimevr/hid/behaviours/legacy/button.kt @@ -6,9 +6,10 @@ import dev.slimevr.hid.HIDDataLegacy import dev.slimevr.hid.HIDReceiver import dev.slimevr.hid.HIDReceiverBehaviour import dev.slimevr.hid.HIDRotationButtonLegacy +import dev.slimevr.tapdetection.TapDetectionManager import solarxr_protocol.rpc.TapDetectionSetupNotification -class HIDButtonLegacyBehaviour : HIDReceiverBehaviour { +class HIDButtonLegacyBehaviour(private val tapDetectionManager: TapDetectionManager) : HIDReceiverBehaviour { private val lastButton = mutableMapOf() override fun observe(receiver: HIDReceiver) { @@ -23,11 +24,8 @@ class HIDButtonLegacyBehaviour : HIDReceiverBehaviour { lastButton[hidId] = button if (!fired) return - if (!receiver.appContext.tapDetectionManager.context.state.value.setupMode) return val tracker = receiver.getTracker(hidId) ?: return - receiver.appContext.server.sendSolarxrRpc( - TapDetectionSetupNotification(tracker.context.state.value.id.toUShort()), - ) + tapDetectionManager.actionTriggered(tracker.context.state.value.id) } companion object { diff --git a/server/core/src/main/java/dev/slimevr/hid/behaviours/v3/button.kt b/server/core/src/main/java/dev/slimevr/hid/behaviours/v3/button.kt index 438dac9c7..da8da8503 100644 --- a/server/core/src/main/java/dev/slimevr/hid/behaviours/v3/button.kt +++ b/server/core/src/main/java/dev/slimevr/hid/behaviours/v3/button.kt @@ -3,16 +3,14 @@ package dev.slimevr.hid.behaviours.v3 import dev.slimevr.hid.HIDButton import dev.slimevr.hid.HIDReceiver import dev.slimevr.hid.HIDReceiverBehaviour +import dev.slimevr.tapdetection.TapDetectionManager import solarxr_protocol.rpc.TapDetectionSetupNotification -class HIDButtonV3Behaviour : HIDReceiverBehaviour { +class HIDButtonV3Behaviour(private val tapDetectionManager: TapDetectionManager) : HIDReceiverBehaviour { override fun observe(receiver: HIDReceiver) { receiver.packetEvents.on { packet -> - if (!receiver.appContext.tapDetectionManager.context.state.value.setupMode) return@on val tracker = receiver.getTracker(packet.hidId) ?: return@on - receiver.appContext.server.sendSolarxrRpc( - TapDetectionSetupNotification(tracker.context.state.value.id.toUShort()), - ) + tapDetectionManager.actionTriggered(tracker.context.state.value.id) }.launchIn(receiver.context.scope) } } diff --git a/server/core/src/main/java/dev/slimevr/hid/module.kt b/server/core/src/main/java/dev/slimevr/hid/module.kt index 0b7118ec6..a17ca8f2c 100644 --- a/server/core/src/main/java/dev/slimevr/hid/module.kt +++ b/server/core/src/main/java/dev/slimevr/hid/module.kt @@ -76,7 +76,7 @@ interface HIDReceiverBehaviour : Behaviour { } typealias HIDPacketDispatcher = EventDispatcher -private fun v3Behaviours(): List = listOf( +private fun v3Behaviours(appContext: AppContextProvider): List = listOf( HIDDongleInfoBehaviour(), HIDTrackerListBehaviour(), HIDDeviceInfoV3Behaviour(), @@ -86,17 +86,17 @@ private fun v3Behaviours(): List = listOf( HIDRssiBehaviour(), HIDPacketLossBehaviour(), HIDSleepV3Behaviour(), - HIDButtonV3Behaviour(), + HIDButtonV3Behaviour(appContext.tapDetectionManager), ) -private fun legacyBehaviours(): List = listOf( +private fun legacyBehaviours(appContext: AppContextProvider): List = listOf( HIDRegistrationBehaviour(), HIDDeviceInfoBehaviour(), HIDRotationBehaviour(), HIDBatteryBehaviour(), HIDStatusBehaviour(), HIDSleepLegacyBehaviour(), - HIDButtonLegacyBehaviour(), + HIDButtonLegacyBehaviour(appContext.tapDetectionManager), ) class HIDReceiver( @@ -119,7 +119,7 @@ class HIDReceiver( AppLogger.hid.warn("HID protocol changed ($current -> $version) for ${context.state.value.serialNumber}; restart to re-wire") return } - val behaviours = if (version == HID_PROTOCOL_V3) v3Behaviours() else legacyBehaviours() + val behaviours = if (version == HID_PROTOCOL_V3) v3Behaviours(appContext) else legacyBehaviours(appContext) context.behaviours.addAll(behaviours) behaviours.forEach { it.observe(this) } context.dispatch(HIDReceiverActions.SetProtocolVersion(version)) diff --git a/server/core/src/main/java/dev/slimevr/solarxr/rpc/tap-detection.kt b/server/core/src/main/java/dev/slimevr/solarxr/rpc/tap-detection.kt index 0a1d0ed90..ac097ef1f 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/rpc/tap-detection.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/rpc/tap-detection.kt @@ -76,7 +76,7 @@ class TapDetectionBehaviour( ) }.launchIn(receiver.context.scope) - // Setup Mode (tap to assign) + // Setup Mode (tap/button press to assign) receiver.rpcDispatcher.on { req -> tapDetectionManager.context.dispatch(TapDetectionActions.SetSetupMode(req.setupMode == true)) }.launchIn(receiver.context.scope) diff --git a/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt b/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt index a89f567cb..902609351 100644 --- a/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt @@ -25,14 +25,47 @@ import kotlin.time.TimeMark class TapDetectionBasicBehaviour : TapDetectionBehaviour { data class TrackerTapDetectionState( var trackerId: Int, - var resetType: ResetType? = null, + var performsReset: Boolean = false, var tapsNeeded: Int = 2, - var actionDelay: Float = 0f, val accelList: ArrayDeque> = ArrayDeque(), val tapTimestamps: ArrayDeque = ArrayDeque(), var waitForLowAccel: Boolean = false, ) + // Loads TapDetection config and initializes a tracker's state + private fun createTrackerTapDetectionState( + tapDetectionConfig: TapDetectionConfig, + tapDetectionState: TapDetectionState, + trackerState: TrackerState, + ): TrackerTapDetectionState { + // This holds a tracker's config and state for tap detection + val trackerTapDetectionState = TrackerTapDetectionState(trackerState.id) + + // setupMode uses defaults + if (!tapDetectionState.setupMode) { + when (trackerState.bodyPart) { + tapDetectionState.yawResetBodyPart -> { + trackerTapDetectionState.performsReset = true + trackerTapDetectionState.tapsNeeded = tapDetectionConfig.yawResetTaps + } + + tapDetectionState.fullResetBodyPart -> { + trackerTapDetectionState.performsReset = true + trackerTapDetectionState.tapsNeeded = tapDetectionConfig.fullResetTaps + } + + tapDetectionState.mountingResetBodyPart -> { + trackerTapDetectionState.performsReset = true + trackerTapDetectionState.tapsNeeded = tapDetectionConfig.mountingResetTaps + } + + else -> {} + } + } + + return trackerTapDetectionState + } + // This + the assigned tap body parts are used. private val falsePositiveBodyParts = setOf( BodyPart.UPPER_CHEST, @@ -72,41 +105,46 @@ class TapDetectionBasicBehaviour : TapDetectionBehaviour { .map { trackers.filter { it.context.state.value.status == TrackerStatus.OK } } } + combine(tapConfigFlow, setupModeFlow, okTrackersFlow, ::Triple).onEach { (tapDetectionConfig, setupMode, trackers) -> + if (setupMode) { + receiver.context.dispatch(TapDetectionActions.UpdateBodyParts(null, null, null)) + return@onEach + } + + val trackersBodyParts = trackers.map { it.context.state.value.bodyPart }.toSet() + val yawResetBodyPart = arrayOf(tapDetectionConfig.yawResetBodyPart, BodyPart.UPPER_CHEST, BodyPart.LOWER_CHEST, BodyPart.HIP, BodyPart.LOWER_WAIST, BodyPart.UPPER_WAIST) + .firstOrNull { it in trackersBodyParts } ?: BodyPart.UPPER_CHEST + val fullResetBodyPart = arrayOf(tapDetectionConfig.fullResetBodyPart, BodyPart.LEFT_UPPER_LEG, BodyPart.LEFT_LOWER_LEG) + .firstOrNull { it in trackersBodyParts } ?: BodyPart.LEFT_UPPER_LEG + val mountingResetBodyPart = arrayOf(tapDetectionConfig.mountingResetBodyPart, BodyPart.RIGHT_UPPER_LEG, BodyPart.RIGHT_LOWER_LEG) + .firstOrNull { it in trackersBodyParts } ?: BodyPart.RIGHT_UPPER_LEG + + receiver.context.dispatch(TapDetectionActions.UpdateBodyParts(yawResetBodyPart, fullResetBodyPart, mountingResetBodyPart)) + }.launchIn(receiver.context.scope) + // Outer flow is refreshed whenever TapDetection config, setupMode, or a tracker's bodyPart or status changes combine( tapConfigFlow, - setupModeFlow, + receiver.context.state, okTrackersFlow, ::Triple, ) - .flatMapLatest { (tapDetectionConfig, setupMode, trackers) -> - // Computed once per outer-flow refresh for all trackers - val trackersBodyParts = trackers.map { it.context.state.value.bodyPart }.toSet() - val yawResetBodyPart = arrayOf(tapDetectionConfig.yawResetBodyPart, BodyPart.UPPER_CHEST, BodyPart.LOWER_CHEST, BodyPart.HIP, BodyPart.LOWER_WAIST, BodyPart.UPPER_WAIST) - .firstOrNull { it in trackersBodyParts } ?: BodyPart.UPPER_CHEST - val fullResetBodyPart = arrayOf(tapDetectionConfig.fullResetBodyPart, BodyPart.LEFT_UPPER_LEG, BodyPart.LEFT_LOWER_LEG) - .firstOrNull { it in trackersBodyParts } ?: BodyPart.LEFT_UPPER_LEG - val mountingResetBodyPart = arrayOf(tapDetectionConfig.mountingResetBodyPart, BodyPart.RIGHT_UPPER_LEG, BodyPart.RIGHT_LOWER_LEG) - .firstOrNull { it in trackersBodyParts } ?: BodyPart.RIGHT_UPPER_LEG - + .flatMapLatest { (tapDetectionConfig, tapDetectionState, trackers) -> // To keep track of which trackers are moving val numberTrackersOverThreshold = tapDetectionConfig.numberTrackersOverThreshold - val bodyPartsToCheck = falsePositiveBodyParts + yawResetBodyPart + fullResetBodyPart + mountingResetBodyPart + val bodyPartsToCheck = falsePositiveBodyParts + tapDetectionState.yawResetBodyPart + tapDetectionState.fullResetBodyPart + tapDetectionState.mountingResetBodyPart val trackersOverThreshold = mutableSetOf() trackers.map { tracker -> val trackerTapDetectionState = createTrackerTapDetectionState( tapDetectionConfig, - setupMode, + tapDetectionState, tracker.context.state.value, - yawResetBodyPart, - fullResetBodyPart, - mountingResetBodyPart, ) // Inner flow emits whenever a tracker's rawAcceleration is updated tracker.context.state - .filter { it.bodyPart in bodyPartsToCheck || setupMode } + .filter { tapDetectionState.setupMode || it.bodyPart in bodyPartsToCheck } .map { it.rawAcceleration to it.origin } .distinctUntilChanged() .onEach { (rawAcceleration, origin) -> @@ -124,7 +162,7 @@ class TapDetectionBasicBehaviour : TapDetectionBehaviour { } // Only trackers that actually have to detect taps - if (trackerTapDetectionState.resetType != null || setupMode) { + if (trackerTapDetectionState.performsReset || tapDetectionState.setupMode) { val othersOverThreshold = trackersOverThreshold.count() - if (isOverThreshold) 1 else 0 val tapTriggered = runTapDetection( timeSource.markNow(), @@ -135,25 +173,7 @@ class TapDetectionBasicBehaviour : TapDetectionBehaviour { ) if (tapTriggered) { - receiver.context.scope.launch { - // If it's in setup mode, tap to assign - if (setupMode) { - receiver.server.sendSolarxrRpc( - TapDetectionSetupNotification( - trackerTapDetectionState.trackerId.toUShort(), - ), - ) - } - - // If it has a reset to execute - trackerTapDetectionState.resetType?.let { reset -> - receiver.resetsManager.scheduleReset( - "TapDetection", - reset, - trackerTapDetectionState.actionDelay, - ) - } - } + receiver.actionTriggered(trackerTapDetectionState.trackerId) } } } @@ -162,46 +182,6 @@ class TapDetectionBasicBehaviour : TapDetectionBehaviour { .launchIn(receiver.context.scope) } - // Loads TapDetection config and initializes a tracker's state - private fun createTrackerTapDetectionState( - tapDetectionConfig: TapDetectionConfig, - setupMode: Boolean, - trackerState: TrackerState, - yawResetBodyPart: BodyPart, - fullResetBodyPart: BodyPart, - mountingResetBodyPart: BodyPart, - ): TrackerTapDetectionState { - // This holds a tracker's config and state for tap detection - val trackerTapDetectionState = TrackerTapDetectionState(trackerState.id) - - // setupMode uses defaults - if (!setupMode) { - when (trackerState.bodyPart) { - yawResetBodyPart if tapDetectionConfig.yawResetEnabled -> { - trackerTapDetectionState.resetType = ResetType.YAW - trackerTapDetectionState.tapsNeeded = tapDetectionConfig.yawResetTaps - trackerTapDetectionState.actionDelay = tapDetectionConfig.yawResetDelay - } - - fullResetBodyPart if tapDetectionConfig.fullResetEnabled -> { - trackerTapDetectionState.resetType = ResetType.FULL - trackerTapDetectionState.tapsNeeded = tapDetectionConfig.fullResetTaps - trackerTapDetectionState.actionDelay = tapDetectionConfig.fullResetDelay - } - - mountingResetBodyPart if tapDetectionConfig.mountingResetEnabled -> { - trackerTapDetectionState.resetType = ResetType.MOUNTING - trackerTapDetectionState.tapsNeeded = tapDetectionConfig.mountingResetTaps - trackerTapDetectionState.actionDelay = tapDetectionConfig.mountingResetDelay - } - - else -> {} - } - } - - return trackerTapDetectionState - } - // Logic loop for tap detection fun runTapDetection( now: TimeMark, diff --git a/server/core/src/main/java/dev/slimevr/tapdetection/module.kt b/server/core/src/main/java/dev/slimevr/tapdetection/module.kt index 4415ebb2d..e60713813 100644 --- a/server/core/src/main/java/dev/slimevr/tapdetection/module.kt +++ b/server/core/src/main/java/dev/slimevr/tapdetection/module.kt @@ -6,15 +6,23 @@ import dev.slimevr.config.Settings import dev.slimevr.context.Behaviour import dev.slimevr.context.Context import dev.slimevr.resets.ResetsManager +import dev.slimevr.tracker.Tracker import kotlinx.coroutines.CoroutineScope +import solarxr_protocol.datatypes.BodyPart +import solarxr_protocol.rpc.ResetType +import solarxr_protocol.rpc.TapDetectionSetupNotification import kotlin.collections.listOf data class TapDetectionState( - val setupMode: Boolean, + val setupMode: Boolean = false, + val yawResetBodyPart: BodyPart? = BodyPart.UPPER_CHEST, + val fullResetBodyPart: BodyPart? = BodyPart.LEFT_UPPER_LEG, + val mountingResetBodyPart: BodyPart? = BodyPart.RIGHT_UPPER_LEG, ) sealed interface TapDetectionActions { data class SetSetupMode(val setupMode: Boolean) : TapDetectionActions + data class UpdateBodyParts(val yawResetBodyPart: BodyPart?, val fullResetBodyPart: BodyPart?, val mountingResetBodyPart: BodyPart?) : TapDetectionActions } typealias TapDetectionContext = Context @@ -23,12 +31,40 @@ typealias TapDetectionBehaviour = Behaviour class TapDetectionManager(val context: TapDetectionContext, val server: VRServer, val resetsManager: ResetsManager, val settings: Settings) { fun startObserving() = context.observeAll(this) + suspend fun actionTriggered(trackerId: Int) { + val tracker = server.getTracker(trackerId) ?: return + val state = context.state.value + + // If it's in setup mode, tap/button press to assign + if (state.setupMode) { + server.sendSolarxrRpc( + TapDetectionSetupNotification( + trackerId.toUShort(), + ), + ) + return + } + + val bodyPart = tracker.context.state.value.bodyPart ?: return + val tapDetectionConfig = settings.context.state.value.data.tapDetectionConfig + val (resetType, actionDelay) = when (bodyPart) { + state.yawResetBodyPart -> ResetType.YAW to tapDetectionConfig.yawResetDelay + state.fullResetBodyPart -> ResetType.FULL to tapDetectionConfig.fullResetDelay + state.mountingResetBodyPart -> ResetType.MOUNTING to tapDetectionConfig.mountingResetDelay + else -> return + } + + resetsManager.scheduleReset( + "TapDetection", + resetType, + actionDelay, + ) + } + companion object { fun create(ctx: Phase1ContextProvider, resetsManager: ResetsManager, scope: CoroutineScope): TapDetectionManager { val context = Context.create( - initialState = TapDetectionState( - setupMode = false, - ), + initialState = TapDetectionState(), scope = scope, reducer = ::reduce, behaviours = listOf(TapDetectionBasicBehaviour()), diff --git a/server/core/src/main/java/dev/slimevr/tapdetection/reducer.kt b/server/core/src/main/java/dev/slimevr/tapdetection/reducer.kt index 3fc933c0a..e303a97ed 100644 --- a/server/core/src/main/java/dev/slimevr/tapdetection/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/tapdetection/reducer.kt @@ -2,4 +2,5 @@ package dev.slimevr.tapdetection fun reduce(state: TapDetectionState, action: TapDetectionActions): TapDetectionState = when (action) { is TapDetectionActions.SetSetupMode -> state.copy(setupMode = action.setupMode) + is TapDetectionActions.UpdateBodyParts -> state.copy(yawResetBodyPart = action.yawResetBodyPart, fullResetBodyPart = action.fullResetBodyPart, mountingResetBodyPart = action.mountingResetBodyPart) }