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
//! Adds important functionalities
use sysfs_gpio::Pin;

/// Represents a single device of any type; GPIO-based, SPI-based, I2C-based,
/// etc.  It defines the basic services applicable to all devices
pub trait Device {
    /// Get the pin
    fn pin(&self) -> Pin ;

    /// Shut down the device and release all associated resources.
    fn close(&self){
        let pin = self.pin();
        if pin.is_exported() {
            //TODO implement better error handling
            pin.unexport().expect("Could not close device");
        }
    }

    /// Returns a value representing the device's state.
    fn value(&self) -> i8;


    /// Returns ``True`` if the device is currently active and ``False``otherwise.
    fn is_active(&self) -> bool{
        let value =  self.value();
        if value >= 1 { true } else {false }
    }
}


/// Adds edge-detected `when_activated` and `when_deactivated`
/// events to a device based on changes to the `is_active`
/// property common to all devices.
pub trait EventsTrait:Device {
    /// Pause the program until the device is activated
    fn wait_for_active(&self) {
        
        loop {
            if self.is_active() == true {break;}
        }

    }

    /// Pause the program until the device is deactivated
    fn wait_for_inactive(&self) {
        
        loop {
            if self.is_active() == false {break;}
        }

    }

    
}

/// Represents a device composed of multiple devices like simple HATs,
/// H-bridge motor controllers, robots composed of multiple motors, etc.
pub trait CompositeDevices {
    /// Shut down the device and release all associated resources.
    fn close(&self);

}