diff --git a/examples/rp-gpio/Cargo.toml b/examples/rp-gpio/Cargo.toml new file mode 100644 index 000000000..0381c55a4 --- /dev/null +++ b/examples/rp-gpio/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rp-gpio" +version = "0.1.0" +edition = "2021" + +[dependencies] +copper = { path = "../../copper" } +rppal = "0.18.0" \ No newline at end of file diff --git a/examples/rp-gpio/src/lib.rs b/examples/rp-gpio/src/lib.rs new file mode 100644 index 000000000..70489c9f7 --- /dev/null +++ b/examples/rp-gpio/src/lib.rs @@ -0,0 +1,44 @@ +use copper::config::NodeInstanceConfig; +use copper::cutask::{CuMsg, CuSinkTask, CuTaskLifecycle}; +use copper::{CuError, CuResult}; + +use rppal::gpio::{Gpio, OutputPin}; + +/// Example of a GPIO output driver for the Raspberry Pi +/// The config takes one config value: `pin` which is the pin you want to address +/// Gpio uses BCM pin numbering. For example: BCM GPIO 23 is tied to physical pin 16. +pub struct RPGpio { + pin: OutputPin, +} + +impl CuTaskLifecycle for RPGpio { + fn new(config: Option<&NodeInstanceConfig>) -> CuResult + where + Self: Sized, + { + let config = + config.ok_or("RPGpio needs a config, None was passed as NodeInstanceConfig")?; + + let pin_nb: u8 = (*config.get("pin").expect( + "RPGpio expects a pin config value pointing to output pin you want to address", + )) + .clone() + .into(); + + let pin = Gpio::new() + .map_err(|e| CuError::new_with_cause("Failed to initialize GPIO", e))? + .get(pin_nb) + .map_err(|e| CuError::new_with_cause("Could not get pin", e))? + .into_output(); + Ok(Self { pin }) + } +} + +impl CuSinkTask for RPGpio { + type Input = u8; + + fn process(&mut self, msg: &CuMsg) -> CuResult<()> { + self.pin.write(msg.payload.into()); + Ok(()) + } +}