-
Notifications
You must be signed in to change notification settings - Fork 1
/
mode_struct.rs
3326 lines (3162 loc) · 147 KB
/
mode_struct.rs
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::{
create_discrete_increment_interval, create_unit_value_interval, full_unit_interval,
mode::feedback_util, negative_if, ControlType, ControlValue, DiscreteIncrement, DiscreteValue,
Interval, MinIsMaxBehavior, OutOfRangeBehavior, PressDurationProcessor, Target, Transformation,
UnitIncrement, UnitValue,
};
use derive_more::Display;
use enum_iterator::IntoEnumIterator;
use num_enum::{IntoPrimitive, TryFromPrimitive};
#[cfg(feature = "serde_repr")]
use serde_repr::{Deserialize_repr, Serialize_repr};
/// Settings for processing all kinds of control values.
///
/// ## How relative control values are processed (or button taps interpreted as increments).
///
/// Here's an overview in which cases step counts are used and in which step sizes.
/// This is the same, no matter if the source emits relative increments or absolute values
/// ("relative one-direction mode").
///
/// - Target wants relative increments: __Step counts__
/// - Example: Action with invocation type "Relative"
/// - Displayed as: "{count} x"
/// - Target wants absolute values
/// - Target is continuous, optionally roundable: __Step sizes__
/// - Example: Track volume
/// - Displayed as: "{size} {unit}"
/// - Target is discrete: __Step counts__
/// - Example: FX preset, some FX params
/// - Displayed as: "{count} x" or "{count}" (former if source emits increments) TODO I
/// think now we have only the "x" variant
#[derive(Clone, Debug)]
pub struct Mode<T: Transformation> {
pub absolute_mode: AbsoluteMode,
pub source_value_interval: Interval<UnitValue>,
pub target_value_interval: Interval<UnitValue>,
/// Negative increments represent fractions (throttling), e.g. -2 fires an increment every
/// 2nd time only.
pub step_count_interval: Interval<DiscreteIncrement>,
pub step_size_interval: Interval<UnitValue>,
pub jump_interval: Interval<UnitValue>,
// TODO-low Not cool to make this public. Maybe derive a builder for this beast.
pub press_duration_processor: PressDurationProcessor,
pub approach_target_value: bool,
pub reverse: bool,
pub rotate: bool,
pub round_target_value: bool,
pub out_of_range_behavior: OutOfRangeBehavior,
pub control_transformation: Option<T>,
pub feedback_transformation: Option<T>,
/// Counter for implementing throttling.
///
/// Throttling is implemented by spitting out control values only every nth time. The counter
/// can take positive or negative values in order to detect direction changes. This is positive
/// when the last change was a positive increment and negative when the last change was a
/// negative increment.
pub increment_counter: i32,
}
#[derive(
Clone, Copy, Debug, PartialEq, Eq, IntoEnumIterator, TryFromPrimitive, IntoPrimitive, Display,
)]
#[cfg_attr(feature = "serde_repr", derive(Serialize_repr, Deserialize_repr))]
#[repr(usize)]
pub enum AbsoluteMode {
#[display(fmt = "Normal")]
Normal = 0,
#[display(fmt = "Incremental buttons")]
IncrementalButtons = 1,
#[display(fmt = "Toggle buttons")]
ToggleButtons = 2,
}
impl Default for AbsoluteMode {
fn default() -> Self {
AbsoluteMode::Normal
}
}
impl<T: Transformation> Default for Mode<T> {
fn default() -> Self {
Mode {
absolute_mode: AbsoluteMode::Normal,
source_value_interval: full_unit_interval(),
target_value_interval: full_unit_interval(),
// 0.01 has been chosen as default minimum step size because it corresponds to 1%.
// 0.01 has also been chosen as default maximum step size because most users probably
// want to start easy, that is without using the "press harder = more increments"
// respectively "dial harder = more increments" features. Activating them right from
// the start by choosing a higher step size maximum could lead to surprising results
// such as ugly parameters jumps, especially if the source is not suited for that.
step_size_interval: create_unit_value_interval(0.01, 0.01),
// Same reasoning like with `step_size_interval`
step_count_interval: create_discrete_increment_interval(1, 1),
jump_interval: full_unit_interval(),
press_duration_processor: Default::default(),
approach_target_value: false,
reverse: false,
round_target_value: false,
out_of_range_behavior: OutOfRangeBehavior::MinOrMax,
control_transformation: None,
feedback_transformation: None,
rotate: false,
increment_counter: 0,
}
}
}
impl<T: Transformation> Mode<T> {
/// Processes the given control value and maybe returns an appropriate target control value.
pub fn control(
&mut self,
control_value: ControlValue,
target: &impl Target,
) -> Option<ControlValue> {
match control_value {
ControlValue::Relative(i) => self.control_relative(i, target),
ControlValue::Absolute(v) => {
use AbsoluteMode::*;
match self.absolute_mode {
Normal => self
.control_absolute_normal(v, target)
.map(ControlValue::Absolute),
IncrementalButtons => self.control_absolute_incremental_buttons(v, target),
ToggleButtons => self
.control_absolute_toggle_buttons(v, target)
.map(ControlValue::Absolute),
}
}
}
}
/// Takes a target value, interprets and transforms it conforming to mode rules and
/// maybe returns an appropriate source value that should be sent to the source.
pub fn feedback(&self, target_value: UnitValue) -> Option<UnitValue> {
feedback_util::feedback(
target_value,
self.reverse,
&self.feedback_transformation,
&self.source_value_interval,
&self.target_value_interval,
self.out_of_range_behavior,
)
}
/// Processes the given control value in absolute mode and maybe returns an appropriate target
/// value.
fn control_absolute_normal(
&mut self,
control_value: UnitValue,
target: &impl Target,
) -> Option<UnitValue> {
let control_value = self.press_duration_processor.process(control_value)?;
let (source_bound_value, min_is_max_behavior) =
if control_value.is_within_interval(&self.source_value_interval) {
// Control value is within source value interval
(control_value, MinIsMaxBehavior::PreferOne)
} else {
// Control value is outside source value interval
use OutOfRangeBehavior::*;
match self.out_of_range_behavior {
MinOrMax => {
if control_value < self.source_value_interval.min_val() {
(
self.source_value_interval.min_val(),
MinIsMaxBehavior::PreferZero,
)
} else {
(
self.source_value_interval.max_val(),
MinIsMaxBehavior::PreferOne,
)
}
}
Min => (
self.source_value_interval.min_val(),
MinIsMaxBehavior::PreferZero,
),
Ignore => return None,
}
};
let current_target_value = target.current_value();
// Control value is within source value interval
let pepped_up_control_value = self.pep_up_control_value(
source_bound_value,
target,
current_target_value,
min_is_max_behavior,
);
self.hitting_target_considering_max_jump(pepped_up_control_value, current_target_value)
}
/// Relative one-direction mode (convert absolute button presses to relative increments)
fn control_absolute_incremental_buttons(
&mut self,
control_value: UnitValue,
target: &impl Target,
) -> Option<ControlValue> {
let control_value = self.press_duration_processor.process(control_value)?;
if control_value.is_zero() || !control_value.is_within_interval(&self.source_value_interval)
{
return None;
}
use ControlType::*;
match target.control_type() {
AbsoluteContinuous
| AbsoluteContinuousRoundable { .. }
// TODO-low I think trigger and switch targets don't make sense at all here because
// instead of +/- n they need just "trigger!" or "on/off!".
| AbsoluteTrigger
| AbsoluteSwitch => {
// Continuous target
//
// Settings:
// - Source value interval (for setting the input interval of relevant source
// values)
// - Minimum target step size (enables accurate minimum increment, atomic)
// - Maximum target step size (enables accurate maximum increment, clamped)
// - Target value interval (absolute, important for rotation only, clamped)
let step_size_value = control_value
.map_to_unit_interval_from(
&self.source_value_interval,
MinIsMaxBehavior::PreferOne,
)
.map_from_unit_interval_to(&self.step_size_interval);
let step_size_increment =
step_size_value.to_increment(negative_if(self.reverse))?;
self.hit_target_absolutely_with_unit_increment(
step_size_increment,
self.step_size_interval.min_val(),
target.current_value()?,
)
}
AbsoluteDiscrete { atomic_step_size } => {
// Discrete target
//
// Settings:
// - Source value interval (for setting the input interval of relevant source
// values)
// - Minimum target step count (enables accurate normal/minimum increment, atomic)
// - Target value interval (absolute, important for rotation only, clamped)
// - Maximum target step count (enables accurate maximum increment, clamped)
let discrete_increment = self.convert_to_discrete_increment(control_value)?;
self.hit_discrete_target_absolutely(discrete_increment, atomic_step_size, || {
target.current_value()
})
}
Relative
// This is cool! With this, we can make controllers without encoders simulate them
// by assigning one - button and one + button to the same virtual multi target.
// Of course, all we can deliver is increments/decrements since virtual targets
// don't provide a current target value. But we also don't need it because all we
// want to do is simulate an encoder.
| VirtualMulti => {
// Target wants increments so we just generate them e.g. depending on how hard the
// button has been pressed
//
// - Source value interval (for setting the input interval of relevant source
// values)
// - Minimum target step count (enables accurate normal/minimum increment, atomic)
// - Maximum target step count (enables accurate maximum increment, mapped)
let discrete_increment = self.convert_to_discrete_increment(control_value)?;
Some(ControlValue::Relative(discrete_increment))
}
VirtualButton => {
// This doesn't make sense at all. Buttons just need to be triggered, not fed with
// +/- n.
None
},
}
}
fn control_absolute_toggle_buttons(
&mut self,
control_value: UnitValue,
target: &impl Target,
) -> Option<UnitValue> {
let control_value = self.press_duration_processor.process(control_value)?;
if control_value.is_zero() {
return None;
}
let center_target_value = self.target_value_interval.center();
// Nothing we can do if we can't get the current target value. This shouldn't happen
// usually because virtual targets are not supposed to be used with toggle mode.
let current_target_value = target.current_value()?;
let desired_target_value = if current_target_value > center_target_value {
self.target_value_interval.min_val()
} else {
self.target_value_interval.max_val()
};
if desired_target_value == current_target_value {
return None;
}
Some(desired_target_value)
}
// Classic relative mode: We are getting encoder increments from the source.
// We don't need source min/max config in this case. At least I can't think of a use case
// where one would like to totally ignore especially slow or especially fast encoder movements,
// I guess that possibility would rather cause irritation.
fn control_relative(
&mut self,
discrete_increment: DiscreteIncrement,
target: &impl Target,
) -> Option<ControlValue> {
use ControlType::*;
match target.control_type() {
AbsoluteContinuous
| AbsoluteContinuousRoundable { .. }
// TODO-low Controlling a switch/trigger target with +/- n doesn't make sense.
| AbsoluteSwitch
| AbsoluteTrigger => {
// Continuous target
//
// Settings which are always necessary:
// - Minimum target step size (enables accurate minimum increment, atomic)
// - Target value interval (absolute, important for rotation only, clamped)
//
// Settings which are necessary in order to support >1-increments:
// - Maximum target step size (enables accurate maximum increment, clamped)
let potentially_reversed_increment = if self.reverse {
discrete_increment.inverse()
} else {
discrete_increment
};
let unit_increment = potentially_reversed_increment
.to_unit_increment(self.step_size_interval.min_val())?;
let clamped_unit_increment =
unit_increment.clamp_to_interval(&self.step_size_interval);
self.hit_target_absolutely_with_unit_increment(
clamped_unit_increment,
self.step_size_interval.min_val(),
target.current_value()?,
)
}
AbsoluteDiscrete { atomic_step_size } => {
// Discrete target
//
// Settings which are always necessary:
// - Minimum target step count (enables accurate normal/minimum increment, atomic)
// - Target value interval (absolute, important for rotation only, clamped)
//
// Settings which are necessary in order to support >1-increments:
// - Maximum target step count (enables accurate maximum increment, clamped)
let pepped_up_increment = self.pep_up_discrete_increment(discrete_increment)?;
self.hit_discrete_target_absolutely(pepped_up_increment, atomic_step_size, || {
target.current_value()
})
}
Relative | VirtualMulti => {
// Target wants increments so we just forward them after some preprocessing
//
// Settings which are always necessary:
// - Minimum target step count (enables accurate normal/minimum increment, clamped)
//
// Settings which are necessary in order to support >1-increments:
// - Maximum target step count (enables accurate maximum increment, clamped)
let pepped_up_increment = self.pep_up_discrete_increment(discrete_increment)?;
Some(ControlValue::Relative(pepped_up_increment))
}
VirtualButton => {
// Controlling a button target with +/- n doesn't make sense.
None
}
}
}
fn pep_up_control_value(
&self,
control_value: UnitValue,
target: &impl Target,
current_target_value: Option<UnitValue>,
min_is_max_behavior: MinIsMaxBehavior,
) -> UnitValue {
// 1. Apply source interval
let mapped_control_value = control_value
.map_to_unit_interval_from(&self.source_value_interval, min_is_max_behavior);
// 2. Apply transformation
let transformed_source_value = self
.control_transformation
.as_ref()
.and_then(|t| {
t.transform(
mapped_control_value,
current_target_value.unwrap_or(UnitValue::MIN),
)
.ok()
})
.unwrap_or(mapped_control_value);
// 3. Apply target interval
let mapped_target_value =
transformed_source_value.map_from_unit_interval_to(&self.target_value_interval);
// 4. Apply reverse
let potentially_inversed_target_value = if self.reverse {
mapped_target_value.inverse()
} else {
mapped_target_value
};
// 5. Apply rounding
if self.round_target_value {
round_to_nearest_discrete_value(
&target.control_type(),
potentially_inversed_target_value,
)
} else {
potentially_inversed_target_value
}
}
fn hitting_target_considering_max_jump(
&self,
control_value: UnitValue,
current_target_value: Option<UnitValue>,
) -> Option<UnitValue> {
let current_target_value = match current_target_value {
// No target value available ... just deliver! Virtual targets take this shortcut.
None => return Some(control_value),
Some(v) => v,
};
if self.jump_interval.is_full() {
// No jump restrictions whatsoever
return self.hit_if_changed(control_value, current_target_value);
}
let distance = control_value.calc_distance_from(current_target_value);
if distance > self.jump_interval.max_val() {
// Distance is too large
if !self.approach_target_value {
// Scaling not desired. Do nothing.
return None;
}
// Scaling desired
let approach_distance = distance.map_from_unit_interval_to(&self.jump_interval);
let approach_increment = approach_distance
.to_increment(negative_if(control_value < current_target_value))?;
let final_target_value =
current_target_value.add_clamping(approach_increment, &self.target_value_interval);
return self.hit_if_changed(final_target_value, current_target_value);
}
// Distance is not too large
if distance < self.jump_interval.min_val() {
return None;
}
// Distance is also not too small
self.hit_if_changed(control_value, current_target_value)
}
fn hit_if_changed(
&self,
desired_target_value: UnitValue,
current_target_value: UnitValue,
) -> Option<UnitValue> {
if current_target_value == desired_target_value {
return None;
}
Some(desired_target_value)
}
fn hit_discrete_target_absolutely(
&self,
discrete_increment: DiscreteIncrement,
target_step_size: UnitValue,
current_value: impl Fn() -> Option<UnitValue>,
) -> Option<ControlValue> {
let unit_increment = discrete_increment.to_unit_increment(target_step_size)?;
self.hit_target_absolutely_with_unit_increment(
unit_increment,
target_step_size,
current_value()?,
)
}
fn hit_target_absolutely_with_unit_increment(
&self,
increment: UnitIncrement,
grid_interval_size: UnitValue,
current_target_value: UnitValue,
) -> Option<ControlValue> {
let snapped_target_value_interval = Interval::new(
self.target_value_interval
.min_val()
.snap_to_grid_by_interval_size(grid_interval_size),
self.target_value_interval
.max_val()
.snap_to_grid_by_interval_size(grid_interval_size),
);
// The add functions don't add anything if the current target value is not within the target
// interval in the first place. Instead they return one of the interval bounds. One issue
// that might occur is that the current target value only *appears* out-of-range
// because of numerical inaccuracies. That could lead to frustrating "it doesn't move"
// experiences. Therefore we snap the current target value to grid first in that case.
let snapped_current_target_value =
if current_target_value.is_within_interval(&snapped_target_value_interval) {
current_target_value
} else {
current_target_value.snap_to_grid_by_interval_size(grid_interval_size)
};
let desired_target_value = if self.rotate {
snapped_current_target_value.add_rotating(increment, &snapped_target_value_interval)
} else {
snapped_current_target_value.add_clamping(increment, &snapped_target_value_interval)
};
if desired_target_value == current_target_value {
return None;
}
Some(ControlValue::Absolute(desired_target_value))
}
fn pep_up_discrete_increment(
&mut self,
increment: DiscreteIncrement,
) -> Option<DiscreteIncrement> {
let factor = increment.clamp_to_interval(&self.step_count_interval);
let actual_increment = if factor.is_positive() {
factor
} else {
let nth = factor.get().abs() as u32;
let (fire, new_counter_value) = self.its_time_to_fire(nth, increment.signum());
self.increment_counter = new_counter_value;
if !fire {
return None;
}
DiscreteIncrement::new(1)
};
let clamped_increment = actual_increment.with_direction(increment.signum());
let result = if self.reverse {
clamped_increment.inverse()
} else {
clamped_increment
};
Some(result)
}
/// `nth` stands for "fire every nth time". `direction_signum` is either +1 or -1.
fn its_time_to_fire(&self, nth: u32, direction_signum: i32) -> (bool, i32) {
if self.increment_counter == 0 {
// Initial fire
return (true, direction_signum);
}
if self.increment_counter.signum() != direction_signum {
// Change of direction. In this case always fire.
return (true, direction_signum);
}
let positive_increment_counter = self.increment_counter.abs() as u32;
if positive_increment_counter >= nth {
// After having waited for a few increments, fire again.
return (true, direction_signum);
}
(false, self.increment_counter + direction_signum)
}
fn convert_to_discrete_increment(
&mut self,
control_value: UnitValue,
) -> Option<DiscreteIncrement> {
let factor = control_value
.map_to_unit_interval_from(&self.source_value_interval, MinIsMaxBehavior::PreferOne)
.map_from_unit_interval_to_discrete_increment(&self.step_count_interval);
// This mode supports positive increment only.
let discrete_value = if factor.is_positive() {
factor.to_value()
} else {
let nth = factor.get().abs() as u32;
let (fire, new_counter_value) = self.its_time_to_fire(nth, 1);
self.increment_counter = new_counter_value;
if !fire {
return None;
}
DiscreteValue::new(1)
};
discrete_value.to_increment(negative_if(self.reverse))
}
}
fn round_to_nearest_discrete_value(
control_type: &ControlType,
approximate_control_value: UnitValue,
) -> UnitValue {
// round() is the right choice here vs. floor() because we don't want slight numerical
// inaccuracies lead to surprising jumps
use ControlType::*;
let step_size = match control_type {
AbsoluteContinuousRoundable { rounding_step_size } => *rounding_step_size,
AbsoluteDiscrete { atomic_step_size } => *atomic_step_size,
AbsoluteTrigger | AbsoluteSwitch | AbsoluteContinuous | Relative | VirtualMulti
| VirtualButton => return approximate_control_value,
};
approximate_control_value.snap_to_grid_by_interval_size(step_size)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mode::test_util::{TestTarget, TestTransformation};
use crate::{create_unit_value_interval, ControlType};
use approx::*;
mod absolute_normal {
use super::*;
#[test]
fn default() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.5), &target).unwrap(), abs(0.5));
assert!(mode.control(abs(0.777), &target).is_none());
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(1.0));
}
#[test]
fn relative_target() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::Relative,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.5), &target).unwrap(), abs(0.5));
assert!(mode.control(abs(0.777), &target).is_none());
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(1.0));
}
#[test]
fn source_interval() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
source_value_interval: create_unit_value_interval(0.2, 0.6),
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.1), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.2), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.4), &target).unwrap(), abs(0.5));
assert_abs_diff_eq!(mode.control(abs(0.6), &target).unwrap(), abs(1.0));
assert_abs_diff_eq!(mode.control(abs(0.8), &target).unwrap(), abs(1.0));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(1.0));
}
#[test]
fn source_interval_out_of_range_ignore() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
source_value_interval: create_unit_value_interval(0.2, 0.6),
out_of_range_behavior: OutOfRangeBehavior::Ignore,
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert!(mode.control(abs(0.0), &target).is_none());
assert!(mode.control(abs(0.1), &target).is_none());
assert_abs_diff_eq!(mode.control(abs(0.2), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.4), &target).unwrap(), abs(0.5));
assert_abs_diff_eq!(mode.control(abs(0.6), &target).unwrap(), abs(1.0));
assert!(mode.control(abs(0.8), &target).is_none());
assert!(mode.control(abs(1.0), &target).is_none());
}
#[test]
fn source_interval_out_of_range_min() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
source_value_interval: create_unit_value_interval(0.2, 0.6),
out_of_range_behavior: OutOfRangeBehavior::Min,
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.1), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.2), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.4), &target).unwrap(), abs(0.5));
assert_abs_diff_eq!(mode.control(abs(0.6), &target).unwrap(), abs(1.0));
assert_abs_diff_eq!(mode.control(abs(0.8), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(0.0));
}
#[test]
fn source_interval_out_of_range_ignore_source_one_value() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
source_value_interval: create_unit_value_interval(0.5, 0.5),
out_of_range_behavior: OutOfRangeBehavior::Ignore,
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert!(mode.control(abs(0.0), &target).is_none());
assert!(mode.control(abs(0.4), &target).is_none());
assert_abs_diff_eq!(mode.control(abs(0.5), &target).unwrap(), abs(1.0));
assert!(mode.control(abs(0.6), &target).is_none());
assert!(mode.control(abs(1.0), &target).is_none());
}
#[test]
fn source_interval_out_of_range_min_source_one_value() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
source_value_interval: create_unit_value_interval(0.5, 0.5),
out_of_range_behavior: OutOfRangeBehavior::Min,
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.4), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.5), &target).unwrap(), abs(1.0));
assert_abs_diff_eq!(mode.control(abs(0.6), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(0.0));
}
#[test]
fn source_interval_out_of_range_min_max_source_one_value() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
source_value_interval: create_unit_value_interval(0.5, 0.5),
out_of_range_behavior: OutOfRangeBehavior::MinOrMax,
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.4), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.5), &target).unwrap(), abs(1.0));
assert_abs_diff_eq!(mode.control(abs(0.6), &target).unwrap(), abs(1.0));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(1.0));
}
#[test]
fn target_interval() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
target_value_interval: create_unit_value_interval(0.2, 0.6),
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.2));
assert_abs_diff_eq!(mode.control(abs(0.2), &target).unwrap(), abs(0.28));
assert_abs_diff_eq!(mode.control(abs(0.25), &target).unwrap(), abs(0.3));
assert_abs_diff_eq!(mode.control(abs(0.5), &target).unwrap(), abs(0.4));
assert_abs_diff_eq!(mode.control(abs(0.75), &target).unwrap(), abs(0.5));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(0.6));
}
#[test]
fn source_and_target_interval() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
source_value_interval: create_unit_value_interval(0.2, 0.6),
target_value_interval: create_unit_value_interval(0.2, 0.6),
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.2));
assert_abs_diff_eq!(mode.control(abs(0.2), &target).unwrap(), abs(0.2));
assert_abs_diff_eq!(mode.control(abs(0.4), &target).unwrap(), abs(0.4));
assert_abs_diff_eq!(mode.control(abs(0.6), &target).unwrap(), abs(0.6));
assert_abs_diff_eq!(mode.control(abs(0.8), &target).unwrap(), abs(0.6));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(0.6));
}
#[test]
fn source_and_target_interval_shifted() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
source_value_interval: create_unit_value_interval(0.2, 0.6),
target_value_interval: create_unit_value_interval(0.4, 0.8),
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.4));
assert_abs_diff_eq!(mode.control(abs(0.2), &target).unwrap(), abs(0.4));
assert_abs_diff_eq!(mode.control(abs(0.4), &target).unwrap(), abs(0.6));
assert_abs_diff_eq!(mode.control(abs(0.6), &target).unwrap(), abs(0.8));
assert_abs_diff_eq!(mode.control(abs(0.8), &target).unwrap(), abs(0.8));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(0.8));
}
#[test]
fn reverse() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
reverse: true,
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(1.0));
assert_abs_diff_eq!(mode.control(abs(0.5), &target).unwrap(), abs(0.5));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(0.0));
}
#[test]
fn round() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
round_target_value: true,
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteDiscrete {
atomic_step_size: UnitValue::new(0.2),
},
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.11), &target).unwrap(), abs(0.2));
assert_abs_diff_eq!(mode.control(abs(0.19), &target).unwrap(), abs(0.2));
assert_abs_diff_eq!(mode.control(abs(0.2), &target).unwrap(), abs(0.2));
assert_abs_diff_eq!(mode.control(abs(0.35), &target).unwrap(), abs(0.4));
assert_abs_diff_eq!(mode.control(abs(0.49), &target).unwrap(), abs(0.4));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(1.0));
}
#[test]
fn jump_interval() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
jump_interval: create_unit_value_interval(0.0, 0.2),
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.5)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert!(mode.control(abs(0.0), &target).is_none());
assert!(mode.control(abs(0.1), &target).is_none());
assert_abs_diff_eq!(mode.control(abs(0.4), &target).unwrap(), abs(0.4));
assert_abs_diff_eq!(mode.control(abs(0.6), &target).unwrap(), abs(0.6));
assert_abs_diff_eq!(mode.control(abs(0.7), &target).unwrap(), abs(0.7));
assert!(mode.control(abs(0.8), &target).is_none());
assert!(mode.control(abs(0.9), &target).is_none());
assert!(mode.control(abs(1.0), &target).is_none());
}
#[test]
fn jump_interval_min() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
jump_interval: create_unit_value_interval(0.1, 1.0),
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.5)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.1), &target).unwrap(), abs(0.1));
assert!(mode.control(abs(0.4), &target).is_none());
assert!(mode.control(abs(0.5), &target).is_none());
assert!(mode.control(abs(0.6), &target).is_none());
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(1.0));
}
#[test]
fn jump_interval_approach() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
jump_interval: create_unit_value_interval(0.0, 0.2),
approach_target_value: true,
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.5)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.4));
assert_abs_diff_eq!(mode.control(abs(0.1), &target).unwrap(), abs(0.42));
assert_abs_diff_eq!(mode.control(abs(0.4), &target).unwrap(), abs(0.4));
assert_abs_diff_eq!(mode.control(abs(0.6), &target).unwrap(), abs(0.6));
assert_abs_diff_eq!(mode.control(abs(0.7), &target).unwrap(), abs(0.7));
assert_abs_diff_eq!(mode.control(abs(0.8), &target).unwrap(), abs(0.56));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(0.6));
}
#[test]
fn transformation_ok() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
control_transformation: Some(TestTransformation::new(|input| Ok(input.inverse()))),
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(1.0));
assert_abs_diff_eq!(mode.control(abs(0.5), &target).unwrap(), abs(0.5));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(0.0));
}
#[test]
fn transformation_err() {
// Given
let mut mode: Mode<TestTransformation> = Mode {
control_transformation: Some(TestTransformation::new(|_| Err("oh no!"))),
..Default::default()
};
let target = TestTarget {
current_value: Some(UnitValue::new(0.777)),
control_type: ControlType::AbsoluteContinuous,
};
// When
// Then
assert_abs_diff_eq!(mode.control(abs(0.0), &target).unwrap(), abs(0.0));
assert_abs_diff_eq!(mode.control(abs(0.5), &target).unwrap(), abs(0.5));
assert_abs_diff_eq!(mode.control(abs(1.0), &target).unwrap(), abs(1.0));
}
#[test]
fn feedback() {
// Given
let mode: Mode<TestTransformation> = Mode {
..Default::default()
};
// When
// Then
assert_abs_diff_eq!(mode.feedback(uv(0.0)).unwrap(), uv(0.0));
assert_abs_diff_eq!(mode.feedback(uv(0.5)).unwrap(), uv(0.5));
assert_abs_diff_eq!(mode.feedback(uv(1.0)).unwrap(), uv(1.0));
}
#[test]
fn feedback_reverse() {
// Given
let mode: Mode<TestTransformation> = Mode {
reverse: true,
..Default::default()
};
// When
// Then
assert_abs_diff_eq!(mode.feedback(uv(0.0)).unwrap(), uv(1.0));
assert_abs_diff_eq!(mode.feedback(uv(0.5)).unwrap(), uv(0.5));