-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example of GPIO task for Raspberry pi.
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "rp-gpio" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
copper = { path = "../../copper" } | ||
rppal = "0.18.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Self> | ||
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<Self::Input>) -> CuResult<()> { | ||
self.pin.write(msg.payload.into()); | ||
Ok(()) | ||
} | ||
} |