From 080b1d1b64f08ed8d5810db9c2183bb16f1916dc Mon Sep 17 00:00:00 2001 From: grabbit Date: Sat, 28 Jun 2025 17:51:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20demos=E7=9B=AE=E5=BD=95=E4=B8=8B?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95demo=E4=B9=9F=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=B9=B3=E5=8F=B0=E6=9D=A5=E5=BC=80=E5=90=AF?= =?UTF-8?q?feature=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demos/Cargo.toml | 4 ++ demos/README.md | 64 +++++++++++++++++++++++++++ demos/build.rs | 84 ++++++++++++++++++++++++++++++++++++ demos/build_demos.sh | 100 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 252 insertions(+) create mode 100644 demos/README.md create mode 100644 demos/build.rs create mode 100755 demos/build_demos.sh diff --git a/demos/Cargo.toml b/demos/Cargo.toml index 5eaf918..2a86c5a 100644 --- a/demos/Cargo.toml +++ b/demos/Cargo.toml @@ -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"] } diff --git a/demos/README.md b/demos/README.md new file mode 100644 index 0000000..ce06e08 --- /dev/null +++ b/demos/README.md @@ -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 支持。 \ No newline at end of file diff --git a/demos/build.rs b/demos/build.rs new file mode 100644 index 0000000..bcb22f9 --- /dev/null +++ b/demos/build.rs @@ -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 +} \ No newline at end of file diff --git a/demos/build_demos.sh b/demos/build_demos.sh new file mode 100755 index 0000000..d4dbfbb --- /dev/null +++ b/demos/build_demos.sh @@ -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 \ No newline at end of file