Some Changes

This commit is contained in:
Piyush मिश्रः 2021-01-02 19:43:33 +05:30
parent bedf72d479
commit f37cbb7096
4 changed files with 210 additions and 84 deletions

View File

@ -1,6 +1,6 @@
use gtk::prelude::*;
use gtk::DrawingArea;
use rand::prelude::*;
use std::rc::Rc;
pub struct Graph {

View File

@ -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: &gtk::Application, config: Arc::<Mutex::<Config>>) {
@ -39,55 +45,171 @@ pub fn build_ui(app: &gtk::Application, config: Arc::<Mutex::<Config>>) {
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::<gtk::ComboBoxText>("log_area").expect("Resource file missing!");
// Bondrate
let bondrate = builder.get_object::<gtk::ComboBoxText>("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::<i32>().unwrap_or(9600),
None => 9600
},
Err(_) => {
Some(txt) => txt.to_string().parse::<u32>().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::<gtk::ToolButton>("refresh_port").expect("Resource file missing!");
let port = builder.get_object::<gtk::ComboBoxText>("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::<gtk::RadioToolButton>("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::<gtk::ToolButton>("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<BufReader<Box<dyn serialport::SerialPort>>> = 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::<gtk::TextView>("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)
});

View File

@ -4,6 +4,7 @@
<requires lib="gtk+" version="3.22"/>
<object class="GtkApplicationWindow" id="win">
<property name="can-focus">False</property>
<property name="title" translatable="yes">Trangam</property>
<property name="default-width">800</property>
<property name="default-height">600</property>
<child>
@ -174,7 +175,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkToolButton">
<object class="GtkToolButton" id="refresh_port">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">refresh</property>
@ -191,7 +192,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkComboBoxText">
<object class="GtkComboBoxText" id="port">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
@ -202,38 +203,12 @@
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToggleToolButton">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Connect</property>
<property name="use-underline">True</property>
<property name="icon-name">media-playback-start</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Stop</property>
<property name="use-underline">True</property>
<property name="icon-name">media-playback-stop</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkComboBoxText">
<object class="GtkComboBoxText" id="bondrate">
<property name="visible">True</property>
<property name="can-focus">False</property>
<items>
@ -247,6 +222,33 @@
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkRadioToolButton" id="jagrit_btn">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Connect</property>
<property name="use-underline">True</property>
<property name="icon-name">media-playback-start</property>
<property name="active">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="avrodith_btn">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Stop</property>
<property name="use-underline">True</property>
<property name="icon-name">media-playback-stop</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>

View File

@ -4,6 +4,7 @@
<requires lib="gtk+" version="3.22"/>
<object class="GtkApplicationWindow" id="win">
<property name="can-focus">False</property>
<property name="title" translatable="yes">Trangam</property>
<property name="default-width">800</property>
<property name="default-height">600</property>
<child>
@ -174,7 +175,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkToolButton">
<object class="GtkToolButton" id="refresh_port">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">refresh</property>
@ -191,7 +192,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkComboBoxText">
<object class="GtkComboBoxText" id="port">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
@ -202,38 +203,12 @@
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToggleToolButton">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Connect</property>
<property name="use-underline">True</property>
<property name="icon-name">media-playback-start</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Stop</property>
<property name="use-underline">True</property>
<property name="icon-name">media-playback-stop</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkComboBoxText">
<object class="GtkComboBoxText" id="bondrate">
<property name="visible">True</property>
<property name="can-focus">False</property>
<items>
@ -247,6 +222,33 @@
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkRadioToolButton" id="jagrit_btn">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Connect</property>
<property name="use-underline">True</property>
<property name="icon-name">media-playback-start</property>
<property name="active">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="avrodith_btn">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Stop</property>
<property name="use-underline">True</property>
<property name="icon-name">media-playback-stop</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>