Skip to content

Commit 7cf0db2

Browse files
committed
switched to crossbeam-channel and removed a lot of clone calls
1 parent e6a7301 commit 7cf0db2

File tree

7 files changed

+229
-139
lines changed

7 files changed

+229
-139
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes to the `Serial Monitor` crate will be documented in this fil
44

55
# Unreleased 0.4.x
66

7+
* Switched to `crossbeam-channel` for more efficient channel routing
8+
* removed many `.clone()` calls to reduce CPU load
79
* Fixed sample rate / disconnect issue
810
* Releases are now linked to libssl 3.4.1 on linux (built on Ubuntu 22.04)
911

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ self_update = { git = "https://github.com/hacknus/self_update", features = ["arc
2727
tempfile = { version = "3.15", optional = true }
2828
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls", "http2"], optional = true }
2929
semver = { version = "1.0.24", optional = true }
30+
crossbeam-channel = "0.5.14"
3031

3132
[features]
3233
self_update = ["dep:self_update", "tempfile", "reqwest", "semver"]

src/gui.rs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use core::f32;
2+
use crossbeam_channel::{Receiver, Sender};
23
use std::cmp::max;
34
use std::ops::RangeInclusive;
45
use std::path::PathBuf;
5-
use std::sync::mpsc::{Receiver, Sender};
66
use std::sync::{Arc, RwLock};
77
use std::time::Duration;
88

@@ -64,6 +64,13 @@ pub enum WindowFeedback {
6464
Cancel,
6565
}
6666

67+
#[derive(Clone)]
68+
pub enum GuiCommand {
69+
Clear,
70+
ShowTimestamps(bool),
71+
ShowSentTraffic(bool),
72+
}
73+
6774
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
6875
pub struct GuiSettingsContainer {
6976
pub device: String,
@@ -134,7 +141,7 @@ pub struct MyApp {
134141
load_tx: Sender<PathBuf>,
135142
load_names_rx: Receiver<Vec<String>>,
136143
send_tx: Sender<String>,
137-
clear_tx: Sender<bool>,
144+
gui_cmd_tx: Sender<GuiCommand>,
138145
history: Vec<String>,
139146
index: usize,
140147
eol: String,
@@ -166,7 +173,7 @@ impl MyApp {
166173
load_tx: Sender<PathBuf>,
167174
load_names_rx: Receiver<Vec<String>>,
168175
send_tx: Sender<String>,
169-
clear_tx: Sender<bool>,
176+
gui_cmd_tx: Sender<GuiCommand>,
170177
) -> Self {
171178
let mut file_dialog = FileDialog::default()
172179
//.initial_directory(PathBuf::from("/path/to/app"))
@@ -227,7 +234,7 @@ impl MyApp {
227234
load_tx,
228235
load_names_rx,
229236
send_tx,
230-
clear_tx,
237+
gui_cmd_tx,
231238
plotting_range: usize::MAX,
232239
plot_serial_display_ratio: 0.45,
233240
command: "".to_string(),
@@ -616,8 +623,8 @@ impl MyApp {
616623
self.device_idx = self.serial_devices.devices.len() - 1;
617624
save_serial_settings(&self.serial_devices);
618625
}
619-
self.clear_tx
620-
.send(true)
626+
self.gui_cmd_tx
627+
.send(GuiCommand::Clear)
621628
.expect("failed to send clear after choosing new device");
622629
// need to clear the data here such that we don't get errors in the gui (plot)
623630
self.data = GuiOutputDataContainer::default();
@@ -913,7 +920,7 @@ impl MyApp {
913920
|| ui.input_mut(|i| i.consume_shortcut(&CLEAR_PLOT_SHORTCUT))
914921
{
915922
log::info!("Cleared recorded Data");
916-
if let Err(err) = self.clear_tx.send(true) {
923+
if let Err(err) = self.gui_cmd_tx.send(GuiCommand::Clear) {
917924
log::error!("clear_tx thread send failed: {:?}", err);
918925
}
919926
// need to clear the data here in order to prevent errors in the gui (plot)
@@ -934,14 +941,34 @@ impl MyApp {
934941
});
935942
ui.add_space(5.0);
936943
ui.horizontal(|ui| {
937-
ui.add(toggle(&mut self.show_sent_cmds))
938-
.on_hover_text("Show sent commands in console.");
944+
if ui
945+
.add(toggle(&mut self.show_sent_cmds))
946+
.on_hover_text("Show sent commands in console.")
947+
.changed()
948+
{
949+
if let Err(err) = self
950+
.gui_cmd_tx
951+
.send(GuiCommand::ShowSentTraffic(self.show_sent_cmds))
952+
{
953+
log::error!("clear_tx thread send failed: {:?}", err);
954+
}
955+
}
939956
ui.label("Show Sent Commands");
940957
});
941958
ui.add_space(5.0);
942959
ui.horizontal(|ui| {
943-
ui.add(toggle(&mut self.show_timestamps))
944-
.on_hover_text("Show timestamp in console.");
960+
if ui
961+
.add(toggle(&mut self.show_timestamps))
962+
.on_hover_text("Show timestamp in console.")
963+
.changed()
964+
{
965+
if let Err(err) = self
966+
.gui_cmd_tx
967+
.send(GuiCommand::ShowTimestamps(self.show_sent_cmds))
968+
{
969+
log::error!("clear_tx thread send failed: {:?}", err);
970+
}
971+
}
945972
ui.label("Show Timestamp");
946973
});
947974
ui.add_space(5.0);

src/io.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct FileOptions {
1717
pub fn open_from_csv(
1818
data: &mut DataContainer,
1919
csv_options: &mut FileOptions,
20-
) -> Result<(), Box<dyn Error>> {
20+
) -> Result<Vec<String>, Box<dyn Error>> {
2121
let mut rdr = ReaderBuilder::new()
2222
.has_headers(true)
2323
.from_path(&csv_options.file_path)?;
@@ -35,6 +35,8 @@ pub fn open_from_csv(
3535
data.time.clear();
3636
data.dataset = vec![vec![]; csv_options.names.len()];
3737

38+
let mut raw_data = vec![];
39+
3840
// Read and parse each record in the CSV
3941
for result in rdr.records() {
4042
let record = result?;
@@ -60,11 +62,14 @@ pub fn open_from_csv(
6062
return Err("Unexpected number of data columns in the CSV".into());
6163
}
6264
}
65+
// Join the row into a single string with ", " as delimiter and push to raw_data
66+
let row = record.iter().collect::<Vec<_>>().join(", ");
67+
raw_data.push(row + "\n");
6368
}
6469

6570
data.loaded_from_file = true;
6671

67-
Ok(())
72+
Ok(raw_data)
6873
}
6974

7075
pub fn save_to_csv(data: &DataContainer, csv_options: &FileOptions) -> Result<(), Box<dyn Error>> {

0 commit comments

Comments
 (0)