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
#![warn(rust_2018_idioms)]

#[cfg(not(any(feature = "rp-sys", feature = "mock")))]
compile_error!("Either feature 'rp-sys' (default) or 'mock' must be enabled for this crate.");

use rp_sys as rp;

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

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

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()
        }
    );
}

pub mod calibration;
pub mod fpga;
pub mod led;
pub mod gpio;
pub mod pin;
pub mod acquire;
pub mod generator;
pub mod cmn;

mod result;

pub use result::{Error, Result};

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

/**
 * Initializes the library.
 *
 * It must be called first, before any other library method.
 */
pub fn init_reset(reset: bool) -> Result<()>
{
    handle_unsafe!(
        rp::rp_InitReset(reset)
    )
}

pub fn calib_init() -> Result<()>
{
    handle_unsafe!(
        rp::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<()>
{
    handle_unsafe!(
        rp::rp_Release()
    )
}

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

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

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