Skip to content

Commit

Permalink
Merge pull request #52 from mutualmobile/sm/low_latency_off_body_dete…
Browse files Browse the repository at this point in the history
…ct_sensor

Support for Low Latency Off-Body Detect sensor
  • Loading branch information
swap-musale authored Apr 24, 2023
2 parents 4a766f1 + e0e1e28 commit 8a1c03f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Pose6DOF | — | N/A
Stationary Detect | ⚠️ | WIP
Motion Detect | ⚠️ | WIP
Heart Beat | — | N/A
Low Latency Off-Body Detect | | N/A
Low Latency Off-Body Detect | ✅️ | rememberLowLatencyOffBodyDetectSensorState()
Accelerometer (Uncalibrated) | ⚠️ | WIP
Hinge Angle | ✅️ | rememberHingeAngleSensorState()
Head Tracker | — | N/A
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.mutualmobile.composesensors

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember

/**
* A low latency off body detect sensor that returns an event every time the device transitions from off-body to on-body and from on-body to off-body (e.g. a wearable device being removed from the wrist would trigger an event indicating an off-body transition).
* This sensor deliver the initial on-body or off-body event representing the current device state within 5 seconds of activating the sensor.
* This sensor must be able to detect and report an on-body to off-body transition within 1 second of the device being removed from the body, and must be able to detect and report an off-body to on-body transition within 5 seconds of the device being put back onto the body.
* @param isDeviceOnBody This sensor produces only two values true and false where true means currently device is in on-body state and false means device is in off-body state and Defaults set to false.
* @param isAvailable Whether the current device has an low latency off body detect sensor. Defaults to false.
* @param accuracy Accuracy factor of the low latency off body detect sensor. Defaults to 0.
*/
@Immutable
class LowLatencyOffBodyDetectSensorState internal constructor(
val isDeviceOnBody: Boolean = false,
val isAvailable: Boolean = false,
val accuracy: Int = 0
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is LowLatencyOffBodyDetectSensorState) return false

if (isDeviceOnBody != other.isDeviceOnBody) return false
if (isAvailable != other.isAvailable) return false
if (accuracy != other.accuracy) return false

return true
}

override fun hashCode(): Int {
var result = isDeviceOnBody.hashCode()
result = 31 * result + isAvailable.hashCode()
result = 31 * result + accuracy.hashCode()
return result
}

override fun toString(): String {
return "LowLatencyOffBodyDetectSensorState(isDeviceOnBody=$isDeviceOnBody, isAvailable=$isAvailable, accuracy=$accuracy)"
}
}

/**
* Creates and [remember]s an instance of [LowLatencyOffBodyDetectSensorState].
* @param sensorDelay The rate at which the raw sensor data should be received.
* Defaults to [SensorDelay.Normal].
* @param onError Callback invoked on every error state.
*/
@Composable
fun rememberLowLatencyOffBodyDetectSensorState(
sensorDelay: SensorDelay = SensorDelay.Normal,
onError: (throwable: Throwable) -> Unit = {}
): LowLatencyOffBodyDetectSensorState {
val sensorState = rememberSensorState(
sensorType = SensorType.LowLatencyOffBodyDetect,
sensorDelay = sensorDelay,
onError = onError
)
val lowLatencyOffBodyDetectSensorState =
remember { mutableStateOf(LowLatencyOffBodyDetectSensorState()) }

LaunchedEffect(
key1 = sensorState,
block = {
val sensorStateValues = sensorState.data
if (sensorStateValues.isNotEmpty()) {
lowLatencyOffBodyDetectSensorState.value = LowLatencyOffBodyDetectSensorState(
isDeviceOnBody = sensorStateValues[0].toInt() == 1,
isAvailable = sensorState.isAvailable,
accuracy = sensorState.accuracy
)
}
}
)

return lowLatencyOffBodyDetectSensorState.value
}

0 comments on commit 8a1c03f

Please sign in to comment.