#![allow(unknown_lints)]
#![allow(unexpected_cfgs)]
fn main() -> anyhow::Result<()> {
esp_idf_svc::sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();
#[cfg(any(esp32h2, esp32c6))]
example::main()?;
#[cfg(not(any(esp32h2, esp32c6)))]
log::error!("This example only works on MCUs that have Thread capabilities, like the ESP32-C6 or ESP32-H2.");
Ok(())
}
#[cfg(any(esp32h2, esp32c6))]
mod example {
use std::sync::Arc;
use log::info;
use esp_idf_svc::eventloop::EspSystemSubscription;
use esp_idf_svc::hal::peripherals::Peripherals;
use esp_idf_svc::io::vfs::MountedEventfs;
use esp_idf_svc::thread::{EspThread, ThreadEvent};
use esp_idf_svc::{eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition};
pub fn main() -> anyhow::Result<()> {
let peripherals = Peripherals::take()?;
let sys_loop = EspSystemEventLoop::take()?;
let nvs = EspDefaultNvsPartition::take()?;
let mounted_event_fs = Arc::new(MountedEventfs::mount(4)?);
info!("Initializing Thread...");
let _subscription = log_thread_sysloop(sys_loop.clone())?;
let mut thread =
EspThread::new(peripherals.modem, sys_loop.clone(), nvs, mounted_event_fs)?;
info!("Thread initialized, now running...");
thread.start()?;
loop {
std::thread::sleep(std::time::Duration::from_secs(2));
}
}
fn log_thread_sysloop(
sys_loop: EspSystemEventLoop,
) -> Result<EspSystemSubscription<'static>, anyhow::Error> {
let subscription = sys_loop.subscribe::<ThreadEvent, _>(|event| {
info!("Got: {event:?}");
})?;
Ok(subscription)
}
}