Skip to content

Commit 5505201

Browse files
committed
enable autoscaling selection for all modes
1 parent 0b68932 commit 5505201

6 files changed

Lines changed: 80 additions & 54 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## 0.4.3
44

5-
- TODO: Fix bug that change interval of graph only applies after restart
5+
- fix bug that change interval of graph only applies after restart
6+
- make auto scaling of units and measurements selectable per mode
7+
- simplify repaint logic and ensure number box update is not tied to graph update
68
- upgrade to egui 0.34.1
79
- upgrade to rust 1.92
810
- use temporary version of egui-dropdown until PR is included to build against egui 0.34.1

src/app/mod.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use chrono::DateTime;
22
use serde::{Deserialize, Serialize};
33
use std::{
4-
collections::{BTreeMap, VecDeque},
4+
collections::{BTreeMap, HashMap, VecDeque},
55
sync::{Arc, Mutex},
6-
time::Duration,
76
};
87

9-
use egui::{Color32, Context, FontData, FontDefinitions, FontFamily};
8+
use egui::{Color32, FontData, FontDefinitions, FontFamily};
109
use egui_dock::DockState;
1110
use mio::{Events, Poll};
1211
use mio_serial::{SerialPortInfo, SerialStream};
@@ -56,6 +55,11 @@ pub struct Record {
5655
pub value: f64,
5756
}
5857

58+
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
59+
struct ModeDisplaySettings {
60+
pub auto_scale_units: bool, // true = use mV / mΩ / kΩ etc. (current default behavior)
61+
}
62+
5963
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
6064
#[derive(Serialize, Deserialize)]
6165
#[serde(default)] // if we add new fields, give them default values when deserializing old state
@@ -94,7 +98,7 @@ pub struct MyApp {
9498
recording_interval_ms: u64, // Persistent, fixed interval duration
9599
recording_active: bool, // Persistent, whether recording is active
96100
recording_timestamp_format: TimestampFormat, // Persistent, timestamp format
97-
cont_disable_unit_scaling: bool,
101+
mode_display_settings: HashMap<MeterMode, ModeDisplaySettings>,
98102
#[serde(skip)]
99103
recording_data: Vec<Record>, // Do not persist recording data
100104
#[serde(skip)]
@@ -254,7 +258,7 @@ impl Default for MyApp {
254258
last_record_time: 0.0, // Initialize last recording time
255259
graph_config: graph::GraphConfig::default(), // Default graph config
256260
plot_dock_state: DockState::new(vec![]), // Initialize empty, populated in update
257-
cont_disable_unit_scaling: false,
261+
mode_display_settings: HashMap::default(),
258262
}
259263
}
260264
}
@@ -384,4 +388,18 @@ impl MyApp {
384388
self.hist_values.clear(); // Clear histogram data
385389
self.meas_count = 0; // Reset measurement counter
386390
}
391+
392+
pub fn auto_scale_units(&self, mode: &MeterMode) -> bool {
393+
self.mode_display_settings
394+
.get(mode)
395+
.map_or(true, |s| s.auto_scale_units) // default = enabled (matches current behavior for all modes)
396+
}
397+
398+
pub fn set_auto_scale_units(&mut self, mode: MeterMode, enabled: bool) {
399+
self.mode_display_settings
400+
.entry(mode)
401+
.or_default()
402+
.auto_scale_units = enabled;
403+
// Optional: self.save_settings() if you have an immediate-save helper
404+
}
387405
}

src/app/settings.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ impl super::MyApp {
2323
self.value_debug = value_debug;
2424
*self.value_debug_shared.lock().unwrap() = value_debug;
2525
}
26-
ui.checkbox(&
27-
mut self.cont_disable_unit_scaling,
28-
"Disable unit scaling in CONT mode (always show raw Ω)"
29-
);
30-
ui.label("Useful when you prefer the exact resistance value without mΩ/kΩ/MΩ prefixes in continuity mode.");
3126
ui.label("Baud rate:");
3227
ui.add(
3328
TextEdit::singleline(&mut self.baud_rate.to_string())

src/app/ui.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl super::MyApp {
381381
1_000_000.0,
382382
0.0001,
383383
&self.metermode,
384-
self.cont_disable_unit_scaling,
384+
self.auto_scale_units(&self.metermode),
385385
);
386386
ui.label(
387387
egui::RichText::new(formatted_value)
@@ -688,6 +688,18 @@ impl super::MyApp {
688688
}
689689
}
690690
}
691+
692+
// scale control per mode
693+
let mut auto_scale = self.auto_scale_units(&self.metermode);
694+
if ui
695+
.checkbox(&mut auto_scale, "Auto-scale units")
696+
.on_hover_text(
697+
"Auto scale values and show prefixed units like mV/mΩ/kΩ",
698+
)
699+
.changed()
700+
{
701+
self.set_auto_scale_units(self.metermode.clone(), auto_scale);
702+
}
691703
});
692704
});
693705
});

src/helpers.rs

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn format_measurement(
66
sci_threshold_high: f64,
77
sci_threshold_low: f64,
88
meter_mode: &MeterMode,
9-
cont_disable_unit_scaling: bool,
9+
auto_scale_units: bool,
1010
) -> (String, String) {
1111
if value.is_nan() {
1212
return (" NaN".to_string(), "".to_string());
@@ -40,33 +40,31 @@ pub fn format_measurement(
4040
.to_string();
4141

4242
// Adjust value and unit based on mode and magnitude
43-
match meter_mode {
44-
MeterMode::Vdc | MeterMode::Vac => {
45-
if abs_value < 1.0 {
46-
display_value = value * 1000.0;
47-
display_unit = if matches!(meter_mode, MeterMode::Vdc) {
48-
"mVDC"
49-
} else {
50-
"mVAC"
43+
if auto_scale_units {
44+
match meter_mode {
45+
MeterMode::Vdc | MeterMode::Vac => {
46+
if abs_value < 1.0 {
47+
display_value = value * 1000.0;
48+
display_unit = if matches!(meter_mode, MeterMode::Vdc) {
49+
"mVDC"
50+
} else {
51+
"mVAC"
52+
}
53+
.to_string();
5154
}
52-
.to_string();
5355
}
54-
}
55-
MeterMode::Adc | MeterMode::Aac => {
56-
if abs_value < 1.0 {
57-
display_value = value * 1000.0;
58-
display_unit = if matches!(meter_mode, MeterMode::Adc) {
59-
"mADC"
60-
} else {
61-
"mAAC"
56+
MeterMode::Adc | MeterMode::Aac => {
57+
if abs_value < 1.0 {
58+
display_value = value * 1000.0;
59+
display_unit = if matches!(meter_mode, MeterMode::Adc) {
60+
"mADC"
61+
} else {
62+
"mAAC"
63+
}
64+
.to_string();
6265
}
63-
.to_string();
6466
}
65-
}
66-
MeterMode::Res | MeterMode::Cont => {
67-
if cont_disable_unit_scaling && *meter_mode == MeterMode::Cont {
68-
// skip formatting in this instance
69-
} else {
67+
MeterMode::Res | MeterMode::Cont => {
7068
if abs_value >= 1_000_000.0 {
7169
display_value = value / 1_000_000.0;
7270
display_unit = "MOhm".to_string();
@@ -78,26 +76,26 @@ pub fn format_measurement(
7876
display_unit = "mOhm".to_string();
7977
}
8078
}
81-
}
82-
MeterMode::Cap => {
83-
if abs_value >= 0.001 {
84-
display_value = value * 1000.0;
85-
display_unit = "mF".to_string();
86-
} else if abs_value >= 0.000_001 {
87-
display_value = value * 1_000_000.0;
88-
display_unit = "μF".to_string();
89-
} else if abs_value > 0.0 {
90-
display_value = value * 1_000_000_000.0;
91-
display_unit = "nF".to_string();
79+
MeterMode::Cap => {
80+
if abs_value >= 0.001 {
81+
display_value = value * 1000.0;
82+
display_unit = "mF".to_string();
83+
} else if abs_value >= 0.000_001 {
84+
display_value = value * 1_000_000.0;
85+
display_unit = "μF".to_string();
86+
} else if abs_value > 0.0 {
87+
display_value = value * 1_000_000_000.0;
88+
display_unit = "nF".to_string();
89+
}
9290
}
93-
}
94-
MeterMode::Per => {
95-
if abs_value < 1.0 {
96-
display_value = value * 1000.0;
97-
display_unit = "ms".to_string();
91+
MeterMode::Per => {
92+
if abs_value < 1.0 {
93+
display_value = value * 1000.0;
94+
display_unit = "ms".to_string();
95+
}
9896
}
97+
_ => {}
9998
}
100-
_ => {}
10199
}
102100

103101
let abs_display_value = display_value.abs();

src/multimeter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use phf::{OrderedMap, phf_ordered_map};
2+
use serde::{Deserialize, Serialize};
23

34
/// A trait that must be implemented for all SCPI command structs.
45
/// Gets passed the struct instance itself and the selected option name
@@ -14,7 +15,7 @@ pub enum ScpiMode {
1415
Meas,
1516
}
1617

17-
#[derive(PartialEq, Clone, Copy, Debug)]
18+
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, Serialize, Deserialize)]
1819
pub enum MeterMode {
1920
Vdc,
2021
Vac,

0 commit comments

Comments
 (0)