Skip to content

Commit e6a7301

Browse files
committed
decrease amount of clone
1 parent b4d3370 commit e6a7301

File tree

4 files changed

+135
-67
lines changed

4 files changed

+135
-67
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,7 @@ dbg-build = false
5151
dbg-name = false
5252
name = "Serial Monitor"
5353
no-build = false
54-
output = "target/wix/SerialMonitorInstaller.msi"
54+
output = "target/wix/SerialMonitorInstaller.msi"
55+
56+
[profile.release]
57+
debug = true

src/data.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use egui_plot::PlotPoint;
12
use std::fmt;
23
use std::time::{SystemTime, UNIX_EPOCH};
34

@@ -62,3 +63,9 @@ impl Default for DataContainer {
6263
}
6364
}
6465
}
66+
67+
#[derive(Clone, Debug, Default)]
68+
pub struct GuiOutputDataContainer {
69+
pub prints: Vec<String>,
70+
pub plots: Vec<(String, Vec<PlotPoint>)>,
71+
}

src/gui.rs

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::time::Duration;
88

99
use crate::color_picker::{color_picker_widget, color_picker_window, COLORS};
1010
use crate::custom_highlighter::highlight_impl;
11-
use crate::data::{DataContainer, SerialDirection};
11+
use crate::data::GuiOutputDataContainer;
1212
use crate::serial::{clear_serial_settings, save_serial_settings, Device, SerialDevices};
1313
use crate::settings_window::settings_window;
1414
use crate::toggle::toggle;
@@ -24,7 +24,7 @@ use eframe::{egui, Storage};
2424
use egui::ThemePreference;
2525
use egui_file_dialog::information_panel::InformationPanel;
2626
use egui_file_dialog::FileDialog;
27-
use egui_plot::{log_grid_spacer, GridMark, Legend, Line, Plot, PlotPoint, PlotPoints};
27+
use egui_plot::{log_grid_spacer, GridMark, Legend, Line, Plot, PlotPoints};
2828
use preferences::Preferences;
2929
#[cfg(feature = "self_update")]
3030
use self_update::update::Release;
@@ -118,7 +118,7 @@ pub struct MyApp {
118118
plot_serial_display_ratio: f32,
119119
picked_path: PathBuf,
120120
plot_location: Option<egui::Rect>,
121-
data: DataContainer,
121+
data: GuiOutputDataContainer,
122122
file_dialog_state: FileDialogState,
123123
file_dialog: FileDialog,
124124
information_panel: InformationPanel,
@@ -129,7 +129,7 @@ pub struct MyApp {
129129
device_lock: Arc<RwLock<Device>>,
130130
devices_lock: Arc<RwLock<Vec<String>>>,
131131
connected_lock: Arc<RwLock<bool>>,
132-
data_lock: Arc<RwLock<DataContainer>>,
132+
data_lock: Arc<RwLock<GuiOutputDataContainer>>,
133133
save_tx: Sender<FileOptions>,
134134
load_tx: Sender<PathBuf>,
135135
load_names_rx: Receiver<Vec<String>>,
@@ -156,7 +156,7 @@ pub struct MyApp {
156156
impl MyApp {
157157
pub fn new(
158158
cc: &eframe::CreationContext,
159-
data_lock: Arc<RwLock<DataContainer>>,
159+
data_lock: Arc<RwLock<GuiOutputDataContainer>>,
160160
device_lock: Arc<RwLock<Device>>,
161161
devices_lock: Arc<RwLock<Vec<String>>>,
162162
devices: SerialDevices,
@@ -203,7 +203,7 @@ impl MyApp {
203203
picked_path: PathBuf::new(),
204204
device: "".to_string(),
205205
old_device: "".to_string(),
206-
data: DataContainer::default(),
206+
data: GuiOutputDataContainer::default(),
207207
file_dialog_state: FileDialogState::None,
208208
file_dialog,
209209
information_panel: InformationPanel::default().add_file_preview("csv", |ui, item| {
@@ -283,25 +283,6 @@ impl MyApp {
283283
window_feedback
284284
}
285285

286-
fn console_text(&self, packet: &crate::data::Packet) -> Option<String> {
287-
match (self.show_sent_cmds, self.show_timestamps, &packet.direction) {
288-
(true, true, _) => Some(format!(
289-
"[{}] t + {:.3}s: {}\n",
290-
packet.direction,
291-
packet.relative_time as f32 / 1000.0,
292-
packet.payload
293-
)),
294-
(true, false, _) => Some(format!("[{}]: {}\n", packet.direction, packet.payload)),
295-
(false, true, SerialDirection::Receive) => Some(format!(
296-
"t + {:.3}s: {}\n",
297-
packet.relative_time as f32 / 1000.0,
298-
packet.payload
299-
)),
300-
(false, false, SerialDirection::Receive) => Some(packet.payload.clone() + "\n"),
301-
(_, _, _) => None,
302-
}
303-
}
304-
305286
fn draw_central_panel(&mut self, ctx: &egui::Context) {
306287
egui::CentralPanel::default().show(ctx, |ui| {
307288
let left_border = 10.0;
@@ -327,11 +308,17 @@ impl MyApp {
327308
ui.horizontal(|ui| {
328309
ui.add_space(left_border);
329310
ui.vertical(|ui| {
330-
if let Ok(read_guard) = self.data_lock.read() {
331-
self.data = read_guard.clone();
311+
if let Ok(gui_data) = self.data_lock.read() {
312+
self.data = gui_data.clone();
313+
self.labels = gui_data.plots.iter().map(|d| d.0.clone()).collect();
314+
self.colors = (0..max(self.labels.len(), 1))
315+
.map(|i| COLORS[i % COLORS.len()])
316+
.collect();
317+
self.color_vals = (0..max(self.data.plots.len(), 1)).map(|_| 0.0).collect();
332318
}
333319

334-
if self.data.loaded_from_file && self.file_opened {
320+
// TODO what about self.data.loaded_from_file
321+
if self.file_opened {
335322
if let Ok(labels) = self.load_names_rx.try_recv() {
336323
self.labels = labels;
337324
self.colors = (0..max(self.labels.len(), 1))
@@ -341,32 +328,40 @@ impl MyApp {
341328
}
342329
}
343330
if self.serial_devices.number_of_plots[self.device_idx] > 0 {
344-
if self.data.dataset.len() != self.labels.len() && !self.file_opened {
345-
self.labels = (0..max(self.data.dataset.len(), 1))
346-
.map(|i| format!("Column {i}"))
347-
.collect();
348-
self.colors = (0..max(self.data.dataset.len(), 1))
331+
if self.data.plots.len() != self.labels.len() && !self.file_opened {
332+
// self.labels = (0..max(self.data.dataset.len(), 1))
333+
// .map(|i| format!("Column {i}"))
334+
// .collect();
335+
self.colors = (0..max(self.data.plots.len(), 1))
349336
.map(|i| COLORS[i % COLORS.len()])
350337
.collect();
351338
self.color_vals =
352-
(0..max(self.data.dataset.len(), 1)).map(|_| 0.0).collect();
339+
(0..max(self.data.plots.len(), 1)).map(|_| 0.0).collect();
353340
}
354341

355-
let mut graphs: Vec<Vec<PlotPoint>> = vec![vec![]; self.data.dataset.len()];
356-
let window = self.data.dataset[0]
357-
.len()
358-
.saturating_sub(self.plotting_range);
359-
360-
for (i, time) in self.data.time[window..].iter().enumerate() {
361-
let x = *time / 1000.0;
362-
for (graph, data) in graphs.iter_mut().zip(&self.data.dataset) {
363-
if self.data.time.len() == data.len() {
364-
if let Some(y) = data.get(i + window) {
365-
graph.push(PlotPoint { x, y: *y as f64 });
366-
}
367-
}
368-
}
369-
}
342+
// offloaded to main thread
343+
344+
// let mut graphs: Vec<Vec<PlotPoint>> = vec![vec![]; self.data.dataset.len()];
345+
// let window = self.data.dataset[0]
346+
// .len()
347+
// .saturating_sub(self.plotting_range);
348+
//
349+
// for (i, time) in self.data.time[window..].iter().enumerate() {
350+
// let x = *time / 1000.0;
351+
// for (graph, data) in graphs.iter_mut().zip(&self.data.dataset) {
352+
// if self.data.time.len() == data.len() {
353+
// if let Some(y) = data.get(i + window) {
354+
// graph.push(PlotPoint { x, y: *y as f64 });
355+
// }
356+
// }
357+
// }
358+
// }
359+
360+
let window = if let Some(first_entry) = self.data.plots.first() {
361+
first_entry.1.len().saturating_sub(self.plotting_range)
362+
} else {
363+
0
364+
};
370365

371366
let t_fmt = |x: GridMark, _range: &RangeInclusive<f64>| {
372367
format!("{:4.2} s", x.value)
@@ -388,13 +383,15 @@ impl MyApp {
388383
.x_axis_formatter(t_fmt);
389384

390385
let plot_inner = signal_plot.show(ui, |signal_plot_ui| {
391-
for (i, graph) in graphs.iter().enumerate() {
386+
for (i, (_label, graph)) in self.data.plots.iter().enumerate() {
392387
// this check needs to be here for when we change devices (not very elegant)
393388
if i < self.labels.len() {
394389
signal_plot_ui.line(
395-
Line::new(PlotPoints::Owned(graph.to_vec()))
396-
.name(&self.labels[i])
397-
.color(self.colors[i]),
390+
Line::new(PlotPoints::Owned(
391+
graph[window..].to_vec(),
392+
))
393+
.name(&self.labels[i])
394+
.color(self.colors[i]),
398395
);
399396
}
400397
}
@@ -430,7 +427,7 @@ impl MyApp {
430427
let serial_height =
431428
panel_height - plot_ui_heigh - left_border * 2.0 - top_spacing;
432429

433-
let num_rows = self.data.raw_traffic.len();
430+
let num_rows = self.data.prints.len();
434431
let row_height = ui.text_style_height(&egui::TextStyle::Body);
435432

436433
let color = if self.gui_conf.dark_mode {
@@ -453,10 +450,10 @@ impl MyApp {
453450
let content: String = row_range
454451
.into_iter()
455452
.flat_map(|i| {
456-
if self.data.raw_traffic.is_empty() {
453+
if self.data.prints.is_empty() {
457454
None
458455
} else {
459-
self.console_text(&self.data.raw_traffic[i])
456+
Some(self.data.prints[i].clone())
460457
}
461458
})
462459
.collect();
@@ -578,7 +575,7 @@ impl MyApp {
578575
// let selected_new_device = response.changed(); //somehow this does not work
579576
// if selected_new_device {
580577
if old_name != self.device {
581-
if !self.data.time.is_empty() {
578+
if !self.data.prints.is_empty() {
582579
self.show_warning_window = WindowFeedback::Waiting;
583580
self.old_device = old_name;
584581
} else {
@@ -623,7 +620,7 @@ impl MyApp {
623620
.send(true)
624621
.expect("failed to send clear after choosing new device");
625622
// need to clear the data here such that we don't get errors in the gui (plot)
626-
self.data = DataContainer::default();
623+
self.data = GuiOutputDataContainer::default();
627624
self.show_warning_window = WindowFeedback::None;
628625
}
629626
WindowFeedback::Cancel => {
@@ -920,7 +917,7 @@ impl MyApp {
920917
log::error!("clear_tx thread send failed: {:?}", err);
921918
}
922919
// need to clear the data here in order to prevent errors in the gui (plot)
923-
self.data = DataContainer::default();
920+
self.data = GuiOutputDataContainer::default();
924921
// self.names_tx.send(self.serial_devices.labels[self.device_idx].clone()).expect("Failed to send names");
925922
}
926923
ui.add_space(5.0);

0 commit comments

Comments
 (0)