loucass003 committed 2026-05-26 13:21:36 +02:00
1 parent 256574dc2c
commit 11783de08b
5 files changed
+19 -13

No files matched your search

@@ -25,7 +25,7 @@ data class HIDDeviceInfo(
val boardType: BoardType,
val mcuType: McuType,
val firmware: String,
val batteryLevel: Float,
val batteryLevel: Float?,
val batteryVoltage: Float,
val rssi: Int,
) : HIDPacket
@@ -42,7 +42,7 @@ data class HIDRotationBattery(
override val hidId: Int,
val rotation: Quaternion,
val acceleration: Vector3,
val batteryLevel: Float,
val batteryLevel: Float?,
val batteryVoltage: Float,
val rssi: Int,
) : HIDPacket
@@ -121,7 +121,7 @@ private fun decodeAccel(data: ByteArray, offset: Int): Vector3 {
)
}
private fun decodeBattery(raw: Int): Float = if (raw == 128) 1f else (raw and 127).toFloat() / 100f
private fun decodeBattery(raw: Int): Float? = if (raw == 128) null else (raw and 127).toFloat() / 100f
private fun decodeBatteryVoltage(raw: Int): Float = (raw.toFloat() + 245f) / 100f
@@ -261,10 +261,16 @@ object DeviceStatsBehaviour : UDPConnectionBehaviour {
override fun observe(receiver: UDPConnection) {
receiver.packetEvents.onPacket<BatteryLevel> { event ->
val device = receiver.getDevice() ?: return@onPacket
val voltage = event.data.voltage
val level = event.data.level
val batteryLevel = if (voltage != null) {
// Gate on voltage validity: too low/high means no battery or measurement error
if (voltage > 2f && voltage < 6f) if (level < 0.01f) null else level else null
} else {
level
}
device.context.dispatch(
DeviceActions.Update {
copy(batteryLevel = event.data.level, batteryVoltage = event.data.voltage)
},
DeviceActions.Update { copy(batteryLevel = batteryLevel, batteryVoltage = voltage) },
)
}
@@ -271,12 +271,11 @@ data class Serial(val serial: String = "") : UDPPacket {
}
}
data class BatteryLevel(val voltage: Float = 0f, val level: Float = 0f) : UDPPacket {
data class BatteryLevel(val voltage: Float?, val level: Float) : UDPPacket {
companion object {
fun read(src: Source): BatteryLevel {
val f = src.readSafeFloat()
return if (src.remaining >= 4) BatteryLevel(f, src.readSafeFloat()) else BatteryLevel(0f, f)
}
fun read(src: Source): BatteryLevel =
if (src.remaining >= 8) BatteryLevel(src.readSafeFloat(), src.readSafeFloat())
else BatteryLevel(null, src.readSafeFloat())
}
}