From fab65c66e5cbbd3ce71b652569ae17e167e07323 Mon Sep 17 00:00:00 2001 From: Piyush Raj Date: Sat, 2 Jan 2021 22:57:31 +0530 Subject: [PATCH] Major comit before connecting graph --- src/lib.rs | 99 ++++++++++++------ tests/serial_port_test.rs | 5 +- ui/main_window.glade | 210 ++++++++++++++++++++------------------ ui/main_window.glade~ | 210 ++++++++++++++++++++------------------ 4 files changed, 296 insertions(+), 228 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4c5875d..ad7b5ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,14 +39,16 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { let win = builder.get_object::("win").expect("Resource file missing!"); win.set_application(Some(app)); let bar = builder.get_object::("status_bar").expect("Resource file missing!"); + let log_area = builder.get_object::("log_area").expect("Resource file missing!"); let _ = Graph::new( builder.get_object::("draw_area").expect("Resource file missing!"), - 0.0, 30.0, - 0.0, 50.0, + 0.0, 100.0, + 0.0, 100.0, vec![ - // Line::new(1.0,1.0,1.0,vec![(10.0,10.0),(20.0,20.0)]), - // Line::new(1.0,0.0,1.0,vec![(15.0,15.0),(50.0,25.0)]) + Line::new(1.0,1.0,1.0,vec![(10.0,10.0),(20.0,20.0)]), + Line::new(1.0,0.0,1.0,vec![(15.0,15.0),(50.0,25.0)]), + Line::new(1.0,0.0,0.0,vec![(50.0,10.0),(70.0,60.0)]) ] ); @@ -105,25 +107,16 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { } }); - //jagrit_btn - let jagrit_btn = builder.get_object::("jagrit_btn").expect("Resource file missing!"); + let jagrit_btn = builder.get_object::("jagrit_btn").expect("Resource file missing!"); let tmp_bar = bar.clone(); let tmp_config = Arc::clone(&config); - jagrit_btn.connect_clicked(move |btn | { - println!("In jagrit_btn!"); + jagrit_btn.connect_clicked(move |_ | { match tmp_config.lock() { Ok(mut config) => { - if btn.get_active() { - tmp_bar.push(1, "Jagrit"); - btn.set_icon_name(Some("media-playback-pause")); - config.status = Status::PARIVARTIT; - } else { - tmp_bar.push(1, "Sayan"); - btn.set_icon_name(Some("media-playback-start")); - config.status = Status::SAYAN; - } + tmp_bar.push(1, "Jagrit"); + config.status = Status::PARIVARTIT; }, Err(_) => { tmp_bar.push(1, "Failed to change port!"); } @@ -135,14 +128,10 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { let tmp_bar = bar.clone(); let tmp_config = Arc::clone(&config); - avrodith_btn.connect_clicked(move |btn| { + avrodith_btn.connect_clicked(move |_| { match tmp_config.lock() { Ok(mut config) => { tmp_bar.push(1, "Avrodhit"); - println!("Before"); - jagrit_btn.set_active(false); - println!("After!"); - jagrit_btn.set_icon_name(Some("media-playback-start")); config.status = Status::AVRODTIH; }, Err(_) => { tmp_bar.push(1, "Failed to change port!"); @@ -150,6 +139,32 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { } }); + //clear_log + let clear_log = builder.get_object::("clear_log").expect("Resource file missing!"); + + let tmp_log_area = log_area.clone(); + clear_log.connect_clicked(move |_| { + tmp_log_area.get_buffer().expect("Couldn't get window").set_text(""); + }); + + // send_entry + let send_entry = builder.get_object::("send_entry").expect("Resource file missing!"); + + let tmp_bar = bar.clone(); + let tmp_config = Arc::clone(&config); + send_entry.connect_activate(move |ent| { + send_text(&tmp_config, ent, &tmp_bar); + }); + + //send_btn + let send_btn = builder.get_object::("send_btn").expect("Resource file missing!"); + + let tmp_bar = bar.clone(); + let tmp_config = Arc::clone(&config); + send_btn.connect_activate(move |_| { + send_text(&tmp_config, &send_entry, &tmp_bar); + }); + // serial let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); @@ -162,10 +177,8 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { Ok(mut config) => { match config.status { Status::AVRODTIH => { - if let Some(_) = bufread { - bufread = None; - println!("Avrohit!"); - } + bufread = None; + config.status = Status::SAYAN; }, Status::JAGRIT => { if let Some(read) = &mut bufread { @@ -198,14 +211,18 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { } }); - - let log_area = builder.get_object::("log_area").expect("Resource file missing!"); + let full_log = builder.get_object::("full_log").expect("Resource file missing!"); receiver.attach(None, move |msg| { match msg { Message::Msg(text) => { - let buf = log_area.get_buffer() - .expect("Couldn't get window"); - buf.insert(&mut buf.get_end_iter(), &text); + if !full_log.get_active() && text.starts_with("#") { + + } else { + let buf = log_area.get_buffer() + .expect("Couldn't get log_area"); + buf.insert(&mut buf.get_end_iter(), &text); + log_area.scroll_to_iter(&mut buf.get_end_iter(), 0.4, true, 0.0, 0.0); + } }, Message::Status(text) => { bar.push(1, &text); @@ -215,3 +232,23 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { }); } +fn send_text(config: &Arc>, entry: >k::Entry, bar: >k::Statusbar) { + match config.lock() { + Ok(config) => { + let mut p = match serialport::new(&config.port, config.bondrate).open() { + Ok(p) => p, + Err(_) => { + bar.push(1, "Failed to change port!"); + return; + } + }; + + unsafe { + p.write_all(entry.get_text().to_string().as_bytes_mut()).unwrap(); + } + entry.set_text(""); + }, Err(_) => { + bar.push(1, "Failed to change port!"); + } + } +} diff --git a/tests/serial_port_test.rs b/tests/serial_port_test.rs index 0ac4c33..7faa48d 100644 --- a/tests/serial_port_test.rs +++ b/tests/serial_port_test.rs @@ -7,9 +7,12 @@ fn start() { let ports = serialport::available_ports(); println!("{:?}",ports); - let p = serialport::new("/dev/ttyUSB0", 9600).timeout(Duration::from_millis(10)) + let mut p = serialport::new("/dev/ttyUSB1", 9600).timeout(Duration::from_millis(10)) .open().expect("Failed to open port"); + unsafe { + p.write_all("buf".to_owned().as_bytes_mut()).unwrap(); + } let mut read = BufReader::new(p); let mut buf = String::new(); loop { diff --git a/ui/main_window.glade b/ui/main_window.glade index 4003d26..bf56256 100644 --- a/ui/main_window.glade +++ b/ui/main_window.glade @@ -2,6 +2,16 @@ + + True + False + document-save + + + True + False + go-next + False Trangam @@ -28,38 +38,11 @@ False - gtk-new + Save Log True False - True - True - - - - - gtk-open - True - False - True - True - - - - - gtk-save - True - False - True - True - - - - - gtk-save-as - True - False - True - True + image1 + False @@ -81,64 +64,6 @@ - - - True - False - _Edit - True - - - True - False - - - gtk-cut - True - False - True - True - - - - - gtk-copy - True - False - True - True - - - - - gtk-paste - True - False - True - True - - - - - gtk-delete - True - False - True - True - - - - - - - - - True - False - _View - True - - True @@ -223,13 +148,12 @@ - + True False Connect True media-playback-start - True False @@ -275,19 +199,109 @@ - - 300 + True - True - True - True - in + False + vertical - + + True + False + + + True + False + Clear + True + + + False + True + + + + + True + False + + + Show full log + True + True + False + True + + + + + False + True + + + + + False + True + 0 + + + + + 300 True True - True + True + True + in + + + True + True + True + + + + False + True + 1 + + + + + True + False + + + True + True + + + True + True + 0 + + + + + Send + True + True + True + send_image + + + False + True + 1 + + + + + False + True + 2 + diff --git a/ui/main_window.glade~ b/ui/main_window.glade~ index 4003d26..bf56256 100644 --- a/ui/main_window.glade~ +++ b/ui/main_window.glade~ @@ -2,6 +2,16 @@ + + True + False + document-save + + + True + False + go-next + False Trangam @@ -28,38 +38,11 @@ False - gtk-new + Save Log True False - True - True - - - - - gtk-open - True - False - True - True - - - - - gtk-save - True - False - True - True - - - - - gtk-save-as - True - False - True - True + image1 + False @@ -81,64 +64,6 @@ - - - True - False - _Edit - True - - - True - False - - - gtk-cut - True - False - True - True - - - - - gtk-copy - True - False - True - True - - - - - gtk-paste - True - False - True - True - - - - - gtk-delete - True - False - True - True - - - - - - - - - True - False - _View - True - - True @@ -223,13 +148,12 @@ - + True False Connect True media-playback-start - True False @@ -275,19 +199,109 @@ - - 300 + True - True - True - True - in + False + vertical - + + True + False + + + True + False + Clear + True + + + False + True + + + + + True + False + + + Show full log + True + True + False + True + + + + + False + True + + + + + False + True + 0 + + + + + 300 True True - True + True + True + in + + + True + True + True + + + + False + True + 1 + + + + + True + False + + + True + True + + + True + True + 0 + + + + + Send + True + True + True + send_image + + + False + True + 1 + + + + + False + True + 2 +