diff --git a/src/ad7172/adc.rs b/src/ad7172/adc.rs index 6cbf561..a881078 100644 --- a/src/ad7172/adc.rs +++ b/src/ad7172/adc.rs @@ -199,7 +199,7 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc ChecksumMode::Crc => ChecksumMode::Crc, }); checksum.feed(&[address]); - checksum.feed(®_data); + checksum.feed(reg_data); let checksum_out = checksum.result(); let mut data = reg_data.clone(); diff --git a/src/channels.rs b/src/channels.rs index c59438e..172ad50 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -113,8 +113,7 @@ impl<'a> Channels<'a> { let center_point = self.get_center(channel); let r_sense = ElectricalResistance::new::(R_SENSE); let voltage = self.get_dac(channel); - let i_tec = (voltage - center_point) / (10.0 * r_sense); - i_tec + (voltage - center_point) / (10.0 * r_sense) } /// i_set DAC @@ -131,7 +130,7 @@ impl<'a> Channels<'a> { } pub fn set_i(&mut self, channel: usize, i_tec: ElectricCurrent) -> ElectricCurrent { - let vref_meas = match channel.into() { + let vref_meas = match channel { 0 => self.channel0.vref_meas, 1 => self.channel1.vref_meas, _ => unreachable!(), @@ -140,8 +139,7 @@ impl<'a> Channels<'a> { let r_sense = ElectricalResistance::new::(R_SENSE); let voltage = i_tec * 10.0 * r_sense + center_point; let voltage = self.set_dac(channel, voltage); - let i_tec = (voltage - center_point) / (10.0 * r_sense); - i_tec + (voltage - center_point) / (10.0 * r_sense) } pub fn read_dac_feedback(&mut self, channel: usize) -> ElectricPotential { @@ -268,9 +266,9 @@ impl<'a> Channels<'a> { let samples = 50; let mut target_voltage = ElectricPotential::new::(0.0); for _ in 0..samples { - target_voltage = target_voltage + self.get_center(channel); + target_voltage += self.get_center(channel); } - target_voltage = target_voltage / samples as f64; + target_voltage /= samples as f64; let mut start_value = 1; let mut best_error = ElectricPotential::new::(100.0); diff --git a/src/command_handler.rs b/src/command_handler.rs index 7011e0a..cd15c83 100644 --- a/src/command_handler.rs +++ b/src/command_handler.rs @@ -35,9 +35,9 @@ pub enum Handler { #[derive(Clone, Debug, PartialEq)] pub enum Error { - ReportError, - PostFilterRateError, - FlashError, + Report, + PostFilterRate, + Flash, } pub type JsonBuffer = Vec; @@ -54,7 +54,7 @@ fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool { data.len(), ); } else { - match socket.send_slice(&data) { + match socket.send_slice(data) { Ok(sent) if sent == data.len() => { let _ = socket.send_slice(b"\n"); // success @@ -87,7 +87,7 @@ impl Handler { Err(e) => { error!("unable to serialize report: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); - return Err(Error::ReportError); + return Err(Error::Report); } } Ok(Handler::Handled) @@ -101,7 +101,7 @@ impl Handler { Err(e) => { error!("unable to serialize pid summary: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); - return Err(Error::ReportError); + return Err(Error::Report); } } Ok(Handler::Handled) @@ -115,7 +115,7 @@ impl Handler { Err(e) => { error!("unable to serialize pwm summary: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); - return Err(Error::ReportError); + return Err(Error::Report); } } Ok(Handler::Handled) @@ -132,7 +132,7 @@ impl Handler { Err(e) => { error!("unable to serialize steinhart-hart summaries: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); - return Err(Error::ReportError); + return Err(Error::Report); } } Ok(Handler::Handled) @@ -146,7 +146,7 @@ impl Handler { Err(e) => { error!("unable to serialize postfilter summary: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); - return Err(Error::ReportError); + return Err(Error::Report); } } Ok(Handler::Handled) @@ -287,7 +287,7 @@ impl Handler { socket, b"{{\"error\": \"unable to choose postfilter rate\"}}", ); - return Err(Error::PostFilterRateError); + return Err(Error::PostFilterRate); } } Ok(Handler::Handled) @@ -313,7 +313,7 @@ impl Handler { Err(e) => { error!("unable to load config from flash: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); - return Err(Error::FlashError); + return Err(Error::Flash); } } } @@ -338,7 +338,7 @@ impl Handler { Err(e) => { error!("unable to save channel {} config to flash: {:?}", c, e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); - return Err(Error::FlashError); + return Err(Error::Flash); } } } @@ -409,7 +409,7 @@ impl Handler { Err(e) => { error!("unable to serialize fan summary: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); - Err(Error::ReportError) + Err(Error::Report) } } } @@ -458,7 +458,7 @@ impl Handler { Err(e) => { error!("unable to serialize HWRev summary: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); - Err(Error::ReportError) + Err(Error::Report) } } } diff --git a/src/command_parser.rs b/src/command_parser.rs index 8f9fe48..f1216ac 100644 --- a/src/command_parser.rs +++ b/src/command_parser.rs @@ -208,7 +208,7 @@ fn unsigned(input: &[u8]) -> IResult<&[u8], Result> { fn float(input: &[u8]) -> IResult<&[u8], Result> { 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)?; + let (input, digits) = take_while1(|c| is_digit(c) || c == b'.')(input)?; let result = from_utf8(digits) .map_err(|e| e.into()) .and_then(|digits| f64::from_str_radix(digits, 10).map_err(|e| e.into())) @@ -580,7 +580,7 @@ fn command(input: &[u8]) -> IResult<&[u8], Result> { impl Command { pub fn parse(input: &[u8]) -> Result { match command(input) { - Ok((input_remain, result)) if input_remain.len() == 0 => result, + Ok((input_remain, result)) if input_remain.is_empty() => result, Ok((input_remain, _)) => Err(Error::UnexpectedInput(input_remain[0])), Err(e) => Err(e.into()), } diff --git a/src/fan_ctrl.rs b/src/fan_ctrl.rs index 891729f..7d50080 100644 --- a/src/fan_ctrl.rs +++ b/src/fan_ctrl.rs @@ -101,9 +101,7 @@ impl FanCtrl { return 0f32; } let fan = self.fan.as_mut().unwrap(); - let fan_pwm = fan_pwm - .min(MAX_USER_FAN_PWM as u32) - .max(MIN_USER_FAN_PWM as u32); + let fan_pwm = fan_pwm.clamp(MIN_USER_FAN_PWM as u32, MAX_USER_FAN_PWM as u32); let duty = scale_number( fan_pwm as f32, self.hw_settings.min_fan_pwm, diff --git a/src/main.rs b/src/main.rs index 68b4dbb..e6d0acd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,7 +74,7 @@ fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool { data.len(), ); } else { - match socket.send_slice(&data) { + match socket.send_slice(data) { Ok(sent) if sent == data.len() => { let _ = socket.send_slice(b"\n"); // success @@ -280,10 +280,10 @@ fn main() -> ! { } // Apply new IPv4 address/gateway - new_ipv4_config.take().map(|config| { + if let Some(config) = new_ipv4_config.take() { server.set_ipv4_config(config.clone()); ipv4_config = config; - }); + } // Update watchdog wd.feed(); diff --git a/src/pid.rs b/src/pid.rs index 4ecb5fe..6eac78d 100644 --- a/src/pid.rs +++ b/src/pid.rs @@ -39,7 +39,7 @@ pub struct Controller { impl Controller { pub const fn new(parameters: Parameters) -> Controller { Controller { - parameters: parameters, + parameters, target: 0.0, u1: 0.0, x1: 0.0, diff --git a/src/pins.rs b/src/pins.rs index 6e1f8eb..e821e6e 100644 --- a/src/pins.rs +++ b/src/pins.rs @@ -170,7 +170,7 @@ impl Pins { let (dac0_spi, dac0_sync) = Self::setup_dac0(clocks, spi4, gpioe.pe2, gpioe.pe4, gpioe.pe6); let mut shdn0 = gpioe.pe10.into_push_pull_output(); - let _ = shdn0.set_low(); + shdn0.set_low(); let vref0_pin = gpioa.pa0.into_analog(); let itec0_pin = gpioa.pa6.into_analog(); let dac_feedback0_pin = gpioa.pa4.into_analog(); @@ -187,7 +187,7 @@ impl Pins { let (dac1_spi, dac1_sync) = Self::setup_dac1(clocks, spi5, gpiof.pf7, gpiof.pf6, gpiof.pf9); let mut shdn1 = gpioe.pe15.into_push_pull_output(); - let _ = shdn1.set_low(); + shdn1.set_low(); let vref1_pin = gpioa.pa3.into_analog(); let itec1_pin = gpiob.pb0.into_analog(); let dac_feedback1_pin = gpioa.pa5.into_analog(); diff --git a/src/server.rs b/src/server.rs index 0ea8c48..ffb9ba8 100644 --- a/src/server.rs +++ b/src/server.rs @@ -103,15 +103,10 @@ impl<'a, 'b, S: Default> Server<'a, 'b, S> { fn set_ipv4_address(&mut self, ipv4_address: Ipv4Cidr) { self.net.update_ip_addrs(|addrs| { for addr in addrs.iter_mut() { - match addr { - IpCidr::Ipv4(_) => { - *addr = IpCidr::Ipv4(ipv4_address); - // done - break; - } - _ => { - // skip - } + if let IpCidr::Ipv4(_) = addr { + *addr = IpCidr::Ipv4(ipv4_address); + // done + break; } } }); diff --git a/src/session.rs b/src/session.rs index f029fa9..2ddf23e 100644 --- a/src/session.rs +++ b/src/session.rs @@ -96,7 +96,7 @@ impl Session { self.report_pending.iter().enumerate().fold( None, |result, (channel, report_pending)| { - result.or_else(|| if *report_pending { Some(channel) } else { None }) + result.or(if *report_pending { Some(channel) } else { None }) }, ) } @@ -111,18 +111,12 @@ impl Session { for (i, b) in buf.iter().enumerate() { buf_bytes = i + 1; let line = self.reader.feed(*b); - match line { - Some(line) => { - let command = Command::parse(&line); - match command { - Ok(Command::Reporting(reporting)) => { - self.reporting = reporting; - } - _ => {} - } - return (buf_bytes, command.into()); + if let Some(line) = line { + let command = Command::parse(line); + if let Ok(Command::Reporting(reporting)) = command { + self.reporting = reporting; } - None => {} + return (buf_bytes, command.into()); } } (buf_bytes, SessionInput::Nothing)