tof_control/ltb_control/
ltb_threshold.rs

1use crate::constant::*;
2use crate::helper::ltb_type::{LTBThreshold, LTBError};
3use crate::device::max5815;
4
5impl LTBThreshold {
6    pub fn new() -> Self {
7        match Self::read_thresholds() {
8            Ok(thresholds) => {
9                thresholds
10            }
11            Err(_) => {
12                Self {
13                    thresh_0: f32::MAX,
14                    thresh_1: f32::MAX,
15                    thresh_2: f32::MAX,
16                }
17            }
18        }
19
20    }
21    pub fn read_threshold(channel: u8) -> Result<f32, LTBError> {
22        let ltb_dac = max5815::MAX5815::new(I2C_BUS, LTB_MAX5815_ADDRESS);
23        let threshold_raw = ltb_dac.read_dacn(channel)?;
24        let threshold = Self::adc_to_mv(threshold_raw);
25
26        Ok(threshold)
27    }
28    pub fn read_thresholds() -> Result<LTBThreshold, LTBError> {
29        let ltb_dac: max5815::MAX5815 = max5815::MAX5815::new(I2C_BUS, LTB_MAX5815_ADDRESS);
30        let mut thresholds: [f32; 3] = Default::default();
31        for i in 0..=2 {
32            let threshold_raw = ltb_dac.read_dacn(i)?;
33            thresholds[i as usize] = Self::adc_to_mv(threshold_raw);
34        }
35
36        Ok(
37            LTBThreshold {
38                thresh_0: thresholds[0],
39                thresh_1: thresholds[1],
40                thresh_2: thresholds[2],
41            }
42        )
43    }
44    fn adc_to_mv(adc: u16) -> f32 {
45        let voltage = LTB_DAC_REF_VOLTAGE * (adc as f32) / 2f32.powf(12.0);
46
47        voltage * 1000.0
48    }
49}
50
51pub fn set_default_threshold() -> Result<(), LTBError> {
52    let ltb_dac = max5815::MAX5815::new(I2C_BUS, LTB_MAX5815_ADDRESS);
53    ltb_dac.configure()?;
54
55    let default_thresholds = [LTB_DAC_THRESHOLD_0, LTB_DAC_THRESHOLD_1, LTB_DAC_THRESHOLD_2];
56
57    for (i, default_threshold) in default_thresholds.iter().enumerate() {
58        ltb_dac.coden_loadn(i as u8, mv_to_adc(*default_threshold))?;
59    };
60
61    Ok(())
62}
63
64pub fn set_threshold(channel: u8, threshold: f32) -> Result<(), LTBError> {
65
66    if !(0..=2).contains(&channel) {
67        return Err(LTBError::SetThreshold)
68    } 
69
70    let ltb_dac = max5815::MAX5815::new(I2C_BUS, LTB_MAX5815_ADDRESS);
71    ltb_dac.configure()?;
72
73    ltb_dac.coden_loadn(channel, mv_to_adc(threshold))?;
74
75    Ok(())
76}
77
78pub fn set_thresholds(thresholds: [f32; 3]) -> Result<(), LTBError> {
79
80    let ltb_dac = max5815::MAX5815::new(I2C_BUS, LTB_MAX5815_ADDRESS);
81    ltb_dac.configure()?;
82
83    for (i, threshold) in thresholds.iter().enumerate() {
84
85        if (*threshold < 0.0) | (*threshold > 1000.0) {
86            return Err(LTBError::SetThreshold)
87        }
88        
89        ltb_dac.coden_loadn(i as u8, mv_to_adc(*threshold))?;
90    }
91
92    Ok(())
93}
94
95fn mv_to_adc(mv: f32) -> u16 {
96let adc = (mv / 1000.0) / LTB_DAC_REF_VOLTAGE * 2f32.powf(12.0);
97
98adc as u16
99}
100
101pub fn reset_threshold() -> Result<(), LTBError> {
102    let ltb_dac = max5815::MAX5815::new(I2C_BUS, LTB_MAX5815_ADDRESS);
103    ltb_dac.reset_dac()?;
104
105    Ok(())
106}