2020-03-14 06:39:22 +08:00
|
|
|
use core::fmt;
|
2020-03-20 02:51:59 +08:00
|
|
|
use core::num::ParseIntError;
|
|
|
|
use core::str::{from_utf8, Utf8Error};
|
2020-03-14 06:39:22 +08:00
|
|
|
use nom::{
|
|
|
|
branch::alt,
|
|
|
|
bytes::complete::{is_a, tag, take_while1},
|
2024-10-14 11:52:15 +08:00
|
|
|
character::{
|
|
|
|
complete::{char, one_of},
|
|
|
|
is_digit,
|
|
|
|
},
|
2020-03-14 06:39:22 +08:00
|
|
|
combinator::{complete, map, opt, value},
|
|
|
|
error::ErrorKind,
|
2024-10-14 11:52:15 +08:00
|
|
|
multi::{fold_many0, fold_many1},
|
|
|
|
sequence::preceded,
|
|
|
|
IResult, Needed,
|
2020-03-14 06:39:22 +08:00
|
|
|
};
|
2020-03-20 02:51:59 +08:00
|
|
|
use num_traits::{Num, ParseFloatError};
|
2024-10-14 11:52:15 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-03-14 06:39:22 +08:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum Error {
|
|
|
|
Parser(ErrorKind),
|
|
|
|
Incomplete,
|
|
|
|
UnexpectedInput(u8),
|
2020-03-20 02:51:59 +08:00
|
|
|
Utf8(Utf8Error),
|
|
|
|
ParseInt(ParseIntError),
|
|
|
|
// `num_traits::ParseFloatError` does not impl Clone
|
|
|
|
ParseFloat,
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t> From<nom::Err<(&'t [u8], ErrorKind)>> for Error {
|
|
|
|
fn from(e: nom::Err<(&'t [u8], ErrorKind)>) -> Self {
|
|
|
|
match e {
|
2024-10-14 11:52:15 +08:00
|
|
|
nom::Err::Incomplete(_) => Error::Incomplete,
|
|
|
|
nom::Err::Error((_, e)) => Error::Parser(e),
|
|
|
|
nom::Err::Failure((_, e)) => Error::Parser(e),
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 02:51:59 +08:00
|
|
|
impl From<Utf8Error> for Error {
|
|
|
|
fn from(e: Utf8Error) -> Self {
|
|
|
|
Error::Utf8(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseIntError> for Error {
|
|
|
|
fn from(e: ParseIntError) -> Self {
|
|
|
|
Error::ParseInt(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseFloatError> for Error {
|
|
|
|
fn from(_: ParseFloatError) -> Self {
|
|
|
|
Error::ParseFloat
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
match self {
|
2024-10-14 11:52:15 +08:00
|
|
|
Error::Incomplete => "incomplete input".fmt(fmt),
|
2020-03-14 06:39:22 +08:00
|
|
|
Error::UnexpectedInput(c) => {
|
|
|
|
"unexpected input: ".fmt(fmt)?;
|
|
|
|
c.fmt(fmt)
|
|
|
|
}
|
|
|
|
Error::Parser(e) => {
|
|
|
|
"parser: ".fmt(fmt)?;
|
|
|
|
(e as &dyn core::fmt::Debug).fmt(fmt)
|
|
|
|
}
|
2020-03-20 02:51:59 +08:00
|
|
|
Error::Utf8(e) => {
|
|
|
|
"utf8: ".fmt(fmt)?;
|
|
|
|
(e as &dyn core::fmt::Debug).fmt(fmt)
|
|
|
|
}
|
|
|
|
Error::ParseInt(e) => {
|
|
|
|
"parsing int: ".fmt(fmt)?;
|
2020-03-14 06:39:22 +08:00
|
|
|
(e as &dyn core::fmt::Debug).fmt(fmt)
|
|
|
|
}
|
2024-10-14 11:52:15 +08:00
|
|
|
Error::ParseFloat => "parsing float".fmt(fmt),
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 06:44:16 +08:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Ipv4Config {
|
|
|
|
pub address: [u8; 4],
|
|
|
|
pub mask_len: u8,
|
|
|
|
pub gateway: Option<[u8; 4]>,
|
|
|
|
}
|
|
|
|
|
2020-03-14 06:39:22 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum ShowCommand {
|
|
|
|
Input,
|
|
|
|
Pwm,
|
|
|
|
Pid,
|
|
|
|
SteinhartHart,
|
|
|
|
PostFilter,
|
2020-12-21 03:44:10 +08:00
|
|
|
Ipv4,
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum PidParameter {
|
|
|
|
Target,
|
|
|
|
KP,
|
|
|
|
KI,
|
|
|
|
KD,
|
|
|
|
OutputMin,
|
|
|
|
OutputMax,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Steinhart-Hart equation parameter
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum ShParameter {
|
2020-03-20 01:34:57 +08:00
|
|
|
T0,
|
2020-03-20 05:56:14 +08:00
|
|
|
B,
|
2020-03-20 01:34:57 +08:00
|
|
|
R0,
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
2020-03-20 01:34:57 +08:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum PwmPin {
|
|
|
|
ISet,
|
|
|
|
MaxIPos,
|
|
|
|
MaxINeg,
|
|
|
|
MaxV,
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 07:18:33 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
2020-09-24 04:30:04 +08:00
|
|
|
pub enum CenterPoint {
|
|
|
|
Vref,
|
2020-09-25 02:45:07 +08:00
|
|
|
Override(f32),
|
2020-09-24 04:30:04 +08:00
|
|
|
}
|
|
|
|
|
2024-08-20 17:42:44 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum Polarity {
|
|
|
|
Normal,
|
|
|
|
Reversed,
|
|
|
|
}
|
|
|
|
|
2020-03-14 06:39:22 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum Command {
|
|
|
|
Quit,
|
2020-12-12 08:25:07 +08:00
|
|
|
Load {
|
|
|
|
channel: Option<usize>,
|
|
|
|
},
|
|
|
|
Save {
|
|
|
|
channel: Option<usize>,
|
|
|
|
},
|
2020-09-25 06:14:29 +08:00
|
|
|
Reset,
|
2020-12-13 06:44:16 +08:00
|
|
|
Ipv4(Ipv4Config),
|
2020-03-14 06:39:22 +08:00
|
|
|
Show(ShowCommand),
|
2020-03-20 01:34:57 +08:00
|
|
|
/// PWM parameter setting
|
2020-03-14 06:39:22 +08:00
|
|
|
Pwm {
|
|
|
|
channel: usize,
|
2020-03-20 01:34:57 +08:00
|
|
|
pin: PwmPin,
|
2020-09-17 04:05:31 +08:00
|
|
|
value: f64,
|
2020-03-14 06:39:22 +08:00
|
|
|
},
|
2020-03-20 01:34:57 +08:00
|
|
|
/// Enable PID control for `i_set`
|
|
|
|
PwmPid {
|
|
|
|
channel: usize,
|
|
|
|
},
|
2024-08-20 17:42:44 +08:00
|
|
|
PwmPolarity {
|
|
|
|
channel: usize,
|
|
|
|
polarity: Polarity,
|
|
|
|
},
|
2020-09-24 04:30:04 +08:00
|
|
|
CenterPoint {
|
|
|
|
channel: usize,
|
|
|
|
center: CenterPoint,
|
|
|
|
},
|
2020-03-20 01:34:57 +08:00
|
|
|
/// PID parameter setting
|
2020-03-14 06:39:22 +08:00
|
|
|
Pid {
|
|
|
|
channel: usize,
|
|
|
|
parameter: PidParameter,
|
2020-03-19 04:56:52 +08:00
|
|
|
value: f64,
|
2020-03-14 06:39:22 +08:00
|
|
|
},
|
|
|
|
SteinhartHart {
|
|
|
|
channel: usize,
|
|
|
|
parameter: ShParameter,
|
2020-03-19 04:56:52 +08:00
|
|
|
value: f64,
|
2020-03-14 06:39:22 +08:00
|
|
|
},
|
|
|
|
PostFilter {
|
|
|
|
channel: usize,
|
2020-09-26 07:29:35 +08:00
|
|
|
rate: Option<f32>,
|
2020-03-14 06:39:22 +08:00
|
|
|
},
|
2021-01-13 11:59:06 +08:00
|
|
|
Dfu,
|
2023-03-22 17:15:49 +08:00
|
|
|
FanSet {
|
2024-10-14 11:52:15 +08:00
|
|
|
fan_pwm: u32,
|
2023-03-22 17:15:49 +08:00
|
|
|
},
|
|
|
|
FanAuto,
|
|
|
|
ShowFan,
|
|
|
|
FanCurve {
|
|
|
|
k_a: f32,
|
|
|
|
k_b: f32,
|
|
|
|
k_c: f32,
|
|
|
|
},
|
|
|
|
FanCurveDefaults,
|
|
|
|
ShowHWRev,
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn end(input: &[u8]) -> IResult<&[u8], ()> {
|
2024-10-14 11:52:15 +08:00
|
|
|
complete(fold_many0(one_of("\r\n\t "), (), |(), _| ()))(input)
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn whitespace(input: &[u8]) -> IResult<&[u8], ()> {
|
|
|
|
fold_many1(char(' '), (), |(), _| ())(input)
|
|
|
|
}
|
|
|
|
|
2020-10-01 07:34:46 +08:00
|
|
|
fn unsigned(input: &[u8]) -> IResult<&[u8], Result<u32, Error>> {
|
2024-10-14 11:52:15 +08:00
|
|
|
take_while1(is_digit)(input).map(|(input, digits)| {
|
|
|
|
let result = from_utf8(digits)
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
.and_then(|digits| u32::from_str_radix(digits, 10).map_err(|e| e.into()));
|
|
|
|
(input, result)
|
|
|
|
})
|
2020-10-01 07:34:46 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 04:56:52 +08:00
|
|
|
fn float(input: &[u8]) -> IResult<&[u8], Result<f64, Error>> {
|
2020-03-14 06:39:22 +08:00
|
|
|
let (input, sign) = opt(is_a("-"))(input)?;
|
|
|
|
let negative = sign.is_some();
|
|
|
|
let (input, digits) = take_while1(|c| is_digit(c) || c == '.' as u8)(input)?;
|
2024-10-14 11:52:15 +08:00
|
|
|
let result = from_utf8(digits)
|
2020-03-20 02:51:59 +08:00
|
|
|
.map_err(|e| e.into())
|
2024-10-14 11:52:15 +08:00
|
|
|
.and_then(|digits| f64::from_str_radix(digits, 10).map_err(|e| e.into()))
|
2020-03-20 02:51:59 +08:00
|
|
|
.map(|result: f64| if negative { -result } else { result });
|
2020-03-14 06:39:22 +08:00
|
|
|
Ok((input, result))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn channel(input: &[u8]) -> IResult<&[u8], usize> {
|
|
|
|
map(one_of("01"), |c| (c as usize) - ('0' as usize))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report(input: &[u8]) -> IResult<&[u8], Command> {
|
|
|
|
preceded(
|
|
|
|
tag("report"),
|
2024-09-16 11:52:15 +08:00
|
|
|
// `report` - Report once
|
2024-10-14 11:52:15 +08:00
|
|
|
value(Command::Show(ShowCommand::Input), end),
|
2020-03-14 06:39:22 +08:00
|
|
|
)(input)
|
|
|
|
}
|
|
|
|
|
2020-05-17 07:23:35 +08:00
|
|
|
fn pwm_setup(input: &[u8]) -> IResult<&[u8], Result<(PwmPin, f64), Error>> {
|
2024-10-14 11:52:15 +08:00
|
|
|
let result_with_pin =
|
|
|
|
|pin: PwmPin| move |result: Result<f64, Error>| result.map(|value| (pin, value));
|
2020-03-14 06:39:22 +08:00
|
|
|
|
|
|
|
alt((
|
2020-12-09 08:07:08 +08:00
|
|
|
map(
|
2024-10-14 11:52:15 +08:00
|
|
|
preceded(tag("i_set"), preceded(whitespace, float)),
|
|
|
|
result_with_pin(PwmPin::ISet),
|
2020-12-09 08:07:08 +08:00
|
|
|
),
|
2020-03-14 06:39:22 +08:00
|
|
|
map(
|
2024-10-14 11:52:15 +08:00
|
|
|
preceded(tag("max_i_pos"), preceded(whitespace, float)),
|
|
|
|
result_with_pin(PwmPin::MaxIPos),
|
2020-03-14 06:39:22 +08:00
|
|
|
),
|
|
|
|
map(
|
2024-10-14 11:52:15 +08:00
|
|
|
preceded(tag("max_i_neg"), preceded(whitespace, float)),
|
|
|
|
result_with_pin(PwmPin::MaxINeg),
|
2020-03-14 06:39:22 +08:00
|
|
|
),
|
|
|
|
map(
|
2024-10-14 11:52:15 +08:00
|
|
|
preceded(tag("max_v"), preceded(whitespace, float)),
|
|
|
|
result_with_pin(PwmPin::MaxV),
|
|
|
|
),
|
|
|
|
))(input)
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// `pwm <0-1> pid` - Set PWM to be controlled by PID
|
2020-03-20 01:34:57 +08:00
|
|
|
fn pwm_pid(input: &[u8]) -> IResult<&[u8], ()> {
|
|
|
|
value((), tag("pid"))(input)
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
2024-08-20 17:42:44 +08:00
|
|
|
fn pwm_polarity(input: &[u8]) -> IResult<&[u8], Polarity> {
|
|
|
|
preceded(
|
|
|
|
tag("polarity"),
|
|
|
|
preceded(
|
|
|
|
whitespace,
|
2024-10-14 11:52:15 +08:00
|
|
|
alt((
|
|
|
|
value(Polarity::Normal, tag("normal")),
|
|
|
|
value(Polarity::Reversed, tag("reversed")),
|
|
|
|
)),
|
|
|
|
),
|
2024-08-20 17:42:44 +08:00
|
|
|
)(input)
|
|
|
|
}
|
|
|
|
|
2020-03-14 06:39:22 +08:00
|
|
|
fn pwm(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, _) = tag("pwm")(input)?;
|
|
|
|
alt((
|
2020-03-20 01:34:57 +08:00
|
|
|
|input| {
|
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
let (input, channel) = channel(input)?;
|
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
let (input, result) = alt((
|
|
|
|
|input| {
|
|
|
|
let (input, ()) = pwm_pid(input)?;
|
|
|
|
Ok((input, Ok(Command::PwmPid { channel })))
|
|
|
|
},
|
2024-08-20 17:42:44 +08:00
|
|
|
|input| {
|
|
|
|
let (input, polarity) = pwm_polarity(input)?;
|
|
|
|
Ok((input, Ok(Command::PwmPolarity { channel, polarity })))
|
|
|
|
},
|
2020-03-20 01:34:57 +08:00
|
|
|
|input| {
|
|
|
|
let (input, config) = pwm_setup(input)?;
|
|
|
|
match config {
|
2024-10-14 11:52:15 +08:00
|
|
|
Ok((pin, value)) => Ok((
|
|
|
|
input,
|
|
|
|
Ok(Command::Pwm {
|
|
|
|
channel,
|
|
|
|
pin,
|
|
|
|
value,
|
|
|
|
}),
|
|
|
|
)),
|
|
|
|
Err(e) => Ok((input, Err(e))),
|
2020-03-20 01:34:57 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
))(input)?;
|
|
|
|
end(input)?;
|
|
|
|
Ok((input, result))
|
|
|
|
},
|
2024-10-14 11:52:15 +08:00
|
|
|
value(Ok(Command::Show(ShowCommand::Pwm)), end),
|
2020-03-14 06:39:22 +08:00
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
2020-09-24 04:30:04 +08:00
|
|
|
fn center_point(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, _) = tag("center")(input)?;
|
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
let (input, channel) = channel(input)?;
|
|
|
|
let (input, _) = whitespace(input)?;
|
2024-10-14 11:52:15 +08:00
|
|
|
let (input, center) = alt((value(Ok(CenterPoint::Vref), tag("vref")), |input| {
|
|
|
|
let (input, value) = float(input)?;
|
|
|
|
Ok((
|
|
|
|
input,
|
|
|
|
value.map(|value| CenterPoint::Override(value as f32)),
|
|
|
|
))
|
|
|
|
}))(input)?;
|
2020-09-24 04:30:04 +08:00
|
|
|
end(input)?;
|
2024-10-14 11:52:15 +08:00
|
|
|
Ok((
|
|
|
|
input,
|
|
|
|
center.map(|center| Command::CenterPoint { channel, center }),
|
|
|
|
))
|
2020-09-24 04:30:04 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 06:39:22 +08:00
|
|
|
/// `pid <0-1> <parameter> <value>`
|
|
|
|
fn pid_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, channel) = channel(input)?;
|
|
|
|
let (input, _) = whitespace(input)?;
|
2024-10-14 11:52:15 +08:00
|
|
|
let (input, parameter) = alt((
|
|
|
|
value(PidParameter::Target, tag("target")),
|
|
|
|
value(PidParameter::KP, tag("kp")),
|
|
|
|
value(PidParameter::KI, tag("ki")),
|
|
|
|
value(PidParameter::KD, tag("kd")),
|
|
|
|
value(PidParameter::OutputMin, tag("output_min")),
|
|
|
|
value(PidParameter::OutputMax, tag("output_max")),
|
|
|
|
))(input)?;
|
2020-03-14 06:39:22 +08:00
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
let (input, value) = float(input)?;
|
2024-10-14 11:52:15 +08:00
|
|
|
let result = value.map(|value| Command::Pid {
|
|
|
|
channel,
|
|
|
|
parameter,
|
|
|
|
value,
|
|
|
|
});
|
2020-03-14 06:39:22 +08:00
|
|
|
Ok((input, result))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `pid` | `pid <pid_parameter>`
|
|
|
|
fn pid(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, _) = tag("pid")(input)?;
|
|
|
|
alt((
|
2024-10-14 11:52:15 +08:00
|
|
|
preceded(whitespace, pid_parameter),
|
|
|
|
value(Ok(Command::Show(ShowCommand::Pid)), end),
|
2020-03-14 06:39:22 +08:00
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `s-h <0-1> <parameter> <value>`
|
|
|
|
fn steinhart_hart_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, channel) = channel(input)?;
|
|
|
|
let (input, _) = whitespace(input)?;
|
2024-10-14 11:52:15 +08:00
|
|
|
let (input, parameter) = alt((
|
|
|
|
value(ShParameter::T0, tag("t0")),
|
|
|
|
value(ShParameter::B, tag("b")),
|
|
|
|
value(ShParameter::R0, tag("r0")),
|
|
|
|
))(input)?;
|
2020-03-14 06:39:22 +08:00
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
let (input, value) = float(input)?;
|
2024-10-14 11:52:15 +08:00
|
|
|
let result = value.map(|value| Command::SteinhartHart {
|
|
|
|
channel,
|
|
|
|
parameter,
|
|
|
|
value,
|
|
|
|
});
|
2020-03-14 06:39:22 +08:00
|
|
|
Ok((input, result))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `s-h` | `s-h <steinhart_hart_parameter>`
|
|
|
|
fn steinhart_hart(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, _) = tag("s-h")(input)?;
|
|
|
|
alt((
|
2024-10-14 11:52:15 +08:00
|
|
|
preceded(whitespace, steinhart_hart_parameter),
|
|
|
|
value(Ok(Command::Show(ShowCommand::SteinhartHart)), end),
|
2020-03-14 06:39:22 +08:00
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn postfilter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, _) = tag("postfilter")(input)?;
|
|
|
|
alt((
|
2024-10-14 11:52:15 +08:00
|
|
|
preceded(whitespace, |input| {
|
|
|
|
let (input, channel) = channel(input)?;
|
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
alt((
|
|
|
|
value(
|
|
|
|
Ok(Command::PostFilter {
|
2020-03-19 04:56:52 +08:00
|
|
|
channel,
|
2020-09-26 07:29:35 +08:00
|
|
|
rate: None,
|
2024-10-14 11:52:15 +08:00
|
|
|
}),
|
|
|
|
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),
|
2020-03-14 06:39:22 +08:00
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:25:07 +08:00
|
|
|
fn load(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, _) = tag("load")(input)?;
|
|
|
|
let (input, channel) = alt((
|
|
|
|
|input| {
|
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
let (input, channel) = channel(input)?;
|
|
|
|
let (input, _) = end(input)?;
|
|
|
|
Ok((input, Some(channel)))
|
|
|
|
},
|
2024-10-14 11:52:15 +08:00
|
|
|
value(None, end),
|
2020-12-12 08:25:07 +08:00
|
|
|
))(input)?;
|
|
|
|
|
|
|
|
let result = Ok(Command::Load { channel });
|
|
|
|
Ok((input, result))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, _) = tag("save")(input)?;
|
|
|
|
let (input, channel) = alt((
|
|
|
|
|input| {
|
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
let (input, channel) = channel(input)?;
|
|
|
|
let (input, _) = end(input)?;
|
|
|
|
Ok((input, Some(channel)))
|
|
|
|
},
|
2024-10-14 11:52:15 +08:00
|
|
|
value(None, end),
|
2020-12-12 08:25:07 +08:00
|
|
|
))(input)?;
|
|
|
|
|
|
|
|
let result = Ok(Command::Save { channel });
|
|
|
|
Ok((input, result))
|
|
|
|
}
|
|
|
|
|
2020-12-13 06:44:16 +08:00
|
|
|
fn ipv4_addr(input: &[u8]) -> IResult<&[u8], Result<[u8; 4], Error>> {
|
2020-10-01 07:34:46 +08:00
|
|
|
let (input, a) = unsigned(input)?;
|
|
|
|
let (input, _) = tag(".")(input)?;
|
|
|
|
let (input, b) = unsigned(input)?;
|
|
|
|
let (input, _) = tag(".")(input)?;
|
|
|
|
let (input, c) = unsigned(input)?;
|
|
|
|
let (input, _) = tag(".")(input)?;
|
|
|
|
let (input, d) = unsigned(input)?;
|
2020-12-13 06:44:16 +08:00
|
|
|
let address = move || Ok([a? as u8, b? as u8, c? as u8, d? as u8]);
|
|
|
|
Ok((input, address()))
|
|
|
|
}
|
2020-10-01 07:34:46 +08:00
|
|
|
|
2020-12-13 06:44:16 +08:00
|
|
|
fn ipv4(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, _) = tag("ipv4")(input)?;
|
2020-12-21 03:44:10 +08:00
|
|
|
alt((
|
2020-12-13 06:44:16 +08:00
|
|
|
|input| {
|
|
|
|
let (input, _) = whitespace(input)?;
|
2020-12-21 03:44:10 +08:00
|
|
|
let (input, address) = ipv4_addr(input)?;
|
|
|
|
let (input, _) = tag("/")(input)?;
|
|
|
|
let (input, mask_len) = unsigned(input)?;
|
|
|
|
let (input, gateway) = alt((
|
|
|
|
|input| {
|
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
let (input, gateway) = ipv4_addr(input)?;
|
|
|
|
Ok((input, gateway.map(Some)))
|
|
|
|
},
|
|
|
|
value(Ok(None), end),
|
|
|
|
))(input)?;
|
2020-12-13 06:44:16 +08:00
|
|
|
|
2020-12-21 03:44:10 +08:00
|
|
|
let result = move || {
|
|
|
|
Ok(Command::Ipv4(Ipv4Config {
|
|
|
|
address: address?,
|
|
|
|
mask_len: mask_len? as u8,
|
|
|
|
gateway: gateway?,
|
|
|
|
}))
|
|
|
|
};
|
|
|
|
Ok((input, result()))
|
|
|
|
},
|
|
|
|
value(Ok(Command::Show(ShowCommand::Ipv4)), end),
|
|
|
|
))(input)
|
2020-10-01 07:34:46 +08:00
|
|
|
}
|
|
|
|
|
2023-03-22 17:15:49 +08:00
|
|
|
fn fan(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, _) = tag("fan")(input)?;
|
|
|
|
alt((
|
|
|
|
|input| {
|
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
|
|
|
|
let (input, result) = alt((
|
|
|
|
|input| {
|
|
|
|
let (input, _) = tag("auto")(input)?;
|
|
|
|
Ok((input, Ok(Command::FanAuto)))
|
|
|
|
},
|
|
|
|
|input| {
|
|
|
|
let (input, value) = unsigned(input)?;
|
2024-10-14 11:52:15 +08:00
|
|
|
Ok((
|
|
|
|
input,
|
|
|
|
Ok(Command::FanSet {
|
|
|
|
fan_pwm: value.unwrap_or(0),
|
|
|
|
}),
|
|
|
|
))
|
2023-03-22 17:15:49 +08:00
|
|
|
},
|
|
|
|
))(input)?;
|
|
|
|
Ok((input, result))
|
|
|
|
},
|
2024-10-14 11:52:15 +08:00
|
|
|
value(Ok(Command::ShowFan), end),
|
2023-03-22 17:15:49 +08:00
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fan_curve(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|
|
|
let (input, _) = tag("fcurve")(input)?;
|
|
|
|
alt((
|
|
|
|
|input| {
|
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
let (input, result) = alt((
|
|
|
|
|input| {
|
|
|
|
let (input, _) = tag("default")(input)?;
|
|
|
|
Ok((input, Ok(Command::FanCurveDefaults)))
|
|
|
|
},
|
|
|
|
|input| {
|
|
|
|
let (input, k_a) = float(input)?;
|
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
let (input, k_b) = float(input)?;
|
|
|
|
let (input, _) = whitespace(input)?;
|
|
|
|
let (input, k_c) = float(input)?;
|
|
|
|
if k_a.is_ok() && k_b.is_ok() && k_c.is_ok() {
|
2024-10-14 11:52:15 +08:00
|
|
|
Ok((
|
|
|
|
input,
|
|
|
|
Ok(Command::FanCurve {
|
|
|
|
k_a: k_a.unwrap() as f32,
|
|
|
|
k_b: k_b.unwrap() as f32,
|
|
|
|
k_c: k_c.unwrap() as f32,
|
|
|
|
}),
|
|
|
|
))
|
2023-03-22 17:15:49 +08:00
|
|
|
} else {
|
|
|
|
Err(nom::Err::Incomplete(Needed::Size(3)))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
))(input)?;
|
|
|
|
Ok((input, result))
|
|
|
|
},
|
2024-10-14 11:52:15 +08:00
|
|
|
value(Err(Error::Incomplete), end),
|
2023-03-22 17:15:49 +08:00
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
2020-03-14 06:39:22 +08:00
|
|
|
fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
2024-10-14 11:52:15 +08:00
|
|
|
alt((
|
|
|
|
value(Ok(Command::Quit), tag("quit")),
|
|
|
|
load,
|
|
|
|
save,
|
|
|
|
value(Ok(Command::Reset), tag("reset")),
|
|
|
|
ipv4,
|
|
|
|
map(report, Ok),
|
|
|
|
pwm,
|
|
|
|
center_point,
|
|
|
|
pid,
|
|
|
|
steinhart_hart,
|
|
|
|
postfilter,
|
|
|
|
value(Ok(Command::Dfu), tag("dfu")),
|
|
|
|
fan,
|
|
|
|
fan_curve,
|
|
|
|
value(Ok(Command::ShowHWRev), tag("hwrev")),
|
2020-03-14 06:39:22 +08:00
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Command {
|
|
|
|
pub fn parse(input: &[u8]) -> Result<Self, Error> {
|
|
|
|
match command(input) {
|
2024-10-14 11:52:15 +08:00
|
|
|
Ok((input_remain, result)) if input_remain.len() == 0 => result,
|
|
|
|
Ok((input_remain, _)) => Err(Error::UnexpectedInput(input_remain[0])),
|
|
|
|
Err(e) => Err(e.into()),
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_quit() {
|
|
|
|
let command = Command::parse(b"quit");
|
|
|
|
assert_eq!(command, Ok(Command::Quit));
|
|
|
|
}
|
|
|
|
|
2020-09-24 07:17:50 +08:00
|
|
|
#[test]
|
|
|
|
fn parse_load() {
|
|
|
|
let command = Command::parse(b"load");
|
2020-12-12 08:25:07 +08:00
|
|
|
assert_eq!(command, Ok(Command::Load { channel: None }));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_load_channel() {
|
|
|
|
let command = Command::parse(b"load 0");
|
|
|
|
assert_eq!(command, Ok(Command::Load { channel: Some(0) }));
|
2020-09-24 07:17:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_save() {
|
|
|
|
let command = Command::parse(b"save");
|
2020-12-12 08:25:07 +08:00
|
|
|
assert_eq!(command, Ok(Command::Save { channel: None }));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_save_channel() {
|
|
|
|
let command = Command::parse(b"save 0");
|
|
|
|
assert_eq!(command, Ok(Command::Save { channel: Some(0) }));
|
2020-09-24 07:17:50 +08:00
|
|
|
}
|
|
|
|
|
2020-12-21 03:44:10 +08:00
|
|
|
#[test]
|
|
|
|
fn parse_show_ipv4() {
|
|
|
|
let command = Command::parse(b"ipv4");
|
|
|
|
assert_eq!(command, Ok(Command::Show(ShowCommand::Ipv4)));
|
|
|
|
}
|
|
|
|
|
2020-03-14 06:39:22 +08:00
|
|
|
#[test]
|
2020-10-01 07:34:46 +08:00
|
|
|
fn parse_ipv4() {
|
2020-12-13 06:44:16 +08:00
|
|
|
let command = Command::parse(b"ipv4 192.168.1.26/24");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::Ipv4(Ipv4Config {
|
|
|
|
address: [192, 168, 1, 26],
|
|
|
|
mask_len: 24,
|
|
|
|
gateway: None,
|
|
|
|
}))
|
|
|
|
);
|
2020-12-13 06:44:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_ipv4_and_gateway() {
|
|
|
|
let command = Command::parse(b"ipv4 10.42.0.126/8 10.1.0.1");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::Ipv4(Ipv4Config {
|
|
|
|
address: [10, 42, 0, 126],
|
|
|
|
mask_len: 8,
|
|
|
|
gateway: Some([10, 1, 0, 1]),
|
|
|
|
}))
|
|
|
|
);
|
2020-10-01 07:34:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-14 06:39:22 +08:00
|
|
|
fn parse_report() {
|
|
|
|
let command = Command::parse(b"report");
|
|
|
|
assert_eq!(command, Ok(Command::Show(ShowCommand::Input)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-12-09 08:07:08 +08:00
|
|
|
fn parse_pwm_i_set() {
|
|
|
|
let command = Command::parse(b"pwm 1 i_set 16383");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::Pwm {
|
|
|
|
channel: 1,
|
|
|
|
pin: PwmPin::ISet,
|
|
|
|
value: 16383.0,
|
|
|
|
})
|
|
|
|
);
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
2024-10-07 09:49:08 +08:00
|
|
|
#[test]
|
|
|
|
fn parse_pwm_polarity() {
|
|
|
|
let command = Command::parse(b"pwm 0 polarity reversed");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::PwmPolarity {
|
|
|
|
channel: 0,
|
|
|
|
polarity: Polarity::Reversed,
|
|
|
|
})
|
|
|
|
);
|
2024-10-07 09:49:08 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 06:39:22 +08:00
|
|
|
#[test]
|
|
|
|
fn parse_pwm_pid() {
|
|
|
|
let command = Command::parse(b"pwm 0 pid");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(command, Ok(Command::PwmPid { channel: 0 }));
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_pwm_max_i_pos() {
|
2020-03-20 01:34:57 +08:00
|
|
|
let command = Command::parse(b"pwm 0 max_i_pos 7");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::Pwm {
|
|
|
|
channel: 0,
|
|
|
|
pin: PwmPin::MaxIPos,
|
|
|
|
value: 7.0,
|
|
|
|
})
|
|
|
|
);
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_pwm_max_i_neg() {
|
2020-03-20 01:34:57 +08:00
|
|
|
let command = Command::parse(b"pwm 0 max_i_neg 128");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::Pwm {
|
|
|
|
channel: 0,
|
|
|
|
pin: PwmPin::MaxINeg,
|
|
|
|
value: 128.0,
|
|
|
|
})
|
|
|
|
);
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_pwm_max_v() {
|
2020-03-20 01:34:57 +08:00
|
|
|
let command = Command::parse(b"pwm 0 max_v 32768");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::Pwm {
|
|
|
|
channel: 0,
|
|
|
|
pin: PwmPin::MaxV,
|
|
|
|
value: 32768.0,
|
|
|
|
})
|
|
|
|
);
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_pid() {
|
|
|
|
let command = Command::parse(b"pid");
|
|
|
|
assert_eq!(command, Ok(Command::Show(ShowCommand::Pid)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_pid_target() {
|
|
|
|
let command = Command::parse(b"pid 0 target 36.5");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::Pid {
|
|
|
|
channel: 0,
|
|
|
|
parameter: PidParameter::Target,
|
|
|
|
value: 36.5,
|
|
|
|
})
|
|
|
|
);
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_steinhart_hart() {
|
|
|
|
let command = Command::parse(b"s-h");
|
|
|
|
assert_eq!(command, Ok(Command::Show(ShowCommand::SteinhartHart)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-09-18 07:16:19 +08:00
|
|
|
fn parse_steinhart_hart_set() {
|
2020-03-20 01:34:57 +08:00
|
|
|
let command = Command::parse(b"s-h 1 t0 23.05");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::SteinhartHart {
|
|
|
|
channel: 1,
|
|
|
|
parameter: ShParameter::T0,
|
|
|
|
value: 23.05,
|
|
|
|
})
|
|
|
|
);
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
2020-09-26 07:29:35 +08:00
|
|
|
#[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");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::PostFilter {
|
|
|
|
channel: 1,
|
|
|
|
rate: None,
|
|
|
|
})
|
|
|
|
);
|
2020-09-26 07:29:35 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 06:39:22 +08:00
|
|
|
#[test]
|
|
|
|
fn parse_postfilter_rate() {
|
|
|
|
let command = Command::parse(b"postfilter 0 rate 21");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::PostFilter {
|
|
|
|
channel: 0,
|
|
|
|
rate: Some(21.0),
|
|
|
|
})
|
|
|
|
);
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|
2020-09-24 04:30:04 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_center_point() {
|
|
|
|
let command = Command::parse(b"center 0 1.5");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::CenterPoint {
|
|
|
|
channel: 0,
|
|
|
|
center: CenterPoint::Override(1.5),
|
|
|
|
})
|
|
|
|
);
|
2020-09-24 04:30:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_center_point_vref() {
|
|
|
|
let command = Command::parse(b"center 1 vref");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::CenterPoint {
|
|
|
|
channel: 1,
|
|
|
|
center: CenterPoint::Vref,
|
|
|
|
})
|
|
|
|
);
|
2020-09-24 04:30:04 +08:00
|
|
|
}
|
2023-03-22 17:15:49 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fan_show() {
|
|
|
|
let command = Command::parse(b"fan");
|
|
|
|
assert_eq!(command, Ok(Command::ShowFan));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fan_set() {
|
|
|
|
let command = Command::parse(b"fan 42");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(command, Ok(Command::FanSet { fan_pwm: 42 }));
|
2023-03-22 17:15:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fan_auto() {
|
|
|
|
let command = Command::parse(b"fan auto");
|
|
|
|
assert_eq!(command, Ok(Command::FanAuto));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fcurve_set() {
|
|
|
|
let command = Command::parse(b"fcurve 1.2 3.4 5.6");
|
2024-10-14 11:52:15 +08:00
|
|
|
assert_eq!(
|
|
|
|
command,
|
|
|
|
Ok(Command::FanCurve {
|
|
|
|
k_a: 1.2,
|
|
|
|
k_b: 3.4,
|
|
|
|
k_c: 5.6
|
|
|
|
})
|
|
|
|
);
|
2023-03-22 17:15:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fcurve_default() {
|
|
|
|
let command = Command::parse(b"fcurve default");
|
|
|
|
assert_eq!(command, Ok(Command::FanCurveDefaults));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_hwrev() {
|
|
|
|
let command = Command::parse(b"hwrev");
|
|
|
|
assert_eq!(command, Ok(Command::ShowHWRev));
|
|
|
|
}
|
2020-03-14 06:39:22 +08:00
|
|
|
}
|