From a43365268f5282fa2ad310dab5cff73b093d2751 Mon Sep 17 00:00:00 2001 From: vsuryakumar Date: Fri, 27 Jan 2023 13:21:04 +0530 Subject: [PATCH] Fixes in usbservice (making usbserviceconnector global) + relevant changes in other fragments --- .../data/constant/TestRightCommands.kt | 2 +- .../refactoredapp/data/model/PatientData.kt | 2 +- .../testRight/TestRightActivity.kt | 13 +- .../testRight/TestRightExpReference.kt | 153 ++++++++++++--- .../testRight/TestRightExpSample.kt | 48 +++-- .../testRight/TestRightViewModel.kt | 185 ++++++++++++++---- .../presentation/testRight/UsbService.kt | 60 ++++-- .../presentation/utils/UIUtils.kt | 9 +- 8 files changed, 376 insertions(+), 96 deletions(-) diff --git a/app/src/main/java/com/example/refactoredapp/data/constant/TestRightCommands.kt b/app/src/main/java/com/example/refactoredapp/data/constant/TestRightCommands.kt index aad89a8..d5b6ac9 100644 --- a/app/src/main/java/com/example/refactoredapp/data/constant/TestRightCommands.kt +++ b/app/src/main/java/com/example/refactoredapp/data/constant/TestRightCommands.kt @@ -6,6 +6,6 @@ enum class TestRightCommands(val command: String) { read("read\r"), autoset("autoset\r"), run("run\r"), - printInRange("print 239 2719\r"), + printInRange("print 239 339\r"), printAll("print\r"), } \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/data/model/PatientData.kt b/app/src/main/java/com/example/refactoredapp/data/model/PatientData.kt index 6f37588..debaf1b 100644 --- a/app/src/main/java/com/example/refactoredapp/data/model/PatientData.kt +++ b/app/src/main/java/com/example/refactoredapp/data/model/PatientData.kt @@ -4,5 +4,5 @@ data class PatientData( val name: String, val age: Int, val gender: String, - val results: String? + var results: String? ) diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightActivity.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightActivity.kt index fb7bbb3..6d720df 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightActivity.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightActivity.kt @@ -66,10 +66,16 @@ class TestRightActivity : AppCompatActivity() { setContentView(binding.root) viewModel = ViewModelProvider(this, MyViewModelFactory(this.applicationContext))[TestRightViewModel::class.java] + // Connected or not connected icon with livetime status using live data + connectUsb(false) +// testing() - connectUsb(false); + } + private fun testing() { + supportFragmentManager.beginTransaction() + .replace(binding.flMain.id, TestRightExpSample()).commit() } private fun connectUsb(permissionGranted: Boolean) { @@ -91,8 +97,8 @@ class TestRightActivity : AppCompatActivity() { } /* - * Request user permission. The response will be received in the BroadcastReceiver - */ + * Request user permission. The response will be received in the BroadcastReceiver + */ private fun requestUserPermission(manager: UsbManager, device: UsbDevice) { Log.d(TAG, String.format("requestUserPermissions", device.vendorId, device.productId)) @@ -115,7 +121,6 @@ class TestRightActivity : AppCompatActivity() { } private fun moveToNext(){ -// setupService() if (supportFragmentManager.isDestroyed) return diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt index f10264c..1bf0f84 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt @@ -1,6 +1,8 @@ package com.example.refactoredapp.presentation.testRight import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.util.Log import android.view.LayoutInflater import android.view.View @@ -23,7 +25,7 @@ class TestRightExpReference : Fragment() { private val TAG = "TestRightExpReference" - val fullReadOutput = StringBuilder() + private val fullReadOutput = StringBuilder() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -40,12 +42,40 @@ class TestRightExpReference : Fragment() { if (viewModel.isReferenceDone && viewModel.sampleReadCounter <= Constants.MAXIMUM_TEST_ALLOWED_BEFORE_REFERENCE) moveToSamplePage() - +// if (viewModel.deviceConstant == null) { sendCmdToFetchDeviceConstant() } } + private fun testing(commands: TestRightCommands) { + viewModel.progressBar.postValue(true) + val fullReadOutput = StringBuilder() + (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.printInRange, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + data?.let { + val stringData = String(it) + fullReadOutput.append(stringData) + Log.d(TAG, fullReadOutput.toString()) + + if (stringData.contains("OK", true)) { + Log.d(TAG, "onUsbRead() called in sendCmdToFetchLightIntensities()") + viewModel.mapIntensityValues(fullReadOutput.toString(), true) + viewModel.isReferenceDone = true + viewModel.progressBar.postValue(false) + moveToSamplePage() + } + } + } + + override fun onUsbError(e: Exception?) { + Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") + viewModel.progressBar.postValue(false) + } + }) + } + private fun setupListeners() { binding.btnSetReference.setOnClickListener { UIUtils().createAlertDialog( @@ -59,16 +89,32 @@ class TestRightExpReference : Fragment() { startTakingReference() } - override fun onClickBtnTwo() {} + override fun onClickBtnTwo() { + // Todo: Only for testing +// testing(TestRightCommands.led2Set50) +// testing(TestRightCommands.autoset) +// testing(TestRightCommands.run) +// requireActivity().runOnUiThread { + testing(TestRightCommands.run) +// } + } }) } - binding.btnRead.setOnClickListener { -// Log.d(TAG, "clicked on text") -// val mService = (activity as TestRightActivity).mService -// mService.read() - Log.d(TAG, "FULL STRING IS -> $fullReadOutput") + viewModel.progressBar.observe(viewLifecycleOwner) { + if (it) { + binding.progressBar.visibility = View.VISIBLE + } else { + binding.progressBar.visibility = View.GONE + } } + +// binding.btnRead.setOnClickListener { +//// Log.d(TAG, "clicked on text") +//// val mService = (activity as TestRightActivity).mService +//// mService.read() +// Log.d(TAG, "FULL STRING IS -> $fullReadOutput") +// } } /** @@ -79,11 +125,13 @@ class TestRightExpReference : Fragment() { * 4. command print */ private fun startTakingReference() { +// requireActivity().runOnUiThread { sendCmdToSetLed() +// } } private fun sendCmdToFetchDeviceConstant() { - binding.progressBar.visibility = View.VISIBLE + viewModel.progressBar.postValue(true) val fullReadOutput = StringBuilder() (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.read, object : UsbServiceListener { @@ -93,93 +141,135 @@ class TestRightExpReference : Fragment() { fullReadOutput.append(stringData) if (stringData.contains("OK", true)) { + Log.d(TAG, "onUsbRead() called in sendCmdToFetchDeviceConstant()") viewModel.mapDeviceConstants(fullReadOutput.toString()) - binding.progressBar.visibility = View.GONE + viewModel.mapPixelNumberToWavelength() + viewModel.progressBar.postValue(false) } } } override fun onUsbError(e: Exception?) { Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") - binding.progressBar.visibility = View.GONE + viewModel.progressBar.postValue(false) } }) } private fun sendCmdToSetLed() { - binding.progressBar.visibility = View.VISIBLE + viewModel.progressBar.postValue(true) val fullReadOutput = StringBuilder() (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.led2Set50, object : UsbServiceListener { override fun onUsbRead(data: ByteArray?) { +// requireActivity().runOnUiThread { data?.let { val stringData = String(it) fullReadOutput.append(stringData) if (stringData.contains("OK", true)) { - binding.progressBar.visibility = View.GONE - sendCmdToAutoset() + Log.d(TAG, "onUsbRead() called in sendCmdToSetLed()") +// viewModel.progressBar.postValue(false) +// requireActivity().runOnUiThread { +// Thread.sleep(2000) +// sendCmdToAutoset() +// } + + Handler(Looper.getMainLooper()).postDelayed( + Runnable { + viewModel.progressBar.postValue(false) + sendCmdToAutoset() + }, + 2000 + ) + + +// sendCmdToAutoset() } } +// } } override fun onUsbError(e: Exception?) { Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") - binding.progressBar.visibility = View.GONE + viewModel.progressBar.postValue(false) } }) } private fun sendCmdToAutoset() { - binding.progressBar.visibility = View.VISIBLE + viewModel.progressBar.postValue(true) val fullReadOutput = StringBuilder() (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.autoset, object : UsbServiceListener { override fun onUsbRead(data: ByteArray?) { +// requireActivity().runOnUiThread { data?.let { val stringData = String(it) fullReadOutput.append(stringData) if (stringData.contains("OK", true)) { - binding.progressBar.visibility = View.GONE - sendCmdToRun() + Log.d(TAG, "onUsbRead() called in sendCmdToAutoset()") +// viewModel.progressBar.postValue(false) +// sendCmdToRun() + + Handler(Looper.getMainLooper()).postDelayed( + Runnable { + viewModel.progressBar.postValue(false) + sendCmdToRun() + }, + 2000 + ) + } } +// } } override fun onUsbError(e: Exception?) { Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") - binding.progressBar.visibility = View.GONE + viewModel.progressBar.postValue(false) } }) } private fun sendCmdToRun() { - binding.progressBar.visibility = View.VISIBLE + viewModel.progressBar.postValue(true) val fullReadOutput = StringBuilder() (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.run, object : UsbServiceListener { override fun onUsbRead(data: ByteArray?) { +// requireActivity().runOnUiThread { data?.let { val stringData = String(it) fullReadOutput.append(stringData) if (stringData.contains("OK", true)) { - binding.progressBar.visibility = View.GONE - sendCmdToFetchLightIntensities() + Log.d(TAG, "onUsbRead() called in sendCmdToRun()") +// viewModel.progressBar.postValue(false) +// sendCmdToFetchLightIntensities() + + Handler(Looper.getMainLooper()).postDelayed( + Runnable { + viewModel.progressBar.postValue(false) + sendCmdToFetchLightIntensities() + }, + 2000 + ) } } +// } } override fun onUsbError(e: Exception?) { Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") - binding.progressBar.visibility = View.GONE + viewModel.progressBar.postValue(false) } }) } private fun sendCmdToFetchLightIntensities() { - binding.progressBar.visibility = View.VISIBLE + viewModel.progressBar.postValue(true) val fullReadOutput = StringBuilder() (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.printAll, object : UsbServiceListener { @@ -187,19 +277,28 @@ class TestRightExpReference : Fragment() { data?.let { val stringData = String(it) fullReadOutput.append(stringData) + Log.d(TAG, stringData) if (stringData.contains("OK", true)) { + Log.d(TAG, "onUsbRead() called in sendCmdToFetchLightIntensities()") viewModel.mapIntensityValues(fullReadOutput.toString(), true) - binding.progressBar.visibility = View.GONE viewModel.isReferenceDone = true - moveToSamplePage() +// viewModel.progressBar.postValue(false) + + Handler(Looper.getMainLooper()).postDelayed( + Runnable { + viewModel.progressBar.postValue(false) + moveToSamplePage() + }, + 2000 + ) } } } override fun onUsbError(e: Exception?) { Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") - binding.progressBar.visibility = View.GONE + viewModel.progressBar.postValue(false) } }) } diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpSample.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpSample.kt index c17967e..2a027b0 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpSample.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpSample.kt @@ -1,6 +1,8 @@ package com.example.refactoredapp.presentation.testRight import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.util.Log import android.view.LayoutInflater import android.view.View @@ -37,6 +39,14 @@ class TestRightExpSample : Fragment() { } private fun setupListeners() { + viewModel.progressBar.observe(viewLifecycleOwner) { + if (it) { + binding.progressBar.visibility = View.VISIBLE + } else { + binding.progressBar.visibility = View.GONE + } + } + binding.btnAcquire.setOnClickListener { val details = isValidInput() if (details != null) { @@ -49,14 +59,14 @@ class TestRightExpSample : Fragment() { binding.etGenderBlock.setOnClickListener { binding.ddGender.isErrorEnabled = false } } - private fun isValidInput() : PatientData? { + private fun isValidInput(): PatientData? { val name = binding.etName.editText?.text?.trim() - if (name.isNullOrEmpty()){ + if (name.isNullOrEmpty()) { binding.etName.error = getString(R.string.name_error) return null } val age = binding.etAge.editText?.text?.trim() - if (age.isNullOrEmpty()){ + if (age.isNullOrEmpty()) { binding.etAge.error = getString(R.string.age_error) return null } else if (age.toString().toInt() < 0 || age.toString().toInt() > 199) { @@ -64,7 +74,7 @@ class TestRightExpSample : Fragment() { return null } val gender = binding.ddGender.editText?.text - if (gender.isNullOrEmpty()){ + if (gender.isNullOrEmpty()) { binding.ddGender.error = getString(R.string.gender_error) return null } @@ -77,11 +87,12 @@ class TestRightExpSample : Fragment() { * 2. command print */ private fun startAcquiring() { +// binding.progressBar.visibility = View.VISIBLE sendCmdToRun() } private fun sendCmdToRun() { - binding.progressBar.visibility = View.VISIBLE + viewModel.progressBar.postValue(true) val fullReadOutput = StringBuilder() (activity as TestRightActivity).mService.eventDrivenWrite( TestRightCommands.run, @@ -92,21 +103,29 @@ class TestRightExpSample : Fragment() { fullReadOutput.append(stringData) if (stringData.contains("OK", true)) { - binding.progressBar.visibility = View.GONE - sendCmdToFetchLightIntensities() +// viewModel.progressBar.postValue(false) + + Handler(Looper.getMainLooper()).postDelayed( + Runnable { + viewModel.progressBar.postValue(false) + sendCmdToFetchLightIntensities() + }, + 2000 + ) + } } } override fun onUsbError(e: Exception?) { Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") - binding.progressBar.visibility = View.GONE + viewModel.progressBar.postValue(false) } }) } private fun sendCmdToFetchLightIntensities() { - binding.progressBar.visibility = View.VISIBLE + viewModel.progressBar.postValue(true) val fullReadOutput = StringBuilder() (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.printAll, object : UsbServiceListener { @@ -114,10 +133,10 @@ class TestRightExpSample : Fragment() { data?.let { val stringData = String(it) fullReadOutput.append(stringData) - +// Log.d(TAG, "Values of print cmd -> $stringData") if (stringData.contains("OK", true)) { viewModel.mapIntensityValues(fullReadOutput.toString(), false) - binding.progressBar.visibility = View.GONE +// viewModel.progressBar.postValue(false) showResultsAfterAcquiring() } } @@ -125,14 +144,17 @@ class TestRightExpSample : Fragment() { override fun onUsbError(e: Exception?) { Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") - binding.progressBar.visibility = View.GONE + viewModel.progressBar.postValue(false) } }) } private fun showResultsAfterAcquiring() { viewModel.calculateResults() - + viewModel.progressBar.postValue(false) +// binding.progressBar.visibility = View.GONE + parentFragmentManager.beginTransaction().replace(R.id.fl_main, TestRightResults()) + .commit() } private fun Button.disable() { diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightViewModel.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightViewModel.kt index a844c78..39a3818 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightViewModel.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightViewModel.kt @@ -1,16 +1,13 @@ package com.example.refactoredapp.presentation.testRight import android.util.Log -import android.util.Patterns -import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel -import androidx.lifecycle.viewModelScope import com.example.refactoredapp.data.Constants -import com.example.refactoredapp.data.model.PatientDetails +import com.example.refactoredapp.data.model.PatientData import com.example.refactoredapp.data.model.TestRightDeviceConstants -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch +import com.example.refactoredapp.domain.SaveRawData +import kotlin.math.log10 import kotlin.math.pow class TestRightViewModel : ViewModel() { @@ -19,22 +16,24 @@ class TestRightViewModel : ViewModel() { var isReferenceDone = false var isServiceConnected = false + val progressBar = MutableLiveData(false) val isUsbConnected = MutableLiveData(false) - lateinit var patientDetails: PatientDetails + lateinit var patientDetails: PatientData + var sampleReadCounter = 0 var deviceConstant: TestRightDeviceConstants? = null - /* Contains wavelength -> pixel no.*/ val wavelengthToPixelArray = ArrayList>() - /* (For reference/baseline) Contains pixel no. -> Intensity of light falling on that pixel */ val intensityReferenceArray = ArrayList>() - /* (For exp sample) Contains pixel no. -> Intensity of light falling on that pixel */ val intensitySampleArray = ArrayList>() + val wavelengthToAbsorbance = ArrayList>() + + fun mapDeviceConstants(string: String) { // viewModelScope.launch { if (string.isNotEmpty()) { @@ -79,6 +78,13 @@ class TestRightViewModel : ViewModel() { fun mapIntensityValues(fullString: String, isReference: Boolean) { // viewModelScope.launch { val listOfString = fullString.split("\n") + +// Log.d(TAG, "inside mapIntensityValues() -> isReference = $isReference") +// Log.d(TAG, "listOfString size = ${listOfString.size}") + +// if(!isReference){ +// Log.d(TAG, "fullString = $fullString") +// } for (line in listOfString) { if ("Buf" in line) { /* Removing trailing spaces and "Buf" from the string then taking lhs & rhs of ':' */ @@ -88,8 +94,15 @@ class TestRightViewModel : ViewModel() { val temp = ArrayList(2) temp.add(numbers[0].toDouble()) temp.add(numbers[1].toDouble()) - if (isReference) intensityReferenceArray.add(temp) - else intensitySampleArray.add(temp) + +// if(!isReference){ +// Log.d(TAG, "temp[0] = ${temp[0]} & temp[1] = ${temp[1]}") +// } + + if (isReference) + intensityReferenceArray.add(temp) + else + intensitySampleArray.add(temp) } else { // Todo: Error handling } @@ -98,28 +111,128 @@ class TestRightViewModel : ViewModel() { // } } -// fun isValidInput() : Boolean{ -// name = email.trim() -// return when { -// email.isEmpty() -> { -// Log.d(TAG, "email is empty") -// _emailError.value = context.getString(R.string.malformed_email_empty_error) -// false -// } -// !Patterns.EMAIL_ADDRESS.matcher(email).matches() -> { -// Log.d(TAG, "email pattern mismatched") -// _emailError.value = context.getString(R.string.malformed_email_error) -// false -// } -// password.length < 6 -> { -// Log.d(TAG, "password is empty") -// _passwordError.value = context.getString(R.string.malformed_password_error) -// false -// } -// else -> { -// Log.d(TAG, "isValidInput: returning true") -// true -// } -// } -// } + + fun calculateDataForCSV(){ + var indexInReference = intensityReferenceArray.size - 1 + var indexInSample = intensitySampleArray.size - 1 + for (each in wavelengthToPixelArray){ + val wavelength = each[0] + val invertedPixel = (Constants.TEST_RIGHT_TOTAL_PIXEL + 1) - each[1] + + var Io = 0.0 + if (intensityReferenceArray[indexInReference][0].toInt() == invertedPixel.toInt()){ + Io = intensityReferenceArray[indexInReference][1] + } + + var I = 0.0 + if (intensitySampleArray[indexInSample][0].toInt() == invertedPixel.toInt()){ + I = intensitySampleArray[indexInSample][1] + } + + val absorbance = log10(Io/I) + + val temp = ArrayList(2) + temp.add(wavelength) + temp.add(absorbance) + wavelengthToAbsorbance.add(temp) + + indexInReference-- + indexInSample-- + } + } + + fun saveRawData(folderPath: String, filename: String) { + SaveRawData().saveCsv(folderPath, filename, wavelengthToAbsorbance) + } + + fun calculateResults() { + Log.d(TAG, "DEBUGGINGGGGG--------------------------------------------------------------------------------") + sampleReadCounter++ + + var pixelNoWithWave_427 = 0; + var pixelNoWithWave_555 = 0; + + Log.d(TAG, "wavelengthToPixelArray size = ${wavelengthToPixelArray.size}") +// println("wavelengthToPixelArray size = ${wavelengthToPixelArray.size}") + for (each in wavelengthToPixelArray){ + var diff = each[0] - 427 + if (pixelNoWithWave_427 == 0 && diff >= 0 && diff < 1){ + pixelNoWithWave_427 = each[1].toInt() + } + + diff = each[0] - 555 + if (pixelNoWithWave_555 == 0 && diff >= 0 && diff < 1){ + pixelNoWithWave_555 = each[1].toInt() + } + + if (pixelNoWithWave_427 != 0 && pixelNoWithWave_555 != 0) { + break + } + } + + Log.d(TAG, "pixelOfInterest_427 = ${pixelNoWithWave_427}") + Log.d(TAG, "pixelOfInterest_555 = ${pixelNoWithWave_555}") + +// println("pixelOfInterest_427 = ${pixelNoWithWave_427}") +// println("pixelOfInterest_555 = ${pixelNoWithWave_555}") + + val invertedPixel_427 = (Constants.TEST_RIGHT_TOTAL_PIXEL + 1) - pixelNoWithWave_427 + val invertedPixel_555 = (Constants.TEST_RIGHT_TOTAL_PIXEL + 1) - pixelNoWithWave_555 + + var io_427 = 0.0 + var io_555 = 0.0 + + for (each in intensityReferenceArray){ + if (each[0].toInt() == invertedPixel_427){ + io_427 = each[1] + } + if (each[0].toInt() == invertedPixel_555){ + io_555 = each[1] + } + } + Log.d(TAG, "I0 values are I0_427 = $io_427 , IO_555 = $io_555") +// println("I0 values are I0_427 = $io_427 , IO_555 = $io_555") + + var i_427 = 0.0 + var i_555 = 0.0 + + for (each in intensitySampleArray){ + if (each[0].toInt() == invertedPixel_427){ + i_427 = each[1] + } + if (each[0].toInt() == invertedPixel_555){ + i_555 = each[1] + } + } + Log.d(TAG, "I values are I_427 = $i_427 , I_555 = $i_555") +// println("I values are I_427 = $i_427 , I_555 = $i_555") + + val absorbanceAtPixelWithWave_427 = log10(io_427/i_427) + val absorbanceAtPixelWithWave_555 = log10(io_555/i_555) + Log.d(TAG, "absorbance values are for 427 = $absorbanceAtPixelWithWave_427 , for 555 = $absorbanceAtPixelWithWave_555") +// println("absorbance values are for 427 = $absorbanceAtPixelWithWave_427 , for 555 = $absorbanceAtPixelWithWave_555") + + var value = 0.0; + if (absorbanceAtPixelWithWave_427 != 0.0 && absorbanceAtPixelWithWave_555 != 0.0) { + value = absorbanceAtPixelWithWave_555 / absorbanceAtPixelWithWave_427 + } + Log.d(TAG, "value = $value") +// println("value = $value") + + var results: String = "" + if (value < 0.24 || value == 0.0) { + results = "Normal" + } else if (value >= 0.24 && value < 0.30) { + results = "Sickle Cell Trait" + } else if (value >= 0.30) { + results = "Sickle-Cell Disease" + } + + Log.d(TAG, "Results = $results") +// println("Results = $results") + patientDetails.results = results + + + } + } \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt index f6a8237..1e78868 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt @@ -33,6 +33,16 @@ class UsbService : Service() { return binder } +// fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) { +// mPort = driver.ports[0] // Most devices have just one port (port 0) +// mPort.open(connection) +// mPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE) +// +// isUsbConnected = true +// Log.d(TAG, "My Usb Connected ${mPort.driver}") +// } + + var listener: UsbServiceListener? = null fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) { mPort = driver.ports[0] // Most devices have just one port (port 0) mPort.open(connection) @@ -40,6 +50,20 @@ class UsbService : Service() { isUsbConnected = true Log.d(TAG, "My Usb Connected ${mPort.driver}") + + val usbIoManager = SerialInputOutputManager(mPort, + object : SerialInputOutputManager.Listener{ + override fun onNewData(data: ByteArray?) { +// Log.e(TAG, "onNewData() called inside eventDrivenWrite()") + listener?.onUsbRead(data) + } + override fun onRunError(e: Exception?) { + Log.e(TAG, "onRunError() called inside eventDrivenWrite()") + listener?.onUsbError(e) + } + + }) + usbIoManager.start(); } fun disconnect() { @@ -50,20 +74,30 @@ class UsbService : Service() { } } +// fun eventDrivenWrite(command: TestRightCommands, listener: UsbServiceListener) { +// val usbIoManager = SerialInputOutputManager(mPort, +// object : SerialInputOutputManager.Listener{ +// override fun onNewData(data: ByteArray?) { +// Log.e(TAG, "onNewData() called inside eventDrivenWrite() :: command = ${command.command}") +// listener.onUsbRead(data) +// } +// override fun onRunError(e: Exception?) { +// Log.e(TAG, "onRunError() called inside eventDrivenWrite()") +// listener.onUsbError(e) +// } +// +// }) +// usbIoManager.start(); +// +// try { +// mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS) +// } catch (e: IOException) { +// listener.onUsbError(e) +// } +// } + fun eventDrivenWrite(command: TestRightCommands, listener: UsbServiceListener) { - val usbIoManager = SerialInputOutputManager(mPort, - object : SerialInputOutputManager.Listener{ - override fun onNewData(data: ByteArray?) { - listener.onUsbRead(data) - } - override fun onRunError(e: Exception?) { - Log.e(TAG, "onRunError() called inside eventDrivenWrite()") - listener.onUsbError(e) - } - - }) - usbIoManager.start(); - + this.listener = listener try { mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS) } catch (e: IOException) { diff --git a/app/src/main/java/com/example/refactoredapp/presentation/utils/UIUtils.kt b/app/src/main/java/com/example/refactoredapp/presentation/utils/UIUtils.kt index ec8d36d..70fb1b6 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/utils/UIUtils.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/utils/UIUtils.kt @@ -5,7 +5,14 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder class UIUtils { - fun createAlertDialog(context: Context, title: String, subtitle: String, btnOneText: String, btnTwoText: String, listener: MyDialogListener){ + fun createAlertDialog( + context: Context, + title: String, + subtitle: String, + btnOneText: String, + btnTwoText: String, + listener: MyDialogListener + ) { MaterialAlertDialogBuilder(context) .setTitle(title) .setMessage(subtitle)