From 97a09e422bf335e28bebae19279b1ec60b9c38e1 Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 26 Sep 2020 01:29:35 +0200 Subject: [PATCH] main: add support for disabling postfilters --- README.md | 2 ++ src/command_parser.rs | 43 +++++++++++++++++++++++++++++++++---------- src/main.rs | 11 +++++++++-- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 57a2892..8ae41e3 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,8 @@ The scope of this setting is per TCP session. | `pid <0/1> integral_max ` | Set integral upper bound | | `s-h` | Show Steinhart-Hart equation parameters | | `s-h <0/1> ` | Set Steinhart-Hart parameter for a channel | +| `postfilter` | Show postfilter settings | +| `postfilter <0/1> off` | Disable postfilter | | `postfilter <0/1> rate ` | Set postfilter output data rate | | `load` | Restore configuration from EEPROM | | `save` | Save configuration to EEPROM | diff --git a/src/command_parser.rs b/src/command_parser.rs index ac8473c..c93545a 100644 --- a/src/command_parser.rs +++ b/src/command_parser.rs @@ -164,7 +164,7 @@ pub enum Command { }, PostFilter { channel: usize, - rate: f32, + rate: Option, }, } @@ -392,15 +392,23 @@ fn postfilter(input: &[u8]) -> IResult<&[u8], Result> { |input| { let (input, channel) = channel(input)?; let (input, _) = whitespace(input)?; - let (input, _) = tag("rate")(input)?; - let (input, _) = whitespace(input)?; - let (input, rate) = float(input)?; - let result = rate - .map(|rate| Command::PostFilter { + alt(( + value(Ok(Command::PostFilter { channel, - rate: rate as f32, - }); - Ok((input, result)) + rate: None, + }), tag("off")), + move |input| { + let (input, _) = tag("rate")(input)?; + let (input, _) = whitespace(input)?; + let (input, rate) = float(input)?; + let result = rate + .map(|rate| Command::PostFilter { + channel, + rate: Some(rate as f32), + }); + Ok((input, result)) + } + ))(input) } ), value(Ok(Command::Show(ShowCommand::PostFilter)), end) @@ -570,12 +578,27 @@ mod test { })); } + #[test] + fn parse_postfilter() { + let command = Command::parse(b"postfilter"); + assert_eq!(command, Ok(Command::Show(ShowCommand::PostFilter))); + } + + #[test] + fn parse_postfilter_off() { + let command = Command::parse(b"postfilter 1 off"); + assert_eq!(command, Ok(Command::PostFilter { + channel: 1, + rate: None, + })); + } + #[test] fn parse_postfilter_rate() { let command = Command::parse(b"postfilter 0 rate 21"); assert_eq!(command, Ok(Command::PostFilter { channel: 0, - rate: 21.0, + rate: Some(21.0), })); } diff --git a/src/main.rs b/src/main.rs index 8e4f197..641b2e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -308,7 +308,7 @@ fn main() -> ! { } None => { let _ = writeln!( - socket, "channel {}: no postfilter", + socket, "channel {}: postfilter disabled", channel ); } @@ -421,7 +421,14 @@ fn main() -> ! { } let _ = writeln!(socket, "Steinhart-Hart equation parameter updated"); } - Command::PostFilter { channel, rate } => { + Command::PostFilter { channel, rate: None } => { + channels.adc.set_postfilter(channel as u8, None).unwrap(); + let _ = writeln!( + socket, "channel {}: postfilter disabled", + channel + ); + } + Command::PostFilter { channel, rate: Some(rate) } => { let filter = ad7172::PostFilter::closest(rate); match filter { Some(filter) => {