diff --git a/src/channels.rs b/src/channels.rs index 7f0d502..5695e8d 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -389,12 +389,20 @@ impl Channels { } pub fn get_max_i_pos(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) { - let duty = self.get_pwm(channel, PwmPin::MaxIPos); + let duty = if self.channel_state(channel).polarity_swapped { + self.get_pwm(channel, PwmPin::MaxINeg) + } else { + self.get_pwm(channel, PwmPin::MaxIPos) + }; (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, MAX_TEC_I) } pub fn get_max_i_neg(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) { - let duty = self.get_pwm(channel, PwmPin::MaxINeg); + let duty = if self.channel_state(channel).polarity_swapped { + self.get_pwm(channel, PwmPin::MaxIPos) + } else { + self.get_pwm(channel, PwmPin::MaxINeg) + }; (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, MAX_TEC_I) } @@ -450,14 +458,22 @@ impl Channels { pub fn set_max_i_pos(&mut self, channel: usize, max_i_pos: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) { let max = ElectricCurrent::new::(3.0); let duty = (max_i_pos.min(MAX_TEC_I).max(ElectricCurrent::zero()) / MAX_TEC_I_DUTY_TO_CURRENT_RATE).get::(); - let duty = self.set_pwm(channel, PwmPin::MaxIPos, duty); + let duty = if self.channel_state(channel).polarity_swapped { + self.set_pwm(channel, PwmPin::MaxINeg, duty) + } else { + self.set_pwm(channel, PwmPin::MaxIPos, duty) + }; (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max) } pub fn set_max_i_neg(&mut self, channel: usize, max_i_neg: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) { let max = ElectricCurrent::new::(3.0); let duty = (max_i_neg.min(MAX_TEC_I).max(ElectricCurrent::zero()) / MAX_TEC_I_DUTY_TO_CURRENT_RATE).get::(); - let duty = self.set_pwm(channel, PwmPin::MaxINeg, duty); + let duty = if self.channel_state(channel).polarity_swapped { + self.set_pwm(channel, PwmPin::MaxIPos, duty) + } else { + self.set_pwm(channel, PwmPin::MaxINeg, duty) + }; (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max) }