v0.4.2: add PVT

This commit is contained in:
GABRIEL Sebastian 2026-11-12 12:08:46 +01:00
commit 5e397019fc
8 changed files with 727 additions and 51 deletions

View file

@ -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, EcgRhythm, Parameters, PulseConduction, Spo2Quality};
use crate::model::{mean_arterial_pressure, AlarmLimits, AlarmState, Parameters, PulseConduction, Spo2Quality, Spo2Status};
use crate::simulation::{Sample, Simulator};
type Color = (f64, f64, f64);
@ -106,17 +106,17 @@ fn pleth_trace_enabled(p: &Parameters) -> bool {
}
fn spo2_vitals(p: &Parameters, pulse_rate: Option<i32>) -> (String, String, String) {
if !p.spo2_sensor_attached {
return ("SpO₂".to_owned(), "NO SENSOR".to_owned(), "SpO₂ -- PR -- bpm".to_owned());
match p.spo2_status(pulse_rate) {
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()),
p.spo2.to_string(),
format!("% PR {} bpm", pulse_rate.unwrap_or_default()),
),
}
if p.spo2_quality == Spo2Quality::None {
return ("SpO₂".to_owned(), "--".to_owned(), "NO SIGNAL PR -- bpm".to_owned());
}
(
format!("SpO₂ {}", p.spo2_quality.stars()),
if pulse_rate.is_some() { p.spo2.to_string() } else { "--".to_owned() },
format!("% PR {} bpm", pulse_rate.map_or("--".to_owned(), |pr| pr.to_string())),
)
}
fn alarmed(limits: AlarmLimits, value: Option<i32>) -> bool {
@ -136,16 +136,15 @@ fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
let number_size = (row_h * 0.375).min(width * 0.26).clamp(16.0, 59.0);
let sub_size = (row_h * 0.16).clamp(11.0, 22.0) * 0.85;
let flash = alarm_flash_on(sim);
let has_capture = p.ecg_rhythm.has_qrs()
let has_capture = p.ecg_rhythm.can_generate_pulse()
&& p.heart_rate > 0
&& p.pulse_conduction != PulseConduction::None;
let pulse_rate = if has_capture { sim.pulse_rate() } else { None };
let ecg_hr = if p.ecg_rhythm.has_qrs() && p.heart_rate > 0 {
Some(p.heart_rate)
} else { None };
let oxygen = if pleth_trace_enabled(p) && pulse_rate.is_some() {
Some(p.spo2)
} else { None };
let spo2_status = p.spo2_status(pulse_rate);
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 {
@ -165,23 +164,23 @@ fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
draw_alarm_number(cr, &ecg_hr.map_or("--".to_owned(), |v| v.to_string()),
x, value_y, number_size, width, color,
alarmed(p.alarms.hr, ecg_hr), flash);
let rhythm = match p.ecg_rhythm {
EcgRhythm::Sinus => "Sinus",
EcgRhythm::AtrialFibrillation => "AF",
EcgRhythm::VentricularTachycardia => "VT",
EcgRhythm::VentricularFibrillation => "VF",
EcgRhythm::Asystole => "Asystole",
};
text(cr, &format!("bpm · {}", rhythm), x, info_y, sub_size, color, false);
// Show only the clinical alarm message, never the selected ECG rhythm.
if let Some(message) = p.ecg_rhythm.rhythm_alarm() {
draw_alarm_number(cr, message, x, info_y, sub_size * 1.1,
width, color, true, flash);
} else {
text(cr, "bpm", x, info_y, sub_size, color, false);
}
}
Channel::Pleth => {
let (label, value, info) = spo2_vitals(p, pulse_rate);
text(cr, &label, x, top + row_h * 0.24, label_size, color, false);
let value_size = if value == "NO SENSOR" {
number_size.min(0.19 * width).min(26.0)
let technical_alarm = spo2_status != Spo2Status::Available;
let value_size = if technical_alarm {
number_size.min(0.17 * width).min(26.0)
} else { number_size };
draw_alarm_number(cr, &value, x, value_y, value_size,
width, color, alarmed(p.alarms.spo2, oxygen), flash);
width, color, technical_alarm || alarmed(p.alarms.spo2, oxygen), flash);
if oxygen.is_some() {
draw_alarm_inline(cr, x, info_y, sub_size, width, color,
"% PR ", &pulse_rate.unwrap_or_default().to_string(), " bpm",
@ -377,7 +376,9 @@ 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)
if matches!(channel, Channel::Pleth)
&& (!pleth_trace_enabled(p)
|| p.spo2_status(sim.pulse_rate()) != Spo2Status::Available)
{
let _ = cr.restore();
return;
@ -544,8 +545,7 @@ mod tests {
p.spo2_sensor_attached = true;
p.spo2_quality = Spo2Quality::None;
assert!(!pleth_trace_enabled(&p));
assert_eq!(spo2_vitals(&p, Some(75)).1, "--");
assert!(spo2_vitals(&p, Some(75)).2.contains("NO SIGNAL"));
assert_eq!(spo2_vitals(&p, Some(75)).1, "NO READING");
assert!(spo2_vitals(&p, Some(75)).2.contains("PR --"));
assert!(p.show_spo2); // no readings, but visible row and status
}
@ -559,10 +559,20 @@ mod tests {
assert!(spo2_vitals(&p, Some(75)).2.contains("PR 75 bpm"));
p.spo2_quality = Spo2Quality::Good;
assert_eq!(spo2_vitals(&p, Some(75)).0, "SpO₂ ★★★");
assert_eq!(spo2_vitals(&p, None).1, "--");
assert_eq!(spo2_vitals(&p, None).1, "NO READING");
assert!(spo2_vitals(&p, None).2.contains("PR --"));
}
#[test]
fn no_sensor_precedes_no_reading_and_both_are_spo2_alarms() {
let mut p = Parameters::default();
assert_eq!(spo2_vitals(&p, None).1, "NO READING");
p.ecg_rhythm = crate::model::EcgRhythm::PulselessVentricularTachycardia;
assert_eq!(spo2_vitals(&p, Some(100)).1, "NO READING");
p.spo2_sensor_attached = false;
assert_eq!(spo2_vitals(&p, Some(100)).1, "NO SENSOR");
}
#[test]
fn arterial_axis_is_absolute_and_fixed_to_200_mmhg() {
let p = Parameters::default();