Add constants to app and add apk
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

Signed-off-by: Severin Kaderli <severin@kaderli.dev>
This commit is contained in:
Severin Kaderli 2023-01-11 22:42:06 +01:00
parent bb50116cbe
commit c44341648d
Signed by: severinkaderli
GPG key ID: F419F8835B72F0C4
3 changed files with 55 additions and 31 deletions

Binary file not shown.

View file

@ -0,0 +1,9 @@
package dev.kaderli.magsend
object Constants {
const val HEADER_LENGTH: Int = 4
const val BYTE_LENGTH: Int = 8
const val CRC_LENGTH: Int = 8
const val SIGNAL_THRESHOLD: Int = 5
const val SIGNAL_SAMPLE_THRESHOLD: Int = 3
}

View file

@ -4,12 +4,15 @@ import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import dev.kaderli.magsend.Constants
import dev.kaderli.magsend.R
import dev.kaderli.magsend.Utility
import dev.kaderli.magsend.model.Sample
import dev.kaderli.magsend.model.Signal
import java.lang.Integer.min
import java.util.stream.Collectors
import kotlin.math.abs
import kotlin.math.pow
class ReceiveActivity : BaseSensorActivity() {
private lateinit var receiveDescription: TextView
@ -33,6 +36,8 @@ class ReceiveActivity : BaseSensorActivity() {
*/
private var preambleReceived = false
private var payloadLength: Int? = null
private var receivingComplete = false
override fun onCreate(savedInstanceState: Bundle?) {
@ -51,24 +56,24 @@ class ReceiveActivity : BaseSensorActivity() {
private fun getRangeOfData(): Float {
val maxValue = samples.maxByOrNull { it.value } ?: Sample(0f)
val minValue = samples.minByOrNull { it.value } ?: Sample(0f)
//Log.i(TAG, "Min: ${minValue}, Max: $maxValue")
return maxValue.value - minValue.value
}
private fun getThresholdForHighSignals(): Float {
if (getRangeOfData() < 3) {
return getMidRangeOfData() + 3
if (getRangeOfData() < Constants.SIGNAL_THRESHOLD) {
return getMidRangeOfData() + Constants.SIGNAL_THRESHOLD
}
return getMidRangeOfData() + 2
return getMidRangeOfData() + Constants.SIGNAL_THRESHOLD
}
private fun getThresholdForLowSignals(): Float {
if (getRangeOfData() < 3) {
return getMidRangeOfData() + 3
if (getRangeOfData() < Constants.SIGNAL_THRESHOLD) {
return getMidRangeOfData() + Constants.SIGNAL_THRESHOLD
}
return getMidRangeOfData() - 2
return getMidRangeOfData() - Constants.SIGNAL_THRESHOLD
}
private fun getMidRangeOfData(): Float {
@ -151,9 +156,11 @@ class ReceiveActivity : BaseSensorActivity() {
nextIndex++
}
elementCount > 3
elementCount >= Constants.SIGNAL_SAMPLE_THRESHOLD
}
return cleanSignal
}
@ -246,39 +253,46 @@ class ReceiveActivity : BaseSensorActivity() {
val packet = analyzePacketSignal()
// Get the payload length of in the data
var payloadLength = 0
if (packet.size >= 4) {
payloadLength = listToInteger(packet.take(4))
if (payloadLength == null && packet.size >= Constants.HEADER_LENGTH) {
payloadLength = listToInteger(packet.take(Constants.HEADER_LENGTH))
headerStatus.text = getString(R.string.payload_length, payloadLength)
}
if (packet.size >= 4 + (8)) {
val numberOfAvailablePayloadBytes = min((packet.size - 4) / 8, payloadLength)
if(payloadLength == null) {
return
}
if (packet.size >= Constants.HEADER_LENGTH + Constants.BYTE_LENGTH) {
val numberOfAvailablePayloadBytes = min((packet.size - Constants.HEADER_LENGTH) / 8, payloadLength!!)
var payload = ""
for (i in 1..numberOfAvailablePayloadBytes) {
payload += listToInteger(packet.take(4 + (8 * i)).takeLast(8)).toChar().toString()
payload += listToInteger(
packet.take(Constants.HEADER_LENGTH + (Constants.BYTE_LENGTH * i)).takeLast(Constants.BYTE_LENGTH)
).toChar().toString()
}
receiveValue.text = payload
}
if (packet.size >= Constants.HEADER_LENGTH + (Constants.BYTE_LENGTH * payloadLength!!) + Constants.CRC_LENGTH) {
// CRC Check
val receivedCrc = listToInteger(
packet.take(Constants.HEADER_LENGTH + (Constants.BYTE_LENGTH * payloadLength!!) + Constants.CRC_LENGTH)
.takeLast(Constants.CRC_LENGTH)
)
val calculatedCrc =
Utility.crc8Autosar(receiveValue.text.codePoints().boxed().collect(Collectors.toList()))
if (packet.size >= 4 + (8 * payloadLength) + 8) {
// CRC Check
val receivedCrc = listToInteger(packet.take(4 + (8 * payloadLength) + 8).takeLast(8))
val calculatedCrc =
Utility.crc8Autosar(receiveValue.text.codePoints().boxed().collect(Collectors.toList()))
if (receivedCrc == calculatedCrc) {
crcStatus.setText(R.string.crc_status_valid)
crcStatus.setTextColor(ContextCompat.getColor(this, R.color.success))
} else {
crcStatus.setText(R.string.crc_status_invalid)
crcStatus.setTextColor(ContextCompat.getColor(this, R.color.error))
}
// Mark receiving as complete
receiveDescription.setText(R.string.receive_description_complete)
receivingComplete = true
if (receivedCrc == calculatedCrc) {
crcStatus.setText(R.string.crc_status_valid)
crcStatus.setTextColor(ContextCompat.getColor(this, R.color.success))
} else {
crcStatus.setText(R.string.crc_status_invalid)
crcStatus.setTextColor(ContextCompat.getColor(this, R.color.error))
}
// Mark receiving as complete
receiveDescription.setText(R.string.receive_description_complete)
receivingComplete = true
}
}
}
@ -296,6 +310,7 @@ class ReceiveActivity : BaseSensorActivity() {
fun restartReceiveProcess(view: View) {
preambleReceived = false
payloadLength = null
receivingComplete = false
receiveDescription.setText(R.string.receive_description)