Satellite tracker and pass predictor for Android, inspired by Gpredict
Kotlin amsatcelestrakgpsham-radiohamlibhamradiokotlinnasaradiosatellitesatellite-datasatellite-positionsatellite-predictionsatellitessatnogssdrspacespacexstarlink
Cleaned up SensorsRepo class, removed deprecated calls
Arty Bishop9147323db2d8
diff --git a/core/data/src/main/java/com/rtbishop/look4sat/core/data/injection/MainContainer.kt b/core/data/src/main/java/com/rtbishop/look4sat/core/data/injection/MainContainer.ktindex 9af7a73d..ceafa38b 100644--- a/core/data/src/main/java/com/rtbishop/look4sat/core/data/injection/MainContainer.kt+++ b/core/data/src/main/java/com/rtbishop/look4sat/core/data/injection/MainContainer.kt@@ -19,10 +19,9 @@ package com.rtbishop.look4sat.core.data.injectionimport android.bluetooth.BluetoothManagerimport android.content.Context-import android.hardware.Sensorimport android.hardware.SensorManager+import android.hardware.display.DisplayManagerimport android.location.LocationManager-import android.view.WindowManagerimport androidx.room.Roomimport com.rtbishop.look4sat.core.data.database.Look4SatDbimport com.rtbishop.look4sat.core.data.framework.BluetoothReporter@@ -120,9 +119,8 @@ class MainContainer(private val context: Context) : IMainContainer {override fun provideSensorsRepo(): ISensorsRepo {val manager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager- val sensor = manager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)- val window = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager- return SensorsRepo(manager,sensor,window)+ val displayManager = context.getSystemService(DisplayManager::class.java)+ return SensorsRepo(manager, displayManager)}private fun provideDatabaseRepo(): IDatabaseRepo {diff --git a/core/data/src/main/java/com/rtbishop/look4sat/core/data/repository/SensorsRepo.kt b/core/data/src/main/java/com/rtbishop/look4sat/core/data/repository/SensorsRepo.ktindex 3553a2ca..05f94ecf 100644--- a/core/data/src/main/java/com/rtbishop/look4sat/core/data/repository/SensorsRepo.kt+++ b/core/data/src/main/java/com/rtbishop/look4sat/core/data/repository/SensorsRepo.kt@@ -22,8 +22,9 @@ import android.hardware.Sensorimport android.hardware.SensorEventimport android.hardware.SensorEventListenerimport android.hardware.SensorManager+import android.hardware.display.DisplayManager+import android.view.Displayimport android.view.Surface-import android.view.WindowManagerimport com.rtbishop.look4sat.core.domain.predict.GeoPosimport com.rtbishop.look4sat.core.domain.predict.RAD2DEGimport com.rtbishop.look4sat.core.domain.repository.ISensorsRepo@@ -31,13 +32,16 @@ import kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.StateFlowimport kotlin.math.round+private const val SMOOTHING_FACTOR = 0.15f+private const val SENSOR_RATE_US = 16_000+class SensorsRepo(private val sensorManager: SensorManager,- private val sensor: Sensor?,- private val windowManager: WindowManager-) : SensorEventListener, ISensorsRepo {+ private val displayManager: DisplayManager?+) : ISensorsRepo, SensorEventListener {- private val _orientation = MutableStateFlow(Pair(0f, 0f))+ private val _sensorData = MutableStateFlow(Pair(0f, 0f))+ private val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)private val rotationMatrix = FloatArray(9)private val tempMatrix = FloatArray(9)private val orientationValues = FloatArray(3)@@ -45,11 +49,7 @@ class SensorsRepo(private var smoothPitch = 0fprivate var hasInitialReading = false- companion object {- private const val SMOOTHING_FACTOR = 0.15f- }-- override val orientation: StateFlow<Pair<Float, Float>> = _orientation+ override val sensorData: StateFlow<Pair<Float, Float>> = _sensorDataoverride fun getMagDeclination(geoPos: GeoPos, time: Long): Float {return GeomagneticField(@@ -62,7 +62,7 @@ class SensorsRepo(override fun enableSensor() {hasInitialReading = false- sensor?.let { sensorManager.registerListener(this, it, 8000) }+ sensor?.let { sensorManager.registerListener(this, it, SENSOR_RATE_US) }}override fun disableSensor() = sensorManager.unregisterListener(this)@@ -70,16 +70,11 @@ class SensorsRepo(override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) = Unitoverride fun onSensorChanged(event: SensorEvent) {- if (event.sensor == sensor) updateOrientation(event.values)+ if (event.sensor.type == Sensor.TYPE_ROTATION_VECTOR) handleSensorEvent(event)}private fun getDisplayRotation(): Int {- return try {- @Suppress("DEPRECATION")- windowManager.defaultDisplay.rotation- } catch (_: Exception) {- Surface.ROTATION_0- }+ return displayManager?.getDisplay(Display.DEFAULT_DISPLAY)?.rotation ?: Surface.ROTATION_0}private fun remapForRotation(rotation: Int) {@@ -101,30 +96,23 @@ class SensorsRepo(if (remapped) System.arraycopy(tempMatrix, 0, rotationMatrix, 0, 9)}- private fun updateOrientation(rotationVector: FloatArray) {- SensorManager.getRotationMatrixFromVector(rotationMatrix, rotationVector)+ private fun handleSensorEvent(event: SensorEvent) {+ SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values)remapForRotation(getDisplayRotation())SensorManager.getOrientation(rotationMatrix, orientationValues)- val azimuth = (orientationValues[0] * RAD2DEG).toFloat()+ val azimuth = normalizeAzimuth((orientationValues[0] * RAD2DEG).toFloat())val pitch = (orientationValues[1] * RAD2DEG).toFloat()- val magneticAzimuth = (azimuth + 360f) % 360f-if (!hasInitialReading) {- smoothAzimuth = magneticAzimuth+ smoothAzimuth = azimuthsmoothPitch = pitchhasInitialReading = true} else {- smoothAzimuth = lowPassAngle(smoothAzimuth, magneticAzimuth)+ smoothAzimuth = lowPassAngle(smoothAzimuth, azimuth)smoothPitch = lowPass(smoothPitch, pitch)}-- _orientation.value = Pair(- round(smoothAzimuth * 10) / 10,- round(smoothPitch * 10) / 10- )+ _sensorData.value = Pair(round(smoothAzimuth * 10) / 10, round(smoothPitch * 10) / 10)}- /** Standard exponential low-pass filter. */private fun lowPass(previous: Float, current: Float): Float {return previous + SMOOTHING_FACTOR * (current - previous)}@@ -135,9 +123,10 @@ class SensorsRepo(*/private fun lowPassAngle(previous: Float, current: Float): Float {var delta = current - previous- // Normalise delta into the range (-180, 180]while (delta > 180f) delta -= 360fwhile (delta <= -180f) delta += 360f- return (previous + SMOOTHING_FACTOR * delta + 360f) % 360f+ return normalizeAzimuth(previous + SMOOTHING_FACTOR * delta)}++ private fun normalizeAzimuth(value: Float): Float = (value + 360f) % 360f}diff --git a/core/domain/src/main/java/com/rtbishop/look4sat/core/domain/repository/ISensorsRepo.kt b/core/domain/src/main/java/com/rtbishop/look4sat/core/domain/repository/ISensorsRepo.ktindex 59cac634..f376c336 100644--- a/core/domain/src/main/java/com/rtbishop/look4sat/core/domain/repository/ISensorsRepo.kt+++ b/core/domain/src/main/java/com/rtbishop/look4sat/core/domain/repository/ISensorsRepo.kt@@ -21,7 +21,7 @@ import com.rtbishop.look4sat.core.domain.predict.GeoPosimport kotlinx.coroutines.flow.StateFlowinterface ISensorsRepo {- val orientation: StateFlow<Pair<Float, Float>>+ val sensorData: StateFlow<Pair<Float, Float>>fun getMagDeclination(geoPos: GeoPos, time: Long = System.currentTimeMillis()): Floatfun enableSensor()fun disableSensor()diff --git a/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarViewModel.kt b/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarViewModel.ktindex 7a5dc86d..d17770e8 100644--- a/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarViewModel.kt+++ b/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarViewModel.kt@@ -78,7 +78,7 @@ class RadarViewModel(private val _uiState = MutableStateFlow(RadarState(isUtc = settingsRepo.otherSettings.value.stateOfUtc,- orientationValues = sensorsRepo.orientation.value,+ orientationValues = sensorsRepo.sensorData.value,shouldShowSweep = settingsRepo.otherSettings.value.stateOfSweep,shouldUseCompass = settingsRepo.otherSettings.value.stateOfSensors,sstv = SstvSubState(selectedMode = settingsRepo.otherSettings.value.sstvMode)@@ -97,7 +97,7 @@ class RadarViewModel(if (!settingsRepo.otherSettings.value.stateOfSensors) returnviewModelScope.launch {sensorsRepo.enableSensor()- sensorsRepo.orientation.collect { data ->+ sensorsRepo.sensorData.collect { data ->val orientationValues = (data.first + magDeclination) to data.second_uiState.update { it.copy(orientationValues = orientationValues) }}