Hi there! I'm coding Rust for ESP32, my repository can be found here: thomas351 / esp32-rust-playground · GitLab
The current state of my main branch works, but if I replace my lines 138 & 153-157:
let mut bme280: Option<BME280<I2cDriver>>;
...
bme280 = Some(BME280::new_primary(i2c_driver));
match bme280.as_mut().unwrap().init(&mut delay::FreeRtos) {
Ok(_) => {
println!("bme280 initialisation ok! starting timer!");
let timer = timer_service?.timer(move || read_bme280(bme280.as_mut().unwrap()))?;
by
let mut bme280: BME280<I2cDriver>;
...
bme280 = BME280::new_primary(i2c_driver);
match bme280.init(&mut delay::FreeRtos) {
Ok(_) => {
println!("bme280 initialisation ok! starting timer!");
let timer = timer_service?.timer(move || read_bme280(&mut bme280))?;
The BME280 library gives me an InvalidData
Error instead of the data struct I want and get in the first example.
The code doing the sensor reading:
fn read_bme280(bme280: &mut BME280<I2cDriver>) {
match bme280.measure(&mut delay::FreeRtos) {
Ok(measurements) => {
println!("Relative Humidity = {}%", measurements.humidity);
println!("Temperature = {} deg C", measurements.temperature);
println!("Pressure = {} pascals", measurements.pressure);
},
Err(error) => {
println!("failed to read bme280 sensor! error: {:?}", error);
},
}
}
Can somebody explain to me the difference of those two code segments? Since I never have to deal with the "None", I thought I should be able to remove the Option without any side effects, but that believe was wrong, and I don't understand why...