loco_control/
loco_control.rs

1use roco_z21_driver::{Loco, Z21Station};
2use std::sync::Arc;
3use tokio;
4
5#[tokio::main]
6async fn main() -> std::io::Result<()> {
7    let station = Arc::new(Z21Station::new("192.168.0.111:21105").await?);
8
9    // Control a locomotive with address 3
10    let loco = Loco::control(station.clone(), 4).await?;
11
12    // Subscribe to locomotive state changes
13    loco.subscribe_loco_state(Box::new(|state| {
14        println!(
15            "Locomotive speed: {}%",
16            state.speed_percentage.unwrap_or(0.)
17        );
18    }));
19
20    // Turn on the headlights
21    loco.set_headlights(true).await?;
22
23    // Set speed to 50% forward
24    loco.drive(50.0).await?;
25
26    // Wait for 5 seconds
27    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
28
29    // Gradually stop
30    loco.stop().await?;
31
32    Ok(())
33}