armor: ESP32-C3 装甲板固件(一主三从)+ 烧录脚本,之前一直没提交

这一块代码本来就写好了、根 README 里也按"已实现"描述,但一直没有进仓库,
只存在于工作区里。同一个提交里带上 `.env.example` 的两个新变量(ARMOR_TEAM、
ARMOR_THRESHOLD)——它们的说明文字就是为这套固件写的。

内容:
- armor-protocol:规则层(血量、每板独立 50ms 受击冷却、受击后只有那一路灯带全黑、
  按键 FSM、上报编码)。纯计算,主机上 22 个测试,已跑通。
- armor-firmware:四路压电片 ADC 连续采样、四路 WS2812(C3 只有一个 RMT 通道可用,
  靠 rmt_tx_switch_gpio 轮流驱动)、NVS、BOOT 键、WiFi + UDP 广播。
- scripts:build_armor.sh / flash_armor.sh / armor_probe.py(不开界面单看广播)。

armor/ 是独立 workspace(目标 RISC-V 的 ESP32-C3),与外层 Xtensa 的图传固件互不相干,
各自 .cargo/config.toml 就是这条边界。构建产物由根 .gitignore 的 `/armor/target/` 覆盖,
本次没有把任何 target/ 或 build/ 下的东西加进来。

顺带把 `.DS_Store` 加进 .gitignore。
This commit is contained in:
Horldsence committed 2026-10-14 19:43:14 +08:00
1 parent 7fdebe3ec7
commit a30eb3200a
22 files changed
+4690

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
//! 板子自己的持久化设置(NVS):队别与颜色。
//!
//! 命名空间与键名都沿用参考实现(`armor` / `color`),所以从参考固件刷过来时,
//! 之前存过的颜色还能被读到。新增一个 `team` 键用来记住这块板子是哪一队。
use anyhow::Result;
use esp_idf_svc::nvs::{EspDefaultNvsPartition, EspNvs, NvsDefault};
use log::info;
use armor_protocol::Rgb;
/// NVS 命名空间(与参考实现一致)。
const NAMESPACE: &str = "armor";
/// 队别键名。
const KEY_TEAM: &str = "team";
/// 颜色键名,值是 `0x00RRGGBB`(与参考实现一致)。
const KEY_COLOR: &str = "color";
/// 打开着的 NVS 句柄。
pub struct Settings {
nvs: EspNvs<NvsDefault>,
}
impl Settings {
/// 打开(必要时创建)命名空间。
pub fn new(partition: EspDefaultNvsPartition) -> Result<Self> {
Ok(Self {
nvs: EspNvs::new(partition, NAMESPACE, true)?,
})
}
/// 定出这次该用哪个颜色,并把它写回 NVS。
///
/// 规则:**编译期注入的 `ARMOR_TEAM` 是权威**。换队伍只要重刷固件,不用先擦
/// NVS;NVS 里的值只用来记住当前状态(也给以后加"手动换色"留位置)。两者
/// 不一致时以 `ARMOR_TEAM` 为准并覆盖,日志里会说一声,免得现场纳闷为什么
/// 灯色变了。
pub fn resolve_color(&self, team: u8) -> Result<Rgb> {
let expected = Rgb::for_team(team);
let stored_team = self.nvs.get_u8(KEY_TEAM)?;
let stored_color = self.nvs.get_u32(KEY_COLOR)?;
let color = match (stored_team, stored_color) {
(Some(stored), Some(raw)) if stored == team => Rgb::from_u32(raw),
(Some(stored), Some(raw)) => {
info!(
"ARMOR_TEAM={team} overrides the stored team {stored} (colour #{raw:06X}); rewriting NVS"
);
expected
}
_ => {
info!(
"first boot as team {team}: storing colour #{:06X}",
expected.to_u32()
);
expected
}
};
self.nvs.set_u8(KEY_TEAM, team)?;
self.nvs.set_u32(KEY_COLOR, color.to_u32())?;
Ok(color)
}
}