Use packed implementation

This commit is contained in:
Johannes Frohnmeyer 2023-01-27 17:09:07 +01:00
parent 2169ad3259
commit 957febea67
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 3 additions and 73 deletions

View File

@ -22,7 +22,9 @@ class BtInputActivity : InputActivity(), ConnectBtTask.Ctx {
override fun send(left: Byte, right: Byte) {
if (btSocket != null) {
try {
btSocket!!.outputStream.write(byteArrayOf(left, right)) // byteArrayOf(pack(left, right))
val pl = left.toUByte() and 0xF0u
val pr = ((right.toUByte() and 0xF0u).div(16u)).toUByte()
btSocket!!.outputStream.write(byteArrayOf((pl or pr).toByte()))
} catch (e: IOException) {
logE("Could not send signal", e)
toast("Error")

View File

@ -1,64 +0,0 @@
package io.gitlab.jfronny.hc05ac.unused
import android.bluetooth.BluetoothSocket
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import io.gitlab.jfronny.hc05ac.live.ConnectBtTask
import io.gitlab.jfronny.hc05ac.live.DeviceListActivity
import io.gitlab.jfronny.hc05ac.R
import io.gitlab.jfronny.hc05ac.util.BaseActivity
import io.gitlab.jfronny.hc05ac.util.logE
import io.gitlab.jfronny.hc05ac.util.toast
import java.io.IOException
class BasicInputActivity : BaseActivity(), ConnectBtTask.Ctx {
override var address: String? = null
override fun close() = finish()
override val context: BasicInputActivity = this
override var btSocket: BluetoothSocket? = null
private var progress: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
address = intent.getStringExtra(DeviceListActivity.EXTRA_ADDRESS)
setContentView(R.layout.activity_led_control)
val btn1: Button = findViewById(R.id.button2)
val btn2: Button = findViewById(R.id.button3)
val btn3: Button = findViewById(R.id.button5)
val btn4: Button = findViewById(R.id.button6)
val btn5: Button = findViewById(R.id.button7)
val btnDis: Button = findViewById(R.id.button4)
progress = findViewById(R.id.textView2)
ConnectBtTask(this).execute()
btn1.setOnClickListener { sendSignal("1") }
btn2.setOnClickListener { sendSignal("2") }
btn3.setOnClickListener { sendSignal("3") }
btn4.setOnClickListener { sendSignal("4") }
btn5.setOnClickListener { sendSignal("5") }
btnDis.setOnClickListener { disconnect() }
}
private fun sendSignal(number: String) {
if (btSocket != null) {
try {
btSocket!!.outputStream.write(number.toByteArray())
} catch (e: IOException) {
logE("Could not send signal", e)
toast("Error")
}
}
}
private fun disconnect() {
if (btSocket != null) {
try {
btSocket!!.close()
} catch (e: IOException) {
logE("Could not disconnect", e)
toast("Error")
}
}
finish()
}
}

View File

@ -1,8 +0,0 @@
package io.gitlab.jfronny.hc05ac.util
private val Byte.u get() = toUByte()
private val UByte.s get() = toByte()
fun pack(left: Byte, right: Byte): Byte = ((left.u and 0xF0u) or ((right.u and 0xF0u).div(16u)).toUByte()).s
fun Byte.unpackLeft(): Byte = (this.u and 0xF0u).s
fun Byte.unpackRight(): Byte = ((this.u and 0x0Fu) * 16u).toByte()