From 8b2abd2f8d292484cc711bb339967c06552e601c Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 19 Jun 2023 22:38:05 +0100 Subject: [PATCH] Changed RSSI percentage strength function to return the correct percentage. Now takes into account min and max RSSIs. Could potentially use some sort of Logarithmic scale to actually determine the correct RSSI --- src/main.rs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1272242..b868ded 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,26 +24,25 @@ fn main() -> Result<(), EspError> { loop { //Synchronously perform a scan and return nearby APs let ap_scan_res = wifi.scan()?; + let nets = ap_scan_res.into_iter().map(|ap| (ap.ssid, ap.signal_strength, rssi_to_percentage(ap.signal_strength))); println!("\n\n======================================="); - for net in ap_scan_res.into_iter().map(|ap| (ap.ssid, ap.signal_strength, rssi_to_percentage(ap.signal_strength))) { - println!("{}: {}dBm, {:.2}%", net.0, net.1, net.2); + for net in nets { + println!("{}: {}dBm, {:.2}% Signal Strength", net.0, net.1, net.2); } println!("======================================="); } } -fn rssi_to_percentage(signal_strength: i8) -> f32 { - let max_i8 = std::u8::MAX as f32; - let percentage = (((signal_strength as f32) / max_i8) + 0.5) * 100.0; +fn rssi_to_percentage(rssi: i8) -> f32 { + const MIN_RSSI: i8 = -100; // Minimum RSSI value in dBm + const MAX_RSSI: i8 = -5; // Maximum RSSI value in dBm + + // Clamp the RSSI value within the specified range + let clamped_rssi = rssi.min(MAX_RSSI).max(MIN_RSSI); + + // Convert the clamped RSSI to a percentage + let percentage = 100.0 * (clamped_rssi - MIN_RSSI) as f32 / (MAX_RSSI - MIN_RSSI) as f32; + + //Return the percentage RSSI percentage } - -mod tests { - #[cfg(test)] - #[test] - fn rssi_to_percentage_test() { - use super::rssi_to_percentage; - println!("{}", rssi_to_percentage(0)); - assert_eq!(rssi_to_percentage(0), 50.0); - } -}