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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use inline_python::{ Context, python };

/// A stand-alone function used to turn on a specified GPIO pin on the Raspberry PI.
/// Once a pin is turned on, it starts emitting an electrical current.
/// If you attempt to turn on a pin that is already on then nothing will happen.
/// 
/// This function takes an unsigned 8-bit integer as an argument. The argument is used to specify
/// which pin on the PI to turn on.
///
/// Returns a Result type which is either empty or contains a String datatype. The String may contain
/// a an error message to aid in debugging.
///
/// Example:
/// ```
/// match turn_on(1) {
///     Ok(()) => {
///         println!("The pin is now on!");
///     },
///     Err(msg) => {
///         println!("The pin could not be turn on: {}", msg);
///     }
/// };
/// ```
pub fn turn_on(pin: u8) -> Result<(), String> {
    let context: Context = python! {
        try:
            import RPi.GPIO as GPIO

            GPIO.setmode(GPIO.BCM)
            GPIO.setup('pin, GPIO.OUT)
            GPIO.output('pin, GPIO.HIGH)

            success = True
            msg = ""
        except Exception as e:
            msg = str(e)
            success = False
    };

    let success: bool = context.get::<bool>("success");
    let msg: String = context.get::<String>("msg");

    if success {
        Result::Ok(())
    } else {
        Result::Err(msg.to_string())
    }
}

/// A stand-alone function used to turn off a specified GPIO pin that is already on.
/// Once a pin is turned off, it will stop emitting any electrical current.
/// If you attempt to turn off a pin that is already off then nothing will happen.
/// 
/// This function takes an unsigned 8-bit integer as an argument. The argument is used to specify
/// which pin on the PI to turn off.
///
/// Returns a Result type which is either empty or contains a String datatype. The String may contain
/// a an error message to aid in debugging.
///
/// Example:
/// ```
/// match turn_off(1) {
///     Ok(()) => {
///         println!("The pin is now off!");
///     },
///     Err(msg) => {
///         println!("The pin could not be turn off: {}", msg);
///     }
/// };
/// ```
pub fn turn_off(pin: u8) -> Result<(), String> {
    let context: Context = python! {
        try:
            import RPi.GPIO as GPIO

            GPIO.setmode(GPIO.BCM)
            GPIO.setup('pin, GPIO.OUT)
            GPIO.output('pin, GPIO.LOW)

            success = True
            msg = ""
        except Exception as e:
            msg = str(e)
            success = False
    };

    let success: bool = context.get::<bool>("success");
    let msg: String = context.get::<String>("msg");

    if success {
        Result::Ok(())
    } else {
        Result::Err(msg.to_string())
    }
}

/// A stand-alone function used to cleanup the Raspberry PI. When a PI is cleaned up, all allocated resources will be freed up.
/// It is important to turn off and cleanup any pins that are not being currently used. This will help to avoid shorting out
/// you PI.
/// 
/// Returns a Result type which is either empty or contains a String datatype. The String may contain
/// a an error message to aid in debugging.
///
/// Example:
/// ```
/// match cleanup() {
///     Ok(()) => {
///         println!("The cleanup has been run!");
///     },
///     Err(msg) => {
///         println!("The cleanup could not be run: {}", msg);
///     }
/// };
/// ```
pub fn cleanup() -> Result<(), String> {
    let context: Context = python! {
        try:
            import RPi.GPIO as GPIO

            GPIO.cleanup()

            success = True
            msg = ""
        except Exception as e:
            msg = str(e)
            success = False
    };

    let success: bool = context.get::<bool>("success");
    let msg: String = context.get::<String>("msg");

    if success {
        Result::Ok(())
    } else {
        Result::Err(msg.to_string())
    }
}