Allow performing resets with button press

Maybe we'd want some UI changes for the resets section, not sure.
Sorry if this code sucks 😭
This commit is contained in:
Sapphire committed 2026-10-28 18:55:35 -05:00
1 parent c911a29d25
commit 02df411819
8 files changed
+114 -101

No files matched your search

@@ -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<Int, Int>()
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 {
@@ -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<HIDButton> { 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)
}
}
@@ -76,7 +76,7 @@ interface HIDReceiverBehaviour : Behaviour<HIDReceiver> {
}
typealias HIDPacketDispatcher = EventDispatcher<HIDPacket>
private fun v3Behaviours(): List<HIDReceiverBehaviour> = listOf(
private fun v3Behaviours(appContext: AppContextProvider): List<HIDReceiverBehaviour> = listOf(
HIDDongleInfoBehaviour(),
HIDTrackerListBehaviour(),
HIDDeviceInfoV3Behaviour(),
@@ -86,17 +86,17 @@ private fun v3Behaviours(): List<HIDReceiverBehaviour> = listOf(
HIDRssiBehaviour(),
HIDPacketLossBehaviour(),
HIDSleepV3Behaviour(),
HIDButtonV3Behaviour(),
HIDButtonV3Behaviour(appContext.tapDetectionManager),
)
private fun legacyBehaviours(): List<HIDReceiverBehaviour> = listOf(
private fun legacyBehaviours(appContext: AppContextProvider): List<HIDReceiverBehaviour> = 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))
@@ -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<TapDetectionSetupModeRequest> { req ->
tapDetectionManager.context.dispatch(TapDetectionActions.SetSetupMode(req.setupMode == true))
}.launchIn(receiver.context.scope)
@@ -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<Pair<Float, TimeMark>> = ArrayDeque(),
val tapTimestamps: ArrayDeque<TimeMark> = 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<Int>()
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,
@@ -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<TapDetectionState, TapDetectionActions>
@@ -23,12 +31,40 @@ typealias TapDetectionBehaviour = Behaviour<TapDetectionManager>
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()),
@@ -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)
}