use std::time::Instant;
use esp_idf_hal::delay::FreeRtos;
mod lcd;
mod gpio;
const TRIG: u8 = 10;
const ECHO: u8 = 2;
unsafe fn get_distance() -> i32 {
gpio::write(TRIG, 0);
FreeRtos::delay_us(2);
gpio::write(TRIG, 1);
FreeRtos::delay_us(10);
gpio::write(TRIG, 0);
while gpio::read(ECHO) == 0 {}
let start = Instant::now();
while gpio::read(ECHO) == 1 {}
let duration: f32 = start.elapsed().as_micros() as f32;
let distance: f32 = (duration * 0.034) / 2.0;
return distance as i32;
}
fn main() {
unsafe {
gpio::output_enable(TRIG);
lcd::init(16, 3, 255, 4, 8, 9, 18, 19);
loop {
let msg = "Distance: ";
lcd::set_cursor(0, 0);
for c in msg.chars() {
lcd::write(c as u8);
}
let distance = get_distance().to_string();
lcd::set_cursor(10, 0);
for c in distance.chars() {
lcd::write(c as u8);
}
let end = " ";
for c in end.chars() {
lcd::write(c as u8);
}
FreeRtos::delay_ms(1000);
}
}
}