From b9d05ff2742186cf3f8c600767c44e56608a18a1 Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 13 May 2020 21:02:26 +0200 Subject: [PATCH] channels: add set_dac() --- src/channels.rs | 30 ++++++++++++++++++------------ src/main.rs | 14 +------------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/channels.rs b/src/channels.rs index ff4d0db..0483463 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -58,24 +58,30 @@ impl Channels { }; if let Some(dac_value) = dac_value { // Forward PID output to i_set DAC - match channel { - 0 => { - self.channel0.dac.set(dac_value).unwrap(); - self.channel0.shdn.set_high().unwrap(); - } - 1 => { - self.channel1.dac.set(dac_value).unwrap(); - self.channel1.shdn.set_high().unwrap(); - } - _ => - unreachable!(), - } + self.set_dac(channel.into(), dac_value); } channel }) } + /// i_set DAC + pub fn set_dac(&mut self, channel: usize, duty: u32) { + match channel { + 0 => { + self.channel0.dac.set(duty).unwrap(); + self.channel0.state.dac_value = duty; + self.channel0.shdn.set_high().unwrap(); + } + 1 => { + self.channel1.dac.set(duty).unwrap(); + self.channel1.state.dac_value = duty; + self.channel1.shdn.set_high().unwrap(); + } + _ => unreachable!(), + } + } + pub fn read_ref_adc(&mut self, channel: usize) -> u16 { match channel { 0 => self.channel0.ref_adc.convert( diff --git a/src/main.rs b/src/main.rs index d0b3ad4..06a377c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,6 @@ use cortex_m_rt::entry; use stm32f4xx_hal::{ hal::{ self, - digital::v2::OutputPin, watchdog::{WatchdogEnable, Watchdog}, }, rcc::RccExt, @@ -253,18 +252,7 @@ fn main() -> ! { } Command::Pwm { channel, pin: PwmPin::ISet, duty } if duty <= ad5680::MAX_VALUE => { channels.channel_state(channel).pid_engaged = false; - match channel { - 0 => { - channels.channel0.dac.set(duty).unwrap(); - channels.channel0.shdn.set_high().unwrap(); - } - 1 => { - channels.channel1.dac.set(duty).unwrap(); - channels.channel1.shdn.set_high().unwrap(); - } - _ => unreachable!(), - } - channels.channel_state(channel).dac_value = duty; + channels.set_dac(channel, duty); let _ = writeln!( socket, "channel {}: PWM duty cycle manually set to {}/{}", channel, duty, ad5680::MAX_VALUE