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 occurred.

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.

Basic Functions

pub fn IndustrialDualAnalogInBricklet::new(uid: &str, ip_connection: &IpConnection) → IndustrialDualAnalogInBricklet
Parameters:
  • uid – Type: &str
  • ip_connection – Type: &IPConnection
Returns:
  • industrial_dual_analog_in – Type: IndustrialDualAnalogInBricklet

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

let industrial_dual_analog_in = IndustrialDualAnalogInBricklet::new("YOUR_DEVICE_UID", &ip_connection);

This device object can be used after the IP connection has been connected.

pub fn IndustrialDualAnalogInBricklet::get_voltage(&self, channel: u8) → ConvertingReceiver<i32>
Parameters:
  • channel – Type: u8, Range: [0 to 1]
Returns:
  • voltage – Type: i32, Unit: 1 mV, Range: [-35000 to 35000]

Returns the voltage for the given channel.

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<()>
Parameters:
  • rate – Type: u8, Range: See constants, Default: 6

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 following constants are available for this function:

For rate:

  • 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:
  • rate – Type: u8, Range: See constants, Default: 6

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

The following constants are available for this function:

For rate:

  • 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<()>
Parameters:
  • offset – Type: [i32; 2], Range: [-223 to 223 - 1]
  • gain – Type: [i32; 2], Range: [-223 to 223 - 1]

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>
Return Object:
  • offset – Type: [i32; 2], Range: [-223 to 223 - 1]
  • gain – Type: [i32; 2], Range: [-223 to 223 - 1]

Returns the calibration as set by IndustrialDualAnalogInBricklet::set_calibration.

pub fn IndustrialDualAnalogInBricklet::get_adc_values(&self) → ConvertingReceiver<[i32; 2]>
Returns:
  • value – Type: [i32; 2], Range: [-223 to 223 - 1]

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_identity(&self) → ConvertingReceiver<Identity>
Return Object:
  • uid – Type: String, Length: up to 8
  • connected_uid – Type: String, Length: up to 8
  • position – Type: char, Range: ['a' to 'h', 'z']
  • hardware_version – Type: [u8; 3]
    • 0: major – Type: u8, Range: [0 to 255]
    • 1: minor – Type: u8, Range: [0 to 255]
    • 2: revision – Type: u8, Range: [0 to 255]
  • firmware_version – Type: [u8; 3]
    • 0: major – Type: u8, Range: [0 to 255]
    • 1: minor – Type: u8, Range: [0 to 255]
    • 2: revision – Type: u8, Range: [0 to 255]
  • device_identifier – Type: u16, Range: [0 to 216 - 1]

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', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an Isolator Bricklet is always at position 'z'.

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<()>
Parameters:
  • channel – Type: u8, Range: [0 to 1]
  • period – Type: u32, Unit: 1 ms, Range: [0 to 232 - 1], Default: 0

Sets the period 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.

pub fn IndustrialDualAnalogInBricklet::get_voltage_callback_period(&self, channel: u8) → ConvertingReceiver<u32>
Parameters:
  • channel – Type: u8, Range: [0 to 1]
Returns:
  • period – Type: u32, Unit: 1 ms, Range: [0 to 232 - 1], Default: 0

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<()>
Parameters:
  • channel – Type: u8, Range: [0 to 1]
  • option – Type: char, Range: See constants, Default: 'x'
  • min – Type: i32, Unit: 1 mV, Range: [-231 to 231 - 1], Default: 0
  • max – Type: i32, Unit: 1 mV, Range: [-231 to 231 - 1], Default: 0

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 following constants are available for this function:

For option:

  • 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>
Parameters:
  • channel – Type: u8, Range: [0 to 1]
Return Object:
  • option – Type: char, Range: See constants, Default: 'x'
  • min – Type: i32, Unit: 1 mV, Range: [-231 to 231 - 1], Default: 0
  • max – Type: i32, Unit: 1 mV, Range: [-231 to 231 - 1], Default: 0

Returns the threshold as set by IndustrialDualAnalogInBricklet::set_voltage_callback_threshold.

The following constants are available for this function:

For option:

  • 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<()>
Parameters:
  • debounce – Type: u32, Unit: 1 ms, Range: [0 to 232 - 1], Default: 100

Sets the period with which the threshold callback

is triggered, if the threshold

keeps being reached.

pub fn IndustrialDualAnalogInBricklet::get_debounce_period(&self) → ConvertingReceiver<u32>
Returns:
  • debounce – Type: u32, Unit: 1 ms, Range: [0 to 232 - 1], Default: 100

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>
Event Object:
  • channel – Type: u8, Range: [0 to 1]
  • voltage – Type: i32, Unit: 1 mV, Range: [-35000 to 35000]

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>
Event Object:
  • channel – Type: u8, Range: [0 to 1]
  • voltage – Type: i32, Unit: 1 mV, Range: [-35000 to 35000]

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.

Virtual Functions

Virtual functions don't communicate with the device itself, but operate only on the API bindings device object. They can be called without the corresponding IP Connection object being connected.

pub fn IndustrialDualAnalogInBricklet::get_api_version(&self) → [u8; 3]
Return Object:
  • api_version – Type: [u8; 3]
    • 0: major – Type: u8, Range: [0 to 255]
    • 1: minor – Type: u8, Range: [0 to 255]
    • 2: revision – Type: u8, Range: [0 to 255]

Returns the version of the API definition 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
Parameters:
  • function_id – Type: u8, Range: See constants
Returns:
  • response_expected – Type: 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 sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For function_id:

  • 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(&mut self, function_id: u8, response_expected: bool) → ()
Parameters:
  • function_id – Type: u8, Range: See constants
  • response_expected – Type: 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 sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For function_id:

  • 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) → ()
Parameters:
  • response_expected – Type: bool

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

Constants

pub const 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.

pub const IndustrialDualAnalogInBricklet::DEVICE_DISPLAY_NAME

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