rsiot/drivers_i2c/pcf8575/
device.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::sync::Arc;

use tokio::{sync::Mutex, task::JoinSet};
use tracing::warn;

use crate::{
    executor::CmpInOut,
    message::{MsgDataBound, ServiceBound},
};

use super::{
    super::{I2cSlaveAddress, RsiotI2cDriverBase},
    state::State,
    task_read_inputs::TaskReadInputs,
    task_write_output::TaskWriteOutput,
    PCF8575PinMode, TPinFnOutput,
};

pub struct PCF8575<TMsg>
where
    TMsg: MsgDataBound,
{
    pub address: I2cSlaveAddress,
    pub pins: Vec<PCF8575PinMode<TMsg>>,
}

impl<TMsg> PCF8575<TMsg>
where
    TMsg: MsgDataBound + 'static,
{
    pub async fn fn_process<TService>(
        &self,
        in_out: CmpInOut<TMsg, TService>,
        driver: Arc<Mutex<impl RsiotI2cDriverBase + 'static>>,
    ) where
        TService: ServiceBound + 'static,
    {
        loop {
            let mut state = State::new();
            let mut task_set: JoinSet<Result<(), String>> = JoinSet::new();

            // Определяем начальную конфигурацию входов / выходов
            let mut input_pins: TPinFnOutput<TMsg> = vec![];
            for (index, pin) in self.pins.iter().enumerate() {
                match pin {
                    PCF8575PinMode::Disabled => {}

                    PCF8575PinMode::Input { fn_output } => {
                        state.set_input(index).await;
                        input_pins.push((index, *fn_output))
                    }

                    PCF8575PinMode::Output { fn_input } => {
                        state.set_output_low(index).await;
                        let mut task_output = TaskWriteOutput {
                            in_out: in_out.clone(),
                            fn_input: *fn_input,
                            state: state.clone(),
                            driver: driver.clone(),
                            address: self.address,
                            pin: index,
                        };
                        task_set.spawn(async move { task_output.spawn().await });
                    }
                }
            }

            let task_input = TaskReadInputs {
                in_out: in_out.clone(),
                driver: driver.clone(),
                address: self.address,
                pin_and_fn_output: input_pins,
                state: state.clone(),
            };
            task_set.spawn(async move { task_input.spawn().await });

            // задачи не должны заканчиваться. Если закончилась хоть одна - отменяем все остальные и
            // запускам все сначала
            while let Some(res) = task_set.join_next().await {
                warn!("{res:?}");
                task_set.abort_all()
            }
        }
    }
}