sht4x-rs 0.1.0

Sensirion SHT4x temperature & humidity sensor driver (embedded-hal 1.0, no_std, blocking + async)
Documentation
//! Sensirion CRC-8: polynomial 0x31, init 0xFF, no reflection, no final XOR.

const POLY: u8 = 0x31;
const INIT: u8 = 0xFF;

/// Compute the Sensirion CRC-8 over `data`.
pub fn crc8(data: &[u8]) -> u8 {
    let mut crc = INIT;
    for &b in data {
        crc ^= b;
        for _ in 0..8 {
            crc = if crc & 0x80 != 0 {
                (crc << 1) ^ POLY
            } else {
                crc << 1
            };
        }
    }
    crc
}

/// Verify a 2-byte word followed by its CRC byte.
#[inline]
pub fn verify_word(word_with_crc: &[u8; 3]) -> bool {
    crc8(&word_with_crc[..2]) == word_with_crc[2]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn canonical_vector() {
        // Sensirion datasheet test vector: 0xBE 0xEF -> 0x92
        assert_eq!(crc8(&[0xBE, 0xEF]), 0x92);
    }

    #[test]
    fn verify_word_ok() {
        assert!(verify_word(&[0xBE, 0xEF, 0x92]));
    }

    #[test]
    fn verify_word_bad() {
        assert!(!verify_word(&[0xBE, 0xEF, 0x00]));
    }

    #[test]
    fn empty_is_init() {
        assert_eq!(crc8(&[]), INIT);
    }
}