feat: ir status

This commit is contained in:
玖叁 2024-12-27 15:03:23 +08:00
parent 54bd378bf7
commit 756d611c31
3 changed files with 31 additions and 8 deletions

View File

@ -10,6 +10,10 @@ object CarCommands {
const val CMD_GET_BT_STATUS = 0x10u
const val CMD_GET_SPIFFS_STATUS = 0x11u
const val CMD_GET_DISTANCE = 0x12u
const val CMD_SET_NAME = 0xa1u
const val CMD_SET_PID = 0xa2u
const val CMD_MOTOR_MOVE_CONTROL = 0x20u
const val CMD_MOTOR_STEER_CONTROL = 0x21u
const val CMD_MOTOR_SINGLE_CONTROL = 0x22u
@ -17,5 +21,6 @@ object CarCommands {
const val CMD_MOTOR_XYR_CONTROL = 0x24u
const val CMD_DEMO_PID = 0xf0u
const val CMD_DEMO_PATH = 0xf1u
const val CMD_STATUS_MOTOR = 0xE0u
const val CMD_STATUS = 0xE0u
}

View File

@ -4,8 +4,6 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.navigation.NavHostController
import icu.fur93.esp32_car.Route
import icu.fur93.esp32_car.repository.ConnectionState
import icu.fur93.esp32_car.ui.card.UnconnectedStatusCard
import icu.fur93.esp32_car.ui.carditem.StatusCardInfo

View File

@ -31,6 +31,7 @@ import icu.fur93.esp32_car.data.PreferencesDataStore
import icu.fur93.esp32_car.entity.LogDirection
import icu.fur93.esp32_car.entity.LogEntry
import icu.fur93.esp32_car.entity.car.CarState
import icu.fur93.esp32_car.entity.car.InfraredState
import icu.fur93.esp32_car.entity.car.MotorState
// 蓝牙通信接口
@ -274,10 +275,23 @@ class BluetoothRepositoryImpl(
private fun parsePacket(packet: ByteArray) {
if (!isValidPacket(packet)) return
when (packet[2].toUByte().toUInt()) {
/**
包体 `01 0E E0 电机A_IN_1_2 电机A_PWM 电机B_IN_1_2 电机B_PWM 电机C_IN_1_2 电机C_PWM 电机D_IN_1_2 电机D_PWM 红外引脚数量 红外数据 FE`
// `01 0C E0 电机A_IN_1_2 电机A_PWM 电机B_IN_1_2 电机B_PWM 电机C_IN_1_2 电机C_PWM 电机D_IN_1_2 电机D_PWM FE`
CarCommands.CMD_STATUS_MOTOR -> updateMotorStatus(packet)
| 数据 | 类型 | 表示 |
| -------- | ---- | ---- |
| IN_1_2 | 无符号整数 | 按位表示 IN1 IN2 的值 0000 00 IN2 IN1 |
| PWM | 无符号整数 | - |
| 红外引脚数量 | 无符号整数 | - |
| 红外数据 | 无符号整数 | 按位表示红外循迹模块数据 0001 1111 |
示例 `01 0E E0 01 FF 02 FF 02 FF 01 FF 05 1F FE`
**/
when (packet[2].toUByte().toUInt()) {
CarCommands.CMD_STATUS -> {
updateStatus(packet)
}
}
}
@ -287,13 +301,14 @@ class BluetoothRepositoryImpl(
packet[packet[1].toUByte()
.toInt() - 1].toUByte() == CarCommands.PACKET_R_TAIL.toUByte()
private fun updateMotorStatus(packet: ByteArray) {
private fun updateStatus(packet: ByteArray) {
val data = packet.sliceArray(3 until packet[1].toUByte().toInt() - 1)
_carState.value = _carState.value.copy(
motorAState = createMotorState(data, 0),
motorBState = createMotorState(data, 2),
motorCState = createMotorState(data, 4),
motorDState = createMotorState(data, 6)
motorDState = createMotorState(data, 6),
infraredState = createInfraredState(data[8].toUByte().toInt(), data[9].toUByte())
)
}
@ -303,6 +318,11 @@ class BluetoothRepositoryImpl(
in2 = (data[offset].toUByte().toUInt() shr 1) and 1u
)
private fun createInfraredState(irPinCount: Int, irData: UByte) = InfraredState(
enable = true,
statusList = irData.toString(2).padStart(irPinCount, '0').reversed().map { if (it == '1') 1u else 0u }
)
// 断开连接方法
@SuppressLint("MissingPermission")
fun disconnect() {