diff --git a/examples/feinfo.rs b/examples/feinfo.rs index 030640d..ccb8713 100644 --- a/examples/feinfo.rs +++ b/examples/feinfo.rs @@ -23,7 +23,9 @@ fn check_frontend(path: &Path) -> Result<()> { let mut status = FeStatus::default(); status.read(&fe)?; - println!("{}", &status.display(1)); + println!("{}", &status); + + dbg!(&status); Ok(()) } diff --git a/examples/femon.rs b/examples/femon.rs index 3795699..ca348b1 100644 --- a/examples/femon.rs +++ b/examples/femon.rs @@ -28,7 +28,7 @@ pub fn start(fepath: &str) -> Result<()> { loop { status.read(&fe)?; - println!("{}", &status.display(0)); + println!("{}", &status); thread::sleep(delay); } } diff --git a/src/fe/mod.rs b/src/fe/mod.rs index cf0661a..2f53047 100644 --- a/src/fe/mod.rs +++ b/src/fe/mod.rs @@ -42,10 +42,7 @@ use { pub use { - status::{ - FeStatus, - FeStatusDisplay, - }, + status::FeStatus, }; diff --git a/src/fe/status.rs b/src/fe/status.rs index f96a878..846918c 100644 --- a/src/fe/status.rs +++ b/src/fe/status.rs @@ -50,162 +50,79 @@ impl Default for FeStatus { } -/// Helper struct for displaying frontend status -pub struct FeStatusDisplay<'a> { - inner: &'a FeStatus, - verbose: u32, -} - - -impl<'a> FeStatusDisplay<'a> { - fn display_0(&self, f: &mut fmt::Formatter) -> fmt::Result { +/// Returns an object that implements `Display` for different verbosity levels +/// +/// ```text +/// Status:SCVYL S:-38.56dBm (59%) Q:14.57dB (70%) BER:0 UNC:0 +/// ``` +/// +/// Status: +/// - S - Signal +/// - C - Carrier +/// - V - FEC +/// - Y - Sync +/// - L - Lock +impl fmt::Display for FeStatus { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Status:")?; - if self.inner.status == FE_NONE { + if self.status == FE_NONE { write!(f, "OFF")?; return Ok(()); } const STATUS_MAP: &[char] = &['S', 'C', 'V', 'Y', 'L']; for (i, s) in STATUS_MAP.iter().enumerate() { - let c = if self.inner.status & (1 << i) != 0 { *s } else { '_' }; + let c = if self.status & (1 << i) != 0 { *s } else { '_' }; write!(f, "{}", c)?; } - if self.inner.status & FE_HAS_SIGNAL == 0 { + if self.status & FE_HAS_SIGNAL == 0 { return Ok(()); } write!(f, " S:")?; - if let Some((decibel, relative)) = self.inner.get_signal_level() { + if let Some((decibel, relative)) = self.get_signal_level() { write!(f, "{:.02}dBm ({:.0}%)", decibel, relative)?; } else { write!(f, "-")?; } - if self.inner.status & FE_HAS_CARRIER == 0 { + if self.status & FE_HAS_CARRIER == 0 { return Ok(()); } write!(f, " Q:")?; - if let Some((decibel, relative)) = self.inner.get_signal_noise_ratio() { + if let Some((decibel, relative)) = self.get_signal_noise_ratio() { write!(f, "{:.02}dB ({}%)", decibel, relative)?; } else { write!(f, "-")?; } - if self.inner.status & FE_HAS_LOCK == 0 { + if self.status & FE_HAS_LOCK == 0 { return Ok(()); } write!(f, " BER:")?; - if let Some(ber) = self.inner.props[2].get_stats_counter() { + if let Some(ber) = self.props[2].get_stats_counter() { write!(f, "{}", ber & 0xFFFF)?; } else { write!(f, "-")?; } write!(f, " UNC:")?; - if let Some(unc) = self.inner.props[3].get_stats_counter() { - write!(f, "{}", unc & 0xFFFF) - } else { - write!(f, "-") - } - } - - fn display_1(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Status:")?; - - if self.inner.status == FE_NONE { - write!(f, " OFF")?; - return Ok(()); - } - - const STATUS_MAP: &[&str] = &[ - "SIGNAL", "CARRIER", "FEC", "SYNC", "LOCK", "TIMEOUT", "REINIT" - ]; - for (i, s) in STATUS_MAP.iter().enumerate() { - if self.inner.status & (1 << i) != 0 { - write!(f, " {}", s)?; - } - } - - if self.inner.status & FE_HAS_SIGNAL == 0 { - return Ok(()); - } - - write!(f, "\nSignal: ")?; - if let Some((decibel, relative)) = self.inner.get_signal_level() { - write!(f, "{:.02}dBm ({:.0}%)", decibel, relative)?; + if let Some(unc) = self.props[3].get_stats_counter() { + write!(f, "{}", unc & 0xFFFF)?; } else { write!(f, "-")?; } - if self.inner.status & FE_HAS_CARRIER == 0 { - return Ok(()); - } - - write!(f, "\nSNR: ")?; - if let Some((decibel, relative)) = self.inner.get_signal_noise_ratio() { - write!(f, "{:.02}dB ({}%)", decibel, relative)?; - } else { - write!(f, "-")?; - } - - if self.inner.status & FE_HAS_LOCK == 0 { - return Ok(()); - } - - write!(f, "\nBER: ")?; - if let Some(ber) = self.inner.props[2].get_stats_counter() { - write!(f, "{}", ber & 0xFFFF)?; - } else { - write!(f, "-")?; - } - - write!(f, "\nUNC: ")?; - if let Some(unc) = self.inner.props[3].get_stats_counter() { - write!(f, "{}", unc & 0xFFFF) - } else { - write!(f, "-") - } - } -} - - -impl<'a> fmt::Display for FeStatusDisplay<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self.verbose { - 1 => self.display_1(f), - _ => self.display_0(f), - } + Ok(()) } } impl FeStatus { - /// Returns an object that implements `Display` for different verbosity levels - /// Verbosity levels: - /// 0 - single line status - /// ```text - /// Status:SCVYL S:-38.56dBm (59%) Q:14.57dB (70%) BER:0 UNC:0 - /// ``` - /// - /// 1 - full report - /// ```text - /// Status: SIGNAL CARRIER FEC SYNC LOCK - /// Signal: -38.20dBm (59%) - /// SNR: 14.65dB (70%) - /// BER: 0 - /// UNC: 0 - /// ``` - pub fn display(&self, verbose: u32) -> FeStatusDisplay { - FeStatusDisplay { - inner: self, - verbose, - } - } - fn get_signal_level(&self) -> Option<(f64, u64)> { // TODO: config for lo/hi // let lo: f64 = -85.0;