feat: gpio feature根据操作系统自动判断

This commit is contained in:
grabbit 2025-04-05 15:16:47 +08:00
parent 2ce3b11628
commit bf6a401cf7
4 changed files with 21 additions and 2 deletions

View File

@ -6,7 +6,7 @@ authors = ["Meteor Detection Team"]
description = "A Raspberry Pi based meteor detection system" description = "A Raspberry Pi based meteor detection system"
[features] [features]
# 默认features在Linux上自动包含gpio # 默认features
default = [] default = []
# 如果在Linux上运行则默认启用GPIO support # 如果在Linux上运行则默认启用GPIO support
@ -14,6 +14,14 @@ default = []
[target.'cfg(target_os = "linux")'.features] [target.'cfg(target_os = "linux")'.features]
default = ["gpio"] default = ["gpio"]
# 树莓派特定target配置
[target.'cfg(all(target_os = "linux", target_arch = "arm"))'.features]
default = ["gpio"]
# 64位树莓派target配置
[target.'cfg(all(target_os = "linux", target_arch = "aarch64"))'.features]
default = ["gpio"]
# 定义常规的features (全平台通用) # 定义常规的features (全平台通用)
gpio = ["rppal", "embedded-hal"] # Feature to enable GPIO functionality gpio = ["rppal", "embedded-hal"] # Feature to enable GPIO functionality
opencv-4-11-plus = [] # For OpenCV 4.11 and newer versions opencv-4-11-plus = [] # For OpenCV 4.11 and newer versions

View File

@ -8,11 +8,14 @@ fn main() {
let supports_gpio = detect_gpio_support(); let supports_gpio = detect_gpio_support();
// 设置环境变量告诉lib.rs是否有实际的GPIO支持 // 设置环境变量告诉lib.rs是否有实际的GPIO支持
// 注意这只影响PLATFORM_SUPPORTS_GPIO的值不影响依赖的启用
if supports_gpio { if supports_gpio {
// 设置编译器配置lib.rs会使用它 // 设置编译器配置lib.rs会使用它
println!("cargo:rustc-env=HAS_GPIO_SUPPORT=true"); println!("cargo:rustc-env=HAS_GPIO_SUPPORT=true");
println!("cargo:warning=GPIO support detected, PLATFORM_SUPPORTS_GPIO will be true"); println!("cargo:warning=GPIO support detected, PLATFORM_SUPPORTS_GPIO will be true");
// 强制启用gpio feature确保依赖包含rppal
println!("cargo:rustc-cfg=feature=\"gpio\"");
println!("cargo:warning=Also enabling gpio feature to include rppal dependency");
} else if cfg!(target_os = "linux") { } else if cfg!(target_os = "linux") {
// 特殊情况Linux但没有GPIO支持 // 特殊情况Linux但没有GPIO支持
println!("cargo:rustc-env=HAS_GPIO_SUPPORT=false"); println!("cargo:rustc-env=HAS_GPIO_SUPPORT=false");

View File

@ -6,6 +6,10 @@ pub const PLATFORM_SUPPORTS_GPIO: bool = option_env!("HAS_GPIO_SUPPORT").map_or(
#[cfg(not(feature = "gpio"))] #[cfg(not(feature = "gpio"))]
pub const PLATFORM_SUPPORTS_GPIO: bool = false; pub const PLATFORM_SUPPORTS_GPIO: bool = false;
// 强制在条件编译中使用rppal库解决编译依赖问题
#[cfg(all(feature = "gpio", target_os = "linux"))]
pub use rppal;
// 将所有模块重新导出,使它们对例子可见 // 将所有模块重新导出,使它们对例子可见
pub mod camera; pub mod camera;
pub mod config; pub mod config;

View File

@ -7,6 +7,10 @@ use std::sync::Mutex;
#[cfg(feature = "gpio")] #[cfg(feature = "gpio")]
use rppal::gpio::{Gpio, Level, Mode, OutputPin}; use rppal::gpio::{Gpio, Level, Mode, OutputPin};
// 确保即使启用了gpio feature但在非Linux平台上也不尝试使用rppal
#[cfg(all(feature = "gpio", not(target_os = "linux")))]
compile_error!("GPIO feature enabled but not on Linux platform");
#[cfg(not(feature = "gpio"))] #[cfg(not(feature = "gpio"))]
use rand::Rng; use rand::Rng;