hauju / scd4x-rs

Rust driver for the Sensirion SCD4x sensors.
Apache License 2.0
5 stars 11 forks source link

Panic: attempt to multiply with overflow #2

Closed tacuna closed 2 years ago

tacuna commented 2 years ago

When running the example i get

serial: [retracted]
Waiting for first measurement... (5 sec)
thread 'main' panicked at 'attempt to multiply with overflow', [retracted]/scd4x-rs/src/scd4x.rs:77:29

Seems like temperature: '((((21875 * temperature) >> 13) - 45000) as f32) / 1000.0' is not safe.

tacuna commented 2 years ago

This seem to work and gives plausible results

    Ok(SensorData {
        co2,
        temperature: temperature as f32 * 175_f32 / 65536_f32 - 45_f32,
        humidity: humidity as f32 * 100_f32 / 65536_f32,
    })

Taken from https://github.com/Sensirion/raspberry-pi-i2c-scd4x/blob/master/scd4x_i2c.c

hauju commented 2 years ago

Looks good, I will change it in the next few days.