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
This commit is contained in:
parent
63b6627867
commit
8b2abd2f8d
29
src/main.rs
29
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user