Skip to main content

mh_z19c/
command.rs

1//! MH-Z19C command definitions.
2
3/// Commands understood by the MH-Z19C sensor.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum Command {
6    /// Read CO₂ concentration and temperature from sensor.
7    /// Requires firmware version 5 or higher.
8    ReadCo2AndTemperature,
9    /// Read CO₂ concentration from sensor.
10    ReadCo2,
11    /// Read out the firmware version of the sensor.
12    GetFirmwareVersion,
13    /// Set self calibration enabled status.
14    SetSelfCalibrate(bool),
15}
16
17impl Command {
18    /// Op code used for the command in communication with the sensor.
19    pub fn op_code(&self) -> u8 {
20        match self {
21            Self::ReadCo2AndTemperature => 0x85,
22            Self::ReadCo2 => 0x86,
23            Self::GetFirmwareVersion => 0xA0,
24            Self::SetSelfCalibrate(_) => 0x79,
25        }
26    }
27
28    /// Serialize the command op code together with its arguments.
29    pub fn serialize(&self) -> [u8; 6] {
30        match self {
31            Self::ReadCo2AndTemperature => [self.op_code(), 0, 0, 0, 0, 0],
32            Self::ReadCo2 => [self.op_code(), 0, 0, 0, 0, 0],
33            Self::GetFirmwareVersion => [self.op_code(), 0, 0, 0, 0, 0],
34            Self::SetSelfCalibrate(true) => [self.op_code(), 0xa0, 0, 0, 0, 0],
35            Self::SetSelfCalibrate(false) => [self.op_code(), 0, 0, 0, 0, 0],
36        }
37    }
38}