rsiot/drivers_i2c/pm_rq8/tasks/
input_periodic.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
use std::{sync::Arc, time::Duration};

use tokio::{sync::Mutex, time::sleep};

use super::{
    super::config::Buffer,
    {I2cRequest, TaskOutput},
};

pub struct InputPeriodic {
    pub output: TaskOutput<I2cRequest>,
    pub buffer: Arc<Mutex<Buffer>>,
    pub period: Duration,
}

impl InputPeriodic {
    pub async fn spawn(self) -> super::Result<()> {
        loop {
            let buffer = { self.buffer.lock().await.clone() };
            let request = I2cRequest::SetOutputs(buffer.into());
            self.output
                .send(request)
                .await
                .map_err(|_| super::Error::TokioTaskSend)?;
            sleep(self.period).await;
        }
    }
}