Skip to content

Commit 0b68932

Browse files
committed
fix graph update interval bug and simplify repaint logic
1 parent f61b887 commit 0b68932

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

src/app/mod.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ pub struct MyApp {
156156
#[serde(skip)]
157157
poll_interval_shared: Arc<Mutex<u64>>, // Shared poll interval for live updates
158158
#[serde(skip)]
159-
graph_update_interval_shared: Arc<Mutex<u64>>, // Shared graph update interval
160-
#[serde(skip)]
161159
last_graph_update: f64, // Track last graph update time
162160
#[serde(skip)]
163161
last_hist_collect_time: f64, // Track last histogram collection time
@@ -248,14 +246,13 @@ impl Default for MyApp {
248246
lock_remote: true, // Default to locking remote mode
249247
value_debug_shared: Arc::new(Mutex::new(false)),
250248
poll_interval_shared: Arc::new(Mutex::new(20)),
251-
graph_update_interval_shared: Arc::new(Mutex::new(20)), // Default shared value to 20ms
252-
last_graph_update: 0.0, // Initialize to 0
253-
last_hist_collect_time: 0.0, // Initialize to 0
254-
connection_state: ConnectionState::Disconnected, // Initially disconnected
255-
connection_error: None, // No error initially
256-
meas_count: 0, // Initialize measurement counter
257-
last_record_time: 0.0, // Initialize last recording time
258-
graph_config: graph::GraphConfig::default(), // Default graph config
249+
last_graph_update: 0.0, // Initialize to 0
250+
last_hist_collect_time: 0.0, // Initialize to 0
251+
connection_state: ConnectionState::Disconnected, // Initially disconnected
252+
connection_error: None, // No error initially
253+
meas_count: 0, // Initialize measurement counter
254+
last_record_time: 0.0, // Initialize last recording time
255+
graph_config: graph::GraphConfig::default(), // Default graph config
259256
plot_dock_state: DockState::new(vec![]), // Initialize empty, populated in update
260257
cont_disable_unit_scaling: false,
261258
}
@@ -291,30 +288,15 @@ impl MyApp {
291288
let app: MyApp = eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
292289
*app.value_debug_shared.lock().unwrap() = app.value_debug;
293290
*app.poll_interval_shared.lock().unwrap() = app.poll_interval_ms;
294-
*app.graph_update_interval_shared.lock().unwrap() = app.graph_update_interval_ms;
295291
return app;
296292
}
297293

298294
let app = Self::default();
299295
*app.value_debug_shared.lock().unwrap() = app.value_debug;
300296
*app.poll_interval_shared.lock().unwrap() = app.poll_interval_ms;
301-
*app.graph_update_interval_shared.lock().unwrap() = app.graph_update_interval_ms;
302297
app
303298
}
304299

305-
fn spawn_graph_update_task(&mut self, ctx: Context) {
306-
let graph_update_interval_shared = self.graph_update_interval_shared.clone();
307-
let ctx = ctx.clone();
308-
309-
tokio::spawn(async move {
310-
loop {
311-
let interval = *graph_update_interval_shared.lock().unwrap();
312-
ctx.request_repaint(); // Trigger a repaint to update the graph
313-
tokio::time::sleep(Duration::from_millis(interval)).await;
314-
}
315-
});
316-
}
317-
318300
fn set_mode(
319301
&mut self,
320302
mode: MeterMode,

src/app/settings.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ impl super::MyApp {
147147
{
148148
self.graph_update_interval_ms =
149149
self.graph_update_interval_max;
150-
*self.graph_update_interval_shared.lock().unwrap() =
151-
self.graph_update_interval_max;
152150
}
153151
}
154152
}

src/app/ui.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl super::MyApp {
189189

190190
// Handle graph and histogram updates and recording based on the configured interval
191191
let current_time = ui.ctx().input(|i| i.time); // Get current time in seconds
192-
let graph_interval = *self.graph_update_interval_shared.lock().unwrap() as f64 / 1000.0; // Convert ms to seconds
192+
let graph_interval = self.graph_update_interval_ms as f64 / 1000.0; // Convert ms to seconds
193193
if current_time - self.last_graph_update >= graph_interval {
194194
if !self.curr_meas.is_nan() {
195195
self.values.push_back(self.curr_meas);
@@ -277,7 +277,6 @@ impl super::MyApp {
277277
self.connection_state =
278278
super::ConnectionState::Connected;
279279
self.spawn_serial_task();
280-
self.spawn_graph_update_task(ui.ctx().clone());
281280
}
282281
}
283282
Err(e) => {
@@ -727,6 +726,13 @@ impl super::MyApp {
727726
// Show settings and recording windows
728727
self.show_settings(ui.ctx());
729728
self.show_recording_window(ui);
729+
730+
// ensure repaint based on update intervals
731+
ui.ctx()
732+
.request_repaint_after(std::time::Duration::from_millis(std::cmp::min(
733+
self.graph_update_interval_ms,
734+
self.poll_interval_ms,
735+
)));
730736
});
731737
}
732738
}

0 commit comments

Comments
 (0)