feat(client): 同步每路镜像并精简操控入口与 HUD 映射
构建客户端 / 检查(类型 + 单元测试) (push) Canceled after 0s
构建客户端 / Linux x64(deb + AppImage) (push) Canceled after 0s
构建客户端 / Windows x64(NSIS 安装包) (push) Canceled after 0s
构建客户端 / macOS 通用(.app + .dmg) (push) Canceled after 0s

This commit is contained in:
sloray committed 2026-11-01 23:38:10 +08:00
1 parent 38410bacd4
commit 1b1fa0f35b
28 files changed
+503 -62

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
use crate::{decode_header, encode_header_into, Header, MsgType, HEADER_LEN};
/// 每路画面的镜像寄存器。revision 是逻辑时钟,origin 用于并发写入的确定排序。
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CameraMirror {
pub camera_id: u32,
pub revision: u64,
pub origin: u64,
pub mirror: bool,
pub mirror_vertical: bool,
}
impl CameraMirror {
pub fn encode(self) -> Option<Vec<u8>> {
if self.camera_id == 0 || self.revision == 0 || self.origin == 0 {
return None;
}
let mut bytes = Vec::new();
encode_header_into(
&Header {
msg_type: MsgType::CameraMirror,
..Default::default()
},
&mut bytes,
);
bytes.extend(self.camera_id.to_le_bytes());
bytes.extend(self.revision.to_le_bytes());
bytes.extend(self.origin.to_le_bytes());
bytes.push(u8::from(self.mirror) | (u8::from(self.mirror_vertical) << 1));
Some(bytes)
}
pub fn decode(bytes: &[u8]) -> Option<Self> {
let h = decode_header(bytes)?;
if h.msg_type != MsgType::CameraMirror
|| bytes.len() != HEADER_LEN + 21
|| bytes[7..HEADER_LEN].iter().any(|v| *v != 0)
{
return None;
}
let p = &bytes[HEADER_LEN..];
if p[20] > 3 {
return None;
}
let value = Self {
camera_id: u32::from_le_bytes(p[..4].try_into().ok()?),
revision: u64::from_le_bytes(p[4..12].try_into().ok()?),
origin: u64::from_le_bytes(p[12..20].try_into().ok()?),
mirror: p[20] & 1 != 0,
mirror_vertical: p[20] & 2 != 0,
};
value.encode()?;
Some(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exact_validated_mirror_roundtrip() {
let p = CameraMirror {
camera_id: 7,
revision: 4,
origin: 2,
mirror: true,
mirror_vertical: false,
};
let bytes = p.encode().unwrap();
assert_eq!(CameraMirror::decode(&bytes), Some(p));
for n in 0..bytes.len() {
assert!(CameraMirror::decode(&bytes[..n]).is_none());
}
for at in [5, 7, HEADER_LEN + 20] {
let mut bad = bytes.clone();
bad[at] = 255;
assert!(CameraMirror::decode(&bad).is_none());
}
}
}
+4
View File
@@ -47,6 +47,8 @@
//! ```
use std::net::{Ipv4Addr, SocketAddrV4};
mod camera_mirror;
pub use camera_mirror::CameraMirror;
mod match_state;
pub use match_state::MatchState;
@@ -136,6 +138,7 @@ pub enum MsgType {
MatchResult = 10,
/// 选手端定向发送给已绑定装甲板的四轴速度与发射状态。
OperatorControl = 11,
CameraMirror = 12,
}
impl MsgType {
@@ -153,6 +156,7 @@ impl MsgType {
9 => MsgType::ArmorReset,
10 => MsgType::MatchResult,
11 => MsgType::OperatorControl,
12 => MsgType::CameraMirror,
_ => return None,
})
}
+2
View File
@@ -40,6 +40,7 @@ MSG_ARMOR_DAMAGE_CONTROL = 8
MSG_ARMOR_RESET = 9
MSG_MATCH_RESULT = 10
MSG_OPERATOR_CONTROL = 11
MSG_CAMERA_MIRROR = 12
TYPE_NAMES = {
MSG_VIDEO: "video",
@@ -53,6 +54,7 @@ TYPE_NAMES = {
MSG_ARMOR_RESET: "armor-reset",
MSG_MATCH_RESULT: "match-result",
MSG_OPERATOR_CONTROL: "operator-control",
MSG_CAMERA_MIRROR: "camera-mirror",
}