Add chart to calibration process

Signed-off-by: Severin Kaderli <severin@kaderli.dev>
This commit is contained in:
Severin Kaderli 2022-11-27 17:24:44 +01:00
parent 4f19c761a0
commit 99d93c573d
Signed by: severinkaderli
GPG key ID: F419F8835B72F0C4
6 changed files with 52 additions and 21 deletions

View file

@ -5,12 +5,12 @@ plugins {
android {
namespace 'dev.kaderli.magsend'
compileSdk 32
compileSdk 33
defaultConfig {
applicationId "dev.kaderli.magsend"
minSdk 28
targetSdk 32
targetSdk 33
versionCode 1
versionName "1.0"
@ -33,11 +33,11 @@ android {
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

View file

@ -2,9 +2,12 @@ package dev.kaderli.magsend
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import kotlin.math.pow
import kotlin.math.sqrt
abstract class BaseSensorActivity : BaseActivity(), SensorEventListener {
private lateinit var sensorManager: SensorManager;
@ -18,4 +21,11 @@ abstract class BaseSensorActivity : BaseActivity(), SensorEventListener {
}
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
override fun onSensorChanged(event: SensorEvent) {
val magneticFieldStrength = sqrt(event.values[0].pow(2) + event.values[1].pow(2) + event.values[2].pow(2))
sensorValueReceived(magneticFieldStrength)
}
abstract fun sensorValueReceived(magneticFieldStrength: Float)
}

View file

@ -1,15 +1,21 @@
package dev.kaderli.magsend
import android.hardware.SensorEvent
import android.os.Bundle
import android.widget.TextView
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
class CalibrationActivity : BaseSensorActivity() {
private lateinit var calibrationValue: TextView
private lateinit var chart: LineChart
private var data = ArrayList<Entry>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
calibrationValue = findViewById(R.id.calibrationValue)
chart = findViewById(R.id.chart)
calibrationValue.text = getString(R.string.calibration_value, 0f)
}
@ -17,7 +23,11 @@ class CalibrationActivity : BaseSensorActivity() {
return R.layout.activity_calibration
}
override fun onSensorChanged(event: SensorEvent) {
calibrationValue.text = getString(R.string.calibration_value, event.values[0])
override fun sensorValueReceived(magneticFieldStrength: Float) {
calibrationValue.text = getString(R.string.calibration_value, magneticFieldStrength)
data.add(Entry(data.size.toFloat(), magneticFieldStrength))
val dataSet = LineDataSet(data, "Test")
chart.data = LineData(dataSet)
chart.invalidate()
}
}

View file

@ -27,4 +27,14 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/calibrationDescription" />
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="@dimen/component_spacing"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/calibrationValue" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -10,6 +10,7 @@ dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
rootProject.name = "MagSend"