embedded_ccs811/
traits.rs

1use crate::{private, AlgorithmResult, FirmwareMode, InterruptMode, MeasurementMode};
2use embedded_hal::delay::DelayNs;
3
4/// General CCS811 methods available in either mode
5pub trait Ccs811Device: private::Sealed {
6    /// Error type
7    type Error;
8
9    /// Get the firmware mode.
10    fn firmware_mode(&mut self) -> Result<FirmwareMode, Self::Error>;
11
12    /// Check if a valid application firmware is loaded.
13    fn has_valid_app(&mut self) -> Result<bool, Self::Error>;
14
15    /// Get the hardware ID (0x81 for the CCS81x family of devices)
16    fn hardware_id(&mut self) -> Result<u8, Self::Error>;
17
18    /// Get the hardware version (major, minor) ((1,X) for the CCS81x family of devices)
19    fn hardware_version(&mut self) -> Result<(u8, u8), Self::Error>;
20
21    /// Get the firmware bootloader verion (major, minor, trivial)
22    fn firmware_bootloader_version(&mut self) -> Result<(u8, u8, u8), Self::Error>;
23
24    /// Get the firmware application verion (major, minor, trivial)
25    fn firmware_application_version(&mut self) -> Result<(u8, u8, u8), Self::Error>;
26}
27
28/// Methods available when on application mode
29pub trait Ccs811AppMode: private::Sealed {
30    /// Error type
31    type Error;
32    /// Boot/App mode change error
33    type ModeChangeError;
34    /// Boot mode type
35    type BootModeType;
36
37    /// Set the measurement mode
38    ///
39    /// NOTE: When changing to a new mode with a lower sample rate,
40    /// place the device in `Idle` mode for at least 10 minutes before
41    /// enabling the new mode.
42    fn set_mode(&mut self, mode: MeasurementMode) -> Result<(), Self::Error>;
43
44    /// Check if there is a new data sample ready.
45    fn has_data_ready(&mut self) -> Result<bool, Self::Error>;
46
47    /// Get the algorithm results data.
48    ///
49    /// Returns a tuple containing the current and voltage through the sensor in
50    /// the format: (current, voltage).
51    /// The current is a value between 0uA and 63uA.
52    /// The voltage contains the value as computed in the ADC. (1023 = 1.65V)
53    fn data(&mut self) -> nb::Result<AlgorithmResult, Self::Error>;
54
55    /// Get the raw sensor data.
56    ///
57    /// Returns a tuple containing the current and voltage through the sensor in
58    /// the format: (current, voltage).
59    /// The current is a value between 0uA and 63uA.
60    /// The voltage contains the value as computed in the ADC. (1023 = 1.65V)
61    fn raw_data(&mut self) -> Result<(u8, u16), Self::Error>;
62
63    /// Get the current baseline
64    fn baseline(&mut self) -> Result<[u8; 2], Self::Error>;
65
66    /// Set the baseline
67    fn set_baseline(&mut self, baseline: [u8; 2]) -> Result<(), Self::Error>;
68
69    /// Set the environment temperature and relative humidity.
70    ///
71    /// The humidity must be provided as percentage: [0.0..100.0].
72    /// The temperature must be provided in Celsius. (Theoretical max: 254.99805ÂșC)
73    fn set_environment(
74        &mut self,
75        humidity_percentage: f32,
76        temperature_celsius: f32,
77    ) -> Result<(), Self::Error>;
78
79    /// Configure the interrupt generation.
80    fn set_interrupt_mode(&mut self, mode: InterruptMode) -> Result<(), Self::Error>;
81
82    /// Set the eCO2 threshold values for interrupt generation (in ppm).
83    ///
84    /// An interrupt will be asserted if the value moved from the current
85    /// range by 50 ppm.
86    fn set_eco2_thresholds(
87        &mut self,
88        low_to_medium: u16,
89        medium_to_high: u16,
90    ) -> Result<(), Self::Error>;
91
92    /// Restart the device in boot mode.
93    ///
94    /// 2ms should be waited before doing any other operation.
95    fn software_reset(self) -> Result<Self::BootModeType, Self::ModeChangeError>;
96}
97
98/// Methods available when on boot mode
99pub trait Ccs811BootMode: private::Sealed {
100    /// Error type
101    type Error;
102    /// Boot/App mode change error
103    type ModeChangeError;
104    /// Application mode type
105    type TargetType;
106
107    /// Start application mode
108    ///
109    /// NOTE: after this call 1ms must be waited before sending application commands.
110    fn start_application(self) -> Result<Self::TargetType, Self::ModeChangeError>;
111
112    /// Reset, erase, download new application and verify it in one step.
113    ///
114    /// This resets the device via a software reset, erases the current application,
115    /// flashes the new binary and verifies it. This takes at least 572ms + 50ms * (bin_size/8).
116    /// Returns `Error::InvalidInputData` if the input binary lengh is not multiple of 8.
117    fn update_application<D: DelayNs>(
118        &mut self,
119        bin: &[u8],
120        delay: &mut D,
121    ) -> Result<(), Self::Error>;
122
123    /// Verify application.
124    ///
125    /// NOTE: After the first call, 70ms must be waited before calling again to
126    /// poll until completion.
127    fn verify_application(&mut self) -> nb::Result<(), Self::Error>;
128
129    /// Erase application.
130    ///
131    /// NOTE: After the first call, 500ms must be waited before calling again to
132    /// poll until completion.
133    fn erase_application(&mut self) -> nb::Result<(), Self::Error>;
134
135    /// Download new application.
136    ///
137    /// Returns `Error::InvalidInputData` if the input binary lengh is not multiple of 8.
138    /// This takes at least 50ms * (bin_size/8).
139    fn download_application<D: DelayNs>(
140        &mut self,
141        bin: &[u8],
142        delay: &mut D,
143    ) -> Result<(), Self::Error>;
144
145    /// Restart the device in boot mode.
146    ///
147    /// 2ms should be waited before doing any other operation.
148    fn software_reset(&mut self) -> Result<(), Self::Error>;
149}