Skip to content

Commit

Permalink
Improve autoranging
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Dec 6, 2023
1 parent 8311e75 commit fe99e73
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
17 changes: 16 additions & 1 deletion gui/examples/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ fn main() -> Result<(), Infallible> {

screen.update_heart_rate(NonZeroU8::new(67));

const INJECT_PULSE: usize = 160;

let mut inect_counter = 0;
let mut progress = 0;
'running: loop {
display.clear(BinaryColor::Off).unwrap();
Expand All @@ -41,7 +44,19 @@ fn main() -> Result<(), Infallible> {
let sample1 = wt.sin();
let sample2 = wt2.sin();

screen.push(sample1 * sample2);
let mut sample = sample1 * sample2;

inect_counter += 1;
if inect_counter == INJECT_PULSE - 1 {
sample += 0.5;
}
if inect_counter == INJECT_PULSE {
inect_counter = 0;

sample -= 2.0;
}

screen.push(sample);

screen.draw(&mut display).unwrap();

Expand Down
35 changes: 27 additions & 8 deletions gui/src/screens/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@ enum LimitKind {

struct Limit {
current: f32,
target: f32,
delta: f32,
kind: LimitKind,
age: usize,
}

impl Limit {
fn new(kind: LimitKind) -> Limit {
let current = match kind {
LimitKind::Min => f32::MAX,
LimitKind::Max => f32::MIN,
};
Self {
current: match kind {
LimitKind::Min => f32::MAX,
LimitKind::Max => f32::MIN,
},
current,
target: current,
delta: 0.0,
kind,
age: 0,
}
Expand All @@ -53,18 +58,32 @@ impl Limit {

if reset {
self.current = value;
self.target = value;
self.delta = 0.0;
self.age = 0;
} else {
self.age += 1;
} else if self.current != value {
// Short circuit if the value hasn't changed

if value != self.target {
// target changed, reset age and compute new delta
self.age = self.age.min(config.shrink_delay);
self.target = value;
self.delta =
(value - self.current) / (config.shrink_end - config.shrink_delay) as f32;
} else {
// target unchanged, increment age
self.age += 1;
}

if self.age > config.shrink_delay {
let remaining_shrink_frames = config.shrink_end - self.age;

if remaining_shrink_frames == 0 {
self.age = 0;
self.current = value;
self.delta = 0.0;
} else {
let delta = (value - self.current) / remaining_shrink_frames as f32;
self.current += delta;
self.current += self.delta;
}
}
}
Expand Down

0 comments on commit fe99e73

Please sign in to comment.