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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! Gain and frequency range types.
//!
//! Represents hardware parameter ranges as a collection of items that may be
//! continuous intervals, discrete values, or stepped ranges with scaling factors.
/// Single element within a parameter range.
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum RangeItem {
/// Continuous range between two endpoints (inclusive).
Interval(f64, f64),
/// Single discrete value.
Value(f64),
/// Stepped range with `min`, `max`, `step`, and `scale` factor.
Step(f64, f64, f64, f64),
}
impl RangeItem {
/// Returns the lower bound of this range item.
pub fn min(&self) -> f64 {
match self {
RangeItem::Interval(min, _max) => *min,
RangeItem::Value(value) => *value,
RangeItem::Step(min, _max, _step, _scale) => *min,
}
}
/// Returns the upper bound of this range item.
pub fn max(&self) -> f64 {
match self {
RangeItem::Interval(_min, max) => *max,
RangeItem::Value(value) => *value,
RangeItem::Step(_min, max, _step, _scale) => *max,
}
}
/// Returns the step increment for stepped ranges, or `None` for other variants.
pub fn step(&self) -> Option<f64> {
match self {
RangeItem::Interval(_min, _max) => None,
RangeItem::Value(_value) => None,
RangeItem::Step(_min, _max, step, _scale) => Some(*step),
}
}
/// Returns the scale factor for stepped ranges, or `None` for other variants.
pub fn scale(&self) -> Option<f64> {
match self {
RangeItem::Interval(_min, _max) => None,
RangeItem::Value(_value) => None,
RangeItem::Step(_min, _max, _step, scale) => Some(*scale),
}
}
}
/// Collection of range items representing valid parameter values.
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Range {
items: Vec<RangeItem>,
}
impl Range {
/// Creates a new range from the given items.
pub fn new(items: Vec<RangeItem>) -> Self {
Self { items }
}
/// Returns the minimum value across all range items, or `None` if empty.
pub fn min(&self) -> Option<f64> {
self.items
.iter()
.reduce(|a, b| if a.min() < b.min() { a } else { b })
.map(|item| item.min())
}
/// Returns the maximum value across all range items, or `None` if empty.
pub fn max(&self) -> Option<f64> {
self.items
.iter()
.reduce(|a, b| if a.max() > b.max() { a } else { b })
.map(|item| item.max())
}
/// Returns the step value from the first range item, or `None` if not applicable.
pub fn step(&self) -> Option<f64> {
self.items.first().and_then(|item| item.step())
}
/// Returns the scale factor from the first range item, or `None` if not applicable.
pub fn scale(&self) -> Option<f64> {
self.items.first().and_then(|item| item.scale())
}
/// Returns the step value, or an error if the range does not have a step.
pub fn step_checked(&self) -> crate::error::Result<f64> {
self.step()
.ok_or(crate::error::Error::BoardState("gain range missing step"))
}
/// Returns the scale factor, or an error if the range does not have a scale.
pub fn scale_checked(&self) -> crate::error::Result<f64> {
self.scale()
.ok_or(crate::error::Error::BoardState("gain range missing scale"))
}
/// Returns the minimum value, or an error if the range is empty.
pub fn min_checked(&self) -> crate::error::Result<f64> {
self.min()
.ok_or(crate::error::Error::BoardState("gain range missing min"))
}
/// Returns the maximum value, or an error if the range is empty.
pub fn max_checked(&self) -> crate::error::Result<f64> {
self.max()
.ok_or(crate::error::Error::BoardState("gain range missing max"))
}
/// Returns `true` if the value falls within any of the range items.
/// For stepped ranges, checks that the value aligns with the step grid.
/// Uses epsilon-aware comparison for floating-point equality.
pub fn contains(&self, value: f64) -> bool {
for item in &self.items {
match *item {
RangeItem::Interval(a, b) => {
if a <= value && value <= b {
return true;
}
}
RangeItem::Value(v) => {
if (v - value).abs() <= v.abs().max(value.abs()) * f64::EPSILON * 2.0 {
return true;
}
}
RangeItem::Step(min, max, step, _scale) => {
if value < min {
continue;
}
let mut v = min + ((value - min) / step).floor() * step;
while v <= max && v <= value {
if (v - value).abs() <= v.abs().max(value.abs()) * f64::EPSILON * 2.0 {
return true;
}
v += step;
}
}
}
}
false
}
/// Finds the value within the range that is closest to the target.
/// If the target is already within the range, returns it as-is.
/// Returns the nearest valid value from all range items.
pub fn closest(&self, value: f64) -> Option<f64> {
fn closer(target: f64, closest: Option<f64>, current: f64) -> f64 {
match closest {
Some(c) => {
if (target - current).abs() < (c - target).abs() {
current
} else {
c
}
}
None => current,
}
}
if self.contains(value) {
Some(value)
} else {
let mut close = None;
for i in self.items.iter() {
match i {
RangeItem::Interval(a, b) => {
close = Some(closer(value, close, *a));
close = Some(closer(value, close, *b));
}
RangeItem::Value(a) => {
close = Some(closer(value, close, *a));
}
RangeItem::Step(min, max, step, _scale) => {
if value <= *min {
close = Some(closer(value, close, *min));
continue;
}
if value >= *max {
close = Some(closer(value, close, *max));
continue;
}
let mut v = min + ((value - min) / step).floor() * step;
while v <= *max && v <= value + step {
close = Some(closer(value, close, v));
v += step;
}
}
}
}
close
}
}
/// Finds the smallest value within the range that is at least the target.
/// If the target is already within the range, returns it as-is.
/// Returns `None` if no valid value meets or exceeds the target.
pub fn at_least(&self, value: f64) -> Option<f64> {
fn closer_at_least(target: f64, closest: Option<f64>, current: f64) -> Option<f64> {
match closest {
Some(c) => {
if (target - current).abs() < (c - target).abs() && current >= target {
Some(current)
} else {
closest
}
}
None => {
if current >= target {
Some(current)
} else {
None
}
}
}
}
if self.contains(value) {
Some(value)
} else {
let mut close = None;
for i in self.items.iter() {
match i {
RangeItem::Interval(a, b) => {
close = closer_at_least(value, close, *a);
close = closer_at_least(value, close, *b);
}
RangeItem::Value(a) => {
close = closer_at_least(value, close, *a);
}
RangeItem::Step(min, max, step, _scale) => {
if value <= *min {
close = closer_at_least(value, close, *min);
continue;
}
if value >= *max {
close = closer_at_least(value, close, *max);
continue;
}
let mut v = min + ((value - min) / step).floor() * step;
while v <= *max && v <= value + step {
close = closer_at_least(value, close, v);
v += step;
}
}
}
}
close
}
}
/// Finds the largest value within the range that does not exceed the target.
/// If the target is already within the range, returns it as-is.
/// Returns `None` if no valid value is at or below the target.
pub fn at_max(&self, value: f64) -> Option<f64> {
fn closer_at_max(target: f64, closest: Option<f64>, current: f64) -> Option<f64> {
match closest {
Some(c) => {
if (target - current).abs() < (c - target).abs() && current <= target {
Some(current)
} else {
closest
}
}
None => {
if current <= target {
Some(current)
} else {
None
}
}
}
}
if self.contains(value) {
Some(value)
} else {
let mut close = None;
for i in self.items.iter() {
match i {
RangeItem::Interval(a, b) => {
close = closer_at_max(value, close, *a);
close = closer_at_max(value, close, *b);
}
RangeItem::Value(a) => {
close = closer_at_max(value, close, *a);
}
RangeItem::Step(min, max, step, _scale) => {
if value <= *min {
close = closer_at_max(value, close, *min);
continue;
}
if value >= *max {
close = closer_at_max(value, close, *max);
continue;
}
let mut v = min + ((value - min) / step).floor() * step;
while v <= *max && v <= value + step {
close = closer_at_max(value, close, v);
v += step;
}
}
}
}
close
}
}
/// Returns an iterator over the range items.
pub fn iter(&self) -> impl Iterator<Item = &RangeItem> {
self.items.iter()
}
}