Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ impl TemplateApp {
.join(" ");
format!("{hex_string} ")
} else {
String::from_utf8_lossy(&data).to_string()
String::from_utf8_lossy(&data).into_owned()
};
self.write_log(&message);
self.file_log_panel.write_to_file(&data);
ctx.request_repaint();
}
CommunicationEvent::ConnectionClosed => {
Expand All @@ -143,7 +144,6 @@ impl TemplateApp {
fn write_log(&mut self, message: &str) {
self.rx_panel
.append_log(message, self.settings.max_log_string_length);
self.file_log_panel.write_to_file(message);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/gui/file_log_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ impl FileLogPanel {
}
}

pub fn write_to_file(&mut self, message: &str) {
pub fn write_to_file(&mut self, message: &[u8]) {
if let Some(ref mut file) = self.log_file {
if let Err(e) = file.write_all(message.as_bytes()) {
if let Err(e) = file.write_all(message) {
eprintln!("Error writing to log file: {e:?}");
}
}
Expand Down
31 changes: 11 additions & 20 deletions src/gui/rx_panel.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use egui::Vec2;
use std::collections::VecDeque;

#[derive(Default)]
pub struct RxPanel {
pub content: String,
pub content: VecDeque<char>,
should_scroll_to_bottom: bool,
}

impl RxPanel {
pub fn new(_max_length: usize) -> Self {
Self {
content: "Starting app\n".to_string(),
content: VecDeque::new(),
should_scroll_to_bottom: false,
}
}
Expand All @@ -23,7 +25,7 @@ impl RxPanel {
scroll_area.show(ui, |ui| {
ui.add_sized(
text_size,
egui::TextEdit::multiline(&mut self.content)
egui::TextEdit::multiline(&mut self.content.iter().collect::<String>())
.font(egui::TextStyle::Monospace)
.code_editor()
.desired_rows(10)
Expand All @@ -41,14 +43,12 @@ impl RxPanel {
}

pub fn append_log(&mut self, message: &str, max_length: usize) {
eprintln!("{message}");
self.content += message;

if self.content.len() > max_length {
let excess_len = self.content.len() - max_length;
self.content.drain(0..excess_len);
for ch in message.chars() {
if self.content.len() == max_length {
self.content.pop_front(); // Elimina el carácter más viejo
}
self.content.push_back(ch); // Agrega el nuevo carácter
}

// Mark that we need to scroll to bottom
self.should_scroll_to_bottom = true;
}
Expand All @@ -72,19 +72,10 @@ impl<'de> serde::Deserialize<'de> for RxPanel {
where
D: serde::Deserializer<'de>,
{
let content = String::deserialize(deserializer)?;
let content = VecDeque::deserialize(deserializer)?;
Ok(Self {
content,
should_scroll_to_bottom: false,
})
}
}

impl Default for RxPanel {
fn default() -> Self {
Self {
content: "Starting app\n".to_string(),
should_scroll_to_bottom: false,
}
}
}
19 changes: 15 additions & 4 deletions src/serial_impl/serial_communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,21 @@ impl CommunicationManager for SerialCommunication {
let size = port_instance.bytes_to_read().unwrap_or(0);
if size > 0 {
let mut serial_buf: Vec<u8> = vec![0; size as usize];
port_instance.read_exact(&mut serial_buf).unwrap();
// self.write_log(message.unwrap_or(String::from("")).as_str());
tx.send(CommunicationEvent::DataReceived(serial_buf))
.expect("Failed to send data to GUI");
match port_instance.read_exact(&mut serial_buf) {
Ok(_) => {
// Handle channel send errors gracefully
if tx
.send(CommunicationEvent::DataReceived(serial_buf))
.is_err()
{
eprintln!("GUI channel disconnected, stopping serial thread");
break; // Exit the loop if GUI is gone
}
}
Err(e) => {
eprintln!("Serial read error: {e}");
}
}
}
}

Expand Down
Loading