feat: demos目录下的测试demo也需要根据平台来开启feature;

This commit is contained in:
grabbit 2025-06-28 17:51:04 +08:00
parent 9733640728
commit 080b1d1b64
4 changed files with 252 additions and 0 deletions

View File

@ -5,6 +5,10 @@ edition = "2021"
authors = ["Meteor Detection Team"]
description = "Demonstration programs for the Meteor Detection System"
[features]
default = []
gpio = ["meteor_detect/gpio"] # Pass gpio feature to the main project
[dependencies]
meteor_detect = { path = ".." }
tokio = { version = "1", features = ["full"] }

64
demos/README.md Normal file
View File

@ -0,0 +1,64 @@
# Meteor Detection System Demos
这个目录包含了流星检测系统的演示程序。
## 编译说明
### 自动编译(推荐)
使用提供的构建脚本,它会自动检测平台并启用相应的功能:
```bash
# 自动检测平台并编译
./build_demos.sh
# 编译 release 版本
./build_demos.sh release
```
### 手动编译
#### 在树莓派上
```bash
# 编译所有 demos启用 GPIO 功能)
cargo build --features gpio
# 编译并运行特定的 demo
cargo run --bin camera_demo --features gpio
cargo run --bin watermark_demo --features gpio
cargo run --bin file_input_demo --features gpio
cargo run --bin star_chart_demo --features gpio
```
#### 在其他平台上
```bash
# 编译所有 demos不启用 GPIO 功能)
cargo build
# 编译并运行特定的 demo
cargo run --bin camera_demo
cargo run --bin watermark_demo
cargo run --bin file_input_demo
cargo run --bin star_chart_demo
```
## 可用的演示程序
1. **camera_demo** - 摄像头演示
2. **watermark_demo** - 水印叠加演示
3. **file_input_demo** - 文件输入演示
4. **star_chart_demo** - 星图绘制演示
## 错误解决
如果遇到 `failed to resolve: use of undeclared crate or module 'rppal'` 错误:
1. 确保在树莓派上使用 `--features gpio` 参数
2. 或者使用自动构建脚本 `./build_demos.sh`
3. 检查是否正确检测到了树莓派平台
## 依赖关系
demos 项目依赖于主项目的功能,当启用 `gpio` feature 时,会同时启用主项目的 GPIO 支持。

84
demos/build.rs Normal file
View File

@ -0,0 +1,84 @@
use std::process::Command;
use std::env;
use std::fs;
use std::path::Path;
fn main() {
// 检测当前是否在树莓派上
let is_raspberry_pi = detect_raspberry_pi();
// 自动启用GPIO功能仅当是树莓派时
if is_raspberry_pi {
println!("cargo:rustc-cfg=feature=\"gpio\"");
println!("cargo:warning=Raspberry Pi detected, enabling gpio feature for demos");
} else {
println!("cargo:warning=Not running on Raspberry Pi, gpio feature NOT enabled for demos");
}
}
fn detect_raspberry_pi() -> bool {
// 方法1: 检查是否是Linux系统必要条件
let is_linux = env::consts::OS == "linux";
if !is_linux {
return false;
}
// 方法2: 检查/proc/cpuinfo是否包含Raspberry Pi的特征
if let Ok(cpuinfo) = fs::read_to_string("/proc/cpuinfo") {
if cpuinfo.contains("Raspberry Pi") ||
cpuinfo.contains("BCM2708") ||
cpuinfo.contains("BCM2709") ||
cpuinfo.contains("BCM2710") ||
cpuinfo.contains("BCM2711") ||
cpuinfo.contains("BCM2835") ||
cpuinfo.contains("BCM2836") ||
cpuinfo.contains("BCM2837") ||
cpuinfo.contains("BCM2838") {
return true;
}
}
// 方法3: 检查树莓派特有的设备树模型文件
if let Ok(model) = fs::read_to_string("/sys/firmware/devicetree/base/model") {
if model.contains("Raspberry Pi") {
return true;
}
}
// 方法4: 检查是否存在树莓派特有的库
let lib_check = Command::new("ldconfig")
.args(["-p"])
.output()
.ok()
.and_then(|output| {
if output.status.success() {
let libraries = String::from_utf8_lossy(&output.stdout);
if libraries.contains("librpgpio") || libraries.contains("libraspberrypi") {
Some(true)
} else {
None
}
} else {
None
}
});
if lib_check.is_some() {
return true;
}
// 方法5: 检查树莓派特有的配置文件
if Path::new("/boot/config.txt").exists() || Path::new("/boot/firmware/config.txt").exists() {
// 这个文件在大多数树莓派系统上存在
return true;
}
// 检查是否在CARGO_FEATURE_GPIO环境变量中强制启用
// 这允许用户手动启用GPIO功能覆盖自动检测
if env::var("CARGO_FEATURE_GPIO").is_ok() {
return true;
}
// 否则默认为false
false
}

100
demos/build_demos.sh Executable file
View File

@ -0,0 +1,100 @@
#!/bin/bash
# 检测平台并编译 demos 的脚本
set -e
# 检测是否在树莓派上
detect_raspberry_pi() {
# 检查是否是Linux系统
if [[ "$OSTYPE" != "linux-gnu"* ]]; then
return 1
fi
# 检查CPU信息
if grep -q "Raspberry Pi\|BCM2708\|BCM2709\|BCM2710\|BCM2711\|BCM2835\|BCM2836\|BCM2837\|BCM2838" /proc/cpuinfo 2>/dev/null; then
return 0
fi
# 检查设备树模型文件
if grep -q "Raspberry Pi" /sys/firmware/devicetree/base/model 2>/dev/null; then
return 0
fi
# 检查配置文件
if [[ -f "/boot/config.txt" ]] || [[ -f "/boot/firmware/config.txt" ]]; then
return 0
fi
return 1
}
# 主编译逻辑
main() {
echo "检测运行平台..."
if detect_raspberry_pi; then
echo "✓ 检测到树莓派平台"
echo "编译 demos 时启用 GPIO 功能..."
if [[ "$1" == "release" ]]; then
echo "编译 release 版本..."
cargo build --release --features gpio
else
echo "编译 debug 版本..."
cargo build --features gpio
fi
echo "✓ demos 编译完成(包含 GPIO 支持)"
else
echo "✓ 检测到非树莓派平台"
echo "编译 demos 时不启用 GPIO 功能..."
if [[ "$1" == "release" ]]; then
echo "编译 release 版本..."
cargo build --release
else
echo "编译 debug 版本..."
cargo build
fi
echo "✓ demos 编译完成(模拟模式)"
fi
}
# 显示帮助信息
show_help() {
echo "用法: $0 [release]"
echo ""
echo "选项:"
echo " release 编译 release 版本"
echo " help 显示此帮助信息"
echo ""
echo "脚本会自动检测是否在树莓派上运行,并相应地启用或禁用 GPIO 功能。"
echo ""
echo "运行特定 demo:"
echo " cargo run --bin camera_demo [--features gpio]"
echo " cargo run --bin watermark_demo [--features gpio]"
echo " cargo run --bin file_input_demo [--features gpio]"
echo " cargo run --bin star_chart_demo [--features gpio]"
}
# 检查参数
case "${1:-}" in
"help"|"-h"|"--help")
show_help
exit 0
;;
"")
main
;;
"release")
main release
;;
*)
echo "错误: 未知参数 '$1'"
echo ""
show_help
exit 1
;;
esac