use anyhow::Result;
use endbasic_std::gpio;
use getoptsargs::prelude::bad_usage;
use std::cell::RefCell;
use std::rc::Rc;
#[cfg(feature = "rpi")]
fn setup_gpio_pins_rppal() -> Result<Rc<RefCell<dyn gpio::Pins>>> {
Ok(Rc::new(RefCell::new(endbasic_rpi::RppalPins::default())))
}
#[cfg(not(feature = "rpi"))]
fn setup_gpio_pins_rppal() -> Result<Rc<RefCell<dyn gpio::Pins>>> {
Err(bad_usage!("--gpio-pins=rppal requires the rpi feature to be compiled in").into())
}
pub(crate) fn setup_gpio_pins(spec: Option<&str>) -> Result<Rc<RefCell<dyn gpio::Pins>>> {
let spec = if cfg!(feature = "rpi") { spec.unwrap_or("rppal") } else { spec.unwrap_or("noop") };
match spec {
"mock" => Ok(Rc::new(RefCell::new(gpio::MockPins::default()))),
"noop" => Ok(Rc::new(RefCell::new(gpio::NoopPins::default()))),
"rppal" => setup_gpio_pins_rppal(),
other => Err(bad_usage!(format!("Unknown --gpio-pins backend: {}", other)).into()),
}
}