diff --git a/Cargo.toml b/Cargo.toml index f776576e5..2c5e003c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,8 @@ riot-wrappers = { version = "^0.8", default-features = false, features = [ static_cell = { version = "2.0.0", features = [ "nightly" ] } ld-memory = { version = "0.2.9" } +konst = { version = "0.3.8", default_features = false } +const_panic = { version = "0.2.8", default_features = false } [profile.dev] incremental = false diff --git a/src/riot-rs-embassy/Cargo.toml b/src/riot-rs-embassy/Cargo.toml index 7677918cc..8351f9c5b 100644 --- a/src/riot-rs-embassy/Cargo.toml +++ b/src/riot-rs-embassy/Cargo.toml @@ -18,6 +18,8 @@ embassy-time = { workspace = true, optional = true } embassy-usb = { workspace = true, optional = true } embassy-net = { workspace = true, optional = true, features = [ "dhcpv4", "medium-ethernet" ] } heapless = "0.8.0" +konst = { workspace = true, features = ["parsing"] } +const_panic = { workspace = true } [target.'cfg(context = "cortex-m")'.dependencies] embassy-executor = { workspace = true, features = [ diff --git a/src/riot-rs-embassy/src/env_utils.rs b/src/riot-rs-embassy/src/env_utils.rs new file mode 100644 index 000000000..e32481271 --- /dev/null +++ b/src/riot-rs-embassy/src/env_utils.rs @@ -0,0 +1,27 @@ +macro_rules! define_env_with_default_macro { + ($macro_name:ident, $parse_fn_name:ident, $output_type_name:literal) => { + #[macro_export] + macro_rules! $macro_name { + ($env_var:literal, $default:expr) => { + if let Some(str_value) = option_env!($env_var) { + if let Ok(value) = konst::primitive::$parse_fn_name(str_value) { + value + } else { + const_panic::concat_panic!( + "Could not parse environment variable `", + $env_var, + "=", + str_value, + "` as ", + $output_type_name, + ); + } + } else { + $default + } + }; + } + }; +} + +define_env_with_default_macro!(usize_env_or, parse_usize, "a usize"); diff --git a/src/riot-rs-embassy/src/lib.rs b/src/riot-rs-embassy/src/lib.rs index fc72b97eb..295330c13 100644 --- a/src/riot-rs-embassy/src/lib.rs +++ b/src/riot-rs-embassy/src/lib.rs @@ -14,6 +14,7 @@ #![feature(used_with_arg)] pub mod define_peripherals; +mod env_utils; #[cfg_attr(context = "nrf52", path = "arch/nrf52.rs")] #[cfg_attr(context = "rp2040", path = "arch/rp2040.rs")]