Skip to content

Commit

Permalink
Merge pull request #155 from card-io-ecg/gui
Browse files Browse the repository at this point in the history
Tweak measurement auto ranging
  • Loading branch information
bugadani committed Dec 6, 2023
2 parents f7d78f1 + d03c938 commit 685dbbd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
21 changes: 18 additions & 3 deletions gui/examples/measure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{convert::Infallible, f32::consts::PI};
use std::{convert::Infallible, f32::consts::PI, num::NonZeroU8};

use embedded_graphics::{
pixelcolor::BinaryColor,
Expand All @@ -22,8 +22,11 @@ fn main() -> Result<(), Infallible> {

let mut screen = EcgScreen::new();

screen.update_heart_rate(67);
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 685dbbd

Please sign in to comment.