Skip to content

Commit 6e44586

Browse files
authored
Merge pull request #124 from hacknus/performance_increase_2
Performance increase 2
2 parents 8f7970d + 56a2c27 commit 6e44586

File tree

4 files changed

+67
-31
lines changed

4 files changed

+67
-31
lines changed

src/gui.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,18 @@ impl MyApp {
12201220
egui_logger::logger_ui().show(ui);
12211221
});
12221222

1223+
ctx.input(|i| {
1224+
// Check if files were dropped
1225+
if let Some(dropped_file) = i.raw.dropped_files.last() {
1226+
let path = dropped_file.clone().path.unwrap();
1227+
self.picked_path = path.to_path_buf();
1228+
self.file_opened = true;
1229+
if let Err(e) = self.load_tx.send(self.picked_path.clone()) {
1230+
log::error!("load_tx thread send failed: {:?}", e);
1231+
}
1232+
}
1233+
});
1234+
12231235
match self.file_dialog_state {
12241236
FileDialogState::Open => {
12251237
if let Some(path) = self

src/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn open_from_csv(
5757
// Parse the remaining columns and populate the dataset
5858
for (i, value) in record.iter().skip(1).enumerate() {
5959
if let Some(dataset_column) = data.dataset.get_mut(i) {
60-
dataset_column.push(value.parse()?);
60+
dataset_column.push(value.trim().parse().unwrap_or(0.0));
6161
} else {
6262
return Err("Unexpected number of data columns in the CSV".into());
6363
}

src/main.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern crate serde;
88
use crate::data::{DataContainer, GuiOutputDataContainer, Packet, SerialDirection};
99
use crate::gui::{load_gui_settings, GuiCommand, MyApp, RIGHT_PANEL_WIDTH};
1010
use crate::io::{open_from_csv, save_to_csv, FileOptions};
11-
use crate::serial::{load_serial_settings, serial_thread, Device};
11+
use crate::serial::{load_serial_settings, serial_devices_thread, serial_thread, Device};
1212
use crossbeam_channel::{select, Receiver, Sender};
1313
use eframe::egui::{vec2, ViewportBuilder, Visuals};
1414
use eframe::{egui, icon_data};
@@ -96,19 +96,18 @@ fn main_thread(
9696
sync_tx.send(true).expect("unable to send sync tx");
9797
data.raw_traffic.push(packet.clone());
9898

99-
if let Ok(mut gui_data) = data_lock.write() {
100-
if let Some(text) = console_text(show_timestamps, show_sent_cmds, &packet) {
101-
// append prints
102-
gui_data.prints.push(text);
103-
}
104-
}
99+
let text = console_text(show_timestamps, show_sent_cmds, &packet);
105100

106101
let split_data = split(&packet.payload);
107102
if data.dataset.is_empty() || failed_format_counter > 10 {
108103
// resetting dataset
109104
data.time = vec![];
110105
data.dataset = vec![vec![]; max(split_data.len(), 1)];
111106
if let Ok(mut gui_data) = data_lock.write() {
107+
// append prints
108+
if let Some(text) = text {
109+
gui_data.prints.push(text);
110+
}
112111
gui_data.plots = (0..max(split_data.len(), 1))
113112
.map(|i| (format!("Column {i}"), vec![]))
114113
.collect();
@@ -127,6 +126,10 @@ fn main_thread(
127126

128127
// appending data for GUI thread
129128
if let Ok(mut gui_data) = data_lock.write() {
129+
// append prints
130+
if let Some(text) = text {
131+
gui_data.prints.push(text);
132+
}
130133
// append plot-points
131134
for ((_label, graph), data_i) in
132135
gui_data.plots.iter_mut().zip(&data.dataset)
@@ -200,8 +203,6 @@ fn main_thread(
200203

201204
gui_data.prints = raw_data;
202205

203-
dbg!(&gui_data.prints);
204-
205206
gui_data.plots = (0..data.dataset.len())
206207
.map(|i| (file_options.names[i].to_string(), vec![]))
207208
.collect();
@@ -257,7 +258,7 @@ fn main_thread(
257258
}
258259
}
259260
}
260-
default(Duration::from_millis(10)) => {
261+
default(Duration::from_millis(1)) => {
261262
// occasionally push data to GUI
262263
}
263264
}
@@ -287,6 +288,12 @@ fn main() {
287288
crossbeam_channel::unbounded();
288289
let (sync_tx, sync_rx): (Sender<bool>, Receiver<bool>) = crossbeam_channel::unbounded();
289290

291+
let serial_2_devices_lock = devices_lock.clone();
292+
293+
let _serial_devices_thread_handler = thread::spawn(|| {
294+
serial_devices_thread(serial_2_devices_lock);
295+
});
296+
290297
let serial_device_lock = device_lock.clone();
291298
let serial_devices_lock = devices_lock.clone();
292299
let serial_connected_lock = connected_lock.clone();

src/serial.rs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ impl Default for Device {
8989
}
9090
}
9191

92+
pub fn serial_devices_thread(devices_lock: Arc<RwLock<Vec<String>>>) {
93+
loop {
94+
if let Ok(mut write_guard) = devices_lock.write() {
95+
*write_guard = available_devices();
96+
}
97+
std::thread::sleep(Duration::from_millis(500));
98+
}
99+
}
100+
92101
fn serial_write(
93102
port: &mut BufReader<Box<dyn SerialPort>>,
94103
cmd: &[u8],
@@ -165,19 +174,17 @@ pub fn serial_thread(
165174
.create();
166175

167176
'connected_loop: loop {
168-
let devices = available_devices();
169-
if let Ok(mut write_guard) = devices_lock.write() {
170-
*write_guard = devices.clone();
171-
}
172-
173-
if disconnected(&device, &devices, &device_lock, &mut last_connected_device) {
177+
if disconnected(
178+
&device,
179+
&device_lock,
180+
&devices_lock,
181+
&mut last_connected_device,
182+
) {
174183
break 'connected_loop;
175184
}
176185

177186
perform_writes(&mut port, &send_rx, &raw_data_tx, t_zero);
178187
perform_reads(&mut port, &raw_data_tx, t_zero);
179-
180-
//std::thread::sleep(Duration::from_millis(10));
181188
}
182189
std::mem::drop(port);
183190
}
@@ -222,28 +229,30 @@ fn get_device(
222229

223230
fn disconnected(
224231
device: &Device,
225-
devices: &[String],
226232
device_lock: &Arc<RwLock<Device>>,
233+
devices_lock: &Arc<RwLock<Vec<String>>>,
227234
last_connected_device: &mut Device,
228235
) -> bool {
229236
// disconnection by button press
230-
if let Ok(read_guard) = device_lock.read() {
237+
if let Ok(read_guard) = device_lock.try_read() {
231238
if device.name != read_guard.name {
232239
*last_connected_device = Device::default();
233240
log::info!("Disconnected from serial port: {}", device.name);
234241
return true;
235242
}
236243
}
237244

238-
// other types of disconnection (e.g. unplugging, power down)
239-
if !devices.contains(&device.name) {
240-
if let Ok(mut write_guard) = device_lock.write() {
241-
write_guard.name.clear();
242-
}
243-
*last_connected_device = device.clone();
244-
log::error!("Device has disconnected from serial port: {}", device.name);
245-
return true;
246-
};
245+
if let Ok(devices) = devices_lock.try_read() {
246+
// other types of disconnection (e.g. unplugging, power down)
247+
if !devices.contains(&device.name) {
248+
if let Ok(mut write_guard) = device_lock.try_write() {
249+
write_guard.name.clear();
250+
}
251+
*last_connected_device = device.clone();
252+
log::error!("Device has disconnected from serial port: {}", device.name);
253+
return true;
254+
};
255+
}
247256
false
248257
}
249258

@@ -279,7 +288,15 @@ fn perform_reads(
279288
let mut buf = "".to_string();
280289
match serial_read(port, &mut buf) {
281290
Ok(_) => {
282-
let delimiter = if buf.contains("\r\n") { "\r\n" } else { "\0\0" };
291+
let delimiter = if buf.contains("\r\n") {
292+
"\r\n"
293+
} else if buf.contains("\r") {
294+
"\r"
295+
} else if buf.contains("\n") {
296+
"\n"
297+
} else {
298+
"\0\0"
299+
};
283300
buf.split_terminator(delimiter).for_each(|s| {
284301
let packet = Packet {
285302
relative_time: Instant::now().duration_since(t_zero).as_millis() as f64,

0 commit comments

Comments
 (0)