diff --git a/src/lib.rs b/src/lib.rs index a93251b..a8e032d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,71 @@ -#[test] -fn it_works() { +// Copyright 2015, Paul Osborne +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , 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 = core::result::Result; + +/// 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; + /// 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; + /// Set the current level of this GPIO (output only) + fn set_level(&mut self, value: Level) -> Result<()>; }