export postfilter + s-h as json

pull/20/head
Astro 2020-09-30 22:53:21 +02:00
parent d4901cbab1
commit 6e0cf26d6a
3 changed files with 53 additions and 32 deletions

View File

@ -16,6 +16,7 @@ use crate::{
channel_state::ChannelState, channel_state::ChannelState,
command_parser::{CenterPoint, PwmPin}, command_parser::{CenterPoint, PwmPin},
pins, pins,
steinhart_hart,
}; };
pub const CHANNELS: usize = 2; pub const CHANNELS: usize = 2;
@ -451,6 +452,17 @@ impl Channels {
max_i_neg: self.get_max_i_neg(channel).into(), 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<u8, heapless::consts::U512>; type JsonBuffer = heapless::Vec<u8, heapless::consts::U512>;
@ -524,6 +536,29 @@ impl PwmSummary {
} }
} }
#[derive(Serialize)]
pub struct PostFilterSummary {
channel: usize,
rate: Option<f32>,
}
impl PostFilterSummary {
pub fn to_json(&self) -> Result<JsonBuffer, serde_json_core::ser::Error> {
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<JsonBuffer, serde_json_core::ser::Error> {
serde_json_core::to_vec(self)
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {

View File

@ -235,44 +235,23 @@ fn main() -> ! {
} }
Command::Show(ShowCommand::SteinhartHart) => { Command::Show(ShowCommand::SteinhartHart) => {
for channel in 0..CHANNELS { for channel in 0..CHANNELS {
let state = channels.channel_state(channel); match channels.steinhart_hart_summary(channel).to_json() {
let _ = writeln!( Ok(buf) => {
socket, "channel {}: Steinhart-Hart equation parameters", send_line(&mut socket, &buf);
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),
);
} }
_ => {} Err(e) =>
error!("unable to serialize steinhart-hart summary: {:?}", e),
} }
let _ = writeln!(socket, "");
} }
} }
Command::Show(ShowCommand::PostFilter) => { Command::Show(ShowCommand::PostFilter) => {
for channel in 0..CHANNELS { for channel in 0..CHANNELS {
match channels.adc.get_postfilter(channel as u8).unwrap() { match channels.postfilter_summary(channel).to_json() {
Some(filter) => { Ok(buf) => {
let _ = writeln!( send_line(&mut socket, &buf);
socket, "channel {}: postfilter={:.2} SPS",
channel, filter.output_rate().unwrap()
);
}
None => {
let _ = writeln!(
socket, "channel {}: postfilter disabled",
channel
);
} }
Err(e) =>
error!("unable to serialize postfilter summary: {:?}", e),
} }
} }
} }

View File

@ -8,9 +8,12 @@ use uom::si::{
ratio::ratio, ratio::ratio,
thermodynamic_temperature::{degree_celsius, kelvin}, thermodynamic_temperature::{degree_celsius, kelvin},
}; };
use serde::Serialize;
type JsonBuffer = heapless::Vec<u8, heapless::consts::U200>;
/// Steinhart-Hart equation parameters /// Steinhart-Hart equation parameters
#[derive(Clone, Debug)] #[derive(Clone, Debug, Serialize)]
pub struct Parameters { pub struct Parameters {
/// Base temperature /// Base temperature
pub t0: ThermodynamicTemperature, pub t0: ThermodynamicTemperature,
@ -30,6 +33,10 @@ impl Parameters {
let inv_temp = 1.0 / self.t0.get::<kelvin>() + (r / self.r0).get::<ratio>().ln() / self.b; let inv_temp = 1.0 / self.t0.get::<kelvin>() + (r / self.r0).get::<ratio>().ln() / self.b;
ThermodynamicTemperature::new::<kelvin>(1.0 / inv_temp) ThermodynamicTemperature::new::<kelvin>(1.0 / inv_temp)
} }
pub fn to_json(&self) -> Result<JsonBuffer, serde_json_core::ser::Error> {
serde_json_core::to_vec(self)
}
} }
impl Default for Parameters { impl Default for Parameters {