From f37cbb7096a55d622b83ede0cc19b35b56574997 Mon Sep 17 00:00:00 2001 From: Piyush Raj Date: Sat, 2 Jan 2021 19:43:33 +0530 Subject: [PATCH] Some Changes --- src/graph.rs | 2 +- src/lib.rs | 172 ++++++++++++++++++++++++++++++++++++------ ui/main_window.glade | 60 ++++++++------- ui/main_window.glade~ | 60 ++++++++------- 4 files changed, 210 insertions(+), 84 deletions(-) diff --git a/src/graph.rs b/src/graph.rs index 468dc45..f6c4c65 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -1,6 +1,6 @@ use gtk::prelude::*; use gtk::DrawingArea; -use rand::prelude::*; + use std::rc::Rc; pub struct Graph { diff --git a/src/lib.rs b/src/lib.rs index ef43a34..4c5875d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,26 +5,32 @@ use graph::{Graph, Line}; use std::sync::{Arc, Mutex}; +use std::io::prelude::*; +use std::io::BufReader; + pub enum Status { - JAGRIT, SAYAN, AVRODTIH + JAGRIT, SAYAN, AVRODTIH, PARIVARTIT, NIKAS } pub struct Config { status: Status, - bondrate: i32 + bondrate: u32, + port: String } impl Config { pub fn new() -> Config { Config { status: Status::AVRODTIH, - bondrate: 9600 + bondrate: 9600, + port: "".to_owned() } } } enum Message { - UpdateLabel(String), + Msg(String), + Status(String) } pub fn build_ui(app: >k::Application, config: Arc::>) { @@ -39,55 +45,171 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { 0.0, 30.0, 0.0, 50.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)]) ] ); win.show_all(); - let bondrate = builder.get_object::("log_area").expect("Resource file missing!"); + // Bondrate + let bondrate = builder.get_object::("bondrate").expect("Resource file missing!"); let tmp_bar = bar.clone(); let tmp_config = Arc::clone(&config); bondrate.connect_changed(move |cbx| { match tmp_config.lock() { - Ok(mut config) => + Ok(mut config) => { config.bondrate = match cbx.get_active_text() { - Some(txt) => txt.to_string().parse::().unwrap_or(9600), - None => 9600 - }, - Err(_) => { + Some(txt) => txt.to_string().parse::().unwrap_or(9600u32), + None => 9600 + }; + }, Err(_) => { tmp_bar.push(1, "Failed to change bondrate!"); } } }); - let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); - - let tmpconfig = Arc::clone(&config); - std::thread::spawn(move || { - match tmpconfig.lock().unwrap().status { - Status::JAGRIT => { - sender.send(Message::UpdateLabel(String::from("jagrit"))).unwrap(); - }, Status::SAYAN => { - - }, Status::AVRODTIH => { - sender.send(Message::UpdateLabel(String::from("avrodhit"))).unwrap(); + // port + let refresh_port = builder.get_object::("refresh_port").expect("Resource file missing!"); + let port = builder.get_object::("port").expect("Resource file missing!"); + + let tmp_bar = bar.clone(); + let tmp_port = port.clone(); + refresh_port.connect_clicked(move |_| { + tmp_port.remove_all(); + match serialport::available_ports() { + Ok(ports) => { + if ports.len() == 0 { tmp_bar.push(1, "No port found!"); } + for p in ports { + tmp_port.append_text(p.port_name.as_str()); + } + }, Err(_) => { + tmp_bar.push(1, "No port found!"); } } }); + let tmp_bar = bar.clone(); + let tmp_config = Arc::clone(&config); + port.connect_changed(move |cbx| { + match tmp_config.lock() { + Ok(mut config) => { + config.port = match cbx.get_active_text() { + Some(txt) => txt.to_string(), + None => "".to_owned() + }; + }, Err(_) => { + tmp_bar.push(1, "Failed to change port!"); + } + } + }); + + + //jagrit_btn + 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!"); + 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; + } + }, Err(_) => { + tmp_bar.push(1, "Failed to change port!"); + } + } + }); + + //jagrit_btn + let avrodith_btn = builder.get_object::("avrodith_btn").expect("Resource file missing!"); + + let tmp_bar = bar.clone(); + let tmp_config = Arc::clone(&config); + avrodith_btn.connect_clicked(move |btn| { + 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!"); + } + } + }); + + // serial + let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); + + let tmp_config = Arc::clone(&config); + std::thread::spawn(move || { + let mut bufread: Option>> = None; + let mut buf = String::new(); + loop { + match tmp_config.lock() { + Ok(mut config) => { + match config.status { + Status::AVRODTIH => { + if let Some(_) = bufread { + bufread = None; + println!("Avrohit!"); + } + }, + Status::JAGRIT => { + if let Some(read) = &mut bufread { + if let Ok(_) = read.read_line(&mut buf) { + sender.send(Message::Msg(buf.clone())).unwrap(); + buf.clear(); + } + } + }, + Status::NIKAS => {}, + Status::PARIVARTIT => { + let p = match serialport::new(&config.port, config.bondrate).open() { + Ok(p) => p, + Err(_) => { + continue; + } + }; + + bufread = Some(BufReader::new(p)); + config.status = Status::JAGRIT; + }, + Status::SAYAN => {} + } + + }, Err(_) => { + sender.send(Message::Status("Faild prepare for communication!".to_owned())).unwrap(); + return; + } + }; + } + }); + let log_area = builder.get_object::("log_area").expect("Resource file missing!"); receiver.attach(None, move |msg| { match msg { - Message::UpdateLabel(text) => { - bar.push(1, &text); + Message::Msg(text) => { let buf = log_area.get_buffer() .expect("Couldn't get window"); buf.insert(&mut buf.get_end_iter(), &text); }, + Message::Status(text) => { + bar.push(1, &text); + } } glib::Continue(true) }); diff --git a/ui/main_window.glade b/ui/main_window.glade index 4a294d2..4003d26 100644 --- a/ui/main_window.glade +++ b/ui/main_window.glade @@ -4,6 +4,7 @@ False + Trangam 800 600 @@ -174,7 +175,7 @@ True False - + True False refresh @@ -191,7 +192,7 @@ True False - + True False @@ -202,38 +203,12 @@ True - - - True - False - Connect - True - media-playback-start - - - False - True - - - - - True - False - Stop - True - media-playback-stop - - - False - True - - True False - + True False @@ -247,6 +222,33 @@ True + + + True + False + Connect + True + media-playback-start + True + + + False + True + + + + + True + False + Stop + True + media-playback-stop + + + False + True + + False diff --git a/ui/main_window.glade~ b/ui/main_window.glade~ index 4a294d2..4003d26 100644 --- a/ui/main_window.glade~ +++ b/ui/main_window.glade~ @@ -4,6 +4,7 @@ False + Trangam 800 600 @@ -174,7 +175,7 @@ True False - + True False refresh @@ -191,7 +192,7 @@ True False - + True False @@ -202,38 +203,12 @@ True - - - True - False - Connect - True - media-playback-start - - - False - True - - - - - True - False - Stop - True - media-playback-stop - - - False - True - - True False - + True False @@ -247,6 +222,33 @@ True + + + True + False + Connect + True + media-playback-start + True + + + False + True + + + + + True + False + Stop + True + media-playback-stop + + + False + True + + False