Skip to content

Implement pull-up and pull-down bias flags #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ bitflags! {
const ACTIVE_LOW = (1 << 2);
const OPEN_DRAIN = (1 << 3);
const OPEN_SOURCE = (1 << 4);
const BIAS_PULL_UP = (1 << 5);
const BIAS_PULL_DOWN = (1 << 6);
const BIAS_DISABLE = (1 << 7);
}
}

Expand Down Expand Up @@ -385,6 +388,9 @@ bitflags! {
const ACTIVE_LOW = (1 << 2);
const OPEN_DRAIN = (1 << 3);
const OPEN_SOURCE = (1 << 4);
const BIAS_PULL_UP = (1 << 5);
const BIAS_PULL_DOWN = (1 << 6);
const BIAS_DISABLE = (1 << 7);
}
}

Expand All @@ -395,6 +401,14 @@ pub enum LineDirection {
Out,
}

/// How the line is biased
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LineBias {
PullUp,
PullDown,
Disabled,
}

unsafe fn cstrbuf_to_string(buf: &[libc::c_char]) -> Option<String> {
if buf[0] == 0 {
None
Expand Down Expand Up @@ -608,6 +622,23 @@ impl LineInfo {
}
}

/// Get how this line is biased
///
/// Some is returned when only one bias flag is present.
/// If more than one flag or no flags are specified, None is returned.
pub fn bias(&self) -> Option<LineBias> {
let up = self.flags.contains(LineFlags::BIAS_PULL_UP);
let down = self.flags.contains(LineFlags::BIAS_PULL_DOWN);
let disabled = self.flags.contains(LineFlags::BIAS_DISABLE);

match (up, down, disabled) {
(true, false, false) => Some(LineBias::PullUp),
(false, true, false) => Some(LineBias::PullDown),
(false, false, true) => Some(LineBias::Disabled),
_ => None,
}
}

/// True if the any flags for the device are set (input or output)
pub fn is_used(&self) -> bool {
!self.flags.is_empty()
Expand Down Expand Up @@ -637,6 +668,21 @@ impl LineInfo {
pub fn is_open_source(&self) -> bool {
self.flags.contains(LineFlags::OPEN_SOURCE)
}

// True if the line is marked as having a pull-up bias
pub fn is_bias_pull_up(&self) -> bool {
self.flags.contains(LineFlags::BIAS_PULL_UP)
}

// True if the line is marked as having a pull-down bias
pub fn is_bias_pull_down(&self) -> bool {
self.flags.contains(LineFlags::BIAS_PULL_DOWN)
}

// True if the line is marked as having a disabled bias
pub fn is_bias_disabled(&self) -> bool {
self.flags.contains(LineFlags::BIAS_DISABLE)
}
}

/// Handle for interacting with a "requested" line
Expand Down
Loading