From 35dfba99e12443dce12b7b85a8a31ca6580f616e Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 19 Sep 2019 03:29:06 +0200 Subject: [PATCH] style --- firmware/src/command_parser.rs | 50 ++++++++++++++++++---------------- firmware/src/main.rs | 10 +++++-- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/firmware/src/command_parser.rs b/firmware/src/command_parser.rs index 3a28ae6..b906738 100644 --- a/firmware/src/command_parser.rs +++ b/firmware/src/command_parser.rs @@ -123,17 +123,22 @@ fn whitespace(input: &[u8]) -> IResult<&[u8], ()> { fold_many1(char(' '), (), |(), _| ())(input) } -fn unsigned(input: &[u8]) -> IResult<&[u8], Result> { +fn unsigned(input: &[u8]) -> IResult<&[u8], Result> { take_while1(is_digit)(input) - .map(|(input, digits)| (input, lexical::parse(digits))) + .map(|(input, digits)| { + let result = lexical::parse(digits) + .map_err(|e| e.into()); + (input, result) + }) } -fn float(input: &[u8]) -> IResult<&[u8], Result> { +fn float(input: &[u8]) -> IResult<&[u8], Result> { let (input, sign) = is_a("-")(input)?; let negative = sign.len() > 0; let (input, digits) = take_while1(|c| is_digit(c) || c == '.' as u8)(input)?; let result = lexical::parse(digits) - .map(|result: f32| if negative { -result } else { result }); + .map(|result: f32| if negative { -result } else { result }) + .map_err(|e| e.into()); Ok((input, result)) } @@ -172,9 +177,7 @@ fn report(input: &[u8]) -> IResult<&[u8], Command> { } /// `pwm <0-1> ` - Set pwm duty cycle -fn pwm_manual(input: &[u8]) -> IResult<&[u8], Result> { - let (input, channel) = channel(input)?; - let (input, _) = whitespace(input)?; +fn pwm_manual(input: &[u8]) -> IResult<&[u8], Result> { let (input, width) = unsigned(input)?; let width = match width { Ok(width) => width, @@ -186,20 +189,12 @@ fn pwm_manual(input: &[u8]) -> IResult<&[u8], Result> { Ok(total) => total, Err(e) => return Ok((input, Err(e.into()))), }; - Ok((input, Ok(Command::Pwm { - channel, - mode: PwmMode::Manual { width, total }, - }))) + Ok((input, Ok(PwmMode::Manual { width, total }))) } /// `pwm <0-1> pid` - Set PWM to be controlled by PID -fn pwm_pid(input: &[u8]) -> IResult<&[u8], Result> { - let (input, channel) = channel(input)?; - let (input, _) = whitespace(input)?; - value(Ok(Command::Pwm { - channel, - mode: PwmMode::Pid, - }), tag("pid"))(input) +fn pwm_pid(input: &[u8]) -> IResult<&[u8], Result> { + value(Ok(PwmMode::Pid), tag("pid"))(input) } fn pwm(input: &[u8]) -> IResult<&[u8], Result> { @@ -207,10 +202,18 @@ fn pwm(input: &[u8]) -> IResult<&[u8], Result> { alt(( preceded( whitespace, - alt(( - pwm_pid, - pwm_manual, - ))), + map( + separated_pair( + channel, + whitespace, + alt(( + pwm_pid, + pwm_manual, + )) + ), + |(channel, mode)| mode.map(|mode| Command::Pwm { channel, mode }) + ) + ), value(Ok(Command::Show(ShowCommand::Pwm)), end) ))(input) } @@ -232,8 +235,7 @@ fn pid_parameter(input: &[u8]) -> IResult<&[u8], Result> { let (input, _) = whitespace(input)?; let (input, value) = float(input)?; let result = value - .map(|value| Command::Pid { channel, parameter, value }) - .map_err(|e| e.into()); + .map(|value| Command::Pid { channel, parameter, value }); Ok((input, result)) } diff --git a/firmware/src/main.rs b/firmware/src/main.rs index a209073..0c13cb1 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -280,7 +280,7 @@ fn main() -> ! { Command::Show(ShowCommand::Pid) => { for (channel, state) in states.iter().enumerate() { let _ = writeln!(socket, "PID settings for channel {}", channel); - let pid = &states[channel].pid; + let pid = &state.pid; let _ = writeln!(socket, "- target={:.4}", pid.get_target()); let p = pid.get_parameters(); macro_rules! out { @@ -299,7 +299,8 @@ fn main() -> ! { } Command::Show(ShowCommand::Pwm) => { for (channel, state) in states.iter().enumerate() { - let _ = writeln!(socket, "PWM {}: PID {}", + let _ = writeln!( + socket, "PWM {}: PID {}", channel, if state.pid_enabled { "engaged" } else { "disengaged" } ); @@ -327,7 +328,10 @@ fn main() -> ! { states[channel].pid_enabled = false; board::set_timer_pwm(width, total); if channel == 0 { // TODO - let _ = writeln!(socket, "channel {}: PWM duty cycle manually set to {}/{}", channel, width, total); + let _ = writeln!( + socket, "channel {}: PWM duty cycle manually set to {}/{}", + channel, width, total + ); } } Command::Pwm { channel, mode: PwmMode::Pid } => {