Apply clippy suggestions

This commit is contained in:
atse 2024-10-07 12:54:19 +08:00
parent 1962135e79
commit d8ec083dbc
11 changed files with 92 additions and 112 deletions

View File

@ -199,7 +199,7 @@ impl<SPI: Transfer<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS>
ChecksumMode::Crc => ChecksumMode::Crc,
});
checksum.feed(&[address]);
checksum.feed(&reg_data);
checksum.feed(reg_data);
let checksum_out = checksum.result();
let mut data = reg_data.clone();
@ -237,10 +237,10 @@ impl<SPI: Transfer<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS>
Ok(())
}
fn transfer<'w>(
fn transfer(
&mut self,
addr: u8,
reg_data: &'w mut [u8],
reg_data: &mut [u8],
checksum: Option<u8>,
) -> Result<Option<u8>, SPI::Error> {
let mut addr_buf = [addr];

View File

@ -43,7 +43,7 @@ impl ChannelState {
adc_time: Instant::from_secs(0),
// default: 10 Hz
adc_interval: Duration::from_millis(100),
center: CenterPoint::Vref,
center: CenterPoint::VRef,
dac_value: ElectricPotential::new::<volt>(0.0),
i_set: ElectricCurrent::new::<ampere>(0.0),
pwm_limits: PwmLimits {

View File

@ -24,7 +24,7 @@ use uom::si::{
};
pub enum PinsAdcReadTarget {
VREF,
VRef,
DacVfb,
ITec,
VTec,
@ -131,7 +131,7 @@ impl Channels {
/// calculate the TEC i_set centerpoint
pub fn get_center(&mut self, channel: usize) -> ElectricPotential {
match self.channel_state(channel).center {
CenterPoint::Vref => self.adc_read(channel, PinsAdcReadTarget::VREF, 8),
CenterPoint::VRef => self.adc_read(channel, PinsAdcReadTarget::VRef, 8),
CenterPoint::Override(center_point) => {
ElectricPotential::new::<volt>(center_point.into())
}
@ -168,7 +168,7 @@ impl Channels {
Polarity::Normal => 1.0,
Polarity::Reversed => -1.0,
};
let vref_meas = match channel.into() {
let vref_meas = match channel {
0 => self.channel0.vref_meas,
1 => self.channel1.vref_meas,
_ => unreachable!(),
@ -177,8 +177,8 @@ impl Channels {
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
let voltage = negate * i_set * 10.0 * r_sense + center_point;
let voltage = self.set_dac(channel, voltage);
let i_set = negate * (voltage - center_point) / (10.0 * r_sense);
i_set
negate * (voltage - center_point) / (10.0 * r_sense)
}
/// AN4073: ADC Reading Dispersion can be reduced through Averaging
@ -192,7 +192,7 @@ impl Channels {
match channel {
0 => {
sample = match adc_read_target {
PinsAdcReadTarget::VREF => match &self.channel0.vref_pin {
PinsAdcReadTarget::VRef => match &self.channel0.vref_pin {
Channel0VRef::Analog(vref_pin) => {
for _ in (0..avg_pt).rev() {
sample += self.pins_adc.convert(
@ -202,7 +202,7 @@ impl Channels {
}
sample / avg_pt as u32
}
Channel0VRef::Disabled(_) => 2048 as u32,
Channel0VRef::Disabled(_) => 2048_u32,
},
PinsAdcReadTarget::DacVfb => {
for _ in (0..avg_pt).rev() {
@ -237,7 +237,7 @@ impl Channels {
}
1 => {
sample = match adc_read_target {
PinsAdcReadTarget::VREF => match &self.channel1.vref_pin {
PinsAdcReadTarget::VRef => match &self.channel1.vref_pin {
Channel1VRef::Analog(vref_pin) => {
for _ in (0..avg_pt).rev() {
sample += self.pins_adc.convert(
@ -247,7 +247,7 @@ impl Channels {
}
sample / avg_pt as u32
}
Channel1VRef::Disabled(_) => 2048 as u32,
Channel1VRef::Disabled(_) => 2048_u32,
},
PinsAdcReadTarget::DacVfb => {
for _ in (0..avg_pt).rev() {
@ -304,9 +304,9 @@ impl Channels {
let samples = 50;
let mut target_voltage = ElectricPotential::new::<volt>(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::<volt>(100.0);
@ -378,7 +378,7 @@ impl Channels {
// Get current passing through TEC
pub fn get_tec_i(&mut self, channel: usize) -> ElectricCurrent {
let tec_i = (self.adc_read(channel, PinsAdcReadTarget::ITec, 16)
- self.adc_read(channel, PinsAdcReadTarget::VREF, 16))
- self.adc_read(channel, PinsAdcReadTarget::VRef, 16))
/ ElectricalResistance::new::<ohm>(0.4);
match self.channel_state(channel).polarity {
Polarity::Normal => tec_i,
@ -608,7 +608,7 @@ impl Serialize for CenterPointJson {
S: Serializer,
{
match self.0 {
CenterPoint::Vref => serializer.serialize_str("vref"),
CenterPoint::VRef => serializer.serialize_str("vref"),
CenterPoint::Override(vref) => serializer.serialize_f32(vref),
}
}

View File

@ -33,9 +33,9 @@ pub enum Handler {
#[derive(Clone, Debug, PartialEq)]
pub enum Error {
ReportError,
PostFilterRateError,
FlashError,
Report,
PostFilterRate,
Flash,
}
pub type JsonBuffer = Vec<u8, U1024>;
@ -52,7 +52,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
@ -75,7 +75,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)
@ -89,7 +89,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)
@ -103,7 +103,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)
@ -120,7 +120,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)
@ -134,7 +134,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)
@ -286,7 +286,7 @@ impl Handler {
socket,
b"{{\"error\": \"unable to choose postfilter rate\"}}",
);
return Err(Error::PostFilterRateError);
return Err(Error::PostFilterRate);
}
}
Ok(Handler::Handled)
@ -298,9 +298,9 @@ impl Handler {
store: &mut FlashStore,
channel: Option<usize>,
) -> Result<Handler, Error> {
for c in 0..CHANNELS {
for (c, key) in CHANNEL_CONFIG_KEY.iter().enumerate().take(CHANNELS) {
if channel.is_none() || channel == Some(c) {
match store.read_value::<ChannelConfig>(CHANNEL_CONFIG_KEY[c]) {
match store.read_value::<ChannelConfig>(key) {
Ok(Some(config)) => {
config.apply(channels, c);
send_line(socket, b"{}");
@ -312,7 +312,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);
}
}
}
@ -326,18 +326,18 @@ impl Handler {
channel: Option<usize>,
store: &mut FlashStore,
) -> Result<Handler, Error> {
for c in 0..CHANNELS {
for (c, key) in CHANNEL_CONFIG_KEY.iter().enumerate().take(CHANNELS) {
let mut store_value_buf = [0u8; 256];
if channel.is_none() || channel == Some(c) {
let config = ChannelConfig::new(channels, c);
match store.write_value(CHANNEL_CONFIG_KEY[c], &config, &mut store_value_buf) {
match store.write_value(key, &config, &mut store_value_buf) {
Ok(()) => {
send_line(socket, b"{}");
}
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);
}
}
}
@ -408,7 +408,7 @@ impl Handler {
Err(e) => {
error!("unable to serialize fan summary: {:?}", e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
Err(Error::ReportError)
Err(Error::Report)
}
}
}
@ -457,7 +457,7 @@ impl Handler {
Err(e) => {
error!("unable to serialize HWRev summary: {:?}", e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
Err(Error::ReportError)
Err(Error::Report)
}
}
}

View File

@ -126,7 +126,7 @@ pub enum PwmPin {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum CenterPoint {
Vref,
VRef,
Override(f32),
}
@ -208,7 +208,7 @@ fn unsigned(input: &[u8]) -> IResult<&[u8], Result<u32, Error>> {
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()));
.and_then(|digits| digits.parse::<u32>().map_err(|e| e.into()));
(input, result)
})
}
@ -216,7 +216,7 @@ fn unsigned(input: &[u8]) -> IResult<&[u8], Result<u32, Error>> {
fn float(input: &[u8]) -> IResult<&[u8], Result<f64, Error>> {
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()))
@ -321,7 +321,7 @@ fn center_point(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
let (input, _) = whitespace(input)?;
let (input, channel) = channel(input)?;
let (input, _) = whitespace(input)?;
let (input, center) = alt((value(Ok(CenterPoint::Vref), tag("vref")), |input| {
let (input, center) = alt((value(Ok(CenterPoint::VRef), tag("vref")), |input| {
let (input, value) = float(input)?;
Ok((
input,
@ -541,13 +541,13 @@ fn fan_curve(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
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() {
if let (Ok(k_a), Ok(k_b), Ok(k_c)) = (k_a, k_b, k_c) {
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,
k_a: k_a as f32,
k_b: k_b as f32,
k_c: k_c as f32,
}),
))
} else {
@ -584,7 +584,7 @@ fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
impl Command {
pub fn parse(input: &[u8]) -> Result<Self, Error> {
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()),
}
@ -820,7 +820,7 @@ mod test {
command,
Ok(Command::CenterPoint {
channel: 1,
center: CenterPoint::Vref,
center: CenterPoint::VRef,
})
);
}

View File

@ -95,9 +95,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,

View File

@ -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
@ -119,24 +119,14 @@ fn main() -> ! {
let (pins, mut leds, mut eeprom, eth_pins, usb, fan, hwrev, hw_settings) = Pins::setup(
clocks,
dp.TIM1,
dp.TIM3,
dp.TIM8,
dp.GPIOA,
dp.GPIOB,
dp.GPIOC,
dp.GPIOD,
dp.GPIOE,
dp.GPIOF,
dp.GPIOG,
(dp.TIM1, dp.TIM3, dp.TIM8),
(
dp.GPIOA, dp.GPIOB, dp.GPIOC, dp.GPIOD, dp.GPIOE, dp.GPIOF, dp.GPIOG,
),
dp.I2C1,
dp.SPI2,
dp.SPI4,
dp.SPI5,
(dp.SPI2, dp.SPI4, dp.SPI5),
dp.ADC1,
dp.OTG_FS_GLOBAL,
dp.OTG_FS_DEVICE,
dp.OTG_FS_PWRCLK,
(dp.OTG_FS_GLOBAL, dp.OTG_FS_DEVICE, dp.OTG_FS_PWRCLK),
);
leds.r1.on();
@ -148,8 +138,8 @@ fn main() -> ! {
let mut store = flash_store::store(dp.FLASH);
let mut channels = Channels::new(pins);
for c in 0..CHANNELS {
match store.read_value::<ChannelConfig>(CHANNEL_CONFIG_KEY[c]) {
for (c, key) in CHANNEL_CONFIG_KEY.iter().enumerate().take(CHANNELS) {
match store.read_value::<ChannelConfig>(key) {
Ok(Some(config)) => config.apply(&mut channels, c),
Ok(None) => error!("flash config not found for channel {}", c),
Err(e) => error!("unable to load config {} from flash: {:?}", c, e),
@ -264,10 +254,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();

View File

@ -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,

View File

@ -133,24 +133,24 @@ impl Pins {
/// Setup GPIO pins and configure MCU peripherals
pub fn setup(
clocks: Clocks,
tim1: TIM1,
tim3: TIM3,
tim8: TIM8,
gpioa: GPIOA,
gpiob: GPIOB,
gpioc: GPIOC,
gpiod: GPIOD,
gpioe: GPIOE,
gpiof: GPIOF,
gpiog: GPIOG,
(tim1, tim3, tim8): (TIM1, TIM3, TIM8),
(gpioa, gpiob, gpioc, gpiod, gpioe, gpiof, gpiog): (
GPIOA,
GPIOB,
GPIOC,
GPIOD,
GPIOE,
GPIOF,
GPIOG,
),
i2c1: I2C1,
spi2: SPI2,
spi4: SPI4,
spi5: SPI5,
(spi2, spi4, spi5): (SPI2, SPI4, SPI5),
adc1: ADC1,
otg_fs_global: OTG_FS_GLOBAL,
otg_fs_device: OTG_FS_DEVICE,
otg_fs_pwrclk: OTG_FS_PWRCLK,
(otg_fs_global, otg_fs_device, otg_fs_pwrclk): (
OTG_FS_GLOBAL,
OTG_FS_DEVICE,
OTG_FS_PWRCLK,
),
) -> (
Self,
Leds,
@ -175,7 +175,11 @@ impl Pins {
let pins_adc = Adc::adc1(adc1, true, Default::default());
let pwm = PwmPins::setup(
clocks, tim1, tim3, gpioc.pc6, gpioc.pc7, gpioe.pe9, gpioe.pe11, gpioe.pe13, gpioe.pe14,
clocks,
(tim1, tim3),
(gpioc.pc6, gpioc.pc7),
(gpioe.pe9, gpioe.pe11),
(gpioe.pe13, gpioe.pe14),
);
let hwrev = HWRev::detect_hw_rev(&HWRevPins {
@ -188,7 +192,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 = if hwrev.major > 2 {
Channel0VRef::Analog(gpioa.pa0.into_analog())
} else {
@ -209,7 +213,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 = if hwrev.major > 2 {
Channel1VRef::Analog(gpioa.pa3.into_analog())
} else {
@ -354,14 +358,10 @@ pub struct PwmPins {
impl PwmPins {
fn setup<M1, M2, M3, M4, M5, M6>(
clocks: Clocks,
tim1: TIM1,
tim3: TIM3,
max_v0: PC6<M1>,
max_v1: PC7<M2>,
max_i_pos0: PE9<M3>,
max_i_pos1: PE11<M4>,
max_i_neg0: PE13<M5>,
max_i_neg1: PE14<M6>,
(tim1, tim3): (TIM1, TIM3),
(max_v0, max_v1): (PC6<M1>, PC7<M2>),
(max_i_pos0, max_i_pos1): (PE9<M3>, PE11<M4>),
(max_i_neg0, max_i_neg1): (PE13<M5>, PE14<M6>),
) -> PwmPins {
let freq = 20u32.khz();

View File

@ -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;
}
}
});

View File

@ -77,12 +77,9 @@ 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);
return (buf_bytes, command.into());
}
None => {}
if let Some(line) = line {
let command = Command::parse(line);
return (buf_bytes, command.into());
}
}
(buf_bytes, SessionInput::Nothing)