Skip to main content

smoke/
smoke.rs

1// Development smoke test exercising every hand-written subsystem end-to-end.
2
3use opendaq::{
4    BatchedPropertyUpdate, Channel, FunctionObject, Instance, StreamReader, TickConverter, Value,
5};
6
7fn main() -> opendaq::Result<()> {
8    let instance = Instance::new()?;
9
10    println!("Available devices:");
11    for info in instance.available_devices()? {
12        println!(" - {}: {}", info.connection_string()?, info.name()?);
13    }
14
15    let device = instance.add_device("daqref://device0")?.expect("device");
16    println!("Added device: {}", device.name()?);
17
18    let channel = instance
19        .find_component("Dev/RefDev0/IO/AI/RefCh0")?
20        .expect("reference channel not found")
21        .cast::<Channel>()?;
22    let signal = &channel.signals()?[0];
23
24    // Batched property updates.
25    {
26        let batch = BatchedPropertyUpdate::new(&[&device, &channel])?;
27        device.set_property_value("GlobalSampleRate", 1000)?;
28        channel.set_property_value("Frequency", 50.0)?;
29        batch.commit()?;
30    }
31    println!(
32        "GlobalSampleRate = {}",
33        device.property_value("GlobalSampleRate")?
34    );
35
36    // Stream reading with domain + tick conversion.
37    let reader = StreamReader::<f64>::new(signal)?;
38    let (samples, ticks) = reader.read_with_domain(100, 2000)?;
39    println!(
40        "read {} samples, {} ticks",
41        samples.sample_count(),
42        ticks.len()
43    );
44    let converter = TickConverter::from_signal(signal)?;
45    if let Some(first) = ticks.first() {
46        println!(
47            "first tick {} -> {:?}",
48            first,
49            converter.tick_to_system_time(*first)
50        );
51    }
52
53    // Callables: a Rust closure as an openDAQ function, called through openDAQ.
54    let sum = FunctionObject::from_fn(|args| {
55        let a = args[0].as_i64().unwrap_or(0);
56        let b = args[1].as_i64().unwrap_or(0);
57        Ok(Value::from(a + b))
58    })?;
59    let result = sum.call(&[Value::from(20), Value::from(22)])?;
60    println!("sum(20, 22) = {result}");
61
62    // Events: subscribe to property changes on the channel.
63    let events = channel
64        .on_property_value_write("Amplitude")?
65        .expect("event");
66    let handler = events.subscribe(|_sender, args| {
67        if let Some(args) = args {
68            println!("  event fired: {args}");
69        }
70    })?;
71    channel.set_property_value("Amplitude", 2.5)?;
72    events.unsubscribe(&handler)?;
73    channel.set_property_value("Amplitude", 1.0)?;
74    println!("Amplitude = {}", channel.property_value("Amplitude")?);
75
76    Ok(())
77}