From d1cc6776688eeb15c2e8e484f63eb510296e284c Mon Sep 17 00:00:00 2001 From: linuswck Date: Fri, 23 Feb 2024 12:58:35 +0800 Subject: [PATCH] thermostat: split poll_adc & update_pid into 2 fns --- src/main.rs | 3 ++- src/thermostat/thermostat.rs | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index ee7e948..4d4dc41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,7 +92,8 @@ fn main() -> ! { laser.poll_and_update_output_current(); - if thermostat.poll_adc_and_update_pid() { + if thermostat.poll_adc() { + thermostat.update_pid(); if thermostat.get_temp_mon_status().over_temp_alarm { laser.power_down(); thermostat.set_pid_engaged(false); diff --git a/src/thermostat/thermostat.rs b/src/thermostat/thermostat.rs index 0ccc8a5..e35b253 100644 --- a/src/thermostat/thermostat.rs +++ b/src/thermostat/thermostat.rs @@ -151,31 +151,36 @@ impl Thermostat{ adc_calibration0 } - pub fn poll_adc_and_update_pid(&mut self) -> bool { + pub fn poll_adc(&mut self) -> bool { let mut data_rdy = false; self.ad7172.data_ready().unwrap().map(|_ch| { let data = self.ad7172.read_data().unwrap(); let state: &mut PidState = &mut self.pid_ctrl_ch0; state.update(data); - debug!("state.get_pid_engaged(): {:?}", state.get_pid_engaged()); let pid_engaged = state.get_pid_engaged(); - if pid_engaged { - match state.update_pid() { - Some(pid_output) => { - self.set_i(ElectricCurrent::new::(pid_output)); - debug!("Temperature Set Point: {:?} degree", self.pid_ctrl_ch0.get_pid_setpoint().get::()); - } - None => { } - } - } let temp = self.get_temperature(); self.temp_mon.update_status(pid_engaged, temp); + debug!("state.get_pid_engaged(): {:?}", pid_engaged); debug!("Temperature: {:?} degree", temp.get::()); data_rdy = true; }); data_rdy } + pub fn update_pid(&mut self) { + let state: &mut PidState = &mut self.pid_ctrl_ch0; + let pid_engaged = state.get_pid_engaged(); + if pid_engaged { + match state.update_pid() { + Some(pid_output) => { + self.set_i(ElectricCurrent::new::(pid_output)); + debug!("Temperature Set Point: {:?} degree", self.pid_ctrl_ch0.get_pid_setpoint().get::()); + } + None => { } + } + } + } + pub fn get_temp_mon_status(&mut self) -> TempStatus { self.temp_mon.get_status() }