1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
use std; use std::fmt::{Display, Formatter}; use {Result, Error}; #[derive(Debug, Copy, Clone)] pub enum Logic { High, Low } impl Into<usize> for Logic { fn into(self) -> usize { match self { Logic::High => 1, Logic::Low => 0 } } } impl Display for Logic { fn fmt(&self, f: &mut Formatter) -> std::result::Result<(), std::fmt::Error> { let v: usize = (*self).into(); write!(f, "{}", v) } } #[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 u32 { fn logic_level(&self) -> Logic { match *self { 0 => Logic::Low, _ => Logic::High } } } impl DigitalLogic for isize { fn logic_level(&self) -> Logic { match *self { 0 => Logic::Low, _ => Logic::High } } } impl DigitalLogic for usize { fn logic_level(&self) -> Logic { match *self { 0 => Logic::Low, _ => Logic::High } } } impl DigitalLogic for Logic { fn logic_level(&self) -> Logic { *self } } pub trait DigitalRead { fn digital_read(&mut self) -> Result<Logic>; fn get(&mut self) -> Result<Logic> { self.digital_read() } fn is_high(&mut self) -> Result<bool> { match try!(self.digital_read()) { Logic::High => Ok(true), Logic::Low => Ok(false) } } fn is_low(&mut self) -> Result<bool> { match try!(self.digital_read()) { Logic::High => Ok(false), Logic::Low => Ok(true) } } } pub trait DigitalWrite { fn digital_write<L: DigitalLogic>(&mut self, level: L) -> Result<()>; fn set<L: DigitalLogic>(&mut self, level: L) -> Result<()> { self.digital_write(level) } fn high(&mut self) -> Result<()> { self.digital_write(Logic::High) } fn low(&mut self) -> Result<()> { self.digital_write(Logic::Low) } } pub trait AnalogRead { fn analog_read(&self) -> usize; } pub trait AnalogWrite { fn analog_write(&self, value: usize); }