1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! # Debouncing Signals
//! Use the [`Debounced`] type for this crates functionality.

#![forbid(unsafe_code)]
#![warn(clippy::cargo, clippy::cognitive_complexity)]
#![warn(missing_docs)]
#![no_std]

mod debounced;
mod status;

pub use debounced::Debounced;
pub use status::Status;
pub mod active;
pub mod strategy;

/// Convenience for [`Debounced<_, Integrator, _>`]
pub type DebouncedIntegrator<A, F> = Debounced<A, strategy::Integrator<A>, F>;
//pub type DebouncedShift<A, F> = Debounced<A, strategy::Shift, F>;

mod private {
	pub trait Sealed {}
}

/// # Computes the samples
///
/// Computes the number of samples which have occured at the `sample_freq` (the
/// rate at which the input is probed in Hz) in the duration of `hold_time_ms`
/// (in ms). If a partial sample will have occurred, rounds up.
///
/// The purpose of this function is to determine the number of samples to wait
/// before the [`Integrator`](struct@strategy::Integrator) can settle on an
/// output.
///
/// As if computing `max = sampling_freq * min_hold_time`.
#[inline]
pub fn samples(sample_freq: usize, hold_time_ms: usize) -> usize {
	let samples_in_1k_secs = sample_freq * hold_time_ms;
	// ceiling division
	if samples_in_1k_secs > 0 {
		1 + (samples_in_1k_secs - 1) / 1000
	} else {
		0
	}
}

#[cfg(test)]
mod test_samples {
	use super::*;

	#[test]
	fn samples_small() {
		assert_eq!(samples(1000, 5), 5);
	}

	#[test]
	fn samples_irregular() {
		fn naive_sample_impl(freq: usize, hold_time_ms: usize) -> usize {
			freq * hold_time_ms / 1000
		}

		assert_eq!(naive_sample_impl(250, 3), 0);
		assert_eq!(samples(250, 3), 1);

		assert_eq!(naive_sample_impl(250, 4), 1);
		assert_eq!(samples(250, 4), 1);

		assert_eq!(naive_sample_impl(250, 5), 1);
		assert_eq!(samples(250, 5), 2);
	}
}