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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! Configuration primitives for the ADXL372 driver.
use crate::params::{
AutoSleep, Bandwidth, ExtClk, ExtSync, HpfDisable, I2cHsmEn, InstantOnThreshold, LinkLoopMode,
LowNoise, LpfDisable, OutputDataRate, PowerMode, SettleFilter, UserOrDisable, WakeUpRate,
};
/// User-facing configuration for the ADXL372 sensor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Config {
/// Output data rate selection.
pub odr: OutputDataRate,
/// Wake-up timer period used when autosleep/link-loop is enabled.
pub wakeup_rate: WakeUpRate,
/// External reference clock enable.
pub ext_clk: ExtClk,
/// External sync/trigger enable.
pub ext_sync: ExtSync,
/// User overrange disable behavior.
pub user_or_disable: UserOrDisable,
/// Autosleep operating mode.
pub autosleep: AutoSleep,
/// Activity/inactivity interaction mode.
pub linkloop: LinkLoopMode,
/// Noise performance mode.
pub low_noise: LowNoise,
/// Analog bandwidth selection.
pub bandwidth: Bandwidth,
/// I2C high-speed enable selection.
pub i2c_hsm_en: I2cHsmEn,
/// Instant-on threshold selection.
pub instant_on_threshold: InstantOnThreshold,
/// Filter settle timing selection.
pub filter_settle: SettleFilter,
/// Low-pass filter disable selection.
pub lpf_disable: LpfDisable,
/// High-pass filter disable selection.
pub hpf_disable: HpfDisable,
/// Operating power mode selection.
pub power_mode: PowerMode,
}
impl Config {
/// Begins building a [`Config`] using the builder pattern.
pub fn new() -> ConfigBuilder {
ConfigBuilder::new()
}
/// Checks whether this configuration is valid according to datasheet rules.
pub fn validate(&self) -> core::result::Result<(), ConfigError> {
if self.bandwidth.max_hz() * 2 > self.odr.hz() {
return Err(ConfigError::NyquistViolation);
}
Ok(())
}
}
/// Builder for [`Config`] allowing piecemeal construction.
#[derive(Debug, Clone, Copy)]
pub struct ConfigBuilder {
config: Config,
}
impl ConfigBuilder {
/// Creates a new builder seeded with [`Config::default()`].
pub fn new() -> Self {
Self {
config: Config::default(),
}
}
/// Overrides the output data rate.
pub fn odr(mut self, odr: OutputDataRate) -> Self {
self.config.odr = odr;
self
}
/// Overrides the analog bandwidth.
pub fn bandwidth(mut self, bandwidth: Bandwidth) -> Self {
self.config.bandwidth = bandwidth;
self
}
/// Sets the wake-up timer period.
pub fn wakeup_rate(mut self, wakeup_rate: WakeUpRate) -> Self {
self.config.wakeup_rate = wakeup_rate;
self
}
/// Enables the external clock selection.
pub fn ext_clk(mut self, ext_clk: ExtClk) -> Self {
self.config.ext_clk = ext_clk;
self
}
/// Enables the external sync selection.
pub fn ext_sync(mut self, ext_sync: ExtSync) -> Self {
self.config.ext_sync = ext_sync;
self
}
/// Configures autosleep behaviour.
pub fn autosleep(mut self, autosleep: AutoSleep) -> Self {
self.config.autosleep = autosleep;
self
}
/// Sets the instant-on threshold selection.
pub fn instant_on_threshold(mut self, threshold: InstantOnThreshold) -> Self {
self.config.instant_on_threshold = threshold;
self
}
/// Sets the filter settle timing selection.
pub fn filter_settle(mut self, settle: SettleFilter) -> Self {
self.config.filter_settle = settle;
self
}
/// Configures the low-pass filter disable bit.
pub fn lpf_disable(mut self, setting: LpfDisable) -> Self {
self.config.lpf_disable = setting;
self
}
/// Configures the high-pass filter disable bit.
pub fn hpf_disable(mut self, setting: HpfDisable) -> Self {
self.config.hpf_disable = setting;
self
}
/// Enables or disables I2C high-speed mode.
pub fn i2c_hsm_en(mut self, setting: I2cHsmEn) -> Self {
self.config.i2c_hsm_en = setting;
self
}
/// Sets the user overrange disable behavior.
pub fn user_or_disable(mut self, setting: UserOrDisable) -> Self {
self.config.user_or_disable = setting;
self
}
/// Sets the link/loop activity processing mode.
pub fn linkloop(mut self, mode: LinkLoopMode) -> Self {
self.config.linkloop = mode;
self
}
/// Sets the desired noise performance mode.
pub fn low_noise(mut self, mode: LowNoise) -> Self {
self.config.low_noise = mode;
self
}
/// Sets the desired operating power mode.
pub fn power_mode(mut self, mode: PowerMode) -> Self {
self.config.power_mode = mode;
self
}
/// Finalizes the builder and returns the [`Config`].
pub fn build(self) -> Config {
self.config
}
}
impl Default for Config {
fn default() -> Self {
Self {
odr: OutputDataRate::Od400Hz,
wakeup_rate: WakeUpRate::Ms52,
ext_clk: ExtClk::Disabled,
ext_sync: ExtSync::Disabled,
user_or_disable: UserOrDisable::Enabled,
autosleep: AutoSleep::Disabled,
linkloop: LinkLoopMode::Default,
low_noise: LowNoise::Normal,
bandwidth: Bandwidth::Bw200Hz,
i2c_hsm_en: I2cHsmEn::Disabled,
instant_on_threshold: InstantOnThreshold::Low,
filter_settle: SettleFilter::Ms370,
lpf_disable: LpfDisable::Enabled,
hpf_disable: HpfDisable::Enabled,
power_mode: PowerMode::Standby,
}
}
}
/// Validation errors generated while verifying a [`Config`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigError {
/// Requested bandwidth violates Nyquist sampling limits for the chosen ODR.
NyquistViolation,
}