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
#![no_std]

extern crate embedded_hal;
extern crate libc;

mod bindings;
use bindings::*;

pub struct InputPin(u8);
pub struct OutputPin(u8);

impl InputPin {
    pub fn new(pin: u8) -> Self {
        InputPin::set_pin_mode(pin);
        InputPin::with_pin_mode_already_set(pin)
    }

    pub fn with_pin_mode_already_set(pin: u8) -> Self {
        InputPin(pin)
    }

    pub fn to_output(self) -> OutputPin {
        OutputPin::new(self.0)
    }

    fn set_pin_mode(pin: u8) {
        unsafe {
            pinMode(pin, INPUT as u8);
        }
    }
}

impl OutputPin {
    pub fn new(pin: u8) -> Self {
        OutputPin::set_pin_mode(pin);
        OutputPin::with_pin_mode_already_set(pin)
    }

    pub fn with_pin_mode_already_set(pin: u8) -> Self {
        OutputPin(pin)
    }

    pub fn to_input(self) -> InputPin {
        InputPin::new(self.0)
    }

    fn set_pin_mode(pin: u8) {
        unsafe {
            pinMode(pin, OUTPUT as u8);
        }
    }
}

impl embedded_hal::digital::InputPin for InputPin {
    fn is_low(&self) -> bool {
        let result;
        unsafe {
            result = digitalRead(self.0) as u8;
        }
        result == (LOW as u8)
    }
    fn is_high(&self) -> bool {
        let result;
        unsafe {
            result = digitalRead(self.0) as u8;
        }
        result == (HIGH as u8)
    }
}

impl embedded_hal::digital::OutputPin for OutputPin {
    fn set_low(&mut self) {
        unsafe {
            digitalWrite(self.0, LOW as u8);
        }
    }
    fn set_high(&mut self) {
        unsafe {
            digitalWrite(self.0, HIGH as u8);
        }
    }
}

impl embedded_hal::digital::StatefulOutputPin for OutputPin {
    fn is_set_low(&self) -> bool {
        let result;
        unsafe {
            result = digitalRead(self.0) as u8;
        }
        result == (LOW as u8)
    }
    fn is_set_high(&self) -> bool {
        let result;
        unsafe {
            result = digitalRead(self.0) as u8;
        }
        result == (HIGH as u8)
    }
}

impl embedded_hal::digital::toggleable::Default for OutputPin {}