A RPi Pico powered Lightning Detector

Updated detector rpc configuration

+120 -47
+33 -26
Cargo.lock
··· 93 checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" 94 95 [[package]] 96 name = "bit-set" 97 version = "0.8.0" 98 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 571 [[package]] 572 name = "embassy-strike-driver" 573 version = "0.1.0" 574 - source = "git+https://tangled.org/sachy.dev/strike-sensor#99f235f794688d6f7756f64b4e0224571421b9a1" 575 dependencies = [ 576 "defmt 1.0.1", 577 "embassy-rp", ··· 961 962 [[package]] 963 name = "jiff" 964 - version = "0.2.21" 965 source = "registry+https://github.com/rust-lang/crates.io-index" 966 - checksum = "b3e3d65f018c6ae946ab16e80944b97096ed73c35b221d1c478a6c81d8f57940" 967 dependencies = [ 968 "jiff-static", 969 "portable-atomic", ··· 972 973 [[package]] 974 name = "jiff-static" 975 - version = "0.2.21" 976 source = "registry+https://github.com/rust-lang/crates.io-index" 977 - checksum = "a17c2b211d863c7fde02cbea8a3c1a439b98e109286554f2860bdded7ff83818" 978 dependencies = [ 979 "proc-macro2", 980 "quote", ··· 1024 1025 [[package]] 1026 name = "libc" 1027 - version = "0.2.182" 1028 source = "registry+https://github.com/rust-lang/crates.io-index" 1029 - checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112" 1030 1031 [[package]] 1032 name = "linux-raw-sys" ··· 1292 1293 [[package]] 1294 name = "pin-project-lite" 1295 - version = "0.2.16" 1296 source = "registry+https://github.com/rust-lang/crates.io-index" 1297 - checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" 1298 1299 [[package]] 1300 name = "pio" ··· 1414 1415 [[package]] 1416 name = "quote" 1417 - version = "1.0.44" 1418 source = "registry+https://github.com/rust-lang/crates.io-index" 1419 - checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" 1420 dependencies = [ 1421 "proc-macro2", 1422 ] ··· 1546 [[package]] 1547 name = "sachy-config" 1548 version = "0.1.0" 1549 - source = "git+https://tangled.org/sachy.dev/sachy-embed-core#7f489f3300e0769817b6b8f7394eb36c8e3f8a03" 1550 dependencies = [ 1551 "miette", 1552 "toml_edit", 1553 ] ··· 1555 [[package]] 1556 name = "sachy-fmt" 1557 version = "0.1.0" 1558 - source = "git+https://tangled.org/sachy.dev/sachy-embed-core#7f489f3300e0769817b6b8f7394eb36c8e3f8a03" 1559 dependencies = [ 1560 "defmt 1.0.1", 1561 ] ··· 1563 [[package]] 1564 name = "sachy-mdns" 1565 version = "0.1.0" 1566 - source = "git+https://tangled.org/sachy.dev/sachy-embed-core#7f489f3300e0769817b6b8f7394eb36c8e3f8a03" 1567 dependencies = [ 1568 "defmt 1.0.1", 1569 "embassy-time", ··· 1574 [[package]] 1575 name = "sachy-sntp" 1576 version = "0.1.0" 1577 - source = "git+https://tangled.org/sachy.dev/sachy-embed-core#7f489f3300e0769817b6b8f7394eb36c8e3f8a03" 1578 dependencies = [ 1579 "defmt 1.0.1", 1580 "embassy-net", ··· 1721 [[package]] 1722 name = "striker-proto" 1723 version = "0.1.0" 1724 - source = "git+https://tangled.org/sachy.dev/striker#9ba3ebbda1208b254b6677d7c385cf79de61bad6" 1725 dependencies = [ 1726 "postcard", 1727 "serde", ··· 1861 1862 [[package]] 1863 name = "toml_datetime" 1864 - version = "0.7.5+spec-1.1.0" 1865 source = "registry+https://github.com/rust-lang/crates.io-index" 1866 - checksum = "92e1cfed4a3038bc5a127e35a2d360f145e1f4b971b551a2ba5fd7aedf7e1347" 1867 dependencies = [ 1868 "serde_core", 1869 ] 1870 1871 [[package]] 1872 name = "toml_edit" 1873 - version = "0.24.1+spec-1.1.0" 1874 source = "registry+https://github.com/rust-lang/crates.io-index" 1875 - checksum = "01f2eadbbc6b377a847be05f60791ef1058d9f696ecb51d2c07fe911d8569d8e" 1876 dependencies = [ 1877 "indexmap", 1878 "toml_datetime", ··· 2069 2070 [[package]] 2071 name = "winnow" 2072 - version = "0.7.14" 2073 source = "registry+https://github.com/rust-lang/crates.io-index" 2074 - checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829" 2075 dependencies = [ 2076 "memchr", 2077 ] 2078 2079 [[package]] 2080 name = "zerocopy" 2081 - version = "0.8.39" 2082 source = "registry+https://github.com/rust-lang/crates.io-index" 2083 - checksum = "db6d35d663eadb6c932438e763b262fe1a70987f9ae936e60158176d710cae4a" 2084 dependencies = [ 2085 "zerocopy-derive", 2086 ] 2087 2088 [[package]] 2089 name = "zerocopy-derive" 2090 - version = "0.8.39" 2091 source = "registry+https://github.com/rust-lang/crates.io-index" 2092 - checksum = "4122cd3169e94605190e77839c9a40d40ed048d305bfdc146e7df40ab0f3e517" 2093 dependencies = [ 2094 "proc-macro2", 2095 "quote",
··· 93 checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" 94 95 [[package]] 96 + name = "base64ct" 97 + version = "1.8.3" 98 + source = "registry+https://github.com/rust-lang/crates.io-index" 99 + checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" 100 + 101 + [[package]] 102 name = "bit-set" 103 version = "0.8.0" 104 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 577 [[package]] 578 name = "embassy-strike-driver" 579 version = "0.1.0" 580 + source = "git+https://tangled.org/sachy.dev/strike-sensor#20f871621244db023615d50c3a1be42470e7c73a" 581 dependencies = [ 582 "defmt 1.0.1", 583 "embassy-rp", ··· 967 968 [[package]] 969 name = "jiff" 970 + version = "0.2.23" 971 source = "registry+https://github.com/rust-lang/crates.io-index" 972 + checksum = "1a3546dc96b6d42c5f24902af9e2538e82e39ad350b0c766eb3fbf2d8f3d8359" 973 dependencies = [ 974 "jiff-static", 975 "portable-atomic", ··· 978 979 [[package]] 980 name = "jiff-static" 981 + version = "0.2.23" 982 source = "registry+https://github.com/rust-lang/crates.io-index" 983 + checksum = "2a8c8b344124222efd714b73bb41f8b5120b27a7cc1c75593a6ff768d9d05aa4" 984 dependencies = [ 985 "proc-macro2", 986 "quote", ··· 1030 1031 [[package]] 1032 name = "libc" 1033 + version = "0.2.183" 1034 source = "registry+https://github.com/rust-lang/crates.io-index" 1035 + checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d" 1036 1037 [[package]] 1038 name = "linux-raw-sys" ··· 1298 1299 [[package]] 1300 name = "pin-project-lite" 1301 + version = "0.2.17" 1302 source = "registry+https://github.com/rust-lang/crates.io-index" 1303 + checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" 1304 1305 [[package]] 1306 name = "pio" ··· 1420 1421 [[package]] 1422 name = "quote" 1423 + version = "1.0.45" 1424 source = "registry+https://github.com/rust-lang/crates.io-index" 1425 + checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" 1426 dependencies = [ 1427 "proc-macro2", 1428 ] ··· 1552 [[package]] 1553 name = "sachy-config" 1554 version = "0.1.0" 1555 + source = "git+https://tangled.org/sachy.dev/sachy-embed-core#ee60e727e6d412cd13009809eb08f51a9b441b44" 1556 dependencies = [ 1557 + "base64ct", 1558 "miette", 1559 "toml_edit", 1560 ] ··· 1562 [[package]] 1563 name = "sachy-fmt" 1564 version = "0.1.0" 1565 + source = "git+https://tangled.org/sachy.dev/sachy-embed-core#ee60e727e6d412cd13009809eb08f51a9b441b44" 1566 dependencies = [ 1567 "defmt 1.0.1", 1568 ] ··· 1570 [[package]] 1571 name = "sachy-mdns" 1572 version = "0.1.0" 1573 + source = "git+https://tangled.org/sachy.dev/sachy-embed-core#ee60e727e6d412cd13009809eb08f51a9b441b44" 1574 dependencies = [ 1575 "defmt 1.0.1", 1576 "embassy-time", ··· 1581 [[package]] 1582 name = "sachy-sntp" 1583 version = "0.1.0" 1584 + source = "git+https://tangled.org/sachy.dev/sachy-embed-core#ee60e727e6d412cd13009809eb08f51a9b441b44" 1585 dependencies = [ 1586 "defmt 1.0.1", 1587 "embassy-net", ··· 1728 [[package]] 1729 name = "striker-proto" 1730 version = "0.1.0" 1731 + source = "git+https://tangled.org/sachy.dev/striker#c4fbeb50a975419c7034ea3523fc3cf05d14008c" 1732 dependencies = [ 1733 "postcard", 1734 "serde", ··· 1868 1869 [[package]] 1870 name = "toml_datetime" 1871 + version = "1.0.0+spec-1.1.0" 1872 source = "registry+https://github.com/rust-lang/crates.io-index" 1873 + checksum = "32c2555c699578a4f59f0cc68e5116c8d7cabbd45e1409b989d4be085b53f13e" 1874 dependencies = [ 1875 "serde_core", 1876 ] 1877 1878 [[package]] 1879 name = "toml_edit" 1880 + version = "0.25.4+spec-1.1.0" 1881 source = "registry+https://github.com/rust-lang/crates.io-index" 1882 + checksum = "7193cbd0ce53dc966037f54351dbbcf0d5a642c7f0038c382ef9e677ce8c13f2" 1883 dependencies = [ 1884 "indexmap", 1885 "toml_datetime", ··· 2076 2077 [[package]] 2078 name = "winnow" 2079 + version = "0.7.15" 2080 source = "registry+https://github.com/rust-lang/crates.io-index" 2081 + checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945" 2082 dependencies = [ 2083 "memchr", 2084 ] 2085 2086 [[package]] 2087 name = "zerocopy" 2088 + version = "0.8.40" 2089 source = "registry+https://github.com/rust-lang/crates.io-index" 2090 + checksum = "a789c6e490b576db9f7e6b6d661bcc9799f7c0ac8352f56ea20193b2681532e5" 2091 dependencies = [ 2092 "zerocopy-derive", 2093 ] 2094 2095 [[package]] 2096 name = "zerocopy-derive" 2097 + version = "0.8.40" 2098 source = "registry+https://github.com/rust-lang/crates.io-index" 2099 + checksum = "f65c489a7071a749c849713807783f70672b28094011623e200cb86dcb835953" 2100 dependencies = [ 2101 "proc-macro2", 2102 "quote",
+21 -6
src/detector.rs
··· 1 use alloc::vec::Vec; 2 use sachy_fmt::{info, unwrap}; 3 4 - use embassy_futures::select::select3; 5 use embassy_rp::peripherals::DMA_CH1; 6 use embassy_strike_driver::{ 7 - DetectorConfig, DetectorDriver, ZeroCopyChannel, 8 drivers::rp::{AdcDriver, PwmDriver}, 9 }; 10 use embassy_sync::{blocking_mutex::raw::NoopRawMutex, zerocopy_channel::Channel as ZChannel}; 11 use embassy_time::Timer; 12 13 use crate::{ 14 - constants::{BLIP_SIZE, BLIP_THRESHOLD, BLOCK_SIZE}, 15 rtc::GlobalRtc, 16 - updates::UpdateConnection, 17 utils::{static_alloc, try_buffer, try_static_timestamped_block_vecs}, 18 }; 19 ··· 39 40 let (mut sender, mut receiver) = buf_channel.split(); 41 42 - let config = DetectorConfig::new(BLIP_THRESHOLD, BLIP_SIZE); 43 let mut detector = DetectorDriver::new(config, rtc.track_time().await, pwm, adc); 44 let mut samples = unwrap!(try_buffer(64), "Failed to allocate sample buffer"); 45 let mut peaks = Vec::new(); ··· 52 loop { 53 detector.tune(samples.as_mut_slice()).await; 54 55 - select3( 56 detector.sample_with_zerocopy(&mut sender), 57 detector.detect_with_zerocopy( 58 &mut receiver, ··· 66 detector.reset_timer_source(rtc).await; 67 } 68 }
··· 1 use alloc::vec::Vec; 2 use sachy_fmt::{info, unwrap}; 3 4 + use embassy_futures::select::select4; 5 use embassy_rp::peripherals::DMA_CH1; 6 use embassy_strike_driver::{ 7 + DetectorDriver, ZeroCopyChannel, 8 drivers::rp::{AdcDriver, PwmDriver}, 9 + traits::{AdcSource, PwmSource, TimeSource}, 10 }; 11 use embassy_sync::{blocking_mutex::raw::NoopRawMutex, zerocopy_channel::Channel as ZChannel}; 12 use embassy_time::Timer; 13 14 use crate::{ 15 + constants::BLOCK_SIZE, 16 rtc::GlobalRtc, 17 + updates::{DetectorConfigurationHandle, UpdateConnection}, 18 utils::{static_alloc, try_buffer, try_static_timestamped_block_vecs}, 19 }; 20 ··· 40 41 let (mut sender, mut receiver) = buf_channel.split(); 42 43 + let config = DetectorConfigurationHandle::get_config(); 44 let mut detector = DetectorDriver::new(config, rtc.track_time().await, pwm, adc); 45 let mut samples = unwrap!(try_buffer(64), "Failed to allocate sample buffer"); 46 let mut peaks = Vec::new(); ··· 53 loop { 54 detector.tune(samples.as_mut_slice()).await; 55 56 + select4( 57 + update_detector_config(&detector), 58 detector.sample_with_zerocopy(&mut sender), 59 detector.detect_with_zerocopy( 60 &mut receiver, ··· 68 detector.reset_timer_source(rtc).await; 69 } 70 } 71 + 72 + async fn update_detector_config<T, P, A>(detector: &DetectorDriver<T, P, A>) 73 + where 74 + T: TimeSource, 75 + P: PwmSource, 76 + A: AdcSource, 77 + { 78 + loop { 79 + let update = DetectorConfigurationHandle::get_updated_config().await; 80 + 81 + detector.set_config(update); 82 + } 83 + }
+1
src/locks.rs
··· 4 pub type RtcLock = SpinlockRawMutex<1>; 5 pub type ConnectionStatusLock = SpinlockRawMutex<2>; 6 pub type NetDataLock = SpinlockRawMutex<3>;
··· 4 pub type RtcLock = SpinlockRawMutex<1>; 5 pub type ConnectionStatusLock = SpinlockRawMutex<2>; 6 pub type NetDataLock = SpinlockRawMutex<3>; 7 + pub type ConfigUpdateLock = SpinlockRawMutex<4>;
+20 -11
src/rpc.rs
··· 1 - use alloc::string::ToString; 2 use striker_proto::{Request, Response, StrikerRequest, StrikerResponse}; 3 4 - use crate::constants::{BLIP_SIZE, BLIP_THRESHOLD}; 5 6 pub struct RpcServer; 7 ··· 9 pub async fn handle_request(req: StrikerRequest) -> Option<StrikerResponse> { 10 match req.request { 11 Request::Ping => Some(StrikerResponse::Response(Response::Pong)), 12 - Request::DetectorInfo => Some(StrikerResponse::Response(Response::DetectorInfo { 13 - blip_threshold: BLIP_THRESHOLD as usize, 14 - blip_size: BLIP_SIZE, 15 - max_duty: 100, 16 - duty: 0, 17 - })), 18 - Request::SetDetectorConfig { .. } => { 19 Some(StrikerResponse::Response(Response::SetDetectorConfig { 20 - success: false, 21 - message: Some("NOT IMPLEMENTED".to_string()), 22 })) 23 } 24 }
··· 1 use striker_proto::{Request, Response, StrikerRequest, StrikerResponse}; 2 3 + use crate::updates::{DetectorConfigUpdate, DetectorConfigurationHandle}; 4 5 pub struct RpcServer; 6 ··· 8 pub async fn handle_request(req: StrikerRequest) -> Option<StrikerResponse> { 9 match req.request { 10 Request::Ping => Some(StrikerResponse::Response(Response::Pong)), 11 + Request::DetectorInfo => { 12 + let config = DetectorConfigurationHandle::get_config(); 13 + 14 + Some(StrikerResponse::Response(Response::DetectorInfo { 15 + blip_threshold: config.blip_threshold(), 16 + blip_size: config.blip_size(), 17 + })) 18 + } 19 + Request::SetDetectorConfig { 20 + blip_size, 21 + blip_threshold, 22 + } => { 23 + DetectorConfigurationHandle::set_config(DetectorConfigUpdate { 24 + blip_threshold, 25 + blip_size, 26 + }); 27 + 28 Some(StrikerResponse::Response(Response::SetDetectorConfig { 29 + success: true, 30 + message: None, 31 })) 32 } 33 }
+11 -2
src/state.rs
··· 1 use core::cell::Cell; 2 3 - use embassy_sync::blocking_mutex::Mutex; 4 5 - use crate::locks::ConnectionStatusLock; 6 7 pub static DEVICE_STATE: Mutex<ConnectionStatusLock, Cell<DeviceState>> = 8 Mutex::new(Cell::new(DeviceState::Disconnected)); ··· 12 Disconnected, 13 Connected, 14 }
··· 1 use core::cell::Cell; 2 3 + use embassy_strike_driver::DetectorConfig; 4 + use embassy_sync::{blocking_mutex::Mutex, signal::Signal}; 5 6 + use crate::{ 7 + constants::{BLIP_SIZE, BLIP_THRESHOLD}, 8 + locks::{ConfigUpdateLock, ConnectionStatusLock}, 9 + }; 10 11 pub static DEVICE_STATE: Mutex<ConnectionStatusLock, Cell<DeviceState>> = 12 Mutex::new(Cell::new(DeviceState::Disconnected)); ··· 16 Disconnected, 17 Connected, 18 } 19 + 20 + pub static DETECTOR_CONFIG_UPDATES: Signal<ConfigUpdateLock, DetectorConfig> = Signal::new(); 21 + 22 + pub static DETECTOR_CONFIG: Mutex<ConfigUpdateLock, DetectorConfig> = 23 + Mutex::new(DetectorConfig::new(BLIP_THRESHOLD, BLIP_SIZE));
+34 -2
src/updates.rs
··· 1 - use embassy_strike_driver::DetectorUpdate; 2 use embassy_sync::channel::{Channel, Receiver, Sender}; 3 use sachy_fmt::info; 4 use striker_proto::{StrikerResponse, Update}; 5 6 use crate::{ 7 locks::NetDataLock, 8 - state::{DEVICE_STATE, DeviceState}, 9 }; 10 11 pub type NetDataChannel = Channel<NetDataLock, StrikerResponse, 8>; ··· 75 }; 76 } 77 }
··· 1 + use embassy_strike_driver::{DetectorConfig, DetectorUpdate}; 2 use embassy_sync::channel::{Channel, Receiver, Sender}; 3 use sachy_fmt::info; 4 use striker_proto::{StrikerResponse, Update}; 5 6 use crate::{ 7 locks::NetDataLock, 8 + state::{DETECTOR_CONFIG, DETECTOR_CONFIG_UPDATES, DEVICE_STATE, DeviceState}, 9 }; 10 11 pub type NetDataChannel = Channel<NetDataLock, StrikerResponse, 8>; ··· 75 }; 76 } 77 } 78 + 79 + pub struct DetectorConfigUpdate { 80 + pub blip_threshold: Option<u16>, 81 + pub blip_size: Option<usize>, 82 + } 83 + 84 + pub struct DetectorConfigurationHandle; 85 + 86 + impl DetectorConfigurationHandle { 87 + pub fn set_config(update: DetectorConfigUpdate) { 88 + let update = DETECTOR_CONFIG.lock(|config| { 89 + if let Some(size) = update.blip_size { 90 + config.set_blip_size(size); 91 + } 92 + 93 + if let Some(threshold) = update.blip_threshold { 94 + config.set_blip_threshold(threshold); 95 + } 96 + 97 + config.clone() 98 + }); 99 + DETECTOR_CONFIG_UPDATES.signal(update); 100 + } 101 + 102 + pub fn get_config() -> DetectorConfig { 103 + DETECTOR_CONFIG.lock(Clone::clone) 104 + } 105 + 106 + pub async fn get_updated_config() -> DetectorConfig { 107 + DETECTOR_CONFIG_UPDATES.wait().await 108 + } 109 + }