Skip to content
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

api: initial gpio apis for review #1

Merged
merged 3 commits into from
Feb 3, 2017
Merged
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
72 changes: 70 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,71 @@
#[test]
fn it_works() {
// Copyright 2015, Paul Osborne <[email protected]>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Generic Interface for GPIOs Across Many Systems
//!
//! This crate provides a set of core traits and related types
//! for accessing GPIOs across a variety of systems. The goal
//! is that devices may be written depending on these traits
//! and consequently be portable to various systems.

#![crate_type = "lib"]
#![crate_name = "gpio"]

// until core is stabilized, we need some hacks to support
// both nightly and stable
#![cfg_attr(nightly, feature(no_std))]
#![cfg_attr(nightly, no_std)]

#[cfg(any(not(nightly), test))]
#[macro_use] extern crate std as core;

/// Indication that an error occurred with a GPIO operation
#[derive(Copy, Clone)]
pub enum Error {
/// The GPIO was configured as an input but an output action was attempted
IllegalOperationOnInput,
/// The GPIO was configured as an output but an input action was attempted
IllegalOperationOnOutput,
/// Some other error occurred
Other(&'static str),
}

/// GPIO Result
pub type Result<T> = core::result::Result<T, Error>;

/// The direction of a GPIO Pin
#[derive(Copy, Clone)]
pub enum Direction {
/// The pin is an input
In,
/// The pin is an output
Out,
}

/// The logic level of a GPIO Pin
#[derive(Copy, Clone, PartialEq)]
pub enum Level {
/// Logic low
Low,
/// Logic high
High,
}

/// Generic trait providing access to a single GPIO
pub trait Gpio {
/// Get the currently configured direciton of this GPIO
fn get_direction(&self) -> Result<Direction>;
/// Set the pin to be an input
fn set_input(&mut self) -> Result<()>;
/// Set the pin to be an output with the provided level
fn set_output(&mut self, value: Level) -> Result<()>;
/// Get the current level of this GPIO (input only)
fn get_level(&self) -> Result<Level>;
/// Set the current level of this GPIO (output only)
fn set_level(&mut self, value: Level) -> Result<()>;
}