Kalibrierung und Einbaulage gehören in den Sensor, nicht in die Apps
Die Kalibrier-Nullpunkte lagen schon immer im ESP und wurden dort auch
abgezogen – nur verriet die Firmware sie niemandem. Die Einbaulage lag
dagegen in jeder App einzeln, und damit stand sie bei drei Endgeräten
dreimal da, im Zweifel dreimal verschieden. Sie beschreibt aber den
Einbau und nicht das Telefon.
Zwei neue Charakteristiken:
...3426 Nullpunkte, zwei Floats, nur lesen
...3428 Einbaulage, acht Byte, lesen und schreiben
0 Version, 1 = gültig gesetzt, 0 = nie geschrieben
1 Längsachse: 0 = Pitch des Sensors, 1 = Roll
2 längs umgekehrt (0/1)
3 quer umgekehrt (0/1)
4..7 Verdrehung um die Hochachse, float32, Grad
Das Versionsbyte löst die Umstellung: Steht dort 0, hat nie jemand
geschrieben – dann gilt weiter, was die App örtlich gespeichert hat, und
beim nächsten Bestimmen wandert sie hinauf. Mit älterer Firmware ohne die
Charakteristik passiert schlicht nichts.
Angewandt wird die Lage weiterhin in den Apps. Der Sensor verwahrt sie
nur: Meldete er die Winkel bereits umgerechnet, rechnete jede ältere App
die Korrektur ein zweites Mal ein.
Beim Verbinden liest jede App die Lage und übernimmt sie; nach dem
Einbaulage-Assistenten schreibt sie sie hinauf. Die Uhr braucht das
iPhone dafür nicht mehr – sie holt sie sich selbst. Die Android-App
bekommt dabei auch die Verdrehung, die ihrer Fassung bisher ganz fehlte:
Modell, Erkennung und Persistenz nachgezogen.
Geprüft: Firmware übersetzt, im erzeugten Code stehen beide
Charakteristiken mit den richtigen Rechten und der Schreib-Handler;
iPhone- und Watch-Ziel bauen; sieben neue Prüfungen in run-tests.sh
nageln das Byte-Format fest, samt Rundlauf und der Bedeutung von
Version 0. Nicht geprüft ist die Android-Fassung: Auf dieser Maschine
liegt nur Java 8, Gradle verlangt mindestens 11.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -372,6 +372,11 @@ class BluetoothManager(
|
||||
onUpdate = { record(it) },
|
||||
onStateChange = { handleLinkState(device, it) },
|
||||
onLevelState = { state -> publish { levelStates[device.id] = state } },
|
||||
onDeviceOrientation = { orientation ->
|
||||
// Im Gerät steht, wie der Sensor eingebaut ist – für alle
|
||||
// Clients dasselbe. Also übernehmen statt überschreiben.
|
||||
adoptOrientation(orientation, device.id)
|
||||
},
|
||||
)
|
||||
session.orientation = device.sensorOrientation
|
||||
levelSessions[device.address] = session
|
||||
@@ -642,8 +647,32 @@ class BluetoothManager(
|
||||
|
||||
// MARK: - Neigungsmesser
|
||||
|
||||
/**
|
||||
* Nach einer Änderung der Einbaulage aufrufen.
|
||||
*
|
||||
* Die Lage wandert zusätzlich ins Gerät. Dort gehört sie hin: Sie
|
||||
* beschreibt den Einbau, und iPhone wie Uhr finden sie dann vor, ohne dass
|
||||
* jemand sie ein zweites Mal bestimmen muss.
|
||||
*/
|
||||
fun updateSensorOrientation(device: ConfiguredDevice) {
|
||||
handler.post { levelSessions[device.address]?.orientation = device.sensorOrientation }
|
||||
handler.post {
|
||||
val session = levelSessions[device.address] ?: return@post
|
||||
session.orientation = device.sensorOrientation
|
||||
session.storeOrientation(device.sensorOrientation)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Übernimmt die Einbaulage, die das Gerät meldet.
|
||||
*
|
||||
* Steht dort noch nichts, meldet das Gerät nichts – dann bleibt es bei der
|
||||
* örtlich gespeicherten Fassung, und die wird beim nächsten Bestimmen
|
||||
* hinaufgeschrieben.
|
||||
*/
|
||||
private fun adoptOrientation(orientation: SensorOrientation, deviceID: UUID) {
|
||||
val device = store.devices.firstOrNull { it.id == deviceID } ?: return
|
||||
if (device.sensorOrientation == orientation) return
|
||||
store.update(device.copy(sensorOrientation = orientation))
|
||||
}
|
||||
|
||||
fun calibrateLevel(deviceID: UUID) {
|
||||
|
||||
@@ -30,6 +30,12 @@ class LevelSession(
|
||||
private val onUpdate: (DeviceSnapshot) -> Unit,
|
||||
private val onStateChange: (DeviceLinkState) -> Unit,
|
||||
private val onLevelState: (LevelState) -> Unit,
|
||||
/**
|
||||
* Meldet die Einbaulage, die im Gerät steht. Sie gilt vor der örtlich
|
||||
* gespeicherten: dort steht, wie der Sensor eingebaut ist, und das ist für
|
||||
* alle Clients dasselbe.
|
||||
*/
|
||||
private val onDeviceOrientation: (SensorOrientation) -> Unit = {},
|
||||
) {
|
||||
private companion object {
|
||||
val SERVICE: UUID = UUID.fromString(VanAlignProtocol.SERVICE_UUID)
|
||||
@@ -37,6 +43,7 @@ class LevelSession(
|
||||
val ROLL: UUID = UUID.fromString(VanAlignProtocol.ROLL_UUID)
|
||||
val OFFSETS: UUID = UUID.fromString(VanAlignProtocol.OFFSETS_UUID)
|
||||
val CALIBRATE: UUID = UUID.fromString(VanAlignProtocol.CALIBRATE_UUID)
|
||||
val ORIENTATION: UUID = UUID.fromString(VanAlignProtocol.ORIENTATION_UUID)
|
||||
val CLIENT_CONFIG: UUID = UUID.fromString("00002902-0000-1000-8000-00805F9B34FB")
|
||||
const val POLL_INTERVAL_MS = 1_000L
|
||||
}
|
||||
@@ -53,6 +60,7 @@ class LevelSession(
|
||||
private var rollCharacteristic: BluetoothGattCharacteristic? = null
|
||||
private var offsetsCharacteristic: BluetoothGattCharacteristic? = null
|
||||
private var calibrateCharacteristic: BluetoothGattCharacteristic? = null
|
||||
private var orientationCharacteristic: BluetoothGattCharacteristic? = null
|
||||
|
||||
/** Ob die Firmware die Werte von sich aus meldet. */
|
||||
private var needsPolling = true
|
||||
@@ -84,6 +92,7 @@ class LevelSession(
|
||||
rollCharacteristic = null
|
||||
offsetsCharacteristic = null
|
||||
calibrateCharacteristic = null
|
||||
orientationCharacteristic = null
|
||||
}
|
||||
|
||||
/** Setzt die aktuelle Lage als neue Null. */
|
||||
@@ -107,6 +116,22 @@ class LevelSession(
|
||||
handler.postDelayed({ readOffsets() }, 400)
|
||||
}
|
||||
|
||||
/**
|
||||
* Schreibt die Einbaulage ins Gerät, damit alle Clients dieselbe sehen.
|
||||
*
|
||||
* Ältere Firmware hat die Charakteristik nicht; dann bleibt es bei der
|
||||
* örtlich gespeicherten Fassung, und es passiert schlicht nichts.
|
||||
*/
|
||||
fun storeOrientation(value: SensorOrientation) {
|
||||
val characteristic = orientationCharacteristic ?: return
|
||||
val g = gatt ?: return
|
||||
characteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
|
||||
characteristic.value = VanAlignProtocol.encoded(value)
|
||||
g.writeCharacteristic(characteristic)
|
||||
// Zurücklesen, damit gilt, was wirklich im Gerät steht.
|
||||
handler.postDelayed({ gatt?.readCharacteristic(characteristic) }, 300)
|
||||
}
|
||||
|
||||
private fun readOffsets() {
|
||||
val characteristic = offsetsCharacteristic ?: return
|
||||
gatt?.readCharacteristic(characteristic)
|
||||
@@ -169,6 +194,7 @@ class LevelSession(
|
||||
rollCharacteristic = service.getCharacteristic(ROLL)
|
||||
offsetsCharacteristic = service.getCharacteristic(OFFSETS)
|
||||
calibrateCharacteristic = service.getCharacteristic(CALIBRATE)
|
||||
orientationCharacteristic = service.getCharacteristic(ORIENTATION)
|
||||
|
||||
val canNotify = pitchCharacteristic?.let {
|
||||
it.properties and BluetoothGattCharacteristic.PROPERTY_NOTIFY != 0
|
||||
@@ -267,6 +293,26 @@ class LevelSession(
|
||||
state = state.copy(pitchOffset = pitch, rollOffset = roll)
|
||||
}
|
||||
publish()
|
||||
// Android lässt nur eine Abfrage gleichzeitig zu, deshalb der
|
||||
// Reihe nach: erst die Offsets, dann die Einbaulage, dann der
|
||||
// Takt. Fehlt die Charakteristik – ältere Firmware –, geht es
|
||||
// sofort weiter.
|
||||
val characteristic = orientationCharacteristic
|
||||
if (characteristic != null) {
|
||||
gatt?.readCharacteristic(characteristic)
|
||||
} else {
|
||||
startPollingIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
ORIENTATION -> {
|
||||
// Was im Gerät steht, gilt. Steht dort nichts (Version 0),
|
||||
// bleibt es bei der örtlichen Fassung, und der Aufrufer
|
||||
// schreibt sie hinauf.
|
||||
VanAlignProtocol.orientation(value)?.let { stored ->
|
||||
if (stored != orientation) orientation = stored
|
||||
onDeviceOrientation(stored)
|
||||
}
|
||||
startPollingIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,6 +206,7 @@ class DeviceStore(context: Context) {
|
||||
put("orientationSource", device.sensorOrientation.longitudinalSource.name)
|
||||
put("invertLongitudinal", device.sensorOrientation.invertLongitudinal)
|
||||
put("invertLateral", device.sensorOrientation.invertLateral)
|
||||
put("twist", device.sensorOrientation.twist)
|
||||
}
|
||||
|
||||
private fun deviceFrom(json: JSONObject): ConfiguredDevice? {
|
||||
@@ -227,6 +228,9 @@ class DeviceStore(context: Context) {
|
||||
}.getOrDefault(SensorOrientation.Source.PITCH),
|
||||
invertLongitudinal = json.optBoolean("invertLongitudinal", false),
|
||||
invertLateral = json.optBoolean("invertLateral", false),
|
||||
// Fehlt der Wert, ist er null – so überleben Einrichtungen aus
|
||||
// der Zeit vor der Verdrehung.
|
||||
twist = json.optDouble("twist", 0.0),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user