Rust - Industrial Dual Analog In Bricklet

This is the description of the Rust API bindings for the Industrial Dual Analog In Bricklet. General information and technical specifications for the Industrial Dual Analog In Bricklet are summarized in its hardware description.

An installation guide for the Rust API bindings is part of their general description. Additional documentation can be found on docs.rs.

Examples

The example code below is Public Domain (CC0 1.0).

Simple

Download (example_simple.rs)

 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
use std::{error::Error, io};

use tinkerforge::{industrial_dual_analog_in_bricklet::*, ip_connection::IpConnection};

const HOST: &str = "localhost";
const PORT: u16 = 4223;
const UID: &str = "XYZ"; // Change XYZ to the UID of your Industrial Dual Analog In Bricklet.

fn main() -> Result<(), Box<dyn Error>> {
    let ipcon = IpConnection::new(); // Create IP connection.
    let idai = IndustrialDualAnalogInBricklet::new(UID, &ipcon); // Create device object.

    ipcon.connect((HOST, PORT)).recv()??; // Connect to brickd.
                                          // Don't use device before ipcon is connected.

    // Get current voltage from channel 1.
    let voltage = idai.get_voltage(1).recv()?;
    println!("Voltage (Channel 1): {} V", voltage as f32 / 1000.0);

    println!("Press enter to exit.");
    let mut _input = String::new();
    io::stdin().read_line(&mut _input)?;
    ipcon.disconnect();
    Ok(())
}

Callback

Download (example_callback.rs)

 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
use std::{error::Error, io, thread};
use tinkerforge::{industrial_dual_analog_in_bricklet::*, ip_connection::IpConnection};

const HOST: &str = "localhost";
const PORT: u16 = 4223;
const UID: &str = "XYZ"; // Change XYZ to the UID of your Industrial Dual Analog In Bricklet.

fn main() -> Result<(), Box<dyn Error>> {
    let ipcon = IpConnection::new(); // Create IP connection.
    let idai = IndustrialDualAnalogInBricklet::new(UID, &ipcon); // Create device object.

    ipcon.connect((HOST, PORT)).recv()??; // Connect to brickd.
                                          // Don't use device before ipcon is connected.

    let voltage_receiver = idai.get_voltage_callback_receiver();

    // Spawn thread to handle received callback messages.
    // This thread ends when the `idai` object
    // is dropped, so there is no need for manual cleanup.
    thread::spawn(move || {
        for voltage in voltage_receiver {
            println!("Channel: {}", voltage.channel);
            println!("Voltage: {} V", voltage.voltage as f32 / 1000.0);
            println!();
        }
    });

    // Set period for voltage (channel 1) receiver to 1s (1000ms).
    // Note: The voltage (channel 1) callback is only called every second
    //       if the voltage (channel 1) has changed since the last call!
    idai.set_voltage_callback_period(1, 1000);

    println!("Press enter to exit.");
    let mut _input = String::new();
    io::stdin().read_line(&mut _input)?;
    ipcon.disconnect();
    Ok(())
}

Threshold

Download (example_threshold.rs)

 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
use std::{error::Error, io, thread};
use tinkerforge::{industrial_dual_analog_in_bricklet::*, ip_connection::IpConnection};

const HOST: &str = "localhost";
const PORT: u16 = 4223;
const UID: &str = "XYZ"; // Change XYZ to the UID of your Industrial Dual Analog In Bricklet.

fn main() -> Result<(), Box<dyn Error>> {
    let ipcon = IpConnection::new(); // Create IP connection.
    let idai = IndustrialDualAnalogInBricklet::new(UID, &ipcon); // Create device object.

    ipcon.connect((HOST, PORT)).recv()??; // Connect to brickd.
                                          // Don't use device before ipcon is connected.

    // Get threshold receivers with a debounce time of 10 seconds (10000ms).
    idai.set_debounce_period(10000);

    let voltage_reached_receiver = idai.get_voltage_reached_callback_receiver();

    // Spawn thread to handle received callback messages.
    // This thread ends when the `idai` object
    // is dropped, so there is no need for manual cleanup.
    thread::spawn(move || {
        for voltage_reached in voltage_reached_receiver {
            println!("Channel: {}", voltage_reached.channel);
            println!("Voltage: {} V", voltage_reached.voltage as f32 / 1000.0);
            println!();
        }
    });

    // Configure threshold for voltage (channel 1) "greater than 10 V".
    idai.set_voltage_callback_threshold(1, '>', 10 * 1000, 0);

    println!("Press enter to exit.");
    let mut _input = String::new();
    io::stdin().read_line(&mut _input)?;
    ipcon.disconnect();
    Ok(())
}

API

To allow non-blocking usage, nearly every function of the Rust bindings returns a wrapper around a mpsc::Receiver. To block until the function has finished and get your result, call one of the receiver's recv variants. Those return either the result sent by the device, or any error occured.

Functions returning a result directly will block until the device has finished processing the request.

All functions listed below are thread-safe, those which return a receiver are lock-free.

The Bricklet has two input channel. Functions that are related directly to a channel have a channel parameter to specify one of the two channels. Valid values for the channel parameter are 0 and 1.

Basic Functions

pub fn IndustrialDualAnalogInBricklet::new(uid: &str, ip_connection: &IpConnection) → IndustrialDualAnalogInBricklet

Creates a new IndustrialDualAnalogInBricklet object with the unique device ID uid and adds it to the IPConnection ipcon:

This device object can be used after the IP connection has been connected (see examples above).

pub fn IndustrialDualAnalogInBricklet::get_voltage(&self, channel: u8) → ConvertingReceiver<i32>

Returns the voltage for the given channel in mV.

If you want to get the voltage periodically, it is recommended to use the IndustrialDualAnalogInBricklet::get_voltage_callback_receiver callback and set the period with IndustrialDualAnalogInBricklet::set_voltage_callback_period.

Advanced Functions

pub fn IndustrialDualAnalogInBricklet::set_sample_rate(&self, rate: u8) → ConvertingReceiver<()>

Sets the sample rate. The sample rate can be between 1 sample per second and 976 samples per second. Decreasing the sample rate will also decrease the noise on the data.

The default value is 6 (2 samples per second).

The following constants are available for this function:

  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_976_SPS = 0
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_488_SPS = 1
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_244_SPS = 2
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_122_SPS = 3
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_61_SPS = 4
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_4_SPS = 5
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_2_SPS = 6
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_1_SPS = 7
pub fn IndustrialDualAnalogInBricklet::get_sample_rate(&self) → ConvertingReceiver<u8>

Returns the sample rate as set by IndustrialDualAnalogInBricklet::set_sample_rate.

The following constants are available for this function:

  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_976_SPS = 0
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_488_SPS = 1
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_244_SPS = 2
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_122_SPS = 3
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_61_SPS = 4
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_4_SPS = 5
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_2_SPS = 6
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_1_SPS = 7
pub fn IndustrialDualAnalogInBricklet::set_calibration(&self, offset: [i32; 2], gain: [i32; 2]) → ConvertingReceiver<()>

Sets offset and gain of MCP3911 internal calibration registers.

See MCP3911 datasheet 7.7 and 7.8. The Industrial Dual Analog In Bricklet is already factory calibrated by Tinkerforge. It should not be necessary for you to use this function

pub fn IndustrialDualAnalogInBricklet::get_calibration(&self) → ConvertingReceiver<Calibration>

Returns the calibration as set by IndustrialDualAnalogInBricklet::set_calibration.

pub fn IndustrialDualAnalogInBricklet::get_adc_values(&self) → ConvertingReceiver<[i32; 2]>

Returns the ADC values as given by the MCP3911 IC. This function is needed for proper calibration, see IndustrialDualAnalogInBricklet::set_calibration.

pub fn IndustrialDualAnalogInBricklet::get_api_version(&self) → [u8; 3]

Returns the version of the API definition (major, minor, revision) implemented by this API bindings. This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.

pub fn IndustrialDualAnalogInBricklet::get_response_expected(&mut self, function_id: u8) → bool

Returns the response expected flag for the function specified by the function ID parameter. It is true if the function is expected to send a response, false otherwise.

For getter functions this is enabled by default and cannot be disabled, because those functions will always send a response. For callback configuration functions it is enabled by default too, but can be disabled by IndustrialDualAnalogInBricklet::set_response_expected. For setter functions it is disabled by default and can be enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is send and errors are silently ignored, because they cannot be detected.

See IndustrialDualAnalogInBricklet::set_response_expected for the list of function ID constants available for this function.

pub fn IndustrialDualAnalogInBricklet::set_response_expected(&mut self, function_id: u8, response_expected: bool) → ()

Changes the response expected flag of the function specified by the function ID parameter. This flag can only be changed for setter (default value: false) and callback configuration functions (default value: true). For getter functions it is always enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is send and errors are silently ignored, because they cannot be detected.

The following function ID constants are available for this function:

  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_FUNCTION_SET_VOLTAGE_CALLBACK_PERIOD = 2
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_FUNCTION_SET_VOLTAGE_CALLBACK_THRESHOLD = 4
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_FUNCTION_SET_DEBOUNCE_PERIOD = 6
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_FUNCTION_SET_SAMPLE_RATE = 8
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_FUNCTION_SET_CALIBRATION = 10
pub fn IndustrialDualAnalogInBricklet::set_response_expected_all(&mut self, response_expected: bool) → ()

Changes the response expected flag for all setter and callback configuration functions of this device at once.

pub fn IndustrialDualAnalogInBricklet::get_identity(&self) → ConvertingReceiver<Identity>

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be 'a', 'b', 'c' or 'd'.

The device identifier numbers can be found here. There is also a constant for the device identifier of this Bricklet.

Callback Configuration Functions

pub fn IndustrialDualAnalogInBricklet::set_voltage_callback_period(&self, channel: u8, period: u32) → ConvertingReceiver<()>

Sets the period in ms with which the IndustrialDualAnalogInBricklet::get_voltage_callback_receiver callback is triggered periodically for the given channel. A value of 0 turns the callback off.

The IndustrialDualAnalogInBricklet::get_voltage_callback_receiver callback is only triggered if the voltage has changed since the last triggering.

The default value is 0.

pub fn IndustrialDualAnalogInBricklet::get_voltage_callback_period(&self, channel: u8) → ConvertingReceiver<u32>

Returns the period as set by IndustrialDualAnalogInBricklet::set_voltage_callback_period.

pub fn IndustrialDualAnalogInBricklet::set_voltage_callback_threshold(&self, channel: u8, option: char, min: i32, max: i32) → ConvertingReceiver<()>

Sets the thresholds for the IndustrialDualAnalogInBricklet::get_voltage_reached_callback_receiver callback for the given channel.

The following options are possible:

Option Description
'x' Callback is turned off
'o' Callback is triggered when the voltage is outside the min and max values
'i' Callback is triggered when the voltage is inside the min and max values
'<' Callback is triggered when the voltage is smaller than the min value (max is ignored)
'>' Callback is triggered when the voltage is greater than the min value (max is ignored)

The default value is ('x', 0, 0).

The following constants are available for this function:

  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_OFF = 'x'
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_OUTSIDE = 'o'
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_INSIDE = 'i'
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_SMALLER = '<'
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_GREATER = '>'
pub fn IndustrialDualAnalogInBricklet::get_voltage_callback_threshold(&self, channel: u8) → ConvertingReceiver<VoltageCallbackThreshold>

Returns the threshold as set by IndustrialDualAnalogInBricklet::set_voltage_callback_threshold.

The following constants are available for this function:

  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_OFF = 'x'
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_OUTSIDE = 'o'
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_INSIDE = 'i'
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_SMALLER = '<'
  • INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_GREATER = '>'
pub fn IndustrialDualAnalogInBricklet::set_debounce_period(&self, debounce: u32) → ConvertingReceiver<()>

Sets the period in ms with which the threshold callback

is triggered, if the threshold

keeps being reached.

The default value is 100.

pub fn IndustrialDualAnalogInBricklet::get_debounce_period(&self) → ConvertingReceiver<u32>

Returns the debounce period as set by IndustrialDualAnalogInBricklet::set_debounce_period.

Callbacks

Callbacks can be registered to receive time critical or recurring data from the device. The registration is done with the corresponding get_*_callback_receiver function, which returns a receiver for callback events.

Note

Using callbacks for recurring events is always preferred compared to using getters. It will use less USB bandwidth and the latency will be a lot better, since there is no round trip time.

pub fn IndustrialDualAnalogInBricklet::get_voltage_callback_receiver(&self) → ConvertingCallbackReceiver<VoltageEvent>

Receivers created with this function receive Voltage events.

This callback is triggered periodically with the period that is set by IndustrialDualAnalogInBricklet::set_voltage_callback_period. The received variable is the voltage of the channel.

The IndustrialDualAnalogInBricklet::get_voltage_callback_receiver callback is only triggered if the voltage has changed since the last triggering.

pub fn IndustrialDualAnalogInBricklet::get_voltage_reached_callback_receiver(&self) → ConvertingCallbackReceiver<VoltageReachedEvent>

Receivers created with this function receive Voltage Reached events.

This callback is triggered when the threshold as set by IndustrialDualAnalogInBricklet::set_voltage_callback_threshold is reached. The received variable is the voltage of the channel.

If the threshold keeps being reached, the callback is triggered periodically with the period as set by IndustrialDualAnalogInBricklet::set_debounce_period.

Constants

IndustrialDualAnalogInBricklet::DEVICE_IDENTIFIER

This constant is used to identify a Industrial Dual Analog In Bricklet.

The IndustrialDualAnalogInBricklet::get_identity function and the IpConnection::get_enumerate_callback_receiver callback of the IP Connection have a device_identifier parameter to specify the Brick's or Bricklet's type.

IndustrialDualAnalogInBricklet::DEVICE_DISPLAY_NAME

This constant represents the human readable name of a Industrial Dual Analog In Bricklet.