simple/
simple.rs

1//! Simple example of using remote_xy library
2//! Just run this example and add a new Ethernet device in the RemoteXY app (port 6377)
3//! Changing the slider and switch on the app will change circular bar and LED.
4
5use anyhow::Result;
6use remote_xy::remote_xy;
7use remote_xy::RemoteXY;
8use serde::Deserialize;
9use serde::Serialize;
10use std::time::Duration;
11
12const CONF_BUF: &[u8] = &[
13    255, 2, 0, 2, 0, 59, 0, 16, 31, 1, 4, 0, 44, 10, 10, 78, 2, 26, 2, 0, 9, 77, 22, 11, 2, 26, 31,
14    31, 79, 78, 0, 79, 70, 70, 0, 72, 12, 9, 16, 23, 23, 2, 26, 140, 38, 0, 0, 0, 0, 0, 0, 200, 66,
15    0, 0, 0, 0, 70, 16, 16, 63, 9, 9, 26, 37, 0,
16];
17
18#[derive(Serialize, Deserialize, Debug, Default)]
19#[repr(C)]
20struct InputData {
21    // input variables
22    slider_1: i8, // =0..100 slider position
23    switch_1: u8, // =1 if switch ON and =0 if OFF
24}
25
26#[derive(Serialize, Deserialize, Debug, Default)]
27#[repr(C)]
28struct OutputData {
29    // output variables
30    circularbar_1: i8, // from 0 to 100
31    led_1: u8,         // led state 0 .. 1
32                       // do not include the `connect_flag` variable
33}
34
35#[tokio::main]
36async fn main() -> Result<()> {
37    let remotexy = remote_xy!(InputData, OutputData, "[::]:6377", CONF_BUF).await?;
38    let mut output = OutputData::default();
39
40    loop {
41        let input = remotexy.get_input();
42        println!("Received input: {:?}", input);
43
44        output.circularbar_1 = input.slider_1;
45        output.led_1 = input.switch_1;
46        remotexy.set_output(&output);
47
48        tokio::time::sleep(Duration::from_millis(200)).await;
49    }
50}