std/
main.rs

1#![deny(unsafe_code)]
2
3use tokio::time::{sleep, Duration, timeout};
4use std::future::Future;
5use log::info;
6use env_logger;
7
8use iol::master;
9
10#[tokio::main]
11async fn main() {
12    setup_logger();
13
14    use_master().await;
15    //use_dl().await;
16
17    sleep(Duration::from_secs(100)).await;
18}
19
20
21
22#[derive(Copy, Clone)]
23struct MasterActions;
24
25impl master::Actions for MasterActions {
26    async fn wait_us(&self, duration: u64) {
27        sleep(Duration::from_micros(duration)).await;
28    }
29
30    async fn wait_ms(&self, duration: u64) {
31        sleep(Duration::from_millis(duration)).await;
32    }
33
34    async fn get_cq(&self) -> l6360::PinState {
35        l6360::PinState::Low
36    }
37
38    async fn wake_up_pulse(&self, _direction: master::WakeUpPulseDirection) {
39        info!("ready pulse done");
40    }
41
42    async fn port_power_on(&self) {
43        info!("port power on");
44    }
45
46    async fn port_power_off(&self) {
47        info!("port power off");
48    }
49
50    async fn await_event_with_timeout_ms<F, T>(&self, duration: u64, future: F) -> Option<T>
51    where
52        F: Future<Output = T>,
53    {
54        info!("await with timeout");
55        let result = timeout(Duration::from_millis(duration), future).await.ok();
56        info!("timeout");
57        result
58    }
59
60    async fn await_ready_pulse_with_timeout_ms(&self, _duration: u64) -> master::ReadyPulseResult {
61        master::ReadyPulseResult::ReadyPulseOk
62    }
63
64    async fn exchange_data(&self, _data: &[u8], _answer: &mut [u8]) {}
65}
66
67async fn use_master() {
68    let mut master = master::Master::new(MasterActions);
69    tokio::spawn(async move { master.run().await; });
70
71    sleep(Duration::from_secs(2)).await;
72
73    master::Master::<MasterActions>::dl_set_mode_startup().await;
74
75    sleep(Duration::from_secs(20)).await;
76}
77
78
79// #[derive(Copy, Clone)]
80// struct MasterDlActionsImpl;
81
82// impl iol::master_dl::Actions for MasterDlActionsImpl {
83//     async fn wait_ms(&self, duration: u64) {
84//         sleep(Duration::from_millis(duration)).await;
85//     }
86// }
87
88// #[allow(unused)]
89// async fn use_dl() {
90//     let (mut dl, dl_mode_handler) = master_dl::DL::new(MasterDlActionsImpl);
91//     tokio::spawn(run_dl(dl_mode_handler));
92
93//     sleep(Duration::from_secs(2)).await;
94
95//     info!("sending signal");
96//     dl.DL_SetMode(master_dl::Mode::STARTUP).await.unwrap();
97// }
98
99// async fn run_dl(mut dl: master_dl::DlModeHandlerStateMachine<MasterDlActionsImpl>) {
100//     info!("run dl");
101//     dl.run().await;
102// }
103
104
105
106fn setup_logger() {
107    env_logger::Builder::from_default_env()
108    .format(|buf, record| {
109        use std::io::Write;
110        use chrono::Local;
111        use anstyle;
112
113        writeln!(
114            buf,
115            "{} {}{}{}\t{}\n  \x1b[2m{} @ {}:{}\x1b[0m",
116            Local::now().format("%Y-%m-%d %H:%M:%S%.6f"),
117            buf.default_level_style(record.level()),
118            record.level(),
119            anstyle::Reset,
120            record.args(),
121            record.module_path().unwrap_or("unknwon module"),
122            record.file().unwrap_or("unknown file"),
123            record.line().unwrap_or(0),
124        )
125    })
126    .init();
127}