use core::fmt::Error;
use ds18b20::{Ds18b20, Resolution};
use esp_idf_svc::hal::delay;
use esp_idf_svc::hal::gpio::*;
use esp_idf_svc::hal::peripherals::Peripherals;
use liquid_crystal::prelude::*;
use liquid_crystal::Parallel;
use one_wire_bus::OneWire;
fn main() -> anyhow::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();
log::info!("Hello, world!");
println!("Hello world!");
let peripherals = Peripherals::take()?;
let mut led = PinDriver::output(peripherals.pins.gpio4)?;
let rs = PinDriver::output(peripherals.pins.gpio42)?;
let en = PinDriver::output(peripherals.pins.gpio41)?;
let d4 = PinDriver::output(peripherals.pins.gpio40)?;
let d5 = PinDriver::output(peripherals.pins.gpio39)?;
let d6 = PinDriver::output(peripherals.pins.gpio38)?;
let d7 = PinDriver::output(peripherals.pins.gpio37)?;
let one_wire_pin = PinDriver::input_output(peripherals.pins.gpio36)?;
let mut lcd_interface = Parallel::new(d4, d5, d6, d7, rs, en, lcd_dummy);
let mut lcd = LiquidCrystal::new(&mut lcd_interface, Bus4Bits, LCD20X4);
let mut delay = delay::Delay::new_default();
lcd.begin(&mut delay);
//Rust logo
let rust1: [u8; 8] = [
0b00001, 0b00011, 0b00011, 0b01110, 0b11100, 0b11000, 0b01000, 0b01000,
];
let rust2: [u8; 8] = [
0b10001, 0b11111, 0b00000, 0b00000, 0b11110, 0b10001, 0b10001, 0b11110,
];
let rust3: [u8; 8] = [
0b10000, 0b11000, 0b11000, 0b01110, 0b00111, 0b00011, 0b00010, 0b000010,
];
let rust4: [u8; 8] = [
0b01000, 0b01000, 0b11000, 0b11100, 0b01110, 0b00011, 0b00011, 0b00001,
];
let rust5: [u8; 8] = [
0b11000, 0b10100, 0b10010, 0b00000, 0b00000, 0b00000, 0b11111, 0b10001,
];
let rust6: [u8; 8] = [
0b00010, 0b00010, 0b00011, 0b00111, 0b01110, 0b11000, 0b11000, 0b10000,
];
lcd.custom_char(&mut delay, &rust1, 0);
lcd.custom_char(&mut delay, &rust2, 1);
lcd.custom_char(&mut delay, &rust3, 2);
lcd.custom_char(&mut delay, &rust4, 3);
lcd.custom_char(&mut delay, &rust5, 4);
lcd.custom_char(&mut delay, &rust6, 5);
lcd.write(&mut delay, Text("hello World!"))
.write(&mut delay, Command(MoveLine2))
.write(&mut delay, Text("made in Rust!"));
lcd.set_cursor(&mut delay, 0, 13)
.write(&mut delay, CustomChar(0))
.write(&mut delay, CustomChar(1))
.write(&mut delay, CustomChar(2));
lcd.set_cursor(&mut delay, 1, 13)
.write(&mut delay, CustomChar(3))
.write(&mut delay, CustomChar(4))
.write(&mut delay, CustomChar(5));
let mut one_wire_bus = OneWire::new(one_wire_pin).unwrap();
loop {
led.set_high()?;
// we are sleeping here to make sure the watchdog isn't triggered
delay.delay_ms(1000);
led.set_low()?;
delay.delay_ms(1000);
for device_address in one_wire_bus.devices(false, &mut delay) {
match device_address {
Ok(device_address) =>
// The family code can be used to identify the type of device
// If supported, another crate can be used to interact with that device at the given address
{
log::info!(
"Found device at address {:?} with family code: {:#x?}",
device_address,
device_address.family_code()
)
}
Err(err) => log::error!("Error searching for devices: {:?}", err),
}
}
ds18b20::start_simultaneous_temp_measurement(&mut one_wire_bus, &mut delay).unwrap();
// wait until the measurement is done. This depends on the resolution you specified
// If you don't know the resolution, you can obtain it from reading the sensor data,
// or just wait the longest time, which is the 13-bit resolution (750ms)
Resolution::Bits12.delay_for_measurement_time(&mut delay);
// iterate over all the devices, and report their temperature
let mut search_state = None;
loop {
let result = one_wire_bus.device_search(search_state.as_ref(), false, &mut delay);
if let Err(err) = result {
log::error!("Error searching for devices: {:?}", err);
break;
}
if let Some((device_address, state)) = result.unwrap() {
search_state = Some(state);
if device_address.family_code() != ds18b20::FAMILY_CODE {
// skip other devices
continue;
}
// You will generally create the sensor once, and save it for later
let sensor: Ds18b20 = Ds18b20::new::<Error>(device_address).unwrap();
// contains the read temperature, as well as config info such as the resolution used
let sensor_data = sensor.read_data(&mut one_wire_bus, &mut delay).unwrap();
lcd.set_cursor(&mut delay, 2, 0).write(
&mut delay,
Text(&format!("temp: {}", sensor_data.temperature)),
);
break;
} else {
log::warn!("No temp sensors found");
break;
}
}
// delay.delay_ms(2000_u16);
}
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1
Loading
ds18b20
ds18b20