Add chart to calibration process
Signed-off-by: Severin Kaderli <severin@kaderli.dev>
This commit is contained in:
parent
4f19c761a0
commit
99d93c573d
6 changed files with 52 additions and 21 deletions
|
@ -5,12 +5,12 @@ plugins {
|
||||||
|
|
||||||
android {
|
android {
|
||||||
namespace 'dev.kaderli.magsend'
|
namespace 'dev.kaderli.magsend'
|
||||||
compileSdk 32
|
compileSdk 33
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "dev.kaderli.magsend"
|
applicationId "dev.kaderli.magsend"
|
||||||
minSdk 28
|
minSdk 28
|
||||||
targetSdk 32
|
targetSdk 33
|
||||||
versionCode 1
|
versionCode 1
|
||||||
versionName "1.0"
|
versionName "1.0"
|
||||||
|
|
||||||
|
@ -33,11 +33,11 @@ android {
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|
||||||
implementation 'androidx.core:core-ktx:1.7.0'
|
implementation 'androidx.core:core-ktx:1.7.0'
|
||||||
implementation 'androidx.appcompat:appcompat:1.5.1'
|
implementation 'androidx.appcompat:appcompat:1.5.1'
|
||||||
implementation 'com.google.android.material:material:1.6.1'
|
implementation 'com.google.android.material:material:1.6.1'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||||
|
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||||
|
|
|
@ -2,9 +2,12 @@ package dev.kaderli.magsend
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.hardware.Sensor
|
import android.hardware.Sensor
|
||||||
|
import android.hardware.SensorEvent
|
||||||
import android.hardware.SensorEventListener
|
import android.hardware.SensorEventListener
|
||||||
import android.hardware.SensorManager
|
import android.hardware.SensorManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import kotlin.math.pow
|
||||||
|
import kotlin.math.sqrt
|
||||||
|
|
||||||
abstract class BaseSensorActivity : BaseActivity(), SensorEventListener {
|
abstract class BaseSensorActivity : BaseActivity(), SensorEventListener {
|
||||||
private lateinit var sensorManager: SensorManager;
|
private lateinit var sensorManager: SensorManager;
|
||||||
|
@ -18,4 +21,11 @@ abstract class BaseSensorActivity : BaseActivity(), SensorEventListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,21 @@
|
||||||
package dev.kaderli.magsend
|
package dev.kaderli.magsend
|
||||||
|
|
||||||
import android.hardware.SensorEvent
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.TextView
|
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() {
|
class CalibrationActivity : BaseSensorActivity() {
|
||||||
private lateinit var calibrationValue: TextView
|
private lateinit var calibrationValue: TextView
|
||||||
|
private lateinit var chart: LineChart
|
||||||
|
private var data = ArrayList<Entry>()
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
calibrationValue = findViewById(R.id.calibrationValue)
|
calibrationValue = findViewById(R.id.calibrationValue)
|
||||||
|
chart = findViewById(R.id.chart)
|
||||||
calibrationValue.text = getString(R.string.calibration_value, 0f)
|
calibrationValue.text = getString(R.string.calibration_value, 0f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +23,11 @@ class CalibrationActivity : BaseSensorActivity() {
|
||||||
return R.layout.activity_calibration
|
return R.layout.activity_calibration
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSensorChanged(event: SensorEvent) {
|
override fun sensorValueReceived(magneticFieldStrength: Float) {
|
||||||
calibrationValue.text = getString(R.string.calibration_value, event.values[0])
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,4 +27,14 @@
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/calibrationDescription" />
|
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>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||||
plugins {
|
plugins {
|
||||||
id 'com.android.application' version '7.3.0' apply false
|
id 'com.android.application' version '7.3.0' apply false
|
||||||
id 'com.android.library' version '7.3.0' apply false
|
id 'com.android.library' version '7.3.0' apply false
|
||||||
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
|
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
|
||||||
}
|
}
|
|
@ -1,16 +1,17 @@
|
||||||
pluginManagement {
|
pluginManagement {
|
||||||
repositories {
|
repositories {
|
||||||
gradlePluginPortal()
|
gradlePluginPortal()
|
||||||
google()
|
google()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dependencyResolutionManagement {
|
dependencyResolutionManagement {
|
||||||
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
|
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
|
||||||
repositories {
|
repositories {
|
||||||
google()
|
google()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
maven { url 'https://jitpack.io' }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
rootProject.name = "MagSend"
|
rootProject.name = "MagSend"
|
||||||
include ':app'
|
include ':app'
|
||||||
|
|
Reference in a new issue