sen66_interface/configuration/temperature.rs
1use crate::{error::DataError, util::check_scaling};
2
3/// Temperature offset parameters to compensate temperature effects of the sensor's design-in
4/// using:
5/// `T_Ambient_Compensated = T_Ambient + (slope * T_Ambient) + offset`
6/// Up to 5 temperature offsets can be stored.
7pub struct TemperatureOffset {
8 offset: i16,
9 slope: i16,
10 time_constant: u16,
11 slot: u16,
12}
13
14impl TemperatureOffset {
15 /// Creates a new [`TemperatureOffset`](TemperatureOffset) configuration with the given scaled parameters:
16 /// - `offset`: Constant temperature offset in °C. Applied scale factor: 200
17 /// - `slope`: Normalized temperature offset slope. Applied scale factor: 10,000
18 /// - `time_constant`: Time constant determining how fast the new slope and offset are applied.
19 /// - `slot`: Temperature offset slot to modify. Available slots range from 0 to 4.
20 ///
21 /// # Errors
22 ///
23 /// - [`ValueOutOfRange`](crate::error::DataError::ValueOutOfRange)`: If the values with scaling
24 /// are not in range.
25 pub fn new(offset: i16, slope: i16, time_constant: u16, slot: u16) -> Result<Self, DataError> {
26 Ok(Self {
27 offset: check_scaling(offset, 200, "Temperature Offset", "°C")?,
28 slope: check_scaling(slope, 10_000, "Temperature Slope", "")?,
29 time_constant,
30 slot: if (0..=4).contains(&slot) {
31 slot
32 } else {
33 return Err(DataError::ValueOutOfRange {
34 parameter: "Temperature Slope",
35 min: 0,
36 max: 4,
37 unit: "",
38 });
39 },
40 })
41 }
42}
43
44impl From<TemperatureOffset> for [u16; 4] {
45 fn from(value: TemperatureOffset) -> Self {
46 [
47 value.offset as u16,
48 value.slope as u16,
49 value.time_constant,
50 value.slot,
51 ]
52 }
53}
54
55/// Temperature acceleration parameters for the RH/T engine. No documentation on these has been
56/// published so far.
57pub struct TemperatureAcceleration {
58 k: u16,
59 p: u16,
60 t1: u16,
61 t2: u16,
62}
63
64impl TemperatureAcceleration {
65 /// Creates a new [`TemperatureAcceleration`](TemperatureAcceleration configuration with the given parameters. All
66 /// parameters are scaled with a factor of 10.
67 ///
68 /// # Errors
69 ///
70 /// - [`ValueOutOfRange`](crate::error::DataError::ValueOutOfRange)`: If the values with scaling
71 /// are not in range.
72 pub fn new(k: u16, p: u16, t1: u16, t2: u16) -> Result<Self, DataError> {
73 Ok(Self {
74 k: check_scaling(k, 10, "Temperature Acceleration K", "")?,
75 p: check_scaling(p, 10, "Temperature Acceleration P", "")?,
76 t1: check_scaling(t1, 10, "Temperature Acceleration T1", "")?,
77 t2: check_scaling(t2, 10, "Temperature Acceleration T2", "")?,
78 })
79 }
80}
81
82impl From<TemperatureAcceleration> for [u16; 4] {
83 fn from(value: TemperatureAcceleration) -> Self {
84 [value.k, value.p, value.t1, value.t2]
85 }
86}