Skip to main content

person_sensor/
person_sensor_builder.rs

1use core::marker::PhantomData;
2
3use embedded_hal_async::{digital::Wait, i2c::I2c};
4
5use crate::{
6    person_sensor::{ContinuousCaptureMode, PersonSensorMode, StandbyMode},
7    PersonSensor,
8};
9
10/// Builder for the [`PersonSensor`] driver
11///
12/// Use this to create a new instance of the `PersonSensor` driver
13pub struct PersonSensorBuilder<I2C, INT, MODE> {
14    i2c: I2C,
15    interrupt: INT,
16    mode: PhantomData<MODE>,
17    id_enabled: bool,
18}
19
20impl<I2C> PersonSensorBuilder<I2C, (), ()>
21where
22    I2C: I2c,
23{
24    /// Create a new driver instance, initialized in standby mode
25    pub fn new_standby(i2c: I2C, id_enabled: bool) -> PersonSensorBuilder<I2C, (), StandbyMode> {
26        PersonSensorBuilder {
27            i2c,
28            interrupt: (),
29            mode: PhantomData,
30            id_enabled,
31        }
32    }
33
34    /// Create a new driver instance, initialized in continuous mode
35    pub fn new_continuous(
36        i2c: I2C,
37        id_enabled: bool,
38    ) -> PersonSensorBuilder<I2C, (), ContinuousCaptureMode> {
39        PersonSensorBuilder {
40            i2c,
41            interrupt: (),
42            mode: PhantomData,
43            id_enabled,
44        }
45    }
46}
47
48impl<I2C, MODE> PersonSensorBuilder<I2C, (), MODE>
49where
50    I2C: I2c,
51{
52    /// Sets an interrupt pin to allow waiting for new sensor results.
53    pub fn with_interrupt<INT: Wait>(self, interrupt: INT) -> PersonSensorBuilder<I2C, INT, MODE> {
54        PersonSensorBuilder {
55            i2c: self.i2c,
56            interrupt,
57            mode: self.mode,
58            id_enabled: self.id_enabled,
59        }
60    }
61}
62
63impl<I2C, INT> PersonSensorBuilder<I2C, INT, ContinuousCaptureMode>
64where
65    I2C: I2c,
66{
67    /// Initialize the sensor in continuous mode
68    #[expect(
69        deprecated,
70        reason = "Use `set_id_mode` when bool is fully deprecated"
71    )]
72    pub async fn build(self) -> Result<PersonSensor<I2C, INT, ContinuousCaptureMode>, I2C::Error> {
73        let mut sensor = PersonSensor {
74            i2c: self.i2c,
75            interrupt: self.interrupt,
76            mode: PhantomData,
77            validate_checksum: true,
78        };
79        sensor.set_mode(PersonSensorMode::Continuous).await?;
80        sensor.enable_id_model(self.id_enabled).await?;
81        Ok(sensor)
82    }
83}
84
85impl<I2C, INT> PersonSensorBuilder<I2C, INT, StandbyMode>
86where
87    I2C: I2c,
88{
89    /// Initialize the sensor in standby mode
90    #[expect(
91        deprecated,
92        reason = "Use `set_id_mode` when bool is fully deprecated"
93    )]
94    pub async fn build(self) -> Result<PersonSensor<I2C, INT, StandbyMode>, I2C::Error> {
95        let mut sensor = PersonSensor {
96            i2c: self.i2c,
97            interrupt: self.interrupt,
98            mode: PhantomData,
99            validate_checksum: true,
100        };
101        sensor.set_mode(PersonSensorMode::Standby).await?;
102        sensor.enable_id_model(true).await?;
103        Ok(sensor)
104    }
105}