Skip to content

Instantly share code, notes, and snippets.

@inre
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inre/bbb83e2d2109d747f1b6 to your computer and use it in GitHub Desktop.
Save inre/bbb83e2d2109d747f1b6 to your computer and use it in GitHub Desktop.
Digital logic
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