Skip to content

Commit

Permalink
Add NagSettings to SettingsUI
Browse files Browse the repository at this point in the history
  • Loading branch information
rnd-ash committed Apr 28, 2023
1 parent 945f51f commit dbd069f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 16 deletions.
59 changes: 59 additions & 0 deletions backend/src/diag/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ where
fn setting_name() -> &'static str;
fn get_revision_name() -> &'static str;
fn get_scn_id() -> u8;
fn effect_immediate() -> bool {
true
}
}

#[derive(Default, Debug, Copy, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
Expand Down Expand Up @@ -197,3 +200,59 @@ impl TcuSettings for SbsSettings {
0x03
}
}

#[derive(Default, Debug, Copy, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[repr(C, packed)]
pub struct GearboxInfo {
max_torque: u16,
ratio_1: f32,
ratio_2: f32,
ratio_3: f32,
ratio_4: f32,
ratio_5: f32,
ratio_r1: f32,
ratio_r2: f32,
power_loss_1: u8,
power_loss_2: u8,
power_loss_3: u8,
power_loss_4: u8,
power_loss_5: u8,
power_loss_r1: u8,
power_loss_r2: u8,
}

#[derive(Default, Debug, Copy, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[repr(C, packed)]
pub struct NagSettings {
max_drift_1: u8,
max_drift_2: u8,
max_drift_3: u8,
max_drift_4: u8,
max_drift_5: u8,
max_drift_r1: u8,
max_drift_r2: u8,
small_nag: GearboxInfo,
large_nag: GearboxInfo,
}

impl TcuSettings for NagSettings {
fn wiki_url() -> Option<&'static str> {
None
}

fn setting_name() -> &'static str {
"NAG Settings"
}

fn get_revision_name() -> &'static str {
"A0 (28/04/23)"
}

fn get_scn_id() -> u8 {
0x04
}

fn effect_immediate() -> bool {
false
}
}
52 changes: 36 additions & 16 deletions config_app/src/ui/settings_ui_gen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{sync::{atomic::AtomicBool, Arc, RwLock}, borrow::Borrow, time::{Instant, Duration}, ops::RangeInclusive, fs::File, io::{Write, Read}};

use backend::{diag::{settings::{TcuSettings, TccSettings, unpack_settings, LinearInterpSettings, pack_settings, SolSettings, SbsSettings}, Nag52Diag}, ecu_diagnostics::{kwp2000::{KwpSessionType, KwpCommand}, DiagServerResult}, serde_yaml::{Value, Mapping, self}};
use backend::{diag::{settings::{TcuSettings, TccSettings, unpack_settings, LinearInterpSettings, pack_settings, SolSettings, SbsSettings, NagSettings}, Nag52Diag}, ecu_diagnostics::{kwp2000::{KwpSessionType, KwpCommand}, DiagServerResult}, serde_yaml::{Value, Mapping, self}};
use eframe::egui::{ProgressBar, DragValue, self, CollapsingHeader, plot::{PlotPoints, Line, Plot}, ScrollArea, Window, TextEdit, TextBuffer, Layout, Label};
use nfd::Response;
use serde::{Serialize, Deserialize, de::DeserializeOwned};
Expand All @@ -25,6 +25,7 @@ pub struct TcuAdvSettingsUi {
tcc_settings: TcuSettingsWrapper<TccSettings>,
sol_settings: TcuSettingsWrapper<SolSettings>,
sbs_settings: TcuSettingsWrapper<SbsSettings>,
nag_settings: TcuSettingsWrapper<NagSettings>,
}

pub fn make_tcu_settings_wrapper<T>() -> (TcuSettingsWrapper<T>, TcuSettingsWrapper<T>) where T: Default {
Expand Down Expand Up @@ -58,6 +59,7 @@ impl TcuAdvSettingsUi {
let (tcc, tcc_t) = make_tcu_settings_wrapper::<TccSettings>();
let (sol, sol_t) = make_tcu_settings_wrapper::<SolSettings>();
let (sbs, sbs_t) = make_tcu_settings_wrapper::<SbsSettings>();
let (gbs, gbs_t) = make_tcu_settings_wrapper::<NagSettings>();
let nag_c = nag.clone();
std::thread::spawn(move|| {
let res = nag_c.with_kwp(|x| {
Expand All @@ -77,6 +79,7 @@ impl TcuAdvSettingsUi {
read_scn_settings(&nag_c, &tcc_t);
read_scn_settings(&nag_c, &sol_t);
read_scn_settings(&nag_c, &sbs_t);
read_scn_settings(&nag_c, &gbs_t);
*is_ready_t.write().unwrap() = PageLoadState::Ok;
});
Self {
Expand All @@ -85,7 +88,8 @@ impl TcuAdvSettingsUi {
start_time: Instant::now(),
tcc_settings: tcc,
sol_settings: sol,
sbs_settings: sbs
sbs_settings: sbs,
nag_settings: gbs
}
}
}
Expand All @@ -96,7 +100,7 @@ where T: Clone + Copy + Serialize + DeserializeOwned {
let mut action = None;
let setting_state = settings_ref.read().unwrap().clone();
if let SettingState::LoadOk(mut settings) = setting_state {
Window::new(T::setting_name()).min_width(300.0).resizable(false).show(ui.ctx(), |ui| {
Window::new(T::setting_name()).min_width(300.0).resizable(true).collapsible(true).show(ui.ctx(), |ui| {
ui.with_layout(Layout::top_down(eframe::emath::Align::Min), |ui| {
ui.label(format!("Setting revision name: {}", T::get_revision_name()));
if let Some(url) = T::wiki_url() {
Expand Down Expand Up @@ -129,10 +133,17 @@ where T: Clone + Copy + Serialize + DeserializeOwned {
});
match res {
Ok(_) => {
action = Some(PageAction::SendNotification {
text: format!("{} write OK!", T::setting_name()),
kind: egui_toast::ToastKind::Success
})
if T::effect_immediate() {
action = Some(PageAction::SendNotification {
text: format!("{} write OK!", T::setting_name()),
kind: egui_toast::ToastKind::Success
});
} else {
action = Some(PageAction::SendNotification {
text: format!("{} write OK, but changes are only applied after a restart!", T::setting_name()),
kind: egui_toast::ToastKind::Warning
});
}
},
Err(e) => {
action = Some(PageAction::SendNotification {
Expand All @@ -148,10 +159,17 @@ where T: Clone + Copy + Serialize + DeserializeOwned {
});
match res {
Ok(_) => {
action = Some(PageAction::SendNotification {
text: format!("{} reset OK!", T::setting_name()),
kind: egui_toast::ToastKind::Success
});
if T::effect_immediate() {
action = Some(PageAction::SendNotification {
text: format!("{} reset OK!", T::setting_name()),
kind: egui_toast::ToastKind::Success
});
} else {
action = Some(PageAction::SendNotification {
text: format!("{} reset OK, but changes are only applied after a restart!", T::setting_name()),
kind: egui_toast::ToastKind::Warning
});
}
if let Ok(x) = nag.with_kwp(|kwp| kwp.send_byte_array_with_response(&[0x21, 0xFC, T::get_scn_id()])) {
if let Ok(res) = unpack_settings(T::get_scn_id(), &x[2..]) {
settings = res;
Expand Down Expand Up @@ -245,6 +263,9 @@ impl InterfacePage for TcuAdvSettingsUi {
if action.is_none() {
action = make_settings_window(&self.nag, &self.sbs_settings, ui);
}
if action.is_none() {
action = make_settings_window(&self.nag, &self.nag_settings, ui);
}
if let Some(act) = action {
act
} else {
Expand Down Expand Up @@ -278,7 +299,7 @@ fn make_ui_for_mapping(setting_name: &'static str, v: &mut Mapping, ui: &mut egu
.striped(true)
.min_col_width(100.0)
.show(ui, |ui| {
ui.heading("Key name");
ui.heading("Variable");
ui.heading("Value");
ui.end_row();
for (i, v) in v.iter_mut() {
Expand Down Expand Up @@ -311,26 +332,25 @@ fn make_ui_for_mapping(setting_name: &'static str, v: &mut Mapping, ui: &mut egu
.show(sub, |p| {
p.line(line)
});

}
make_ui_for_mapping(setting_name,&mut v.as_mapping_mut().unwrap(), sub);
});
ui.end_row();
} else if v.is_bool() {
ui.label(key);
ui.code(format!("{key}"));
let mut o = v.as_bool().unwrap();
ui.checkbox(&mut o, "");
*v = Value::from(o);
ui.end_row();
} else if v.is_f64() {
ui.label(format!("{key}: "));
ui.code(format!("{key}: "));
let mut o = v.as_f64().unwrap();
let d = DragValue::new(&mut o).max_decimals(3).speed(0);
ui.add(d);
*v = Value::from(o);
ui.end_row();
} else if v.is_u64(){
ui.label(format!("{key}: "));
ui.code(format!("{key}: "));
let mut o = v.as_u64().unwrap();
let d = DragValue::new(&mut o).max_decimals(0).speed(0).clamp_range(RangeInclusive::new(0, i32::MAX));
ui.add(d);
Expand Down

0 comments on commit dbd069f

Please sign in to comment.