diff --git a/src/main.rs b/src/main.rs index f9a0e35..fcd4749 100644 --- a/src/main.rs +++ b/src/main.rs @@ -162,7 +162,7 @@ fn main() -> ! { laser.power_down(); thermostat.power_down(); } - + thermostat.process_tec_readings(); thermostat.start_tec_readings_conversion(); } diff --git a/src/thermostat/max1968.rs b/src/thermostat/max1968.rs index 471df8a..df99ec9 100644 --- a/src/thermostat/max1968.rs +++ b/src/thermostat/max1968.rs @@ -78,6 +78,8 @@ pub struct MAX1968 { pub pins_adc: Adc, pub dma_adc: DMA_Transfer, 1, Adc, PeripheralToMemory, &'static mut [u16; 16]>, pub dac_out_range: ElectricPotential, + vtec_volt_iir: u16, + itec_volt_iir: u16, prev_vtec_volt: ElectricPotential, prev_itec_volt: ElectricPotential, } @@ -243,6 +245,8 @@ impl MAX1968 { pins_adc: pins_adc1, dma_adc: dma_adc, dac_out_range: dac_out_range, + vtec_volt_iir: 1861, // VTEC value centers at 1.5V + itec_volt_iir: 1861, // ITEC value centers at 1.5V prev_vtec_volt: ElectricPotential::new::(0.0), prev_itec_volt: ElectricPotential::new::(0.0), } @@ -261,6 +265,10 @@ impl MAX1968 { } pub fn get_tec_readings(&mut self) -> (ElectricPotential, ElectricPotential) { + (self.prev_vtec_volt, self.prev_itec_volt) + } + + pub fn cal_tec_readings(&mut self) -> (ElectricPotential, ElectricPotential) { if unsafe { DMA_TRANSFER_COMPLETE } { let buffer: &[u16; 16]; unsafe { @@ -276,18 +284,20 @@ impl MAX1968 { itec += *data; } itec = itec >> 3; + self.itec_volt_iir = (7 * self.itec_volt_iir + itec) >> 3; let mut vtec: u16 = 0; for data in buffer.into_iter().skip(1).step_by(2) { vtec += *data; } vtec = vtec >> 3; + self.vtec_volt_iir = (7 * self.vtec_volt_iir + vtec) >> 3; unsafe { ADC2_LOCAL_BUFFER = *buffer; } - self.prev_vtec_volt = ElectricPotential::new::(sample_to_millivolts(vtec) as f32); - self.prev_itec_volt = ElectricPotential::new::(sample_to_millivolts(itec) as f32); + self.prev_vtec_volt = ElectricPotential::new::(sample_to_millivolts(self.vtec_volt_iir) as f32); + self.prev_itec_volt = ElectricPotential::new::(sample_to_millivolts(self.itec_volt_iir) as f32); } (self.prev_vtec_volt, self.prev_itec_volt) } diff --git a/src/thermostat/thermostat.rs b/src/thermostat/thermostat.rs index 8748e8d..7fb47c9 100644 --- a/src/thermostat/thermostat.rs +++ b/src/thermostat/thermostat.rs @@ -158,6 +158,10 @@ impl Thermostat { self.max1968.dma_adc_start_conversion(); } + pub fn process_tec_readings(&mut self) { + self.max1968.cal_tec_readings(); + } + fn tec_setup(&mut self) { self.power_down();