v0.5.0: tweak NIBP
This commit is contained in:
parent
e831364e43
commit
ef269099bd
12 changed files with 2900 additions and 109 deletions
|
|
@ -1,4 +1,5 @@
|
|||
mod model;
|
||||
mod nibp;
|
||||
mod render;
|
||||
mod simulation;
|
||||
mod ui;
|
||||
|
|
|
|||
|
|
@ -94,6 +94,10 @@ pub enum Spo2Quality {
|
|||
}
|
||||
|
||||
impl Spo2Quality {
|
||||
pub fn min(self, other: Self) -> Self {
|
||||
if (self as u8) <= (other as u8) { self } else { other }
|
||||
}
|
||||
|
||||
pub fn stars(self) -> &'static str {
|
||||
match self {
|
||||
Self::None => "",
|
||||
|
|
@ -207,8 +211,12 @@ pub struct Parameters {
|
|||
pub art_diastolic: i32, // mmHg
|
||||
pub arterial_shape: ArterialShape,
|
||||
pub pulse_pressure_variation: i32, // PPV %, 0 disables respiratory variation
|
||||
pub nibp_systolic: i32, // mmHg, historical/static simulated cuff value
|
||||
pub nibp_diastolic: i32, // mmHg
|
||||
pub nibp_systolic: i32, // mmHg, initial measured NIBP result
|
||||
pub nibp_diastolic: i32, // mmHg, initial measured NIBP result
|
||||
pub nibp_interval_minutes: i32, // 1 / 3 / 5 / 10 / 20 / 30 / 60
|
||||
pub nibp_no_reading: bool, // force unsuccessful cuff measurement
|
||||
pub nibp_cuff_on_spo2: bool, // simulated probe on cuff extremity
|
||||
pub nibp_cuff_on_art: bool, // simulated arterial catheter on cuff extremity
|
||||
pub etco2: i32, // mmHg
|
||||
pub respiratory_rate: i32, // breaths/min
|
||||
pub capno_shape: CapnoShape,
|
||||
|
|
@ -237,13 +245,14 @@ impl Parameters {
|
|||
self.spo2_status_during(pulse_rate, false)
|
||||
}
|
||||
|
||||
/// Poor-quality signal interruptions are generated by the simulator and
|
||||
/// override the normal reading, but not a physically disconnected sensor.
|
||||
pub fn spo2_status_during(&self, pulse_rate: Option<i32>, signal_lost: bool) -> Spo2Status {
|
||||
/// Effective quality can be temporarily reduced by an NIBP cuff on the
|
||||
/// same extremity. A detached probe still has highest priority.
|
||||
pub fn spo2_status_at_quality(&self, pulse_rate: Option<i32>, signal_lost: bool,
|
||||
effective_quality: Spo2Quality) -> Spo2Status {
|
||||
if !self.spo2_sensor_attached {
|
||||
Spo2Status::NoSensor
|
||||
} else if (signal_lost && self.spo2_quality == Spo2Quality::Poor)
|
||||
|| self.spo2_quality == Spo2Quality::None || pulse_rate.is_none()
|
||||
|| effective_quality == Spo2Quality::None || pulse_rate.is_none()
|
||||
|| !self.ecg_rhythm.can_generate_pulse()
|
||||
|| self.pulse_conduction == PulseConduction::None
|
||||
|| self.heart_rate <= 0
|
||||
|
|
@ -253,6 +262,12 @@ impl Parameters {
|
|||
Spo2Status::Available
|
||||
}
|
||||
}
|
||||
|
||||
/// Quality without cuff interference; retained for regression tests.
|
||||
#[cfg(test)]
|
||||
pub fn spo2_status_during(&self, pulse_rate: Option<i32>, signal_lost: bool) -> Spo2Status {
|
||||
self.spo2_status_at_quality(pulse_rate, signal_lost, self.spo2_quality)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Parameters {
|
||||
|
|
@ -272,6 +287,10 @@ impl Default for Parameters {
|
|||
pulse_pressure_variation: 0,
|
||||
nibp_systolic: 118,
|
||||
nibp_diastolic: 74,
|
||||
nibp_interval_minutes: 5,
|
||||
nibp_no_reading: false,
|
||||
nibp_cuff_on_spo2: false,
|
||||
nibp_cuff_on_art: false,
|
||||
etco2: 38,
|
||||
respiratory_rate: 14,
|
||||
capno_shape: CapnoShape::Normal,
|
||||
|
|
|
|||
308
patient-monitor-simulation/src/nibp.rs
Normal file
308
patient-monitor-simulation/src/nibp.rs
Normal file
|
|
@ -0,0 +1,308 @@
|
|||
//! Synthetic oscillometric NIBP cuff cycle; no physiological or diagnostic validity.
|
||||
//! All time is measured in simulation seconds so the behavior is independent of FPS.
|
||||
|
||||
use crate::model::{Parameters, Spo2Quality};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct NibpReading {
|
||||
pub systolic: i32,
|
||||
pub diastolic: i32,
|
||||
}
|
||||
|
||||
impl NibpReading {
|
||||
pub fn mean(self) -> i32 {
|
||||
crate::model::mean_arterial_pressure(self.systolic, self.diastolic)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum CuffZone {
|
||||
Free,
|
||||
Partial,
|
||||
Occluded,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Cycle {
|
||||
start: f64,
|
||||
duration: f64,
|
||||
quality_at_start: Spo2Quality,
|
||||
first: bool,
|
||||
first_reading: NibpReading,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NibpMachine {
|
||||
cycle: Option<Cycle>,
|
||||
ever_started: bool,
|
||||
ever_successful: bool,
|
||||
last_completed: Option<f64>,
|
||||
last_reading: Option<NibpReading>,
|
||||
last_success_at: Option<f64>,
|
||||
rng: u64,
|
||||
}
|
||||
|
||||
impl NibpMachine {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
cycle: None,
|
||||
ever_started: false,
|
||||
ever_successful: false,
|
||||
last_completed: None,
|
||||
last_reading: None,
|
||||
last_success_at: None,
|
||||
rng: 0xC4E3_1A86_957B_1D29,
|
||||
}
|
||||
}
|
||||
|
||||
fn random_unit(&mut self) -> f64 {
|
||||
self.rng = self.rng.wrapping_mul(6_364_136_223_846_793_005)
|
||||
.wrapping_add(1_442_695_040_888_963_407);
|
||||
(self.rng >> 11) as f64 / (1_u64 << 53) as f64
|
||||
}
|
||||
|
||||
fn random_between(&mut self, low: f64, high: f64) -> f64 {
|
||||
low + self.random_unit() * (high - low)
|
||||
}
|
||||
|
||||
pub fn measuring(&self) -> bool {
|
||||
self.cycle.is_some()
|
||||
}
|
||||
|
||||
pub fn reading(&self) -> Option<NibpReading> {
|
||||
self.last_reading
|
||||
}
|
||||
|
||||
/// Time since the last completed measurement as a fraction of the interval.
|
||||
/// While measuring, the waiting progress resets to zero.
|
||||
pub fn interval_progress(&self, now: f64, minutes: i32) -> f64 {
|
||||
if self.cycle.is_some() { return 0.0; }
|
||||
let Some(last) = self.last_completed else { return 0.0; };
|
||||
((now - last) / (f64::from(minutes.max(1)) * 60.0)).clamp(0.0, 1.0)
|
||||
}
|
||||
|
||||
/// Five appearances/disappearances over five seconds (no color inversion).
|
||||
pub fn show_new_value(&self, now: f64) -> bool {
|
||||
let Some(last) = self.last_success_at else { return true; };
|
||||
let elapsed = now - last;
|
||||
!(0.0..5.0).contains(&elapsed) || (elapsed * 2.0).floor() as i32 % 2 == 0
|
||||
}
|
||||
|
||||
pub fn cuff_pressure(&self, now: f64) -> f64 {
|
||||
let Some(cycle) = self.cycle else { return 0.0; };
|
||||
let t = ((now - cycle.start) / cycle.duration).clamp(0.0, 1.0);
|
||||
let poor = matches!(cycle.quality_at_start, Spo2Quality::Poor | Spo2Quality::None);
|
||||
(if !poor {
|
||||
if t < 0.13 { 200.0 * t / 0.13 }
|
||||
else { 200.0 * (1.0 - t) / 0.87 }
|
||||
} else if t < 0.13 {
|
||||
200.0 * t / 0.13
|
||||
} else if t < 0.54 {
|
||||
// First deflation stops at one third of the maximum pressure.
|
||||
200.0 - (200.0 - 200.0 / 3.0) * (t - 0.13) / 0.41
|
||||
} else if t < 0.71 {
|
||||
// Reinflation to two thirds of maximum pressure.
|
||||
200.0 / 3.0 + (200.0 / 3.0) * (t - 0.54) / 0.17
|
||||
} else {
|
||||
(400.0 / 3.0) * (1.0 - t) / 0.29
|
||||
}).clamp(0.0, 200.0)
|
||||
}
|
||||
|
||||
pub fn cuff_zone(&self, now: f64, params: &Parameters) -> CuffZone {
|
||||
if !self.measuring() { return CuffZone::Free; }
|
||||
let pressure = self.cuff_pressure(now);
|
||||
if pressure > f64::from(params.art_systolic) { CuffZone::Occluded }
|
||||
else if pressure > f64::from(params.art_diastolic) { CuffZone::Partial }
|
||||
else { CuffZone::Free }
|
||||
}
|
||||
|
||||
pub fn effective_spo2_quality(&self, now: f64, p: &Parameters) -> Spo2Quality {
|
||||
if !p.nibp_cuff_on_spo2 { return p.spo2_quality; }
|
||||
match self.cuff_zone(now, p) {
|
||||
CuffZone::Occluded => Spo2Quality::None,
|
||||
CuffZone::Partial => p.spo2_quality.min(Spo2Quality::Poor),
|
||||
CuffZone::Free => p.spo2_quality,
|
||||
}
|
||||
}
|
||||
|
||||
fn start(&mut self, now: f64, p: &Parameters, quality: Spo2Quality) {
|
||||
let quality = if p.nibp_no_reading { Spo2Quality::None } else { quality };
|
||||
let duration = match quality {
|
||||
Spo2Quality::Good => 20.0,
|
||||
Spo2Quality::Adequate => 40.0,
|
||||
Spo2Quality::Poor => self.random_between(60.0, 120.0),
|
||||
Spo2Quality::None => 120.0,
|
||||
};
|
||||
self.cycle = Some(Cycle {
|
||||
start: now,
|
||||
duration,
|
||||
quality_at_start: quality,
|
||||
first: !self.ever_successful,
|
||||
first_reading: NibpReading {
|
||||
systolic: p.nibp_systolic,
|
||||
diastolic: p.nibp_diastolic,
|
||||
},
|
||||
});
|
||||
self.ever_started = true;
|
||||
}
|
||||
|
||||
/// The initial cycle starts at simulation time zero. Afterwards, the
|
||||
/// interval is counted from the last completed measurement, not cycle start.
|
||||
pub fn update(&mut self, now: f64, p: &Parameters, quality_at_start: Spo2Quality) {
|
||||
if now < 0.0 { return; }
|
||||
if let Some(cycle) = self.cycle {
|
||||
if now - cycle.start >= cycle.duration {
|
||||
self.complete(now, p, cycle);
|
||||
}
|
||||
} else if !self.ever_started || self.last_completed.is_some_and(|last| {
|
||||
now - last >= f64::from(p.nibp_interval_minutes.max(1)) * 60.0
|
||||
}) {
|
||||
self.start(now, p, quality_at_start);
|
||||
}
|
||||
}
|
||||
|
||||
/// Instructor-triggered measurement; ignored if a cuff cycle is in progress.
|
||||
pub fn measure_now(&mut self, now: f64, p: &Parameters, quality: Spo2Quality) {
|
||||
if !self.measuring() { self.start(now.max(0.0), p, quality); }
|
||||
}
|
||||
|
||||
fn complete(&mut self, now: f64, p: &Parameters, cycle: Cycle) {
|
||||
let successful = match cycle.quality_at_start {
|
||||
Spo2Quality::None => false,
|
||||
Spo2Quality::Poor => self.random_unit() < 0.65,
|
||||
Spo2Quality::Adequate | Spo2Quality::Good => true,
|
||||
};
|
||||
self.last_reading = if successful {
|
||||
let reading = if cycle.first {
|
||||
cycle.first_reading
|
||||
} else {
|
||||
let (low, high) = match cycle.quality_at_start {
|
||||
Spo2Quality::Good => (-0.05, 0.05),
|
||||
Spo2Quality::Adequate => (-0.10, 0.05),
|
||||
Spo2Quality::Poor => (-0.15, -0.05),
|
||||
Spo2Quality::None => (0.0, 0.0),
|
||||
};
|
||||
// Independent random deviation for SYS and DIA; preserve SYS >= DIA.
|
||||
let sys = (f64::from(p.art_systolic) * (1.0 + self.random_between(low, high)))
|
||||
.round().clamp(0.0, 260.0) as i32;
|
||||
let dia = (f64::from(p.art_diastolic) * (1.0 + self.random_between(low, high)))
|
||||
.round().clamp(0.0, f64::from(sys)) as i32;
|
||||
NibpReading { systolic: sys, diastolic: dia }
|
||||
};
|
||||
self.last_success_at = Some(now);
|
||||
self.ever_successful = true;
|
||||
Some(reading)
|
||||
} else {
|
||||
self.last_success_at = None;
|
||||
None
|
||||
};
|
||||
self.cycle = None;
|
||||
self.last_completed = Some(now);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn intervals_and_first_values() {
|
||||
let mut m = NibpMachine::new();
|
||||
let p = Parameters::default();
|
||||
m.update(0.0, &p, Spo2Quality::Good);
|
||||
assert!(m.measuring());
|
||||
assert_eq!(m.reading(), None);
|
||||
assert!((m.cuff_pressure(2.6) - 200.0).abs() < 0.1);
|
||||
m.update(20.0, &p, Spo2Quality::Good);
|
||||
assert_eq!(m.reading(), Some(NibpReading { systolic: 118, diastolic: 74 }));
|
||||
assert!(!m.measuring());
|
||||
assert!(m.show_new_value(20.1));
|
||||
assert!(!m.show_new_value(20.6));
|
||||
assert!(m.show_new_value(25.1));
|
||||
assert!((m.interval_progress(170.0, 5) - 0.5).abs() < 1e-6);
|
||||
m.update(319.9, &p, Spo2Quality::Good);
|
||||
assert!(!m.measuring());
|
||||
m.update(320.0, &p, Spo2Quality::Good);
|
||||
assert!(m.measuring());
|
||||
m.update(340.0, &p, Spo2Quality::Good);
|
||||
assert!(m.reading().is_some());
|
||||
let result = m.reading().unwrap();
|
||||
assert!((result.systolic - p.art_systolic).abs() <= 6);
|
||||
assert!((result.diastolic - p.art_diastolic).abs() <= 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_measurement_and_retry_cycle() {
|
||||
let p = Parameters { nibp_no_reading: true, ..Parameters::default() };
|
||||
let mut m = NibpMachine::new();
|
||||
m.update(0.0, &p, Spo2Quality::Good);
|
||||
assert!(m.measuring());
|
||||
assert!((m.cuff_pressure(15.6) - 200.0).abs() < 0.1);
|
||||
assert!((m.cuff_pressure(64.8) - 200.0/3.0).abs() < 0.1);
|
||||
assert!((m.cuff_pressure(85.2) - 400.0/3.0).abs() < 0.1);
|
||||
m.update(120.0, &p, Spo2Quality::Good);
|
||||
assert!(m.reading().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cuff_interference_never_improves_quality() {
|
||||
let p = Parameters { nibp_cuff_on_spo2: true, ..Parameters::default() };
|
||||
let mut m = NibpMachine::new();
|
||||
m.update(0.0, &p, Spo2Quality::Good);
|
||||
assert_eq!(m.effective_spo2_quality(2.6, &p), Spo2Quality::None);
|
||||
assert_eq!(m.effective_spo2_quality(10.0, &p), Spo2Quality::Poor);
|
||||
assert_eq!(m.effective_spo2_quality(19.0, &p), Spo2Quality::Good);
|
||||
let p = Parameters { spo2_quality: Spo2Quality::Poor, ..p };
|
||||
assert_eq!(m.effective_spo2_quality(10.0, &p), Spo2Quality::Poor);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod more_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn quality_determines_cycle_duration() {
|
||||
for (quality, seconds) in [
|
||||
(Spo2Quality::Good, 20.0),
|
||||
(Spo2Quality::Adequate, 40.0),
|
||||
(Spo2Quality::None, 120.0),
|
||||
] {
|
||||
let mut machine = NibpMachine::new();
|
||||
let p = Parameters::default();
|
||||
machine.update(0.0, &p, quality);
|
||||
machine.update(seconds - 0.01, &p, quality);
|
||||
assert!(machine.measuring());
|
||||
machine.update(seconds, &p, quality);
|
||||
assert!(!machine.measuring());
|
||||
assert_eq!(machine.reading().is_some(), quality != Spo2Quality::None);
|
||||
}
|
||||
let mut machine = NibpMachine::new();
|
||||
let p = Parameters::default();
|
||||
machine.update(0.0, &p, Spo2Quality::Poor);
|
||||
assert!((60.0..=120.0).contains(&machine.cycle.unwrap().duration));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn first_successful_reading_uses_admin_even_after_failed_cycle() {
|
||||
let mut p = Parameters { nibp_no_reading: true, ..Parameters::default() };
|
||||
let mut machine = NibpMachine::new();
|
||||
machine.update(0.0, &p, Spo2Quality::None);
|
||||
machine.update(120.0, &p, Spo2Quality::None);
|
||||
assert!(machine.reading().is_none());
|
||||
p.nibp_no_reading = false;
|
||||
machine.measure_now(121.0, &p, Spo2Quality::Good);
|
||||
machine.update(141.0, &p, Spo2Quality::Good);
|
||||
assert_eq!(machine.reading(), Some(NibpReading { systolic: 118, diastolic: 74 }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn partial_pressure_uses_diastic_and_systolic_boundaries() {
|
||||
let p = Parameters { nibp_cuff_on_art: true, ..Parameters::default() };
|
||||
let mut machine = NibpMachine::new();
|
||||
machine.update(0.0, &p, Spo2Quality::Good);
|
||||
assert_eq!(machine.cuff_zone(2.6, &p), CuffZone::Occluded);
|
||||
assert_eq!(machine.cuff_zone(10.0, &p), CuffZone::Partial);
|
||||
assert_eq!(machine.cuff_zone(19.0, &p), CuffZone::Free);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
//! Size-independent Cairo drawing of the synthetic bedside monitor.
|
||||
use gtk::cairo::{Context, FontSlant, FontWeight, LineCap, LineJoin};
|
||||
|
||||
use crate::model::{mean_arterial_pressure, AlarmLimits, AlarmState, Parameters, PulseConduction, Spo2Quality, Spo2Status};
|
||||
use crate::model::{AlarmLimits, AlarmState, Parameters, PulseConduction, Spo2Quality, Spo2Status};
|
||||
use crate::simulation::{Sample, Simulator};
|
||||
|
||||
type Color = (f64, f64, f64);
|
||||
|
|
@ -33,10 +33,14 @@ pub fn draw(cr: &Context, width: i32, height: i32, p: &Parameters, sim: &Simulat
|
|||
|
||||
let pad = (w.min(h) * 0.018).clamp(6.0, 20.0);
|
||||
let header = (h * 0.083).clamp(32.0, 68.0);
|
||||
let footer = if p.show_nibp { (h * 0.13).clamp(52.0, 102.0) } else { 0.0 };
|
||||
let channels = visible_channels(p);
|
||||
let row_height = ((h - header - footer - pad).max(20.0))
|
||||
/ (channels.len().max(1) as f64);
|
||||
let body_height = (h - header - pad).max(20.0);
|
||||
// NIBP occupies a dedicated box at the bottom of the numerical right column.
|
||||
// Trace rows remain full height on the left.
|
||||
let nibp_box_height = if p.show_nibp { (h * 0.24).clamp(135.0, 210.0).min(body_height * 0.55) }
|
||||
else { 0.0 };
|
||||
let trace_row_height = body_height / channels.len().max(1) as f64;
|
||||
let vital_row_height = (body_height - nibp_box_height) / channels.len().max(1) as f64;
|
||||
let sidebar = (w * 0.235).clamp(125.0, 370.0).min(w * 0.44);
|
||||
let split_x = w - sidebar - pad;
|
||||
let trace_left = pad + 6.0;
|
||||
|
|
@ -60,60 +64,112 @@ pub fn draw(cr: &Context, width: i32, height: i32, p: &Parameters, sim: &Simulat
|
|||
11.0 * text_scale, MUTED, false);
|
||||
}
|
||||
stroke_line(cr, pad, header, w - pad, header, MUTED, 0.8);
|
||||
stroke_line(cr, split_x, header, split_x, h - footer, MUTED, 0.9);
|
||||
stroke_line(cr, split_x, header, split_x, h - pad, MUTED, 0.9);
|
||||
|
||||
for (index, (channel, title, color)) in channels.iter().copied().enumerate() {
|
||||
let top = header + index as f64 * row_height;
|
||||
let bottom = top + row_height;
|
||||
stroke_line(cr, pad, bottom, w - pad, bottom, MUTED, 0.65);
|
||||
text(cr, title, trace_left + 4.0, top + row_height * 0.17,
|
||||
(row_height * 0.11).clamp(10.0, 17.0), color, false);
|
||||
let top = header + index as f64 * trace_row_height;
|
||||
let bottom = top + trace_row_height;
|
||||
stroke_line(cr, pad, bottom, split_x, bottom, MUTED, 0.65);
|
||||
text(cr, title, trace_left + 4.0, top + trace_row_height * 0.17,
|
||||
(trace_row_height * 0.11).clamp(10.0, 17.0), color, false);
|
||||
draw_trace(cr, channel, color, trace_left, trace_right,
|
||||
top + row_height * 0.25, bottom - row_height * 0.06, p, sim);
|
||||
top + trace_row_height * 0.25, bottom - trace_row_height * 0.06, p, sim);
|
||||
let vital_top = header + index as f64 * vital_row_height;
|
||||
stroke_line(cr, split_x, vital_top + vital_row_height,
|
||||
w - pad, vital_top + vital_row_height, MUTED, 0.65);
|
||||
draw_vitals(cr, channel, color, split_x + pad,
|
||||
top, row_height, sidebar - 2.0 * pad, p, sim);
|
||||
vital_top, vital_row_height, sidebar - 2.0 * pad, p, sim);
|
||||
}
|
||||
|
||||
if p.show_nibp {
|
||||
let footer_top = h - footer;
|
||||
let flash = alarm_flash_on(sim);
|
||||
let mean = mean_arterial_pressure(p.nibp_systolic, p.nibp_diastolic);
|
||||
text(cr, "NIBP", pad + 6.0, footer_top + footer * 0.32,
|
||||
(footer * 0.21).clamp(12.0, 20.0), LIGHT_GREY, false);
|
||||
draw_pressure_value(cr, p.nibp_systolic, p.nibp_diastolic,
|
||||
pad + 6.0, footer_top + footer * 0.77,
|
||||
(footer * 0.42).clamp(20.0, 44.0),
|
||||
(w * 0.28).min(255.0).max(70.0), LIGHT_GREY,
|
||||
alarmed(p.alarms.nibp_sys, Some(p.nibp_systolic)),
|
||||
alarmed(p.alarms.nibp_dia, Some(p.nibp_diastolic)), flash);
|
||||
if w >= 510.0 {
|
||||
draw_alarm_inline(cr, pad + (w * 0.28).min(260.0),
|
||||
footer_top + footer * 0.73, (footer * 0.24).clamp(12.0, 23.0),
|
||||
(w * 0.25).max(90.0), LIGHT_GREY,
|
||||
"MAP ", &mean.to_string(), " mmHg",
|
||||
alarmed(p.alarms.nibp_map, Some(mean)), flash);
|
||||
}
|
||||
draw_alarm_annotations(cr, &[
|
||||
("SYS", p.alarms.nibp_sys),
|
||||
("MAP", p.alarms.nibp_map),
|
||||
("DIA", p.alarms.nibp_dia),
|
||||
], w - pad - 8.0, footer_top, footer, (w * 0.24).max(115.0));
|
||||
draw_nibp_box(cr, split_x + pad, h - pad - nibp_box_height,
|
||||
sidebar - 2.0 * pad, nibp_box_height, p, sim);
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_nibp_box(cr: &Context, x: f64, top: f64, width: f64, height: f64,
|
||||
p: &Parameters, sim: &Simulator) {
|
||||
let right = x + width;
|
||||
let pressure = sim.nibp().cuff_pressure(sim.time());
|
||||
let in_progress = sim.nibp().measuring();
|
||||
let reading = sim.nibp().reading();
|
||||
let value_width = (width - 35.0).max(30.0);
|
||||
let small = (height * 0.095).clamp(10.0, 14.0);
|
||||
let number_size = (height * 0.25).clamp(18.0, 41.0);
|
||||
// Subtle red outline distinguishes the independent cuff reading box.
|
||||
let _ = cr.save();
|
||||
cr.set_source_rgb(0.28, 0.09, 0.10);
|
||||
cr.set_line_width(1.0);
|
||||
cr.rectangle(x - 7.0, top, width + 13.0, height - 1.0);
|
||||
let _ = cr.stroke();
|
||||
let _ = cr.restore();
|
||||
text(cr, "NIBP", x, top + height * 0.13, small * 1.15, RED, true);
|
||||
if in_progress {
|
||||
text(cr, "MEASURING", x, top + height * 0.27, small, RED, false);
|
||||
} else {
|
||||
text(cr, "mmHg", x, top + height * 0.27, small, MUTED, false);
|
||||
}
|
||||
|
||||
if sim.nibp().show_new_value(sim.time()) {
|
||||
let (sys, dia) = reading.map_or(("---".to_owned(), "---".to_owned()), |r|
|
||||
(r.systolic.to_string(), r.diastolic.to_string()));
|
||||
draw_pressure_components(cr, &sys, &dia, x, top + height * 0.60,
|
||||
number_size, value_width, RED,
|
||||
alarmed(p.alarms.nibp_sys, reading.map(|r| r.systolic)),
|
||||
alarmed(p.alarms.nibp_dia, reading.map(|r| r.diastolic)), alarm_flash_on(sim));
|
||||
let map = reading.map_or("---".to_owned(), |r| r.mean().to_string());
|
||||
draw_alarm_inline(cr, x, top + height * 0.72, small * 1.15, value_width,
|
||||
RED, "MAP ", &map, " mmHg",
|
||||
alarmed(p.alarms.nibp_map, reading.map(|r| r.mean())), alarm_flash_on(sim));
|
||||
}
|
||||
|
||||
// Horizontal: elapsed fraction of the selected measurement interval.
|
||||
let progress_y = top + height * 0.85;
|
||||
let progress_width = (width - 9.0).max(8.0);
|
||||
stroke_line(cr, x, progress_y, x + progress_width, progress_y, MUTED, 4.0);
|
||||
stroke_line(cr, x, progress_y,
|
||||
x + progress_width * sim.nibp().interval_progress(sim.time(), p.nibp_interval_minutes),
|
||||
progress_y, RED, 4.0);
|
||||
text(cr, &format!("Every {} min", p.nibp_interval_minutes),
|
||||
x, top + height * 0.97, small * 0.85, MUTED, false);
|
||||
|
||||
// Vertical cuff-pressure gauge (0..200 mmHg), always visible when measuring.
|
||||
let gauge_x = right - 15.0;
|
||||
let gauge_top = top + height * 0.31;
|
||||
let gauge_h = height * 0.40;
|
||||
stroke_line(cr, gauge_x, gauge_top, gauge_x, gauge_top + gauge_h, MUTED, 7.0);
|
||||
if in_progress {
|
||||
stroke_line(cr, gauge_x, gauge_top + gauge_h,
|
||||
gauge_x, gauge_top + gauge_h * (1.0 - pressure / 200.0), RED, 6.0);
|
||||
}
|
||||
text(cr, &format!("{:.0}", pressure), gauge_x - 12.0,
|
||||
gauge_top - 3.0, small * 0.75, RED, false);
|
||||
draw_alarm_annotations(cr, &[
|
||||
("SYS", p.alarms.nibp_sys),
|
||||
("MAP", p.alarms.nibp_map),
|
||||
("DIA", p.alarms.nibp_dia),
|
||||
], right, top, height * 0.65, width * 0.72);
|
||||
}
|
||||
|
||||
fn pleth_trace_enabled(p: &Parameters) -> bool {
|
||||
p.spo2_sensor_attached && p.spo2_quality != Spo2Quality::None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn spo2_vitals(p: &Parameters, pulse_rate: Option<i32>, signal_lost: bool)
|
||||
-> (String, String, String) {
|
||||
match p.spo2_status_during(pulse_rate, signal_lost) {
|
||||
spo2_vitals_at_quality(p, pulse_rate, signal_lost, p.spo2_quality)
|
||||
}
|
||||
|
||||
fn spo2_vitals_at_quality(p: &Parameters, pulse_rate: Option<i32>, signal_lost: bool,
|
||||
quality: Spo2Quality) -> (String, String, String) {
|
||||
match p.spo2_status_at_quality(pulse_rate, signal_lost, quality) {
|
||||
Spo2Status::NoSensor => (
|
||||
"SpO₂".to_owned(), "NO SENSOR".to_owned(), "PR -- bpm".to_owned()),
|
||||
Spo2Status::NoReading => (
|
||||
"SpO₂".to_owned(), "NO READING".to_owned(), "PR -- bpm".to_owned()),
|
||||
Spo2Status::Available => (
|
||||
format!("SpO₂ {}", p.spo2_quality.stars()),
|
||||
format!("SpO₂ {}", quality.stars()),
|
||||
p.spo2.to_string(),
|
||||
format!("% PR {} bpm", pulse_rate.unwrap_or_default()),
|
||||
),
|
||||
|
|
@ -144,9 +200,8 @@ fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
|
|||
let ecg_hr = if p.ecg_rhythm.has_qrs() && p.heart_rate > 0 {
|
||||
Some(p.heart_rate)
|
||||
} else { None };
|
||||
let spo2_status = p.spo2_status_during(pulse_rate, sim.poor_signal_lost());
|
||||
let spo2_status = p.spo2_status_at_quality(pulse_rate, sim.poor_signal_lost(), sim.spo2_quality(p));
|
||||
let oxygen = if spo2_status == Spo2Status::Available { Some(p.spo2) } else { None };
|
||||
let art_map = mean_arterial_pressure(p.art_systolic, p.art_diastolic);
|
||||
|
||||
let limits: Vec<(&str, AlarmLimits)> = match ch {
|
||||
Channel::Ecg => vec![("HR", p.alarms.hr)],
|
||||
|
|
@ -174,7 +229,8 @@ fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
|
|||
}
|
||||
}
|
||||
Channel::Pleth => {
|
||||
let (label, value, info) = spo2_vitals(p, pulse_rate, sim.poor_signal_lost());
|
||||
let (label, value, info) = spo2_vitals_at_quality(p, pulse_rate,
|
||||
sim.poor_signal_lost(), sim.spo2_quality(p));
|
||||
text(cr, &label, x, top + row_h * 0.24, label_size, color, false);
|
||||
let technical_alarm = spo2_status != Spo2Status::Available;
|
||||
let value_size = if technical_alarm {
|
||||
|
|
@ -192,36 +248,27 @@ fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
|
|||
}
|
||||
Channel::Art => {
|
||||
text(cr, "ART", x, top + row_h * 0.24, label_size, color, false);
|
||||
if pulse_rate.is_some() {
|
||||
draw_pressure_value(cr, p.art_systolic, p.art_diastolic,
|
||||
x, value_y, number_size * 0.78, width, color,
|
||||
alarmed(p.alarms.art_sys, Some(p.art_systolic)),
|
||||
alarmed(p.alarms.art_dia, Some(p.art_diastolic)), flash);
|
||||
if p.pulse_pressure_variation > 0 || p.alarms.ppv.enabled() {
|
||||
let map_text = art_map.to_string();
|
||||
let ppv_text = p.pulse_pressure_variation.to_string();
|
||||
draw_alarm_parts(cr, x, info_y, sub_size, width, color, &[
|
||||
("MAP ", false),
|
||||
(&map_text, alarmed(p.alarms.art_map, Some(art_map))),
|
||||
(" · PPV ", false),
|
||||
(&ppv_text, alarmed(p.alarms.ppv, Some(p.pulse_pressure_variation))),
|
||||
("%", false),
|
||||
], flash);
|
||||
} else {
|
||||
draw_alarm_inline(cr, x, info_y, sub_size, width, color,
|
||||
"MAP ", &art_map.to_string(), " mmHg",
|
||||
alarmed(p.alarms.art_map, Some(art_map)), flash);
|
||||
}
|
||||
let (sys, dia, map) = sim.displayed_art_pressures(p, pulse_rate.is_some());
|
||||
let sys_text = sys.map_or("--".to_owned(), |v| v.to_string());
|
||||
let dia_text = dia.map_or("--".to_owned(), |v| v.to_string());
|
||||
draw_pressure_components(cr, &sys_text, &dia_text,
|
||||
x, value_y, number_size * 0.78, width, color,
|
||||
alarmed(p.alarms.art_sys, sys),
|
||||
alarmed(p.alarms.art_dia, dia), flash);
|
||||
let map_text = map.map_or("--".to_owned(), |v| v.to_string());
|
||||
if p.pulse_pressure_variation > 0 || p.alarms.ppv.enabled() {
|
||||
let ppv_text = p.pulse_pressure_variation.to_string();
|
||||
draw_alarm_parts(cr, x, info_y, sub_size, width, color, &[
|
||||
("MAP ", false),
|
||||
(&map_text, alarmed(p.alarms.art_map, map)),
|
||||
(" · PPV ", false),
|
||||
(&ppv_text, alarmed(p.alarms.ppv, Some(p.pulse_pressure_variation))),
|
||||
("%", false),
|
||||
], flash);
|
||||
} else {
|
||||
// Without mechanical activity the transducer still reads the
|
||||
// slowly drifting 0..5 mmHg pressure. DIA/MAP are indeterminate.
|
||||
let pulseless = !has_capture;
|
||||
let sys = if pulseless { sim.measured_art_pressure() } else { None };
|
||||
let displayed_sys = sys.map_or_else(|| "--".to_owned(), |v| v.to_string());
|
||||
draw_pressure_components(cr, &displayed_sys, "--", x, value_y,
|
||||
number_size * 0.78, width, color,
|
||||
alarmed(p.alarms.art_sys, sys), false, flash);
|
||||
text(cr, "MAP -- mmHg", x, info_y, sub_size, color, false);
|
||||
draw_alarm_inline(cr, x, info_y, sub_size, width, color,
|
||||
"MAP ", &map_text, " mmHg",
|
||||
alarmed(p.alarms.art_map, map), flash);
|
||||
}
|
||||
}
|
||||
Channel::Capno => {
|
||||
|
|
@ -270,17 +317,7 @@ fn draw_alarm_number(cr: &Context, value: &str, x: f64, baseline: f64,
|
|||
let _ = cr.restore();
|
||||
}
|
||||
|
||||
/// Draw pressure components independently: only SYS or DIA flashes when it
|
||||
/// breaches its respective limit. The separator remains the normal color.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn draw_pressure_value(cr: &Context, systolic: i32, diastolic: i32,
|
||||
x: f64, baseline: f64, nominal_size: f64,
|
||||
available_width: f64, color: Color,
|
||||
sys_alarm: bool, dia_alarm: bool, flash: bool) {
|
||||
draw_pressure_components(cr, &systolic.to_string(), &diastolic.to_string(),
|
||||
x, baseline, nominal_size, available_width, color, sys_alarm, dia_alarm, flash);
|
||||
}
|
||||
|
||||
/// Draw SYS and DIA independently; their threshold alarms can invert separately.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn draw_pressure_components(cr: &Context, sys: &str, dia: &str,
|
||||
x: f64, baseline: f64, nominal_size: f64,
|
||||
|
|
@ -392,7 +429,8 @@ fn draw_trace(cr: &Context, channel: Channel, color: Color,
|
|||
// Disconnected or absent pleth signal: no curve, including historical pulses.
|
||||
if matches!(channel, Channel::Pleth)
|
||||
&& (!pleth_trace_enabled(p)
|
||||
|| p.spo2_status_during(sim.pulse_rate(), sim.poor_signal_lost()) != Spo2Status::Available)
|
||||
|| p.spo2_status_at_quality(sim.pulse_rate(), sim.poor_signal_lost(),
|
||||
sim.spo2_quality(p)) != Spo2Status::Available)
|
||||
{
|
||||
let _ = cr.restore();
|
||||
return;
|
||||
|
|
@ -421,7 +459,7 @@ fn draw_trace(cr: &Context, channel: Channel, color: Color,
|
|||
fn normalized(channel: Channel, s: &Sample, p: &Parameters) -> f64 {
|
||||
match channel {
|
||||
Channel::Ecg => (0.40 + s.ecg * 0.38).clamp(0.02, 0.98),
|
||||
Channel::Pleth => (0.10 + pleth_visual_amplitude(s.pleth, s.time, p.spo2_quality) * 0.78)
|
||||
Channel::Pleth => (0.10 + pleth_visual_amplitude(s.pleth, s.time, s.pleth_quality) * 0.78)
|
||||
.clamp(0.02, 0.98),
|
||||
Channel::Art => (s.art / 200.0).clamp(0.0, 1.0),
|
||||
Channel::Capno => (0.08 + (s.co2 / f64::from(p.etco2.max(1))) * 0.80)
|
||||
|
|
@ -602,7 +640,7 @@ mod tests {
|
|||
#[test]
|
||||
fn arterial_axis_is_absolute_and_fixed_to_200_mmhg() {
|
||||
let p = Parameters::default();
|
||||
let mut s = Sample { time: 0.0, ecg: 0.0, pleth: 0.0, pleth_valid: true, art: 100.0, co2: 0.0 };
|
||||
let mut s = Sample { time: 0.0, ecg: 0.0, pleth: 0.0, pleth_valid: true, pleth_quality: Spo2Quality::Good, art: 100.0, co2: 0.0 };
|
||||
assert_eq!(normalized(Channel::Art, &s, &p), 0.5);
|
||||
s.art = 200.0;
|
||||
assert_eq!(normalized(Channel::Art, &s, &p), 1.0);
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use crate::model::{ArterialShape, CapnoShape, EcgRhythm, Parameters, PulseConduction, Spo2Quality};
|
||||
use crate::nibp::{CuffZone, NibpMachine, NibpReading};
|
||||
|
||||
const SAMPLE_STEP: f64 = 0.004; // 250 Hz; adequately samples the QRS upstroke
|
||||
const HISTORY_SECONDS: f64 = 10.0;
|
||||
|
|
@ -33,6 +34,7 @@ pub struct Sample {
|
|||
pub ecg: f64, // arbitrary amplitude
|
||||
pub pleth: f64, // attenuated, low-pass-filtered amplitude 0..1
|
||||
pub pleth_valid: bool, // gaps during intermittent signal dropout
|
||||
pub pleth_quality: Spo2Quality, // effective quality at sample time
|
||||
pub art: f64, // absolute pressure in mmHg; display clips above 200
|
||||
pub co2: f64, // mmHg
|
||||
}
|
||||
|
|
@ -98,6 +100,8 @@ pub struct Simulator {
|
|||
previous_conduction: PulseConduction,
|
||||
previous_heart_rate: i32,
|
||||
poor_signal_outages: PoorSignalOutages,
|
||||
nibp: NibpMachine,
|
||||
art_hold_pressure: Option<f64>,
|
||||
pending_time: f64,
|
||||
}
|
||||
|
||||
|
|
@ -116,6 +120,8 @@ impl Simulator {
|
|||
previous_conduction: params.pulse_conduction,
|
||||
previous_heart_rate: params.heart_rate,
|
||||
poor_signal_outages: PoorSignalOutages::new(),
|
||||
nibp: NibpMachine::new(),
|
||||
art_hold_pressure: None,
|
||||
pending_time: 0.0,
|
||||
};
|
||||
// Prepopulate the trace and pulse history, including the initial pulse rate.
|
||||
|
|
@ -138,6 +144,53 @@ impl Simulator {
|
|||
self.poor_signal_outages.active
|
||||
}
|
||||
|
||||
pub fn nibp(&self) -> &NibpMachine { &self.nibp }
|
||||
|
||||
pub fn time(&self) -> f64 { self.time }
|
||||
|
||||
pub fn spo2_quality(&self, p: &Parameters) -> Spo2Quality {
|
||||
self.nibp.effective_spo2_quality(self.time, p)
|
||||
}
|
||||
|
||||
pub fn start_nibp_now(&mut self, p: &Parameters) {
|
||||
let quality = self.nibp_start_quality(p);
|
||||
self.nibp.measure_now(self.time, p, quality);
|
||||
}
|
||||
|
||||
fn nibp_start_quality(&self, p: &Parameters) -> Spo2Quality {
|
||||
if !p.spo2_sensor_attached || self.poor_signal_outages.active {
|
||||
Spo2Quality::None
|
||||
} else {
|
||||
p.spo2_quality
|
||||
}
|
||||
}
|
||||
|
||||
/// Three displayed arterial values track the cuff-altered transducer curve.
|
||||
pub fn displayed_art_pressures(&self, p: &Parameters, has_capture: bool)
|
||||
-> (Option<i32>, Option<i32>, Option<i32>) {
|
||||
if !has_capture {
|
||||
return (self.measured_art_pressure(), None, None);
|
||||
}
|
||||
if p.nibp_cuff_on_art {
|
||||
match self.nibp.cuff_zone(self.time, p) {
|
||||
CuffZone::Occluded => {
|
||||
let v = self.measured_art_pressure();
|
||||
return (v, v, v);
|
||||
}
|
||||
CuffZone::Partial => {
|
||||
let target = self.nibp.reading().map_or(p.nibp_systolic, |r| r.systolic) as f64;
|
||||
let cuff = self.nibp.cuff_pressure(self.time);
|
||||
let sys = target.max(cuff).round() as i32;
|
||||
let dia = target.min(cuff).round() as i32;
|
||||
return (Some(sys), Some(dia), Some(crate::model::mean_arterial_pressure(sys, dia)));
|
||||
}
|
||||
CuffZone::Free => {}
|
||||
}
|
||||
}
|
||||
(Some(p.art_systolic), Some(p.art_diastolic),
|
||||
Some(crate::model::mean_arterial_pressure(p.art_systolic, p.art_diastolic)))
|
||||
}
|
||||
|
||||
/// Last arterial transducer sample, in mmHg (including slow 0..5 drift).
|
||||
pub fn measured_art_pressure(&self) -> Option<i32> {
|
||||
self.samples.back().map(|sample| sample.art.round() as i32)
|
||||
|
|
@ -168,6 +221,10 @@ impl Simulator {
|
|||
self.time += SAMPLE_STEP;
|
||||
self.poor_signal_outages.update(self.time,
|
||||
params.spo2_quality == Spo2Quality::Poor && params.spo2_sensor_attached);
|
||||
// Snapshot the unoccluded sensor quality at the start of a cuff cycle.
|
||||
// Cuff-induced signal loss during that cycle must not change its duration.
|
||||
let start_quality = self.nibp_start_quality(params);
|
||||
self.nibp.update(self.time, params, start_quality);
|
||||
|
||||
if params.ecg_rhythm != self.previous_rhythm
|
||||
|| params.pulse_conduction != self.previous_conduction
|
||||
|
|
@ -233,7 +290,8 @@ impl Simulator {
|
|||
}
|
||||
|
||||
let ecg = ecg_amplitude(self.ecg_phase, self.time, params.ecg_rhythm, params.heart_rate);
|
||||
let (art, pleth_target) = self.mechanical_signals(params);
|
||||
let (normal_art, pleth_target) = self.mechanical_signals(params);
|
||||
let art = self.art_with_cuff(normal_art, params);
|
||||
let smoothing = 1.0 - (-SAMPLE_STEP / PLETH_FILTER_TAU).exp();
|
||||
self.filtered_pleth += smoothing * (pleth_target - self.filtered_pleth);
|
||||
let pleth = self.filtered_pleth.clamp(0.0, 1.0);
|
||||
|
|
@ -242,15 +300,52 @@ impl Simulator {
|
|||
capno_amplitude(self.respiratory_phase, params.capno_shape)
|
||||
};
|
||||
|
||||
let pleth_quality = self.spo2_quality(params);
|
||||
let pleth_valid = params.spo2_sensor_attached
|
||||
&& params.spo2_quality != Spo2Quality::None
|
||||
&& pleth_quality != Spo2Quality::None
|
||||
&& !self.poor_signal_outages.active;
|
||||
self.samples.push_back(Sample { time: self.time, ecg, pleth, pleth_valid, art, co2 });
|
||||
self.samples.push_back(Sample {
|
||||
time: self.time, ecg, pleth, pleth_valid, pleth_quality, art, co2,
|
||||
});
|
||||
while self.samples.front().is_some_and(|first| first.time < self.time - HISTORY_SECONDS) {
|
||||
self.samples.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
fn art_with_cuff(&mut self, normal_art: f64, params: &Parameters) -> f64 {
|
||||
if !params.nibp_cuff_on_art || !params.ecg_rhythm.can_generate_pulse()
|
||||
|| params.pulse_conduction == PulseConduction::None || params.heart_rate <= 0
|
||||
{
|
||||
self.art_hold_pressure = None;
|
||||
return normal_art;
|
||||
}
|
||||
match self.nibp.cuff_zone(self.time, params) {
|
||||
CuffZone::Free => {
|
||||
self.art_hold_pressure = None;
|
||||
normal_art
|
||||
}
|
||||
CuffZone::Occluded => {
|
||||
if let Some(held) = self.art_hold_pressure {
|
||||
held
|
||||
} else {
|
||||
let held = self.samples.back().map_or(normal_art, |s| s.art);
|
||||
self.art_hold_pressure = Some(held);
|
||||
held
|
||||
}
|
||||
}
|
||||
CuffZone::Partial => {
|
||||
self.art_hold_pressure = None;
|
||||
let cuff = self.nibp.cuff_pressure(self.time);
|
||||
let target = f64::from(self.nibp.reading().map_or(
|
||||
params.nibp_systolic, |r: NibpReading| r.systolic));
|
||||
let original_span = f64::from((params.art_systolic - params.art_diastolic).max(1));
|
||||
let shape = ((normal_art - f64::from(params.art_diastolic)) / original_span)
|
||||
.clamp(0.0, 1.0);
|
||||
(cuff + (target - cuff) * shape).clamp(0.0, 260.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn mechanical_signals(&self, params: &Parameters) -> (f64, f64) {
|
||||
let systolic = params.art_systolic.clamp(0, 200) as f64;
|
||||
let diastolic = params.art_diastolic.clamp(0, 200).min(params.art_systolic.clamp(0, 200)) as f64;
|
||||
|
|
@ -603,6 +698,31 @@ mod tests {
|
|||
assert!(sim.samples.back().unwrap().pleth_valid);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nibp_cuff_can_occlude_art_and_reduce_spo2_quality() {
|
||||
let p = Parameters {
|
||||
nibp_cuff_on_spo2: true,
|
||||
nibp_cuff_on_art: true,
|
||||
..Parameters::default()
|
||||
};
|
||||
let mut sim = Simulator::new(&p);
|
||||
while sim.time < 3.0 { sim.step(&p); }
|
||||
assert_eq!(sim.nibp.cuff_zone(sim.time, &p), CuffZone::Occluded);
|
||||
assert_eq!(sim.spo2_quality(&p), Spo2Quality::None);
|
||||
let held = sim.samples.back().unwrap().art;
|
||||
for _ in 0..50 { sim.step(&p); }
|
||||
assert!((sim.samples.back().unwrap().art - held).abs() < 1e-9);
|
||||
assert!(!sim.samples.back().unwrap().pleth_valid);
|
||||
let (sys, dia, map) = sim.displayed_art_pressures(&p, true);
|
||||
assert!(sys.is_some() && sys == dia && dia == map);
|
||||
while sim.time < 10.0 { sim.step(&p); }
|
||||
assert_eq!(sim.nibp.cuff_zone(sim.time, &p), CuffZone::Partial);
|
||||
assert_eq!(sim.spo2_quality(&p), Spo2Quality::Poor);
|
||||
while sim.time < 19.0 { sim.step(&p); }
|
||||
assert_eq!(sim.nibp.cuff_zone(sim.time, &p), CuffZone::Free);
|
||||
assert_eq!(sim.spo2_quality(&p), Spo2Quality::Good);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pulse_rate_follows_two_to_one_capture() {
|
||||
let p = Parameters { pulse_conduction: PulseConduction::TwoToOne, ..Parameters::default() };
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ use crate::simulation::Simulator;
|
|||
type SharedParams = Rc<RefCell<Parameters>>;
|
||||
|
||||
// Match the RGB colors of the monitor traces (see render.rs).
|
||||
// NIBP uses the same light grey as the NIBP footer and capnography.
|
||||
// NIBP shares the arterial-pressure red; capnography is light grey.
|
||||
type AccentColor = (f64, f64, f64);
|
||||
const ECG_GREEN: AccentColor = (0.26, 1.0, 0.39);
|
||||
const SPO2_YELLOW: AccentColor = (1.0, 0.85, 0.17);
|
||||
|
|
@ -25,8 +25,8 @@ const MONITOR_GREY: AccentColor = (0.80, 0.84, 0.87);
|
|||
pub fn create_windows(app: &Application) {
|
||||
let parameters = Rc::new(RefCell::new(Parameters::default()));
|
||||
let simulator = Rc::new(RefCell::new(Simulator::new(¶meters.borrow())));
|
||||
let monitor = create_monitor(app, parameters.clone(), simulator);
|
||||
let admin = create_admin(app, parameters, &monitor);
|
||||
let monitor = create_monitor(app, parameters.clone(), simulator.clone());
|
||||
let admin = create_admin(app, parameters, simulator, &monitor);
|
||||
|
||||
// Present monitor first, then leave controls in the foreground.
|
||||
monitor.present();
|
||||
|
|
@ -89,6 +89,7 @@ fn create_monitor(app: &Application, params: SharedParams,
|
|||
}
|
||||
|
||||
fn create_admin(app: &Application, params: SharedParams,
|
||||
simulator: Rc<RefCell<Simulator>>,
|
||||
monitor: &ApplicationWindow) -> ApplicationWindow {
|
||||
let window = ApplicationWindow::builder()
|
||||
.application(app)
|
||||
|
|
@ -283,9 +284,9 @@ ART and pleth are delayed. VT can be perfusing; PVT, VF and asystole are pulsele
|
|||
};
|
||||
});
|
||||
|
||||
let nibp = group(&controls, "Non-invasive blood pressure (NIBP)", Some(MONITOR_GREY));
|
||||
let nibp = group(&controls, "Non-invasive blood pressure (NIBP)", Some(ART_RED));
|
||||
add_visibility(&nibp, params.borrow().show_nibp, params.clone(), |p, show| p.show_nibp = show);
|
||||
let (nibp_sys_row, _, _) = add_numeric(&nibp, "Systolic", "mmHg", 0, 260, params.borrow().nibp_systolic,
|
||||
let (nibp_sys_row, _, _) = add_numeric(&nibp, "Initial systolic", "mmHg", 0, 260, params.borrow().nibp_systolic,
|
||||
params.clone(), |p, v| p.nibp_systolic = v);
|
||||
alarm_editors.push((add_alarm_limits(&nibp_sys_row, "", "", 260,
|
||||
params.borrow().alarms.nibp_sys, params.clone(), |p, v| p.alarms.nibp_sys = v,
|
||||
|
|
@ -293,12 +294,49 @@ ART and pleth are delayed. VT can be perfusing; PVT, VF and asystole are pulsele
|
|||
alarm_editors.push((add_alarm_limits(&nibp, "MAP (derived)", "mmHg", 260,
|
||||
params.borrow().alarms.nibp_map, params.clone(), |p, v| p.alarms.nibp_map = v,
|
||||
&preset_dropdown, &updating_preset), |s| s.nibp_map));
|
||||
let (nibp_dia_row, _, _) = add_numeric(&nibp, "Diastolic", "mmHg", 0, 200, params.borrow().nibp_diastolic,
|
||||
let (nibp_dia_row, _, _) = add_numeric(&nibp, "Initial diastolic", "mmHg", 0, 200, params.borrow().nibp_diastolic,
|
||||
params.clone(), |p, v| p.nibp_diastolic = v);
|
||||
alarm_editors.push((add_alarm_limits(&nibp_dia_row, "", "", 200,
|
||||
params.borrow().alarms.nibp_dia, params.clone(), |p, v| p.alarms.nibp_dia = v,
|
||||
&preset_dropdown, &updating_preset), |s| s.nibp_dia));
|
||||
|
||||
let _nibp_interval = add_dropdown_at(&nibp, "Measurement interval",
|
||||
&["1 min", "3 min", "5 min", "10 min", "20 min", "30 min", "60 min"],
|
||||
2, params.clone(), |p, selected| {
|
||||
p.nibp_interval_minutes = match selected {
|
||||
0 => 1, 1 => 3, 2 => 5, 3 => 10, 4 => 20, 5 => 30, _ => 60,
|
||||
};
|
||||
});
|
||||
for (caption, current, update) in [
|
||||
("No reading (simulate failure)", params.borrow().nibp_no_reading,
|
||||
(|p: &mut Parameters, on| p.nibp_no_reading = on) as fn(&mut Parameters, bool)),
|
||||
("Cuff on SpO₂ extremity", params.borrow().nibp_cuff_on_spo2,
|
||||
(|p: &mut Parameters, on| p.nibp_cuff_on_spo2 = on) as fn(&mut Parameters, bool)),
|
||||
("Cuff on ART extremity", params.borrow().nibp_cuff_on_art,
|
||||
(|p: &mut Parameters, on| p.nibp_cuff_on_art = on) as fn(&mut Parameters, bool)),
|
||||
] {
|
||||
let checkbox = gtk::CheckButton::with_label(caption);
|
||||
checkbox.set_active(current);
|
||||
checkbox.connect_toggled({
|
||||
let params = params.clone();
|
||||
move |button| update(&mut params.borrow_mut(), button.is_active())
|
||||
});
|
||||
nibp.append(&checkbox);
|
||||
}
|
||||
let measure_now = gtk::Button::with_label("Measure now");
|
||||
measure_now.connect_clicked({
|
||||
let params = params.clone();
|
||||
let simulator = simulator.clone();
|
||||
move |_| simulator.borrow_mut().start_nibp_now(¶ms.borrow())
|
||||
});
|
||||
nibp.append(&measure_now);
|
||||
let nibp_hint = gtk::Label::new(Some(
|
||||
"First successful NIBP: instructor SYS/DIA above. Later values: simulated IBP-dependent readings."
|
||||
));
|
||||
nibp_hint.set_xalign(0.0);
|
||||
nibp_hint.set_wrap(true);
|
||||
nibp.append(&nibp_hint);
|
||||
|
||||
let capno = group(&controls, "Capnography", Some(MONITOR_GREY));
|
||||
add_visibility(&capno, params.borrow().show_capno, params.clone(), |p, show| p.show_capno = show);
|
||||
add_dropdown(&capno, "Capnogram", &["Normal", "Obstructive / shark fin"],
|
||||
|
|
|
|||
Loading…
Reference in a new issue