Skip to content

Commit 14a1603

Browse files
committed
adc: demo_mode: fix divide by zero for channels with parents
The simulated total USB Host current is calculates by summing up the individual host currents. This channel does not have anything like exponential decay and thus sets the time constants as zero. Prevent divide by zero and provide immediate decay. Signed-off-by: Leonard Göhrs <[email protected]>
1 parent cca43b5 commit 14a1603

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/adc/iio/demo_mode.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,14 @@ impl CalibratedChannel {
128128

129129
let mut value = f32::from_bits(self.inner.value.load(Ordering::Relaxed));
130130

131+
let decay = if time_constant.abs() < 0.01 {
132+
0.0
133+
} else {
134+
(-dt / time_constant).exp()
135+
};
136+
131137
value -= nominal;
132-
value *= (-dt / time_constant).exp();
138+
value *= decay;
133139
value += (2.0 * thread_rng().gen::<f32>() - 1.0) * self.inner.noise;
134140
value += self
135141
.inner

0 commit comments

Comments
 (0)