|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Meshtastic LLC |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.meshtastic.feature.node.compass |
| 19 | + |
| 20 | +import android.content.Context |
| 21 | +import android.hardware.Sensor |
| 22 | +import android.hardware.SensorEvent |
| 23 | +import android.hardware.SensorEventListener |
| 24 | +import android.hardware.SensorManager |
| 25 | +import dagger.hilt.android.qualifiers.ApplicationContext |
| 26 | +import kotlinx.coroutines.channels.awaitClose |
| 27 | +import kotlinx.coroutines.flow.Flow |
| 28 | +import kotlinx.coroutines.flow.callbackFlow |
| 29 | +import javax.inject.Inject |
| 30 | + |
| 31 | +private const val ROTATION_MATRIX_SIZE = 9 |
| 32 | +private const val ORIENTATION_SIZE = 3 |
| 33 | +private const val FULL_CIRCLE_DEGREES = 360f |
| 34 | + |
| 35 | +data class HeadingState( |
| 36 | + val heading: Float? = null, // 0..360 degrees |
| 37 | + val hasSensor: Boolean = true, |
| 38 | + val accuracy: Int = SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM, |
| 39 | +) |
| 40 | + |
| 41 | +class CompassHeadingProvider @Inject constructor(@ApplicationContext private val context: Context) { |
| 42 | + |
| 43 | + /** |
| 44 | + * Emits compass heading in degrees (magnetic). Callers can correct for true north using the latest location data |
| 45 | + * when available. |
| 46 | + */ |
| 47 | + fun headingUpdates(): Flow<HeadingState> = callbackFlow { |
| 48 | + val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as? SensorManager |
| 49 | + if (sensorManager == null) { |
| 50 | + trySend(HeadingState(hasSensor = false)) |
| 51 | + close() |
| 52 | + return@callbackFlow |
| 53 | + } |
| 54 | + |
| 55 | + val rotationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) |
| 56 | + val accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) |
| 57 | + val magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) |
| 58 | + |
| 59 | + if (rotationSensor == null && (accelerometer == null || magnetometer == null)) { |
| 60 | + trySend(HeadingState(hasSensor = false)) |
| 61 | + awaitClose {} |
| 62 | + return@callbackFlow |
| 63 | + } |
| 64 | + |
| 65 | + val rotationMatrix = FloatArray(ROTATION_MATRIX_SIZE) |
| 66 | + val orientation = FloatArray(ORIENTATION_SIZE) |
| 67 | + val accelValues = FloatArray(ORIENTATION_SIZE) |
| 68 | + val magnetValues = FloatArray(ORIENTATION_SIZE) |
| 69 | + var hasAccel = false |
| 70 | + var hasMagnet = false |
| 71 | + |
| 72 | + val listener = |
| 73 | + object : SensorEventListener { |
| 74 | + override fun onSensorChanged(event: SensorEvent) { |
| 75 | + when (event.sensor.type) { |
| 76 | + Sensor.TYPE_ROTATION_VECTOR -> { |
| 77 | + SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values) |
| 78 | + } |
| 79 | + Sensor.TYPE_ACCELEROMETER -> { |
| 80 | + System.arraycopy(event.values, 0, accelValues, 0, accelValues.size) |
| 81 | + hasAccel = true |
| 82 | + } |
| 83 | + Sensor.TYPE_MAGNETIC_FIELD -> { |
| 84 | + System.arraycopy(event.values, 0, magnetValues, 0, magnetValues.size) |
| 85 | + hasMagnet = true |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + val ready = rotationSensor != null || (hasAccel && hasMagnet) |
| 90 | + if (ready) { |
| 91 | + if (rotationSensor == null) { |
| 92 | + SensorManager.getRotationMatrix(rotationMatrix, null, accelValues, magnetValues) |
| 93 | + } |
| 94 | + |
| 95 | + SensorManager.getOrientation(rotationMatrix, orientation) |
| 96 | + var azimuth = Math.toDegrees(orientation[0].toDouble()).toFloat() |
| 97 | + val heading = (azimuth + FULL_CIRCLE_DEGREES) % FULL_CIRCLE_DEGREES |
| 98 | + |
| 99 | + trySend(HeadingState(heading = heading, hasSensor = true, accuracy = event.accuracy)) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) { |
| 104 | + // No-op |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + rotationSensor?.let { sensorManager.registerListener(listener, it, SensorManager.SENSOR_DELAY_UI) } |
| 109 | + if (rotationSensor == null) { |
| 110 | + accelerometer?.let { sensorManager.registerListener(listener, it, SensorManager.SENSOR_DELAY_UI) } |
| 111 | + magnetometer?.let { sensorManager.registerListener(listener, it, SensorManager.SENSOR_DELAY_UI) } |
| 112 | + } |
| 113 | + |
| 114 | + awaitClose { sensorManager.unregisterListener(listener) } |
| 115 | + } |
| 116 | +} |
0 commit comments