use i2c::Address;
use std::io::{Read, Write};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
const I2C_ADDR: u16 = 0x44; const CMD_RESET: u16 = 0x30a2;
const CMD_MEASURE_SINGLE: u16 = 0x2400;
const CMD_FETCH_DATA: u16 = 0xe000;
pub fn main() -> Result<()> {
use std::thread::sleep;
use std::time::Duration;
let mut bus = i2c_tiny_usb::I2c::open_single_device()?;
bus.set_slave_address(I2C_ADDR, false)?;
bus.write_all(&CMD_RESET.to_be_bytes())?;
sleep(Duration::from_millis(10));
bus.write_all(&CMD_MEASURE_SINGLE.to_be_bytes())?;
sleep(Duration::from_millis(100));
let mut buf = [0u8; 6];
bus.write_all(&CMD_FETCH_DATA.to_be_bytes())?;
bus.read_exact(&mut buf)?;
let temp = u16::from_be_bytes([buf[0], buf[1]]) as f32 * (175.0 / 65565.0) - 45.0;
let humidity = u16::from_be_bytes([buf[3], buf[4]]) as f32 * (100.0 / 65565.0);
println!(" T = {:.2}°C", temp);
println!("RH = {:.2}%", humidity);
Ok(())
}