A RPi Pico powered Lightning Detector
1use alloc::vec;
2use embassy_net::udp::UdpSocket;
3use embassy_time::{Timer, WithTimeout};
4use sachy_fmt::unwrap;
5use sachy_mdns::{GROUP_SOCK_V4, MDNS_PORT, service::MdnsService, state::MdnsAction};
6
7pub async fn mdns_loop<'device>(service: &mut MdnsService, udp: &mut UdpSocket<'device>) {
8 unwrap!(udp.bind(MDNS_PORT));
9
10 let mut send_buf = vec![0u8; 2048];
11
12 loop {
13 if let Some(bytes) = match service.next_action() {
14 MdnsAction::Announce => service.send_announcement(&mut send_buf),
15
16 MdnsAction::ListenFor { timeout } => udp
17 .recv_from_with(|buf, _from| service.listen_for_queries(buf, &mut send_buf))
18 .with_timeout(timeout)
19 .await
20 .ok()
21 .flatten(),
22
23 MdnsAction::WaitFor { duration } => {
24 Timer::after(duration).await;
25
26 None
27 }
28 } {
29 if udp.send_to(bytes, GROUP_SOCK_V4).await.is_ok() {
30 udp.flush().await;
31 } else {
32 break;
33 }
34 }
35 }
36}