Skip to content

Commit

Permalink
Disable serious flight mode buttons until armed
Browse files Browse the repository at this point in the history
  • Loading branch information
KoffeinFlummi committed Apr 17, 2024
1 parent 990dbfa commit a5c4eb9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 45 deletions.
23 changes: 0 additions & 23 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,6 @@ impl Sam {
self.tab = GuiTab::Configure;
}

let shortcut_mode = if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F5)) {
Some(FlightMode::Idle)
} else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F6)) {
Some(FlightMode::HardwareArmed)
} else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F7)) {
Some(FlightMode::Armed)
} else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F8)) {
Some(FlightMode::Flight)
} else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F9)) {
Some(FlightMode::RecoveryDrogue)
} else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F10)) {
Some(FlightMode::RecoveryMain)
} else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F11)) {
Some(FlightMode::Landed)
} else {
None
};

// Set new flight mode if keyboard shortcut was used
if let Some(fm) = shortcut_mode {
self.data_source_mut().send_command(Command::SetFlightMode(fm)).unwrap();
}

// Redefine text_styles
let colors = ThemeColors::new(ctx);
let mut style = (*ctx.style()).clone();
Expand Down
56 changes: 34 additions & 22 deletions src/gui/top_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
use eframe::egui;
use egui::widgets::{Button, ProgressBar};
use egui::{Color32, Label, RichText, SelectableLabel, Stroke, Rect, Vec2};
use egui::{Color32, Label, RichText, SelectableLabel, Stroke, Rect, Vec2, Key, Modifiers};

use mithril::telemetry::*;

use crate::data_source::*;
use crate::telemetry_ext::*;

// TODO: move to telemetry_ext?
fn flight_mode_style(fm: FlightMode) -> (&'static str, &'static str, Color32, Color32) {
fn flight_mode_style(fm: FlightMode) -> (&'static str, Color32, Color32) {
let fg = Color32::from_rgb(0x28, 0x28, 0x28);
match fm {
FlightMode::Idle => ("IDLE", "F5", fg, fm.color()),
FlightMode::HardwareArmed => ("HWARMED", "F6", fg, fm.color()),
FlightMode::Armed => ("ARMED", "F7", fg, fm.color()),
FlightMode::Flight => ("FLIGHT", "F8", fg, fm.color()),
FlightMode::RecoveryDrogue => ("DROGUE", "F9", fg, fm.color()),
FlightMode::RecoveryMain => ("MAIN", "F10", fg, fm.color()),
FlightMode::Landed => ("LANDED", "F11", fg, fm.color()),
FlightMode::Idle => ("IDLE", fg, fm.color()),
FlightMode::HardwareArmed => ("HWARMED", fg, fm.color()),
FlightMode::Armed => ("ARMED", fg, fm.color()),
FlightMode::Flight => ("FLIGHT", fg, fm.color()),
FlightMode::RecoveryDrogue => ("DROGUE", fg, fm.color()),
FlightMode::RecoveryMain => ("MAIN", fg, fm.color()),
FlightMode::Landed => ("LANDED", fg, fm.color()),
}
}

Expand All @@ -47,6 +47,7 @@ pub trait TopBarUiExt {
fm: FlightMode,
current: Option<FlightMode>,
data_source: &mut dyn DataSource,
shortcut: Key,
);
fn flight_mode_buttons(&mut self, current: Option<FlightMode>, data_source: &mut dyn DataSource);
}
Expand Down Expand Up @@ -174,10 +175,15 @@ impl TopBarUiExt for egui::Ui {
fm: FlightMode,
current: Option<FlightMode>,
data_source: &mut dyn DataSource,
shortcut: Key,
) {
let (label, shortcut, fg, bg) = flight_mode_style(fm);
let (label, fg, bg) = flight_mode_style(fm);
let is_current = current.map(|c| c == fm).unwrap_or(false);

// We disable the serious flight modes until we are software armed
let is_armed = current.map(|c| c >= FlightMode::Armed).unwrap_or(false);
let is_enabled = (fm <= FlightMode::Armed) || is_armed;

let main_text = RichText::new(label).monospace();
let button = if is_current {
Button::new(main_text.color(fg)).wrap(true).fill(bg)
Expand All @@ -192,27 +198,33 @@ impl TopBarUiExt for egui::Ui {
let pos = self.next_widget_position();
let size = Vec2::new(self.available_width(), 50.0);

let shortcut = if is_current {
Label::new(RichText::new(format!("Shift+{}", shortcut)).size(9.0).color(fg))
let shortcut_text = if is_current {
Label::new(RichText::new(format!("Shift+{:?}", shortcut)).size(9.0).color(fg))
} else {
Label::new(RichText::new(format!("Shift+{}", shortcut)).size(9.0).weak())
Label::new(RichText::new(format!("Shift+{:?}", shortcut)).size(9.0).weak())
};

self.put(Rect::from_two_pos(pos + size * Vec2::new(0.0, 0.6), pos + size), shortcut);
if self.put(Rect::from_two_pos(pos, pos + size), button).clicked() {
self.add_enabled_ui(is_enabled, |ui| {
ui.put(Rect::from_two_pos(pos + size * Vec2::new(0.0, 0.6), pos + size), shortcut_text);
if ui.put(Rect::from_two_pos(pos, pos + size), button).clicked() && is_enabled {
data_source.send_command(Command::SetFlightMode(fm)).unwrap();
}
});

if self.ctx().input_mut(|i| i.consume_key(Modifiers::SHIFT, shortcut)) && is_enabled {
data_source.send_command(Command::SetFlightMode(fm)).unwrap();
}
}

fn flight_mode_buttons(&mut self, current: Option<FlightMode>, data_source: &mut dyn DataSource) {
self.columns(7, |columns| {
columns[0].flight_mode_button(FlightMode::Idle, current, data_source);
columns[1].flight_mode_button(FlightMode::HardwareArmed, current, data_source);
columns[2].flight_mode_button(FlightMode::Armed, current, data_source);
columns[3].flight_mode_button(FlightMode::Flight, current, data_source);
columns[4].flight_mode_button(FlightMode::RecoveryDrogue, current, data_source);
columns[5].flight_mode_button(FlightMode::RecoveryMain, current, data_source);
columns[6].flight_mode_button(FlightMode::Landed, current, data_source);
columns[0].flight_mode_button(FlightMode::Idle, current, data_source, Key::F5);
columns[1].flight_mode_button(FlightMode::HardwareArmed, current, data_source, Key::F6);
columns[2].flight_mode_button(FlightMode::Armed, current, data_source, Key::F7);
columns[3].flight_mode_button(FlightMode::Flight, current, data_source, Key::F8);
columns[4].flight_mode_button(FlightMode::RecoveryDrogue, current, data_source, Key::F9);
columns[5].flight_mode_button(FlightMode::RecoveryMain, current, data_source, Key::F10);
columns[6].flight_mode_button(FlightMode::Landed, current, data_source, Key::F11);
});
}
}

0 comments on commit a5c4eb9

Please sign in to comment.