2026-11-12 09:36:13 +01:00
|
|
|
//! Shared configuration of the synthetic monitor signals.
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
|
pub enum EcgRhythm {
|
|
|
|
|
Sinus,
|
|
|
|
|
AtrialFibrillation,
|
|
|
|
|
VentricularTachycardia,
|
|
|
|
|
VentricularFibrillation,
|
|
|
|
|
Asystole,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl EcgRhythm {
|
|
|
|
|
/// VF and asystole have no organized QRS to trigger a mechanical pulse.
|
|
|
|
|
pub fn has_qrs(self) -> bool {
|
|
|
|
|
!matches!(self, Self::VentricularFibrillation | Self::Asystole)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
|
pub enum PulseConduction {
|
|
|
|
|
OneToOne,
|
|
|
|
|
TwoToOne,
|
|
|
|
|
ThreeToOne,
|
|
|
|
|
None,
|
|
|
|
|
Random,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PulseConduction {
|
|
|
|
|
pub fn conducts(self, beat: u64) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
Self::OneToOne => true,
|
|
|
|
|
// One mechanical pulse for every two QRS complexes.
|
|
|
|
|
Self::TwoToOne => (beat - 1) % 2 == 0,
|
|
|
|
|
Self::ThreeToOne => (beat - 1) % 3 == 0,
|
|
|
|
|
Self::None => false,
|
|
|
|
|
Self::Random => {
|
|
|
|
|
// Deterministic pseudo-random 65% mechanical capture for reproducible lessons.
|
|
|
|
|
let mut x = beat.wrapping_mul(0x9E37_79B9_7F4A_7C15);
|
|
|
|
|
x ^= x >> 30;
|
|
|
|
|
x = x.wrapping_mul(0xBF58_476D_1CE4_E5B9);
|
|
|
|
|
x ^= x >> 27;
|
|
|
|
|
(x % 100) < 65
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
|
pub enum ArterialShape {
|
|
|
|
|
Normal,
|
|
|
|
|
Damped,
|
|
|
|
|
Hyperdynamic,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
|
pub enum CapnoShape {
|
|
|
|
|
Normal,
|
|
|
|
|
Obstructive,
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-12 10:43:11 +01:00
|
|
|
/// Instructor-selected plethysmography signal quality; separate from the
|
|
|
|
|
/// physiological oxygen-saturation setting and mechanical pulse capture.
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
|
pub enum Spo2Quality {
|
|
|
|
|
None,
|
|
|
|
|
Poor,
|
|
|
|
|
Adequate,
|
|
|
|
|
Good,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Spo2Quality {
|
|
|
|
|
pub fn stars(self) -> &'static str {
|
|
|
|
|
match self {
|
|
|
|
|
Self::None => "",
|
|
|
|
|
Self::Poor => "★",
|
|
|
|
|
Self::Adequate => "★★",
|
|
|
|
|
Self::Good => "★★★",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn amplitude_scale(self) -> f64 {
|
|
|
|
|
match self {
|
|
|
|
|
Self::None => 0.0,
|
|
|
|
|
Self::Poor => 0.25,
|
|
|
|
|
Self::Adequate => 0.56,
|
|
|
|
|
Self::Good => 1.0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2026-11-12 09:36:13 +01:00
|
|
|
pub struct Parameters {
|
2026-11-12 10:43:11 +01:00
|
|
|
pub patient_name: String, // displayed in the monitor header, blank = generic title
|
2026-11-12 09:36:13 +01:00
|
|
|
pub ecg_rhythm: EcgRhythm,
|
|
|
|
|
pub heart_rate: i32, // bpm, organized electrical rate; not mechanical pulse rate
|
|
|
|
|
pub pulse_conduction: PulseConduction,
|
2026-11-12 10:43:11 +01:00
|
|
|
pub spo2: i32, // %; instructor-set nominal saturation
|
|
|
|
|
pub spo2_quality: Spo2Quality,
|
|
|
|
|
pub spo2_sensor_attached: bool,
|
2026-11-12 09:36:13 +01:00
|
|
|
pub art_systolic: i32, // mmHg, nominal pressure; display axis 0..200
|
|
|
|
|
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 etco2: i32, // mmHg
|
|
|
|
|
pub respiratory_rate: i32, // breaths/min
|
|
|
|
|
pub capno_shape: CapnoShape,
|
|
|
|
|
// These affect display only; the underlying physiological signals continue.
|
|
|
|
|
pub show_ecg: bool,
|
|
|
|
|
pub show_spo2: bool,
|
|
|
|
|
pub show_art: bool,
|
|
|
|
|
pub show_capno: bool,
|
|
|
|
|
pub show_nibp: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Parameters {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
2026-11-12 10:43:11 +01:00
|
|
|
patient_name: String::new(),
|
2026-11-12 09:36:13 +01:00
|
|
|
ecg_rhythm: EcgRhythm::Sinus,
|
|
|
|
|
heart_rate: 75,
|
|
|
|
|
pulse_conduction: PulseConduction::OneToOne,
|
|
|
|
|
spo2: 98,
|
2026-11-12 10:43:11 +01:00
|
|
|
spo2_quality: Spo2Quality::Good,
|
|
|
|
|
spo2_sensor_attached: true,
|
2026-11-12 09:36:13 +01:00
|
|
|
art_systolic: 120,
|
|
|
|
|
art_diastolic: 75,
|
|
|
|
|
arterial_shape: ArterialShape::Normal,
|
|
|
|
|
pulse_pressure_variation: 0,
|
|
|
|
|
nibp_systolic: 118,
|
|
|
|
|
nibp_diastolic: 74,
|
|
|
|
|
etco2: 38,
|
|
|
|
|
respiratory_rate: 14,
|
|
|
|
|
capno_shape: CapnoShape::Normal,
|
|
|
|
|
show_ecg: true,
|
|
|
|
|
show_spo2: true,
|
|
|
|
|
show_art: true,
|
|
|
|
|
show_capno: true,
|
|
|
|
|
show_nibp: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn mean_arterial_pressure(systolic: i32, diastolic: i32) -> i32 {
|
|
|
|
|
((systolic as f64 + 2.0 * diastolic as f64) / 3.0).round() as i32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn mean_pressure_uses_one_third_pulse_pressure() {
|
|
|
|
|
assert_eq!(mean_arterial_pressure(120, 75), 90);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-12 10:43:11 +01:00
|
|
|
#[test]
|
|
|
|
|
fn spo2_quality_stars_and_amplitude_progress_monotonically() {
|
|
|
|
|
assert_eq!(Spo2Quality::None.stars(), "");
|
|
|
|
|
assert_eq!(Spo2Quality::Poor.stars(), "★");
|
|
|
|
|
assert_eq!(Spo2Quality::Adequate.stars(), "★★");
|
|
|
|
|
assert_eq!(Spo2Quality::Good.stars(), "★★★");
|
|
|
|
|
assert_eq!(Spo2Quality::None.amplitude_scale(), 0.0);
|
|
|
|
|
assert!(Spo2Quality::Poor.amplitude_scale() < Spo2Quality::Adequate.amplitude_scale());
|
|
|
|
|
assert!(Spo2Quality::Adequate.amplitude_scale() < Spo2Quality::Good.amplitude_scale());
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-12 09:36:13 +01:00
|
|
|
#[test]
|
|
|
|
|
fn grouped_mechanical_capture_follows_requested_patterns() {
|
|
|
|
|
assert_eq!((1..=8).filter(|&b| PulseConduction::TwoToOne.conducts(b)).collect::<Vec<_>>(),
|
|
|
|
|
vec![1, 3, 5, 7]);
|
|
|
|
|
assert_eq!((1..=9).filter(|&b| PulseConduction::ThreeToOne.conducts(b)).collect::<Vec<_>>(),
|
|
|
|
|
vec![1, 4, 7]);
|
|
|
|
|
assert!((1..=20).all(|b| PulseConduction::OneToOne.conducts(b)));
|
|
|
|
|
assert!((1..=20).all(|b| !PulseConduction::None.conducts(b)));
|
|
|
|
|
}
|
|
|
|
|
}
|