#reading seesaw i2c w/ rust embedded

1 messages · Page 1 of 1 (latest)

blissful crescent
#

here's my code running on the esp32:

fn main() -> Result<()> {
    // It is necessary to call this function once. Otherwise some patches to the runtime
    // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
    esp_idf_svc::sys::link_patches();

    // Bind the log crate to the ESP Logging facilities
    esp_idf_svc::log::EspLogger::initialize_default();

    let peripherals = Peripherals::take().unwrap();

    let sda = peripherals.pins.gpio25;
    let scl = peripherals.pins.gpio26;
    let config = I2cConfig::new().baudrate(100.kHz().into());
    let mut i2c = I2cDriver::new(peripherals.i2c0, sda, scl, &config)?;

    let mut result: Vec<u8> = vec![0; 4];
    let sensor_addr = 54;
    let to_write: Vec<u8> = vec![0, 4];

    let err = i2c.write_read(sensor_addr, to_write.as_slice(), result.as_mut_slice(), 5);
    if err.is_err_and(|e| {
        esp_nofail!(e.code());
        true
    }) {
        log::info!("Got result! {:#?}", result);
    }

    let num_result = i32::from_be_bytes(result[0..4].try_into().unwrap());
    log::info!("Got result! {}F", num_result);

    let wakeup_source = peripherals.pins.gpio4;
    unsafe {
        esp_sleep_enable_ext0_wakeup(wakeup_source.pin(), 1);
    }

    std::thread::sleep(std::time::Duration::from_secs(2));
    log::info!("Entering deep sleep");

    unsafe { esp_deep_sleep_start() }
}
mighty robin
#

i've converted some code to Java/Kotlin, so i might be able to help -- what's the error?

blissful crescent
blissful crescent
#

Alright so i'm getting back the following bytes, ```
[
0,
25,
149,
156,
]

which converted to a signed int 32 is `1676700`
#

That int is in no temperature unit I know of

#

no idea what the significance of multiplying by 0.00001525878 is

#

but when I multiply my result by that, I do indeed get 25.5 (which I am assuming is celsius) and that sounds roughly correct (my room temp is like 23/24C according to external temp reader)

#

I guess i'll just hardcode that same number in my code but would love an explanation if someone knows!

blissful crescent
#

ah yeah its the scaling factor of the actual sensor

#

sensor's raw data is probably using 16 bits for the fractional part