added methods to parse read string from device into java objects + added progress bar
This commit is contained in:
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@@ -3,6 +3,7 @@
|
||||
<component name="DesignSurface">
|
||||
<option name="filePathToZoomLevelMap">
|
||||
<map>
|
||||
<entry key="app/src/main/res/drawable/progressbar_drawable.xml" value="0.1885" />
|
||||
<entry key="app/src/main/res/font/inter_bold.xml" value="0.33487179487179486" />
|
||||
<entry key="app/src/main/res/layout/activity_main.xml" value="0.25" />
|
||||
<entry key="app/src/main/res/layout/activity_splash.xml" value="0.24375" />
|
||||
|
||||
@@ -44,6 +44,9 @@ dependencies {
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||
|
||||
// androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1'
|
||||
|
||||
|
||||
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"
|
||||
implementation 'com.opencsv:opencsv:4.6'
|
||||
implementation 'com.github.mik3y:usb-serial-for-android:3.4.6'
|
||||
|
||||
@@ -6,4 +6,6 @@ object Constants {
|
||||
const val WRITE_TIMEOUT_MILLIS = 60000 // 1 min
|
||||
const val READ_TIMEOUT_MILLIS = 60000 // 1 min
|
||||
|
||||
// const val TEST_RIGHT_TOTAL_PIXEL = 3694
|
||||
const val TEST_RIGHT_TOTAL_PIXEL = 12
|
||||
}
|
||||
@@ -16,4 +16,5 @@ enum class TestRightCommands(val command: String) {
|
||||
autoset("autoset\r"),
|
||||
print("print 239 2719\r"),
|
||||
print_all("print\r"),
|
||||
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.example.refactoredapp.data.model
|
||||
|
||||
data class TestRightDeviceConstants(
|
||||
val a: String,
|
||||
val b: String,
|
||||
var a: String,
|
||||
var b: String,
|
||||
val c: String,
|
||||
val d: String
|
||||
)
|
||||
|
||||
@@ -66,13 +66,9 @@ class TestRightActivity : AppCompatActivity() {
|
||||
setContentView(binding.root)
|
||||
viewModel = ViewModelProvider(this, MyViewModelFactory(this.applicationContext))[TestRightViewModel::class.java]
|
||||
|
||||
connectUsb(false);
|
||||
|
||||
// if(connectUsb(false)) {
|
||||
// moveToNext()
|
||||
// } else {
|
||||
// onBackPressed()
|
||||
// }
|
||||
|
||||
connectUsb(false);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.example.refactoredapp.presentation.testRight
|
||||
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
@@ -24,7 +23,8 @@ class TestRightExpReference : Fragment() {
|
||||
|
||||
private val TAG = "TestRightExpReference"
|
||||
|
||||
var fullString: String = ""
|
||||
val fullReadOutput = StringBuilder()
|
||||
private var readingDeviceConstantInProgress = false
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
@@ -40,9 +40,13 @@ class TestRightExpReference : Fragment() {
|
||||
setupListeners()
|
||||
|
||||
if (DataHolder.isReferenceTaken)
|
||||
parentFragmentManager.beginTransaction().replace(R.id.fl_main, TestRightExpSample())
|
||||
.commit()
|
||||
moveToSamplePage()
|
||||
|
||||
if (viewModel.deviceConstant == null) {
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
readingDeviceConstantInProgress = true
|
||||
writeAndReadFromDevice(TestRightCommands.read)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupListeners() {
|
||||
@@ -55,7 +59,8 @@ class TestRightExpReference : Fragment() {
|
||||
getString(R.string.no),
|
||||
object : MyDialogListener {
|
||||
override fun onClickBtnOne() {
|
||||
startTakingReference()
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
writeAndReadFromDevice(TestRightCommands.print_all)
|
||||
}
|
||||
|
||||
override fun onClickBtnTwo() {}
|
||||
@@ -75,11 +80,17 @@ class TestRightExpReference : Fragment() {
|
||||
// Log.d(TAG, "onUsbRead(data) call back called")
|
||||
data?.let {
|
||||
val stringData = String(it)
|
||||
// Log.d(TAG, stringData)
|
||||
fullString += stringData
|
||||
fullReadOutput.append(stringData)
|
||||
|
||||
if(stringData.contains("OK")){
|
||||
viewModel.mapValues(fullString)
|
||||
if(stringData.contains("OK", true)){
|
||||
if (readingDeviceConstantInProgress) {
|
||||
readingDeviceConstantInProgress = false
|
||||
viewModel.mapDeviceConstants(fullReadOutput.toString())
|
||||
} else {
|
||||
viewModel.mapIntensityValues(fullReadOutput.toString(), true)
|
||||
moveToSamplePage()
|
||||
}
|
||||
binding.progressBar.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
// val stringData = String(data!!)
|
||||
@@ -99,16 +110,27 @@ class TestRightExpReference : Fragment() {
|
||||
// Log.d(TAG, "clicked on text")
|
||||
// val mService = (activity as TestRightActivity).mService
|
||||
// mService.read()
|
||||
Log.d(TAG, "FULL STRING IS -> $fullString")
|
||||
|
||||
Log.d(TAG, "FULL STRING IS -> $fullReadOutput")
|
||||
}
|
||||
}
|
||||
|
||||
private fun startTakingReference() {
|
||||
|
||||
private fun writeAndReadFromDevice(command: TestRightCommands) {
|
||||
fullReadOutput.clear()
|
||||
val mService = (activity as TestRightActivity).mService
|
||||
mService.eventDrivenWrite(TestRightCommands.print_all)
|
||||
mService.eventDrivenWrite(command)
|
||||
}
|
||||
|
||||
private fun moveToSamplePage() {
|
||||
parentFragmentManager.beginTransaction().replace(R.id.fl_main, TestRightExpSample())
|
||||
.commit()
|
||||
}
|
||||
|
||||
// private fun startReadingReference() {
|
||||
// val mService = (activity as TestRightActivity).mService
|
||||
// mService.eventDrivenWrite(TestRightCommands.print_all)
|
||||
// }
|
||||
|
||||
// private fun startTakingReference() {
|
||||
// val mService = (activity as TestRightActivity).mService
|
||||
// mService.write(TestRightCommands.print)
|
||||
|
||||
@@ -1,16 +1,107 @@
|
||||
package com.example.refactoredapp.presentation.testRight
|
||||
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.refactoredapp.data.Constants
|
||||
import com.example.refactoredapp.data.model.TestRightDeviceConstants
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlin.math.pow
|
||||
|
||||
class TestRightViewModel : ViewModel() {
|
||||
|
||||
var isReferenceDone = false
|
||||
var isServiceConnected = false
|
||||
private val TAG = "TestRightViewModel"
|
||||
|
||||
val isUsbConnected = MutableLiveData(false)
|
||||
|
||||
fun mapValues(fullString: String) {
|
||||
TODO("Not yet implemented")
|
||||
var deviceConstant: TestRightDeviceConstants? = null
|
||||
|
||||
/* Contains wavelength -> pixel no.*/
|
||||
val wavelengthToPixelArray = ArrayList<ArrayList<Double>>()
|
||||
|
||||
/* (For reference/baseline) Contains pixel no. -> Intensity of light falling on that pixel */
|
||||
val intensityReferenceArray = ArrayList<ArrayList<Double>>()
|
||||
|
||||
/* (For exp sample) Contains pixel no. -> Intensity of light falling on that pixel */
|
||||
val intensitySampleArray = ArrayList<ArrayList<Double>>()
|
||||
|
||||
fun mapDeviceConstants(string: String) {
|
||||
// viewModelScope.launch {
|
||||
if (string.isNotEmpty()) {
|
||||
val listOfStrings = string.split(",")
|
||||
if (listOfStrings.size >= 4) {
|
||||
deviceConstant = TestRightDeviceConstants(
|
||||
listOfStrings[0].trim(),
|
||||
listOfStrings[1].trim(),
|
||||
listOfStrings[2].trim(),
|
||||
listOfStrings[3].trim()
|
||||
)
|
||||
} else {
|
||||
// Todo: Throws error
|
||||
}
|
||||
} else {
|
||||
// Todo: Throws error (showing error if empty by using a mutable error string)
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
fun mapPixelNumberToWavelength() {
|
||||
// viewModelScope.launch {
|
||||
if (deviceConstant != null) {
|
||||
for (x in 1..Constants.TEST_RIGHT_TOTAL_PIXEL) {
|
||||
val wavelength: Double =
|
||||
(deviceConstant!!.a.toDouble() * (x.toDouble().pow((3).toDouble()))) +
|
||||
(deviceConstant!!.b.toDouble() * (x.toDouble().pow((2).toDouble()))) +
|
||||
(deviceConstant!!.c.toDouble() * x) +
|
||||
(deviceConstant!!.d.toDouble())
|
||||
|
||||
val temp = ArrayList<Double>(2)
|
||||
temp.add(wavelength)
|
||||
temp.add(x.toDouble())
|
||||
wavelengthToPixelArray.add(temp)
|
||||
}
|
||||
} else {
|
||||
// Todo: Throws error
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
fun mapIntensityValues(fullString: String, isReference: Boolean) {
|
||||
// viewModelScope.launch {
|
||||
val listOfString = fullString.split("\n")
|
||||
for (line in listOfString) {
|
||||
if ("Buf" in line) {
|
||||
/* Removing trailing spaces and "Buf" from the string then taking lhs & rhs of ':' */
|
||||
val numbers = line.trim()
|
||||
.substring(3).split(":")
|
||||
if (numbers.size >= 2) {
|
||||
val temp = ArrayList<Double>(2)
|
||||
temp.add(numbers[0].toDouble())
|
||||
temp.add(numbers[1].toDouble())
|
||||
if (isReference) intensityReferenceArray.add(temp)
|
||||
else intensitySampleArray.add(temp)
|
||||
} else {
|
||||
// Todo: Error handling
|
||||
}
|
||||
}
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
// val dispatcher = TestCoroutineDispatcher()
|
||||
//
|
||||
// @Before
|
||||
// fun setup() {
|
||||
// Dispatchers.setMain(dispatcher)
|
||||
// }
|
||||
//
|
||||
// @After
|
||||
// fun tearDown() {
|
||||
// Dispatchers.resetMain()
|
||||
// }
|
||||
}
|
||||
27
app/src/main/res/drawable/progressbar_drawable.xml
Normal file
27
app/src/main/res/drawable/progressbar_drawable.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:duration="4000"
|
||||
android:fromDegrees="0"
|
||||
android:pivotX="50%"
|
||||
android:pivotY="50%"
|
||||
android:toDegrees="360">
|
||||
|
||||
<shape
|
||||
android:innerRadius="20dp"
|
||||
android:shape="ring"
|
||||
android:thickness="4dp"
|
||||
android:useLevel="false">
|
||||
<size
|
||||
android:width="54dp"
|
||||
android:height="54dp" />
|
||||
|
||||
<gradient
|
||||
android:centerColor="#0288D1"
|
||||
android:centerY="0.5"
|
||||
android:endColor="#01579B"
|
||||
android:startColor="#00ec7e2a"
|
||||
android:type="sweep"
|
||||
android:useLevel="false" />
|
||||
</shape>
|
||||
|
||||
</rotate>
|
||||
@@ -12,21 +12,46 @@
|
||||
android:id="@+id/tv_title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/hello_blank_fragment"
|
||||
android:text="@string/Instructions"
|
||||
android:layout_margin="16dp"
|
||||
style="@style/title1"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_set_reference"
|
||||
<TextView
|
||||
android:id="@+id/tv_subtitle1"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/set_reference"
|
||||
android:text="@string/step1"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
style="@style/title2"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/tv_title" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_subtitle2"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/step2"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
style="@style/title2"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/tv_subtitle1" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_set_reference"
|
||||
android:layout_width="172dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="16dp"
|
||||
android:clickable="false"
|
||||
app:layout_constraintTop_toBottomOf="@id/tv_title"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
android:text="@string/set_reference"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/tv_subtitle2" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_read"
|
||||
@@ -38,6 +63,19 @@
|
||||
app:layout_constraintTop_toBottomOf="@id/btn_set_reference"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progressBar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerInParent="true"
|
||||
android:indeterminate="true"
|
||||
android:indeterminateDrawable="@drawable/progressbar_drawable"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent"/>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</layout>
|
||||
@@ -18,4 +18,7 @@
|
||||
<string name="connect">Connect</string>
|
||||
<string name="connected">Connected</string>
|
||||
<string name="read">Read</string>
|
||||
<string name="Instructions">Instructions</string>
|
||||
<string name="step1"><b><u>Step 1</u> : </b> Place the cuvette containing reference/baseline sample for referencing.</string>
|
||||
<string name="step2"><b><u>Step 2</u> : </b> Press the button below to start the device reading values.</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user