Skip to content

Commit

Permalink
fix #46
Browse files Browse the repository at this point in the history
Signed-off-by: Valerian Saliou <[email protected]>
  • Loading branch information
valeriansaliou committed Nov 4, 2020
1 parent 3bec332 commit ed572b9
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 32 deletions.
25 changes: 16 additions & 9 deletions src/chip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pub struct Chip {
pub last_tick: u64,
pub data_pressure: DataPressure,
pub last_machine_snapshot: MachineStateSnapshot,
pub last_data_snapshot: Option<DataSnapshot>,
pub ongoing_alarms: HashMap<AlarmCode, AlarmPriority>,
pub battery_level: Option<u8>,
pub settings: ChipSettings,
state: ChipState,
lora_tx: Option<Sender<TelemetryMessage>>,
Expand All @@ -70,8 +70,8 @@ impl Chip {
last_tick: 0,
data_pressure: VecDeque::with_capacity(GRAPH_NUMBER_OF_POINTS + 100),
last_machine_snapshot,
last_data_snapshot: None,
ongoing_alarms: HashMap::new(),
battery_level: None,
settings: ChipSettings::new(cycles_per_minute),
state: ChipState::WaitingData,
lora_tx: lora_sender,
Expand All @@ -81,9 +81,9 @@ impl Chip {

pub fn reset(&mut self, new_tick: u64) {
self.last_tick = new_tick;
self.data_pressure.clear();

self.last_machine_snapshot = MachineStateSnapshot::default();
self.clean();

self.ongoing_alarms.clear();

self.update_boot_time();
Expand Down Expand Up @@ -128,7 +128,9 @@ impl Chip {
}

pub fn get_battery_level(&self) -> Option<u8> {
self.battery_level
self.last_data_snapshot
.as_ref()
.map(|snapshot| snapshot.battery_level)
}

pub fn get_state(&self) -> &ChipState {
Expand Down Expand Up @@ -231,7 +233,7 @@ impl Chip {
self.update_tick(snapshot.systick);
self.add_pressure(&snapshot);

self.battery_level = Some(snapshot.battery_level);
self.last_data_snapshot = Some(snapshot);

self.update_state_running();

Expand Down Expand Up @@ -356,11 +358,16 @@ impl Chip {
}
}

fn clean(&mut self) {
self.data_pressure.clear();

self.last_machine_snapshot = MachineStateSnapshot::default();
self.last_data_snapshot = None;
}

fn clean_if_stopped(&mut self) {
if self.state == ChipState::Stopped {
self.data_pressure.clear();

self.last_machine_snapshot = MachineStateSnapshot::default();
self.clean();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/config/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ pub const RUN_SETTINGS_MODAL_HEIGTH: f64 = 140.0;
pub const RUN_SETTINGS_MODAL_FORM_PADDING_LEFT: f64 = 240.0;

pub const ADVANCED_SETTINGS_MODAL_PADDING: f64 = 20.0;
pub const ADVANCED_SETTINGS_MODAL_WIDTH: f64 = 640.0;
pub const ADVANCED_SETTINGS_MODAL_WIDTH: f64 = 600.0;
pub const ADVANCED_SETTINGS_MODAL_HEIGTH: f64 = 380.0;
pub const ADVANCED_SETTINGS_LINES_COUNT: usize = 13;
pub const ADVANCED_SETTINGS_LINE_MARGIN_TOP: f64 = 8.0;
pub const ADVANCED_SETTINGS_LINE_FONT_SIZE: u32 = 14;
pub const ADVANCED_SETTINGS_LINE_VALUE_PADDING_LEFT: f64 = 260.0;
pub const ADVANCED_SETTINGS_LINE_VALUE_PADDING_LEFT: f64 = 280.0;
pub const ADVANCED_SETTINGS_LINE_VALUE_EMPTY: &str = "--";

pub const TRIGGER_SETTINGS_MODAL_PADDING: f64 = 20.0;
Expand Down
1 change: 1 addition & 0 deletions src/display/drawer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl<'a> DisplayDrawer<'a> {
let image_map = self.renderer.render(
&self.chip.data_pressure,
&self.chip.last_machine_snapshot,
self.chip.last_data_snapshot.as_ref(),
&self.chip.ongoing_alarms_sorted(),
&self.display,
&mut self.interface,
Expand Down
10 changes: 7 additions & 3 deletions src/display/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use image::load_from_memory;
use plotters::prelude::*;
use plotters::style::TextStyle;
use telemetry::alarm::AlarmCode;
use telemetry::structures::{AlarmPriority, MachineStateSnapshot};
use telemetry::structures::{AlarmPriority, DataSnapshot, MachineStateSnapshot};

use crate::chip::settings::{ChipSettings, ChipSettingsEvent};
use crate::chip::ChipState;
Expand Down Expand Up @@ -105,6 +105,7 @@ impl DisplayRenderer {
&mut self,
data_pressure: &DataPressure,
machine_snapshot: &MachineStateSnapshot,
data_snapshot: Option<&DataSnapshot>,
ongoing_alarms: &[(AlarmCode, AlarmPriority)],
display: &GliumDisplayWinitWrapper,
interface: &mut Ui,
Expand All @@ -126,6 +127,7 @@ impl DisplayRenderer {
image_map,
data_pressure,
machine_snapshot,
data_snapshot,
ongoing_alarms,
battery_level,
chip_state,
Expand Down Expand Up @@ -175,7 +177,7 @@ impl DisplayRenderer {
connecting: is_connecting,
};

let mut screen = Screen::new(ui, &self.ids, &self.fonts, None, None);
let mut screen = Screen::new(ui, &self.ids, &self.fonts, None, None, None);

screen.render_initializing(screen_bootloader);

Expand All @@ -190,7 +192,7 @@ impl DisplayRenderer {
) -> conrod_core::image::Map<texture::Texture2d> {
let ui = interface.set_widgets();

let mut screen = Screen::new(ui, &self.ids, &self.fonts, None, None);
let mut screen = Screen::new(ui, &self.ids, &self.fonts, None, None, None);

screen.render_error(error);

Expand All @@ -205,6 +207,7 @@ impl DisplayRenderer {
mut image_map: conrod_core::image::Map<texture::Texture2d>,
data_pressure: &DataPressure,
machine_snapshot: &MachineStateSnapshot,
data_snapshot: Option<&DataSnapshot>,
ongoing_alarms: &[(AlarmCode, AlarmPriority)],
battery_level: Option<u8>,
chip_state: &ChipState,
Expand Down Expand Up @@ -283,6 +286,7 @@ impl DisplayRenderer {
&self.ids,
&self.fonts,
Some(machine_snapshot),
data_snapshot,
Some(ongoing_alarms),
);

Expand Down
8 changes: 6 additions & 2 deletions src/display/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use conrod_core::color::{self, Color};
use conrod_core::widget::Id as WidgetId;

use telemetry::alarm::AlarmCode;
use telemetry::structures::{AlarmPriority, MachineStateSnapshot};
use telemetry::structures::{AlarmPriority, DataSnapshot, MachineStateSnapshot};

use crate::chip::settings::{
cycles::SettingsCycles, expiration_term::SettingsExpirationTerm, pressure::SettingsPressure,
Expand All @@ -27,6 +27,7 @@ use super::widget::{ControlWidget, ControlWidgetType};
pub struct Screen<'a> {
ids: &'a Ids,
machine_snapshot: Option<&'a MachineStateSnapshot>,
data_snapshot: Option<&'a DataSnapshot>,
ongoing_alarms: Option<&'a [(AlarmCode, AlarmPriority)]>,
widgets: ControlWidget<'a>,
}
Expand All @@ -46,11 +47,13 @@ impl<'a> Screen<'a> {
ids: &'a Ids,
fonts: &'a Fonts,
machine_snapshot: Option<&'a MachineStateSnapshot>,
data_snapshot: Option<&'a DataSnapshot>,
ongoing_alarms: Option<&'a [(AlarmCode, AlarmPriority)]>,
) -> Screen<'a> {
Screen {
ids,
machine_snapshot,
data_snapshot,
ongoing_alarms,
widgets: ControlWidget::new(ui, fonts),
}
Expand Down Expand Up @@ -651,7 +654,8 @@ impl<'a> Screen<'a> {
- MODAL_VALIDATE_BUTTON_HEIGHT
- (ADVANCED_SETTINGS_MODAL_PADDING * 2.0),

snapshot: &self.machine_snapshot.unwrap(),
machine_snapshot: &self.machine_snapshot.unwrap(),
data_snapshot: self.data_snapshot,

advanced_container_parent: self.ids.modal_container,
advanced_container_widget: self.ids.advanced_container,
Expand Down
4 changes: 4 additions & 0 deletions src/utilities/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ pub fn parse_non_empty_number_to_string(value: usize) -> String {
value.to_string()
}
}

pub fn parse_non_empty_optional_number_to_string(value: Option<usize>) -> String {
parse_non_empty_number_to_string(value.unwrap_or(0))
}
99 changes: 83 additions & 16 deletions src/widget/advanced_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ use conrod_core::{
Positionable, Sizeable, Widget,
};

use telemetry::structures::MachineStateSnapshot;
use telemetry::structures::{DataSnapshot, MachineStateSnapshot};

use crate::config::environment::*;
use crate::display::widget::ControlWidget;
use crate::utilities::parse::parse_non_empty_number_to_string;
use crate::utilities::parse::{
parse_non_empty_number_to_string, parse_non_empty_optional_number_to_string,
};
use crate::APP_CONTEXT;

const CONTROL_UI_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -27,7 +29,8 @@ pub struct Config<'a> {
pub width: f64,
pub height: f64,

pub snapshot: &'a MachineStateSnapshot,
pub machine_snapshot: &'a MachineStateSnapshot,
pub data_snapshot: Option<&'a DataSnapshot>,

pub advanced_container_parent: WidgetId,
pub advanced_container_widget: WidgetId,
Expand All @@ -50,34 +53,100 @@ pub fn render<'a>(master: &mut ControlWidget<'a>, config: Config) -> f64 {

// Generate line data
let line_data: [(&str, &str); ADVANCED_SETTINGS_LINES_COUNT] = [
// Telemetry version
(
"telemetry-version",
&parse_non_empty_number_to_string(config.snapshot.telemetry_version as usize),
&parse_non_empty_number_to_string(config.machine_snapshot.telemetry_version as usize),
),
// Control UI version
("control-ui-version", CONTROL_UI_VERSION),
(
"control-ui-uptime-seconds",
&parse_non_empty_number_to_string(APP_CONTEXT.start_time.elapsed().as_secs() as usize),
),
("firmware-version", &config.snapshot.version),
("firmware-target", ""), // TODO: BootMessage::mode
("runtime-device-id", &config.snapshot.device_id.to_string()),
// Firmware version
("firmware-version", &config.machine_snapshot.version),
// Runtime device identifier (randomly generated at each boot of the motherboard)
(
"runtime-device-id",
&config.machine_snapshot.device_id.to_string(),
),
// Runtime uptime (in seconds), try to use the data snapshot systick which is refreshed \
// almost instantly when the machine is running, otherwise fallback on the less-often \
// refreshed data snapshot systick.
(
"runtime-uptime-seconds",
&parse_non_empty_number_to_string(config.snapshot.systick as usize / 1000000),
&parse_non_empty_number_to_string(
config
.data_snapshot
.map(|data| data.systick)
.unwrap_or(config.machine_snapshot.systick) as usize
/ 1000000,
),
),
// Runtime cycles (ie. respiratory cycles)
(
"runtime-cycles",
&parse_non_empty_number_to_string(config.snapshot.cycle as usize),
&parse_non_empty_number_to_string(config.machine_snapshot.cycle as usize),
),
// Ventilation phase (ie. current respiration phase, called 'subphase' internally)
(
"ventilation-phase",
&config
.data_snapshot
.map(|data| format!("{:?}", data.subphase))
.unwrap_or_else(|| "".to_string()),
),
// Time spent since the beginning of the current respiratory cycle (in milliseconds)
(
"ventilation-cycle-milliseconds",
&parse_non_empty_optional_number_to_string(
config
.data_snapshot
.map(|data| (data.centile * 10) as usize),
),
),
// Pinch valve angle (inhale circuit pinch valve)
(
"pinch-angle-inhale-degrees",
&parse_non_empty_optional_number_to_string(
config
.data_snapshot
.map(|data| data.blower_valve_position as usize),
),
),
// Pinch valve angle (exhale circuit pinch valve)
(
"pinch-angle-exhale-degrees",
&parse_non_empty_optional_number_to_string(
config
.data_snapshot
.map(|data| data.patient_valve_position as usize),
),
),
// Blower speed (in RPM, ie. rotations per minute)
(
"blower-speed-rpm",
&parse_non_empty_optional_number_to_string(
config.data_snapshot.map(|data| data.blower_rpm as usize),
),
),
// Battery level (in volts)
(
"battery-level-volts",
&parse_non_empty_optional_number_to_string(
config.data_snapshot.map(|data| data.battery_level as usize),
),
),
("runtime-phase", ""), // TODO: DataSnapshot::subphase
("pinch-angle-inhale", ""), // TODO: DataSnapshot::blower_valve_position
("pinch-angle-exhale", ""), // TODO: DataSnapshot::patient_valve_position
("blower-rpm", ""), // TODO: DataSnapshot::blower_rpm
("battery-voltage", ""), // TODO: DataSnapshot::battery_level
];

// Append lines
lines(master, config, &line_data);

0 as _
}

pub fn lines<'a>(master: &mut ControlWidget<'a>, config: Config, line_data: &[(&str, &str)]) {
for (index, container_line) in config.advanced_container_line_labels.iter().enumerate() {
let line_text = line_data[index].0;

Expand Down Expand Up @@ -128,6 +197,4 @@ pub fn render<'a>(master: &mut ControlWidget<'a>, config: Config) -> f64 {
)
.set(config.advanced_container_line_values[index], &mut master.ui);
}

0 as _
}

0 comments on commit ed572b9

Please sign in to comment.