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
#[allow(unused_must_use)]

extern crate rp_sys;
extern crate libc;

/**
 * Type representing Input/Output channels.
 */
pub use rp_sys::rp_channel_t as Channel;

macro_rules! handle_unsafe {
    ($e:expr) => (
        match unsafe { $e } as u32 {
            rp_sys::RP_OK => Ok(()),
            code => Err($crate::get_error(code as i32)),
        }
    );
}

macro_rules! convert_string {
    ($e:expr) => (
        {
            let buffer = unsafe {
                ::std::ffi::CStr::from_ptr($e)
            };

            ::std::str::from_utf8(buffer.to_bytes())
                .unwrap()
                .to_owned()
        }
    );
}

#[macro_use]
pub mod calibration;
#[macro_use]
pub mod fpga;
#[macro_use]
pub mod led;
#[macro_use]
pub mod gpio;
#[macro_use]
pub mod pin;
#[macro_use]
pub mod acquire;
#[macro_use]
pub mod generator;
#[macro_use]
pub mod cmn;

/**
 * Initializes the library.
 *
 * It must be called first, before any other library method.
 */
pub fn init() -> Result<(), String>
{
    handle_unsafe!(
        rp_sys::rp_Init()
    )
}

pub fn calib_init() -> Result<(), String>
{
    handle_unsafe!(
        rp_sys::rp_CalibInit()
    )
}

/**
 * Releases the library resources.
 *
 * It must be called last, after library is not used anymore. Typically before
 * application exits.
 */
pub fn release() -> Result<(), String>
{
    handle_unsafe!(
        rp_sys::rp_Release()
    )
}

/**
 * Resets all modules.
 *
 * Typically calles after `init()` application exits.
 */
pub fn reset() -> Result<(), String>
{
    handle_unsafe!(
        rp_sys::rp_Reset()
    )
}

/**
 * Retrieves the library version number
 */
pub fn get_version() -> String
{
    convert_string!(
        rp_sys::rp_GetVersion()
    )
}

/**
 * Returns textual representation of error code.
 */
fn get_error(code: i32) -> String
{
    convert_string!(
        rp_sys::rp_GetError(code)
    )
}

/**
 * Enable or disables digital loop.
 *
 * This internally connect output to input.
 */
pub fn enable_digital_loop(enable: bool) -> Result<(), String>
{
    handle_unsafe!(
        rp_sys::rp_EnableDigitalLoop(enable)
    )
}