Support fan PWM settings #73

Merged
sb10q merged 16 commits from esavkin/thermostat:69-fan_pwm into master 2023-03-22 17:15:49 +08:00
2 changed files with 104 additions and 53 deletions
Showing only changes of commit 33070abd81 - Show all commits

View File

@ -342,26 +342,29 @@ impl Handler {
Ok(Handler::Reset)
}
fn fan(socket: &mut TcpSocket, fan_pwm: Option<u32>, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
match fan_pwm {
Some(val) => {
fan_ctrl.set_auto_mode(val == 0);
fan_ctrl.set_pwm(val);
},
None => {
match fan_ctrl.summary() {
Ok(buf) => {
send_line(socket, &buf);
return Ok(Handler::Handled);
}
Err(e) => {
error!("unable to serialize fan summary: {:?}", e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
return Err(Error::ReportError);
}
};
fn fan(socket: &mut TcpSocket, fan_pwm: u32, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
fan_ctrl.set_auto_mode(false);
fan_ctrl.set_pwm(fan_pwm);
send_line(socket, b"{}");
Ok(Handler::Handled)
}
fn show_fan(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
match fan_ctrl.summary() {
Ok(buf) => {
send_line(socket, &buf);
Ok(Handler::Handled)
}
};
Err(e) => {
error!("unable to serialize fan summary: {:?}", e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
Err(Error::ReportError)
}
}
}
fn fan_auto(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
fan_ctrl.set_auto_mode(true);
send_line(socket, b"{}");
Ok(Handler::Handled)
}
@ -401,9 +404,11 @@ impl Handler {
Command::Ipv4(config) => Handler::set_ipv4(socket, store, config),
Command::Reset => Handler::reset(&mut fan_ctrl.channels),
Command::Dfu => Handler::dfu(&mut fan_ctrl.channels),
Command::Fan {fan_pwm} => Handler::fan(socket, fan_pwm, fan_ctrl),
Command::FanSet {fan_pwm} => Handler::fan(socket, fan_pwm, fan_ctrl),
Command::ShowFan => Handler::show_fan(socket, fan_ctrl),
Command::FanAuto => Handler::fan_auto(socket, fan_ctrl),
Command::FanCurve { k_a, k_b, k_c } => Handler::fan_curve(socket, fan_ctrl, k_a, k_b, k_c),
Command::FanDefaults => Handler::fan_defaults(socket, fan_ctrl),
Command::FanCurveDefaults => Handler::fan_defaults(socket, fan_ctrl),
}
}
}

View File

@ -179,15 +179,17 @@ pub enum Command {
rate: Option<f32>,
},
Dfu,
Fan {
fan_pwm: Option<u32>
FanSet {
fan_pwm: u32
},
FanAuto,
ShowFan,
FanCurve {
k_a: f64,
k_b: f64,
k_c: f64,
},
FanDefaults,
FanCurveDefaults,
}
fn end(input: &[u8]) -> IResult<&[u8], ()> {
@ -532,45 +534,56 @@ fn ipv4(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
fn fan(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
let (input, _) = tag("fan")(input)?;
let (input, fan_pwm) = alt((
alt((
|input| {
let (input, _) = whitespace(input)?;
let (input, value) = unsigned(input)?;
let (input, _) = end(input)?;
Ok((input, Some(value.unwrap_or(0))))
},
value(None, end)
))(input)?;
let result = Ok(Command::Fan { fan_pwm });
Ok((input, result))
let (input, result) = alt((
|input| {
let (input, _) = tag("auto")(input)?;
Ok((input, Ok(Command::FanAuto)))
},
|input| {
let (input, value) = unsigned(input)?;
Ok((input, Ok(Command::FanSet { fan_pwm: value.unwrap_or(0)})))
},
))(input)?;
let (input, _) = end(input)?;
Ok((input, result))
},
value(Ok(Command::ShowFan), end)
))(input)
}
fn fan_curve(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
let (input, _) = tag("fcurve")(input)?;
let (input, curve) = alt((
alt((
|input| {
let (input, _) = whitespace(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)?;
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)?;
let (input, _) = end(input)?;
if k_a.is_ok() && k_b.is_ok() && k_c.is_ok() {
Ok((input, Ok(Command::FanCurve { k_a: k_a.unwrap(), k_b: k_b.unwrap(), k_c: k_c.unwrap() })))
} else {
Err(nom::Err::Incomplete(Needed::Size(3)))
}
},
))(input)?;
let (input, _) = end(input)?;
if k_a.is_ok() && k_b.is_ok() && k_c.is_ok() {
Ok((input, Some((k_a.unwrap(), k_b.unwrap(), k_c.unwrap()))))
} else {
Err(nom::Err::Incomplete(Needed::Size(3)))
}
Ok((input, result))
},
value(None, end)
))(input)?;
let result = match curve {
Some(curve) => Ok(Command::FanCurve { k_a: curve.0, k_b: curve.1, k_c: curve.2 }),
None => Err(Error::ParseFloat)
};
Ok((input, result))
value(Err(Error::Incomplete), end)
))(input)
}
fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
@ -586,7 +599,6 @@ fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
steinhart_hart,
postfilter,
value(Ok(Command::Dfu), tag("dfu")),
value(Ok(Command::FanDefaults), tag("fcurve-restore")),
fan,
fan_curve,
))(input)
@ -810,4 +822,38 @@ mod test {
center: CenterPoint::Vref,
}));
}
#[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");
assert_eq!(command, Ok(Command::FanSet {fan_pwm: 42}));
}
#[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");
assert_eq!(command, Ok(Command::FanCurve {
k_a: 1.2,
k_b: 3.4,
k_c: 5.6
}));
}
#[test]
fn parse_fcurve_default() {
let command = Command::parse(b"fcurve default");
assert_eq!(command, Ok(Command::FanCurveDefaults));
}
}