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

use embedded_hal::digital;

use futures::prelude::*;

use crate::common::*;
use crate::error::Error;
use super::{Mux, Requester};

#[derive(Clone)]
pub struct Pin {
    device: String,
    mux: Mux,
}

impl Pin {
    pub (crate) fn new(device: String, mux: Mux) -> Self {
        Pin{device, mux}
    }

    fn set(&mut self, value: bool) -> Result<(), Error> {
        let resp = self.mux.do_request(&self.device, RequestKind::PinSet(Value{value})).wait()?;
        match resp {
            ResponseKind::Ok => Ok(()),
             _ => Err(Error::InvalidResponse(resp)),
        }
    }

    fn get(&self) -> Result<bool, Error> {
        let mut mux = self.mux.clone();
        let resp = mux.do_request(&self.device, RequestKind::PinGet).wait()?;
        match resp {
            ResponseKind::PinGet(v) => Ok(v),
             _ => Err(Error::InvalidResponse(resp)),
        }
    }
}

impl Drop for Pin {
    fn drop(&mut self) {
        self.mux.do_request(&self.device, RequestKind::PinDisconnect).wait().unwrap();
    }
}

impl digital::InputPin for Pin {
    //type Error = Error;

    fn is_high(&self) -> bool {
        self.get().unwrap() == true
    }

    fn is_low(&self) -> bool {
        self.get().unwrap() == false
    }
}

impl digital::OutputPin for Pin {
    //type Error = Error;

    fn set_high(&mut self) {
        self.set(true).unwrap();
    }

    fn set_low(&mut self) {
        self.set(false).unwrap();
    }
}