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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#![no_std]

use embedded_hal::serial::{Read, Write};
use nb::block;
use scroll::{Pread, Pwrite, BE};

mod read_fsm;

const CMD_FRAME_SIZE: usize = 7;
const OUTPUT_FRAME_SIZE: usize = 32;
const RESPONSE_FRAME_SIZE: usize = 8;
const CHECKSUM_SIZE: usize = 2;

type Response = [u8; RESPONSE_FRAME_SIZE];

pub const MN1: u8 = 0x42;
pub const MN2: u8 = 0x4D;
const PASSIVE_MODE_RESPONSE: Response = [MN1, MN1, 0x00, 0x04, 0xE1, 0x00, 0x01, 0x74];
const ACTIVE_MODE_RESPONSE: Response = [MN1, MN2, 0x00, 0x04, 0xE1, 0x01, 0x01, 0x75];
const SLEEP_RESPONSE: Response = [MN1, MN2, 0x00, 0x04, 0xE4, 0x00, 0x01, 0x77];

#[derive(Debug)]
pub enum Error {
    SendFailed,
    ReadFailed,
    ChecksumError,
    IncorrectResponse,
    NoResponse,
}

/// Sensor interface
pub struct Pms7003Sensor<Serial>
where
    Serial: Read<u8> + Write<u8>,
{
    serial: Serial,
}

impl<Serial> Pms7003Sensor<Serial>
where
    Serial: Read<u8> + Write<u8>,
{
    /// Creates a new sensor instance
    /// * `serial` - single object implementing embedded hal serial traits
    pub fn new(mut serial: Serial) -> Self {
        loop {
            if serial.read().is_err() {
                break;
            }
        }

        Self { serial }
    }

    fn read_from_device<T: AsMut<[u8]>>(&mut self, mut buffer: T) -> Result<T, Error> {
        use read_fsm::*;

        let mut read = ReadStateMachine::new(buffer.as_mut(), 10);
        loop {
            match read.update(self.serial.read()) {
                ReadStatus::Failed => return Err(Error::ReadFailed),
                ReadStatus::Finished => return Ok(buffer),
                ReadStatus::InProgress => {}
            }
        }
    }

    /// Reads sensor status. Blocks until status is available.
    pub fn read(&mut self) -> Result<OutputFrame, Error> {
        OutputFrame::from_buffer(&self.read_from_device([0_u8; OUTPUT_FRAME_SIZE])?)
    }

    /// Sleep mode. May fail because of incorrect reposnse because of race condition between response and air quality status
    pub fn sleep(&mut self) -> Result<(), Error> {
        self.send_cmd(&create_command(0xe4, 0))?;
        self.receive_response(SLEEP_RESPONSE)
    }

    pub fn wake(&mut self) -> Result<(), Error> {
        self.send_cmd(&create_command(0xe4, 1))
    }

    /// Passive mode - sensor reports air quality on request
    pub fn passive(&mut self) -> Result<(), Error> {
        self.send_cmd(&create_command(0xe1, 0))?;
        self.receive_response(PASSIVE_MODE_RESPONSE)
    }

    /// Active mode - sensor reports air quality continuously
    pub fn active(&mut self) -> Result<(), Error> {
        self.send_cmd(&create_command(0xe1, 1))?;
        self.receive_response(ACTIVE_MODE_RESPONSE)
    }

    /// Requests status in passive mode
    pub fn request(&mut self) -> Result<(), Error> {
        self.send_cmd(&create_command(0xe2, 0))
    }

    fn send_cmd(&mut self, cmd: &[u8]) -> Result<(), Error> {
        for byte in cmd {
            block!(self.serial.write(*byte)).map_err(|_| Error::SendFailed)?;
        }
        Ok(())
    }

    fn receive_response(&mut self, expected_response: Response) -> Result<(), Error> {
        if self.read_from_device([0u8; RESPONSE_FRAME_SIZE])? != expected_response {
            Err(Error::IncorrectResponse)
        } else {
            Ok(())
        }
    }
}

fn create_command(cmd: u8, data: u16) -> [u8; CMD_FRAME_SIZE] {
    let mut buffer = [0_u8; CMD_FRAME_SIZE];
    let mut offset = 0usize;

    buffer.gwrite::<u8>(MN1, &mut offset).unwrap();
    buffer.gwrite::<u8>(MN2, &mut offset).unwrap();
    buffer.gwrite::<u8>(cmd, &mut offset).unwrap();
    buffer.gwrite_with::<u16>(data, &mut offset, BE).unwrap();

    let checksum = buffer
        .iter()
        .take(CMD_FRAME_SIZE - CHECKSUM_SIZE)
        .map(|b| *b as u16)
        .sum::<u16>();
    buffer
        .gwrite_with::<u16>(checksum, &mut offset, BE)
        .unwrap();

    buffer
}

/// Contains data reported by the sensor
#[derive(Default, Debug)]
pub struct OutputFrame {
    pub start1: u8,
    pub start2: u8,
    pub frame_length: u16,
    pub pm1_0: u16,
    pub pm2_5: u16,
    pub pm10: u16,
    pub pm1_0_atm: u16,
    pub pm2_5_atm: u16,
    pub pm10_atm: u16,
    pub beyond_0_3: u16,
    pub beyond_0_5: u16,
    pub beyond_1_0: u16,
    pub beyond_2_5: u16,
    pub beyond_5_0: u16,
    pub beyond_10_0: u16,
    pub reserved: u16,
    pub check: u16,
}

impl OutputFrame {
    pub fn from_buffer(buffer: &[u8; OUTPUT_FRAME_SIZE]) -> Result<Self, Error> {
        let sum: usize = buffer
            .iter()
            .take(OUTPUT_FRAME_SIZE - CHECKSUM_SIZE)
            .map(|e| *e as usize)
            .sum();

        let mut frame = OutputFrame::default();
        let mut offset = 0usize;

        frame.start1 = buffer.gread::<u8>(&mut offset).unwrap();
        frame.start2 = buffer.gread::<u8>(&mut offset).unwrap();
        frame.frame_length = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.pm1_0 = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.pm2_5 = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.pm10 = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.pm1_0_atm = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.pm2_5_atm = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.pm10_atm = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.beyond_0_3 = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.beyond_0_5 = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.beyond_1_0 = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.beyond_2_5 = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.beyond_5_0 = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.beyond_10_0 = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.reserved = buffer.gread_with::<u16>(&mut offset, BE).unwrap();
        frame.check = buffer.gread_with::<u16>(&mut offset, BE).unwrap();

        if sum != frame.check as usize {
            return Err(Error::ChecksumError);
        }

        Ok(frame)
    }
}

impl<TX, RX> Pms7003Sensor<Wrapper<TX, RX>>
where
    TX: Write<u8>,
    RX: Read<u8>,
{
    /// Creates a new sensor instance
    /// * `tx` - embedded hal serial Write
    /// * `rx` - embedded hal serial Read
    pub fn new_tx_rx(tx: TX, rx: RX) -> Self {
        Self::new(Wrapper(tx, rx))
    }
}

/// Combines two serial traits objects into one
pub struct Wrapper<TX, RX>(TX, RX)
where
    TX: Write<u8>,
    RX: Read<u8>;

impl<TX, RX> Read<u8> for Wrapper<TX, RX>
where
    TX: Write<u8>,
    RX: Read<u8>,
{
    type Error = RX::Error;

    fn read(&mut self) -> nb::Result<u8, Self::Error> {
        self.1.read()
    }
}

impl<TX, RX> Write<u8> for Wrapper<TX, RX>
where
    TX: Write<u8>,
    RX: Read<u8>,
{
    type Error = TX::Error;

    fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
        self.0.write(word)
    }

    fn flush(&mut self) -> nb::Result<(), Self::Error> {
        self.0.flush()
    }
}