-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make utility methods for creating raspberry PI peripherals
- Loading branch information
1 parent
e5acba6
commit 51b560b
Showing
5 changed files
with
109 additions
and
14 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
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
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
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
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,67 @@ | ||
//! Extensions of the Peripheral Enum to construct Raspberry Pi Peripherals | ||
|
||
use rppal::{ | ||
gpio::Gpio, | ||
i2c::I2c, | ||
spi::{Bus, Mode, SlaveSelect, Spi}, | ||
}; | ||
|
||
use super::Peripheral; | ||
|
||
impl Peripheral { | ||
/// Creates a Peripheral Input Node for a Raspberry Pi GPIO pin | ||
pub fn gpio_input(pin: u8) -> rppal::gpio::Result<Self> { | ||
let gpio = Gpio::new()?; | ||
let pin = gpio.get(pin)?.into_input(); | ||
|
||
Ok(Self::Input(Box::new(pin))) | ||
} | ||
|
||
/// Creates a Peripheral Output Node for a Raspberry Pi GPIO pin | ||
pub fn gpio_output(pin: u8) -> rppal::gpio::Result<Self> { | ||
let gpio = Gpio::new()?; | ||
let pin = gpio.get(pin)?.into_output(); | ||
|
||
Ok(Self::Output(Box::new(pin))) | ||
} | ||
|
||
/// Creates a Peripheral Iput Node for a Raspberry Pi i2c channel | ||
pub fn i2c_input(address: u16) -> rppal::i2c::Result<Self> { | ||
let mut i2c = I2c::new()?; | ||
i2c.set_slave_address(address)?; | ||
|
||
Ok(Self::Input(Box::new(i2c))) | ||
} | ||
|
||
/// Creates a Peripheral Output Node for a Raspberry Pi i2c channel | ||
pub fn i2c_output(address: u16) -> rppal::i2c::Result<Self> { | ||
let mut i2c = I2c::new()?; | ||
i2c.set_slave_address(address)?; | ||
|
||
Ok(Self::Input(Box::new(i2c))) | ||
} | ||
|
||
/// Creates a Peripheral Input Node for a Raspberry Pi SPI Bus | ||
pub fn spi_input( | ||
bus: Bus, | ||
slave_select: SlaveSelect, | ||
clock_speed: u32, | ||
mode: Mode, | ||
) -> rppal::spi::Result<Self> { | ||
let spi = Spi::new(bus, slave_select, clock_speed, mode)?; | ||
|
||
Ok(Self::Input(Box::new(spi))) | ||
} | ||
|
||
/// Creates a Peripheral Output Node for a Raspberry Pi SPI Bus | ||
pub fn spi_output( | ||
bus: Bus, | ||
slave_select: SlaveSelect, | ||
clock_speed: u32, | ||
mode: Mode, | ||
) -> rppal::spi::Result<Self> { | ||
let spi = Spi::new(bus, slave_select, clock_speed, mode)?; | ||
|
||
Ok(Self::Output(Box::new(spi))) | ||
} | ||
} |