Last active
August 29, 2015 14:27
-
-
Save inre/bbb83e2d2109d747f1b6 to your computer and use it in GitHub Desktop.
Digital logic
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
use std::io; | |
#[derive(Debug, Copy, Clone)] | |
pub enum Logic { | |
High, | |
Low | |
} | |
#[derive(Debug, Copy, Clone)] | |
pub enum Logic3 { | |
High, | |
Low, | |
Z | |
} | |
pub trait DigitalLogic { | |
fn logic_level(&self) -> Logic; | |
} | |
impl DigitalLogic for i32 { | |
fn logic_level(&self) -> Logic { | |
match *self { 0 => Logic::Low, _ => Logic::High } | |
} | |
} | |
impl DigitalLogic for Logic { | |
fn logic_level(&self) -> Logic { | |
*self | |
} | |
} | |
pub trait DigitalPinInput { | |
fn input<D: DigitalRead>(&self) -> io::Result<D>; | |
} | |
pub trait DigitalPinOutput { | |
fn output<D: DigitalWrite>(&self) -> io::Result<D>; | |
} | |
pub trait DigitalRead { | |
fn digital_read(&self) -> io::Result<Logic>; | |
fn get(&self) -> io::Result<Logic> { | |
self.digital_read() | |
} | |
} | |
pub trait DigitalWrite { | |
fn digital_write<L: DigitalLogic>(&self, level: L) -> io::Result<()>; | |
fn set<L: DigitalLogic>(&self, level: L) -> io::Result<()> { | |
self.digital_write(level) | |
} | |
fn high(&self) -> io::Result<()> { | |
self.digital_write(Logic::High) | |
} | |
fn low(&self) -> io::Result<()> { | |
self.digital_write(Logic::Low) | |
} | |
} | |
pub trait AnalogRead { | |
fn analog_read(&self) -> i32; | |
} | |
pub trait AnalogWrite { | |
fn analog_write(&self, value: i32); | |
} | |
fn main() { | |
println!("{:?}", 1.logic_level()); | |
println!("{:?}", Logic::Low.logic_level()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment