Skip to content

Commit

Permalink
more example and formatting tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
warthog618 committed Feb 19, 2024
1 parent 45f810a commit 6a35904
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
13 changes: 7 additions & 6 deletions lib/src/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ impl Chip {
///
/// # Examples
///```no_run
/// # use gpiocdev::Chip;
/// # fn example() -> gpiocdev::Result<Chip>{
/// let chip = Chip::from_path("/dev/gpiochip0")?;
/// # fn example() -> gpiocdev::Result<gpiocdev::Chip>{
/// let chip = gpiocdev::Chip::from_path("/dev/gpiochip0")?;
/// # Ok(chip)
/// # }
///```
Expand All @@ -156,9 +155,8 @@ impl Chip {
///
/// # Examples
///```no_run
/// # use gpiocdev::Chip;
/// # fn example() -> gpiocdev::Result<Chip>{
/// let chip = Chip::from_name("gpiochip0")?;
/// # fn example() -> gpiocdev::Result<gpiocdev::Chip>{
/// let chip = gpiocdev::Chip::from_name("gpiochip0")?;
/// # Ok(chip)
/// # }
///```
Expand Down Expand Up @@ -413,12 +411,14 @@ impl Chip {
mem::size_of::<uapi::LineInfoChangeEvent>()
}
}

impl AsFd for Chip {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.f.as_fd()
}
}

impl AsRawFd for Chip {
#[inline]
fn as_raw_fd(&self) -> i32 {
Expand Down Expand Up @@ -452,6 +452,7 @@ pub struct Info {
/// The number of lines provided by the chip.
pub num_lines: u32,
}

impl From<uapi::ChipInfo> for Info {
fn from(ci: uapi::ChipInfo) -> Self {
Info {
Expand Down
24 changes: 10 additions & 14 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
//!
//! Request an input line and output line, and read from the input and change the output to that value:
//! ```no_run
//! use gpiocdev::Request;
//! # fn example() -> Result<(), gpiocdev::Error> {
//! use gpiocdev::line::{Bias, Value};
//!
//! # fn example() -> Result<(), gpiocdev::Error> {
//! let req = Request::builder()
//! let req = gpiocdev::Request::builder()
//! .on_chip("/dev/gpiochip0")
//! .with_line(3)
//! .as_input()
Expand All @@ -35,15 +34,12 @@
//! ```
//! Monitor a line for debounced edges:
//! ```no_run
//! use gpiocdev::line::EdgeDetection;
//! use std::time::Duration;
//!
//! # fn example() -> Result<(), gpiocdev::Error> {
//! let req = gpiocdev::Request::builder()
//! .on_chip("/dev/gpiochip0")
//! .with_line(5)
//! .with_edge_detection(EdgeDetection::BothEdges)
//! .with_debounce_period(Duration::from_millis(5))
//! .with_edge_detection(gpiocdev::line::EdgeDetection::BothEdges)
//! .with_debounce_period(std::time::Duration::from_millis(5))
//! .request()?;
//! for edge in req.edge_events() {
//! println!("{:?}", edge);
Expand Down Expand Up @@ -102,8 +98,8 @@ pub fn lines() -> Result<LineIterator> {
/// # Examples
/// The found line can be used to request the line:
/// ```no_run
/// # use gpiocdev::line::Value;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::Value;
/// let led0 = gpiocdev::find_named_line("LED0").unwrap();
/// let req = gpiocdev::Request::builder()
/// .with_found_line(&led0)
Expand All @@ -115,8 +111,8 @@ pub fn lines() -> Result<LineIterator> {
///
/// Using the chip and offset from the found line to request the line:
/// ```no_run
/// # use gpiocdev::line::Value;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::Value;
/// let led0 = gpiocdev::find_named_line("LED0").unwrap();
/// let req = gpiocdev::Request::builder()
/// .on_chip(&led0.chip)
Expand Down Expand Up @@ -146,8 +142,8 @@ pub fn find_named_line(name: &str) -> Option<FoundLine> {
/// # Examples
/// Adding the found lines to the request directly:
/// ```no_run
/// # use gpiocdev::line::Value;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::Value;
/// let sensors = gpiocdev::find_named_lines(&["SENSOR0", "SENSOR1"], true)?;
/// let req = gpiocdev::Request::builder()
/// .with_found_lines(&sensors)
Expand All @@ -162,8 +158,8 @@ pub fn find_named_line(name: &str) -> Option<FoundLine> {
/// Using the individual found lines to request the lines with different
/// configuration for each line:
/// ```no_run
/// # use gpiocdev::line::Value;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::Value;
/// let lines = gpiocdev::find_named_lines(&["SENSOR0", "LED0"], true)?;
/// let sensor0 = lines.get("SENSOR0").unwrap();
/// let led0 = lines.get("LED0").unwrap();
Expand Down Expand Up @@ -208,8 +204,8 @@ pub fn find_named_lines<'a>(
///
/// The discovered line can be used to request the line:
/// ```no_run
/// # use gpiocdev::line::Value;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::Value;
/// let led0 = gpiocdev::find_named_line("LED0").unwrap();
/// let req = gpiocdev::Request::builder()
/// .with_found_line(&led0)
Expand Down Expand Up @@ -240,8 +236,8 @@ impl From<line::Offset> for FoundLine {
///
/// Used by [`find_named_line`] and [`find_named_lines`] to find lines based on line name.
/// ```no_run
/// # use gpiocdev::line::Value;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::Value;
/// // replicating find_named_line...
/// let led2 = gpiocdev::LineIterator::new()?.find(|l| l.info.name == "LED2").unwrap();
/// let req = gpiocdev::Request::builder()
Expand Down
2 changes: 2 additions & 0 deletions lib/src/line/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub enum EdgeKind {
/// Indicates the line transitioned from active to inactive.
Falling = 2,
}

impl From<uapi::LineEdgeEventKind> for EdgeKind {
fn from(kind: uapi::LineEdgeEventKind) -> Self {
match kind {
Expand Down Expand Up @@ -149,6 +150,7 @@ pub enum InfoChangeKind {
/// Line has been reconfigured.
Reconfigured = 3,
}

impl From<uapi::LineInfoChangeKind> for InfoChangeKind {
fn from(kind: uapi::LineInfoChangeKind) -> Self {
match kind {
Expand Down
6 changes: 6 additions & 0 deletions lib/src/line/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum Value {
/// The line is active.
Active,
}

impl Value {
/// The value opposite the current value.
pub fn not(&self) -> Value {
Expand Down Expand Up @@ -58,6 +59,7 @@ impl From<Value> for bool {
}
}
}

impl From<Value> for u8 {
fn from(l: Value) -> u8 {
match l {
Expand All @@ -66,6 +68,7 @@ impl From<Value> for u8 {
}
}
}

impl From<bool> for Value {
fn from(b: bool) -> Value {
match b {
Expand All @@ -74,6 +77,7 @@ impl From<bool> for Value {
}
}
}

impl From<u8> for Value {
fn from(i: u8) -> Value {
match i {
Expand Down Expand Up @@ -373,6 +377,7 @@ impl Values {
true
}
}

impl<'a> FromIterator<&'a Offset> for Values {
fn from_iter<I: IntoIterator<Item = &'a Offset>>(iter: I) -> Self {
let mut values = Values::default();
Expand All @@ -383,6 +388,7 @@ impl<'a> FromIterator<&'a Offset> for Values {
values
}
}

impl FromIterator<(Offset, Value)> for Values {
fn from_iter<I: IntoIterator<Item = (Offset, Value)>>(iter: I) -> Self {
let mut values = Values::default();
Expand Down
18 changes: 10 additions & 8 deletions lib/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ impl Request {
/// # Examples
///
/// ```no_run
/// # use gpiocdev::line::Values;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::Values;
/// let req = gpiocdev::Request::builder()
/// .on_chip("/dev/gpiochip0")
/// .with_lines(&[3,5])
Expand All @@ -121,9 +121,9 @@ impl Request {
/// # Examples
///
/// ```no_run
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::Value;
/// # use gpiocdev::request::Config;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// let mut cfg = Config::default();
/// cfg.with_line(5).as_output(Value::Active);
/// let req = gpiocdev::Request::from_config(cfg)
Expand All @@ -144,8 +144,8 @@ impl Request {
///
/// # Examples
/// ```no_run
/// # use gpiocdev::line::Values;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::Values;
/// let req = gpiocdev::Request::builder()
/// .on_chip("/dev/gpiochip0")
/// .with_lines(&[3,5,6,8])
Expand Down Expand Up @@ -196,8 +196,8 @@ impl Request {
///
/// # Examples
/// ```no_run
/// # use gpiocdev::line::Values;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::Values;
/// let req = gpiocdev::Request::builder()
/// .on_chip("/dev/gpiochip0")
/// .with_lines(&[3,5])
Expand Down Expand Up @@ -250,8 +250,8 @@ impl Request {
/// Set the values for a subset of the requested lines.
/// # Examples
/// ```no_run
/// # use gpiocdev::line::{Value::{Active, Inactive}, Values};
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::{Value::{Active, Inactive}, Values};
/// let req = gpiocdev::Request::builder()
/// .on_chip("/dev/gpiochip0")
/// .with_lines(&[3,5,6,8])
Expand Down Expand Up @@ -306,9 +306,9 @@ impl Request {
///
/// # Examples
/// ```no_run
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::line::Value;
/// # use gpiocdev::request::Config;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// let mut cfg = Config::default();
/// cfg.with_line(5).as_output(Value::Active);
/// let req = gpiocdev::Request::from_config(cfg)
Expand Down Expand Up @@ -481,9 +481,9 @@ impl Request {
///
/// # Examples
/// ```no_run
/// # use gpiocdev::{Request, Result};
/// # fn example() -> Result<(), gpiocdev::Error> {
/// # use gpiocdev::Request;
/// # use gpiocdev::line::{EdgeDetection, Value};
/// # fn example() -> Result<()> {
/// let req = Request::builder()
/// .on_chip("/dev/gpiochip0")
/// .with_consumer("watcher")
Expand Down Expand Up @@ -657,12 +657,14 @@ impl Request {
mem::size_of::<uapi::LineEdgeEvent>()
}
}

impl AsFd for Request {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.f.as_fd()
}
}

impl AsRawFd for Request {
#[inline]
fn as_raw_fd(&self) -> i32 {
Expand Down

0 comments on commit 6a35904

Please sign in to comment.