From 6e0cf26d6a88aa2078a6aa52331d8d9482a89089 Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 30 Sep 2020 22:53:21 +0200 Subject: [PATCH] export postfilter + s-h as json --- src/channels.rs | 35 +++++++++++++++++++++++++++++++++++ src/main.rs | 41 ++++++++++------------------------------- src/steinhart_hart.rs | 9 ++++++++- 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/src/channels.rs b/src/channels.rs index cdd591b..d1bb531 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -16,6 +16,7 @@ use crate::{ channel_state::ChannelState, command_parser::{CenterPoint, PwmPin}, pins, + steinhart_hart, }; pub const CHANNELS: usize = 2; @@ -451,6 +452,17 @@ impl Channels { max_i_neg: self.get_max_i_neg(channel).into(), } } + + pub fn postfilter_summary(&mut self, channel: usize) -> PostFilterSummary { + let rate = self.adc.get_postfilter(channel as u8).unwrap() + .and_then(|filter| filter.output_rate()); + PostFilterSummary { channel, rate } + } + + pub fn steinhart_hart_summary(&mut self, channel: usize) -> SteinhartHartSummary { + let params = self.channel_state(channel).sh.clone(); + SteinhartHartSummary { channel, params } + } } type JsonBuffer = heapless::Vec; @@ -524,6 +536,29 @@ impl PwmSummary { } } +#[derive(Serialize)] +pub struct PostFilterSummary { + channel: usize, + rate: Option, +} + +impl PostFilterSummary { + pub fn to_json(&self) -> Result { + serde_json_core::to_vec(self) + } +} + +#[derive(Serialize)] +pub struct SteinhartHartSummary { + channel: usize, + params: steinhart_hart::Parameters, +} + +impl SteinhartHartSummary { + pub fn to_json(&self) -> Result { + serde_json_core::to_vec(self) + } +} #[cfg(test)] mod test { diff --git a/src/main.rs b/src/main.rs index 53f8b12..05b503d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -235,44 +235,23 @@ fn main() -> ! { } Command::Show(ShowCommand::SteinhartHart) => { for channel in 0..CHANNELS { - let state = channels.channel_state(channel); - let _ = writeln!( - socket, "channel {}: Steinhart-Hart equation parameters", - channel, - ); - let _ = writeln!(socket, "- t0={}", state.sh.t0.into_format_args(degree_celsius, Abbreviation)); - let _ = writeln!(socket, "- b={}", state.sh.b); - let _ = writeln!(socket, "- r0={}", state.sh.r0.into_format_args(ohm, Abbreviation)); - match (state.get_adc(), state.get_sens(), state.get_temperature()) { - (Some(adc), Some(sens), Some(temp)) => { - let _ = writeln!( - socket, "- adc={:.6} r={:.0} temp{}={:.3}", - adc.into_format_args(volt, Abbreviation), - sens.into_format_args(ohm, Abbreviation), - channel, - temp.into_format_args(degree_celsius, Abbreviation), - ); + match channels.steinhart_hart_summary(channel).to_json() { + Ok(buf) => { + send_line(&mut socket, &buf); } - _ => {} + Err(e) => + error!("unable to serialize steinhart-hart summary: {:?}", e), } - let _ = writeln!(socket, ""); } } Command::Show(ShowCommand::PostFilter) => { for channel in 0..CHANNELS { - match channels.adc.get_postfilter(channel as u8).unwrap() { - Some(filter) => { - let _ = writeln!( - socket, "channel {}: postfilter={:.2} SPS", - channel, filter.output_rate().unwrap() - ); - } - None => { - let _ = writeln!( - socket, "channel {}: postfilter disabled", - channel - ); + match channels.postfilter_summary(channel).to_json() { + Ok(buf) => { + send_line(&mut socket, &buf); } + Err(e) => + error!("unable to serialize postfilter summary: {:?}", e), } } } diff --git a/src/steinhart_hart.rs b/src/steinhart_hart.rs index f0b231f..faab417 100644 --- a/src/steinhart_hart.rs +++ b/src/steinhart_hart.rs @@ -8,9 +8,12 @@ use uom::si::{ ratio::ratio, thermodynamic_temperature::{degree_celsius, kelvin}, }; +use serde::Serialize; + +type JsonBuffer = heapless::Vec; /// Steinhart-Hart equation parameters -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize)] pub struct Parameters { /// Base temperature pub t0: ThermodynamicTemperature, @@ -30,6 +33,10 @@ impl Parameters { let inv_temp = 1.0 / self.t0.get::() + (r / self.r0).get::().ln() / self.b; ThermodynamicTemperature::new::(1.0 / inv_temp) } + + pub fn to_json(&self) -> Result { + serde_json_core::to_vec(self) + } } impl Default for Parameters {