embedded-aht20 0.3.1

Platform-agnostic Rust driver for the AHT20 temperature & humidity sensor.
Documentation
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#![doc = include_str!("../README.md")]
#![deny(unsafe_code, missing_docs)]
#![no_std]

use bitflags::bitflags;
use crc::{Crc, CRC_8_NRSC_5};

#[cfg(not(feature = "async"))]
use embedded_hal as hal;
#[cfg(feature = "async")]
use embedded_hal_async as hal;

use hal::delay::DelayNs;
use hal::i2c::{I2c, SevenBitAddress};

pub use weather_utils::Temperature;
use weather_utils::{Celsius, RelativeHumidity, TemperatureAndRelativeHumidity};

/// The default I2C address.
pub const DEFAULT_I2C_ADDRESS: SevenBitAddress = 0x38;

const CHECK_STATUS_COMMAND: &[u8] = &[0b0111_0001];
const INITIALIZATION_COMMAND: &[u8] = &[0b1011_1110, 0x08, 0x00];
const TRIGGER_MEASUREMENT_COMMAND: &[u8] = &[0b1010_1100, 0x33, 0x00];
const SOFT_RESET_COMMAND: &[u8] = &[0b1011_1010];

/// All possible errors generated when using the Aht20 struct.
#[derive(Debug)]
pub enum Error<I2cError>
where
    I2cError: hal::i2c::Error,
{
    /// I²C bus error.
    I2c(I2cError),
    /// The computed CRC and the one sent by the device mismatch.
    InvalidCrc,
    /// The device is busy at a time where it was not expected to.
    UnexpectedBusy,
}

impl<I2cError> From<I2cError> for Error<I2cError>
where
    I2cError: hal::i2c::Error,
{
    fn from(value: I2cError) -> Self {
        Error::I2c(value)
    }
}

#[derive(Debug)]
struct SensorMeasurement {
    raw_humidity: u32,
    raw_temperature: u32,
}

impl From<&[u8]> for SensorMeasurement {
    fn from(data: &[u8]) -> Self {
        let raw_humidity: u32 =
            ((data[0] as u32) << 12) | ((data[1] as u32) << 4) | ((data[2] >> 4) as u32);
        let raw_temperature: u32 =
            (((data[2] & 0b0000_1111) as u32) << 16) | ((data[3] as u32) << 8) | (data[4] as u32);
        SensorMeasurement {
            raw_humidity,
            raw_temperature,
        }
    }
}

impl SensorMeasurement {
    /// The measured relative humidity (in %).
    pub fn humidity(&self) -> f32 {
        ((self.raw_humidity as f32) / ((1 << 20) as f32)) * 100.0
    }

    /// The measured temperature (in °C).
    pub fn temperature(&self) -> f32 {
        ((self.raw_temperature as f32) / ((1 << 20) as f32)) * 200.0 - 50.0
    }
}

impl From<SensorMeasurement> for TemperatureAndRelativeHumidity<Celsius> {
    fn from(value: SensorMeasurement) -> Self {
        TemperatureAndRelativeHumidity {
            temperature: Celsius(value.temperature()),
            relative_humidity: RelativeHumidity::new(value.humidity()).unwrap(),
        }
    }
}

bitflags! {
    struct SensorStatus: u8 {
        const BUSY = 0b1000_0000;
        const CALIBRATED = 0b0000_1000;
    }
}

impl SensorStatus {
    fn is_calibrated(&self) -> bool {
        self.contains(SensorStatus::CALIBRATED)
    }

    fn is_ready(&self) -> bool {
        !self.contains(SensorStatus::BUSY)
    }
}

/// AHT20 device driver.
#[derive(Debug)]
pub struct Aht20<I2C, D> {
    i2c: I2C,
    address: SevenBitAddress,
    delay: D,
}

impl<I2C, D> Aht20<I2C, D>
where
    I2C: I2c,
    D: DelayNs,
{
    /// Create a new instance of the AHT20 device.
    #[maybe_async_cfg::maybe(
        sync(not(feature = "async"), keep_self),
        async(feature = "async", keep_self)
    )]
    pub async fn new(
        i2c: I2C,
        address: SevenBitAddress,
        delay: D,
    ) -> Result<Self, Error<I2C::Error>> {
        let mut dev = Self {
            i2c,
            address,
            delay,
        };

        while !dev.check_status().await?.is_calibrated() {
            dev.send_initialize().await?;
            dev.delay_ms(10).await;
        }

        Ok(dev)
    }

    /// Perform a measurement.
    ///
    /// The measurement takes at least 80 ms to be performed.
    #[maybe_async_cfg::maybe(
        sync(not(feature = "async"), keep_self),
        async(feature = "async", keep_self)
    )]
    pub async fn measure(
        &mut self,
    ) -> Result<TemperatureAndRelativeHumidity<Celsius>, Error<I2C::Error>> {
        self.send_trigger_measurement().await?;

        // Wait for measurement to be ready
        self.delay_ms(80).await;
        while !self.check_status().await?.is_ready() {
            self.delay_ms(1).await;
        }

        let mut buffer = [0u8; 7];
        self.i2c
            .read(self.address, &mut buffer)
            .await
            .map_err(Error::I2c)?;

        let data = &buffer[..6];
        let crc = buffer[6];
        self.check_crc(data, crc)?;

        let status = SensorStatus::from_bits_retain(buffer[0]);
        if !status.is_ready() {
            return Err(Error::UnexpectedBusy);
        }

        let measurement = SensorMeasurement::from(&data[1..6]);
        Ok(measurement.into())
    }

    /// Perform a soft reset to force the device into a well-defined state
    /// without removing the power supply.
    #[maybe_async_cfg::maybe(
        sync(not(feature = "async"), keep_self),
        async(feature = "async", keep_self)
    )]
    pub async fn soft_reset(&mut self) -> Result<(), Error<I2C::Error>> {
        self.i2c
            .write(self.address, SOFT_RESET_COMMAND)
            .await
            .map_err(Error::I2c)?;
        self.delay_ms(20).await;
        Ok(())
    }

    fn check_crc(&self, data: &[u8], crc_value: u8) -> Result<(), Error<I2C::Error>> {
        let crc = Crc::<u8>::new(&CRC_8_NRSC_5);
        let mut digest = crc.digest();
        digest.update(data);
        if digest.finalize() != crc_value {
            return Err(Error::InvalidCrc);
        }
        Ok(())
    }

    #[maybe_async_cfg::maybe(
        sync(not(feature = "async"), keep_self),
        async(feature = "async", keep_self)
    )]
    async fn check_status(&mut self) -> Result<SensorStatus, Error<I2C::Error>> {
        let mut buffer = [0];
        self.i2c
            .write_read(self.address, CHECK_STATUS_COMMAND, &mut buffer)
            .await
            .map_err(Error::I2c)?;
        Ok(SensorStatus::from_bits_retain(buffer[0]))
    }

    #[maybe_async_cfg::maybe(
        sync(not(feature = "async"), keep_self),
        async(feature = "async", keep_self)
    )]
    async fn delay_ms(&mut self, duration: u32) {
        self.delay.delay_ms(duration).await;
    }

    #[maybe_async_cfg::maybe(
        sync(not(feature = "async"), keep_self),
        async(feature = "async", keep_self)
    )]
    async fn send_initialize(&mut self) -> Result<(), Error<I2C::Error>> {
        self.i2c
            .write(self.address, INITIALIZATION_COMMAND)
            .await
            .map_err(Error::I2c)?;
        Ok(())
    }

    #[maybe_async_cfg::maybe(
        sync(not(feature = "async"), keep_self),
        async(feature = "async", keep_self)
    )]
    async fn send_trigger_measurement(&mut self) -> Result<(), Error<I2C::Error>> {
        self.i2c
            .write(self.address, TRIGGER_MEASUREMENT_COMMAND)
            .await
            .map_err(Error::I2c)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use approx::assert_relative_eq;
    use embedded_hal::i2c::ErrorKind;
    use embedded_hal_mock::eh1::delay::StdSleep as Delay;
    use embedded_hal_mock::eh1::i2c::{Mock as I2cMock, Transaction as I2cTransaction};

    use super::*;
    use crate::Temperature;

    #[test]
    fn test_i2c_error() {
        let error: Error<hal::i2c::ErrorKind> = hal::i2c::ErrorKind::Other.into();
        assert!(matches!(error, Error::I2c(_)));
    }

    #[test]
    fn test_sensor_measurement() {
        let measurement: SensorMeasurement = [0x7b, 0xb3, 0x05, 0x9d, 0x49].as_slice().into();
        assert_eq!(measurement.raw_humidity, 0x0007bb30);
        assert_eq!(measurement.raw_temperature, 0x00059d49);
        assert_relative_eq!(measurement.humidity(), 48.32, epsilon = 0.01);
        assert_relative_eq!(measurement.temperature(), 20.18, epsilon = 0.01);
    }

    #[test]
    fn test_aht20_creation_with_busy() {
        let expectations = [
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::BUSY.bits()].to_vec(),
            ),
            I2cTransaction::write(DEFAULT_I2C_ADDRESS, INITIALIZATION_COMMAND.to_vec()),
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
        ];
        let mut i2c = I2cMock::new(&expectations);
        let _device = Aht20::new(&mut i2c, DEFAULT_I2C_ADDRESS, Delay {}).unwrap();
        i2c.done();
    }

    #[test]
    fn test_aht20_creation_with_check_status_error() {
        let expectations = [I2cTransaction::write_read(
            DEFAULT_I2C_ADDRESS,
            CHECK_STATUS_COMMAND.to_vec(),
            [SensorStatus::BUSY.bits()].to_vec(),
        )
        .with_error(ErrorKind::Bus)];
        let mut i2c = I2cMock::new(&expectations);
        let err = Aht20::new(&mut i2c, DEFAULT_I2C_ADDRESS, Delay {});
        assert!(matches!(err, Err(Error::I2c(ErrorKind::Bus))));
        i2c.done();
    }

    #[test]
    fn test_aht20_creation_with_initialization_error() {
        let expectations = [
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::BUSY.bits()].to_vec(),
            ),
            I2cTransaction::write(DEFAULT_I2C_ADDRESS, INITIALIZATION_COMMAND.to_vec())
                .with_error(ErrorKind::Other),
        ];
        let mut i2c = I2cMock::new(&expectations);
        let err = Aht20::new(&mut i2c, DEFAULT_I2C_ADDRESS, Delay {});
        assert!(matches!(err, Err(Error::I2c(ErrorKind::Other))));
        i2c.done();
    }

    #[test]
    fn test_measure() {
        let expectations = [
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
            I2cTransaction::write(DEFAULT_I2C_ADDRESS, TRIGGER_MEASUREMENT_COMMAND.to_vec()),
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
            I2cTransaction::read(
                DEFAULT_I2C_ADDRESS,
                [
                    SensorStatus::CALIBRATED.bits(),
                    0x7b,
                    0xb3,
                    0x05,
                    0x9d,
                    0x49,
                    0x7d,
                ]
                .to_vec(),
            ),
        ];
        let mut i2c = I2cMock::new(&expectations);
        let mut device = Aht20::new(&mut i2c, DEFAULT_I2C_ADDRESS, Delay {}).unwrap();
        let measurement = device.measure().unwrap();
        assert_eq!(measurement.temperature.celsius(), Celsius(20.18));
        assert_eq!(
            measurement.relative_humidity,
            RelativeHumidity::new(48.32).unwrap()
        );
        i2c.done();
    }

    #[test]
    fn test_measure_with_trigger_measurement_error() {
        let expectations = [
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
            I2cTransaction::write(DEFAULT_I2C_ADDRESS, TRIGGER_MEASUREMENT_COMMAND.to_vec())
                .with_error(ErrorKind::ArbitrationLoss),
        ];
        let mut i2c = I2cMock::new(&expectations);
        let mut device = Aht20::new(&mut i2c, DEFAULT_I2C_ADDRESS, Delay {}).unwrap();
        let err = device.measure().expect_err("Arbitration loss");
        assert!(matches!(err, Error::I2c(ErrorKind::ArbitrationLoss)));
        i2c.done();
    }

    #[test]
    fn test_measure_with_measure_read_error() {
        let expectations = [
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
            I2cTransaction::write(DEFAULT_I2C_ADDRESS, TRIGGER_MEASUREMENT_COMMAND.to_vec()),
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
            I2cTransaction::read(
                DEFAULT_I2C_ADDRESS,
                [
                    SensorStatus::CALIBRATED.bits(),
                    0x7b,
                    0xb3,
                    0x05,
                    0x9d,
                    0x49,
                    0x7d,
                ]
                .to_vec(),
            )
            .with_error(ErrorKind::ArbitrationLoss),
        ];
        let mut i2c = I2cMock::new(&expectations);
        let mut device = Aht20::new(&mut i2c, DEFAULT_I2C_ADDRESS, Delay {}).unwrap();
        let err = device.measure().expect_err("Arbitration loss");
        assert!(matches!(err, Error::I2c(ErrorKind::ArbitrationLoss)));
        i2c.done();
    }

    #[test]
    fn test_measure_with_busy_and_unexpected_busy_error() {
        let expectations = [
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
            I2cTransaction::write(DEFAULT_I2C_ADDRESS, TRIGGER_MEASUREMENT_COMMAND.to_vec()),
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [(SensorStatus::CALIBRATED | SensorStatus::BUSY).bits()].to_vec(),
            ),
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
            I2cTransaction::read(
                DEFAULT_I2C_ADDRESS,
                [
                    (SensorStatus::CALIBRATED | SensorStatus::BUSY).bits(),
                    0x7b,
                    0xb3,
                    0x05,
                    0x9d,
                    0x49,
                    0x91,
                ]
                .to_vec(),
            ),
        ];
        let mut i2c = I2cMock::new(&expectations);
        let mut device = Aht20::new(&mut i2c, DEFAULT_I2C_ADDRESS, Delay {}).unwrap();
        let err = device.measure().expect_err("Unexpected Busy");
        assert!(matches!(err, Error::UnexpectedBusy));
        i2c.done();
    }

    #[test]
    fn test_measure_with_invalid_crc() {
        let expectations = [
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
            I2cTransaction::write(DEFAULT_I2C_ADDRESS, TRIGGER_MEASUREMENT_COMMAND.to_vec()),
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [(SensorStatus::CALIBRATED | SensorStatus::BUSY).bits()].to_vec(),
            ),
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
            I2cTransaction::read(
                DEFAULT_I2C_ADDRESS,
                [
                    (SensorStatus::CALIBRATED | SensorStatus::BUSY).bits(),
                    0x7b,
                    0xb3,
                    0x05,
                    0x9d,
                    0x49,
                    0x90,
                ]
                .to_vec(),
            ),
        ];
        let mut i2c = I2cMock::new(&expectations);
        let mut device = Aht20::new(&mut i2c, DEFAULT_I2C_ADDRESS, Delay {}).unwrap();
        let err = device.measure().expect_err("Invalid CRC");
        assert!(matches!(err, Error::InvalidCrc));
        i2c.done();
    }

    #[test]
    fn test_soft_reset() {
        let expectations = [
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
            I2cTransaction::write(DEFAULT_I2C_ADDRESS, SOFT_RESET_COMMAND.to_vec()),
        ];
        let mut i2c = I2cMock::new(&expectations);
        let mut device = Aht20::new(&mut i2c, DEFAULT_I2C_ADDRESS, Delay {}).unwrap();
        device.soft_reset().unwrap();
        i2c.done();
    }

    #[test]
    fn test_soft_reset_with_error() {
        let expectations = [
            I2cTransaction::write_read(
                DEFAULT_I2C_ADDRESS,
                CHECK_STATUS_COMMAND.to_vec(),
                [SensorStatus::CALIBRATED.bits()].to_vec(),
            ),
            I2cTransaction::write(DEFAULT_I2C_ADDRESS, SOFT_RESET_COMMAND.to_vec())
                .with_error(ErrorKind::Overrun),
        ];
        let mut i2c = I2cMock::new(&expectations);
        let mut device = Aht20::new(&mut i2c, DEFAULT_I2C_ADDRESS, Delay {}).unwrap();
        let err = device.soft_reset().expect_err("Overrun");
        assert!(matches!(err, Error::I2c(ErrorKind::Overrun)));
        i2c.done();
    }
}