कुछ-कुछ किए है।

This commit is contained in:
Piyush मिश्रः 2021-01-01 17:40:52 +05:30
parent e625bc863a
commit bedf72d479
8 changed files with 189 additions and 42 deletions

80
Cargo.lock generated
View File

@ -309,6 +309,17 @@ dependencies = [
"system-deps", "system-deps",
] ]
[[package]]
name = "getrandom"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]] [[package]]
name = "gio" name = "gio"
version = "0.9.1" version = "0.9.1"
@ -345,9 +356,9 @@ dependencies = [
[[package]] [[package]]
name = "glib" name = "glib"
version = "0.10.2" version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f11a45bde4cf9567653a14ff96cdbb01e6d2a89651746dbc118319dfdac433e" checksum = "0c685013b7515e668f1b57a165b009d4d28cb139a8a989bbd699c10dad29d0c5"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"futures-channel", "futures-channel",
@ -539,9 +550,9 @@ dependencies = [
[[package]] [[package]]
name = "nix" name = "nix"
version = "0.14.1" version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c722bee1037d430d0f8e687bbdbf222f27cc6e4e68d5caf630857bb2b6dbdce" checksum = "dd0eaf8df8bab402257e0a5c17a254e4cc1f72a93588a1ddfb5d356c801aa7cb"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"cc", "cc",
@ -617,9 +628,9 @@ checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33"
[[package]] [[package]]
name = "png" name = "png"
version = "0.16.7" version = "0.16.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfe7f9f1c730833200b134370e1d5098964231af8450bce9b78ee3ab5278b970" checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"crc32fast", "crc32fast",
@ -627,6 +638,12 @@ dependencies = [
"miniz_oxide", "miniz_oxide",
] ]
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]] [[package]]
name = "proc-macro-crate" name = "proc-macro-crate"
version = "0.1.5" version = "0.1.5"
@ -713,6 +730,28 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "rand"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
dependencies = [
"libc",
"rand_chacha",
"rand_core 0.6.0",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core 0.6.0",
]
[[package]] [[package]]
name = "rand_core" name = "rand_core"
version = "0.3.1" version = "0.3.1"
@ -728,6 +767,24 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8b34ba8cfb21243bd8df91854c830ff0d785fff2e82ebd4434c2644cb9ada18"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core 0.6.0",
]
[[package]] [[package]]
name = "rdrand" name = "rdrand"
version = "0.4.0" version = "0.4.0"
@ -763,9 +820,9 @@ checksum = "96fe57af81d28386a513cbc6858332abc6117cfdb5999647c6444b8f43a370a5"
[[package]] [[package]]
name = "serialport" name = "serialport"
version = "3.3.0" version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b8d3ecaf58010bedccae17be55d4ed6f2ecde5646fc48ce8c66ea2d35a1419c" checksum = "22f37409d980045734250d679750bdf11bd875fec5bb5417dd21bb75d04d31a1"
dependencies = [ dependencies = [
"CoreFoundation-sys", "CoreFoundation-sys",
"IOKit-sys", "IOKit-sys",
@ -876,6 +933,7 @@ dependencies = [
"gtk", "gtk",
"libmath", "libmath",
"png", "png",
"rand 0.8.0",
"serialport", "serialport",
] ]
@ -909,6 +967,12 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View File

@ -8,9 +8,10 @@ edition = "2018"
[dependencies] [dependencies]
libmath = "0.2.1" libmath = "0.2.1"
serialport = "3.3.0" serialport = "4.0.0"
gtk = "0.9.2" gtk = "0.9.2"
gio = "0.9.1" gio = "0.9.1"
glib = "0.10.2" glib = "0.10.3"
png = "0.16.7" png = "0.16.8"
cairo-rs = { version = "0.9.1", features = ["png"] } cairo-rs = { version = "0.9.1", features = ["png"] }
rand = "0.8.0"

View File

@ -1,5 +1,6 @@
use gtk::prelude::*; use gtk::prelude::*;
use gtk::DrawingArea; use gtk::DrawingArea;
use rand::prelude::*;
use std::rc::Rc; use std::rc::Rc;
pub struct Graph { pub struct Graph {
@ -8,7 +9,21 @@ pub struct Graph {
scale_x_end: f64, scale_x_end: f64,
scale_y_start: f64, scale_y_start: f64,
scale_y_end: f64, scale_y_end: f64,
points: Vec<(f64,f64)> lines: Vec<Line>
}
pub struct Line {
points: Vec<(f64,f64)>,
color: (f64, f64, f64)
}
impl Line {
pub fn new(r: f64, g: f64, b: f64, points: Vec<(f64,f64)>) -> Self {
Line {
points,
color: (r,g,b)
}
}
} }
impl Graph { impl Graph {
@ -17,7 +32,7 @@ impl Graph {
scale_x_end: f64, scale_x_end: f64,
scale_y_start: f64, scale_y_start: f64,
scale_y_end: f64, scale_y_end: f64,
points: Vec<(f64,f64)>) -> Rc<Graph> { lines: Vec<Line>) -> Rc<Self> {
let graph = Rc::new(Graph { let graph = Rc::new(Graph {
area, area,
@ -25,7 +40,7 @@ impl Graph {
scale_x_end, scale_x_end,
scale_y_start, scale_y_start,
scale_y_end, scale_y_end,
points lines
}); });
let graph_tmp = Rc::clone(&graph); let graph_tmp = Rc::clone(&graph);
@ -76,12 +91,15 @@ impl Graph {
ctx.set_line_width(2.0); ctx.set_line_width(2.0);
ctx.set_line_cap(cairo::LineCap::Round); ctx.set_line_cap(cairo::LineCap::Round);
for p in graph.points.iter().skip(1).enumerate() { for line in graph.lines.iter() {
let xp = graph.points[p.0]; ctx.set_source_rgb(line.color.0, line.color.1, line.color.2);
ctx.move_to(((cell_size as f64)*(xp.0 - graph.scale_x_start))/h_scale + 40.0, height - ((cell_size as f64)*(xp.1 - graph.scale_y_start))/v_scale - 20.0); for p in line.points.iter().skip(1).enumerate() {
ctx.line_to(((cell_size as f64)*(p.1.0 - graph.scale_x_start))/h_scale + 40.0, height - ((cell_size as f64)*(p.1.1 - graph.scale_y_start))/v_scale - 20.0); let xp = line.points[p.0];
ctx.move_to(((cell_size as f64)*(xp.0 - graph.scale_x_start))/h_scale + 40.0, height - ((cell_size as f64)*(xp.1 - graph.scale_y_start))/v_scale - 20.0);
ctx.line_to(((cell_size as f64)*(p.1.0 - graph.scale_x_start))/h_scale + 40.0, height - ((cell_size as f64)*(p.1.1 - graph.scale_y_start))/v_scale - 20.0);
}
ctx.stroke();
} }
ctx.stroke();
} }
fn draw_boxes(ctx: &cairo::Context, area_width: f64, area_height: f64, src_x: f64, src_y: f64, cell_size: f64, color: f64) { fn draw_boxes(ctx: &cairo::Context, area_width: f64, area_height: f64, src_x: f64, src_y: f64, cell_size: f64, color: f64) {

View File

@ -1,22 +1,24 @@
mod graph; mod graph;
use gtk::prelude::*; use gtk::prelude::*;
use graph::Graph; use graph::{Graph, Line};
use std::sync::Arc; use std::sync::{Arc, Mutex};
pub enum Status { pub enum Status {
JAGRIT, SAYAN, AVRODTIH JAGRIT, SAYAN, AVRODTIH
} }
pub struct Config { pub struct Config {
status: Status status: Status,
bondrate: i32
} }
impl Config { impl Config {
pub fn new() -> Config { pub fn new() -> Config {
Config { Config {
status: Status::AVRODTIH status: Status::AVRODTIH,
bondrate: 9600
} }
} }
} }
@ -25,41 +27,66 @@ enum Message {
UpdateLabel(String), UpdateLabel(String),
} }
pub fn build_ui(app: &gtk::Application, config: Arc::<Config>) { pub fn build_ui(app: &gtk::Application, config: Arc::<Mutex::<Config>>) {
let builder = gtk::Builder::from_file("ui/main_window.glade"); let builder = gtk::Builder::from_file("ui/main_window.glade");
let win = builder.get_object::<gtk::ApplicationWindow>("win").expect("Resource file missing!"); let win = builder.get_object::<gtk::ApplicationWindow>("win").expect("Resource file missing!");
win.set_application(Some(app)); win.set_application(Some(app));
let bar = builder.get_object::<gtk::Statusbar>("status_bar").expect("Resource file missing!");
let graph = Graph::new( let _ = Graph::new(
builder.get_object::<gtk::DrawingArea>("draw_area").expect("Resource file missing!"), builder.get_object::<gtk::DrawingArea>("draw_area").expect("Resource file missing!"),
0.0, 100.0, 0.0, 30.0,
0.0, 100.0, 0.0, 50.0,
vec![(10.0,10.0),(20.0,20.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)])
]
); );
win.show_all(); win.show_all();
let bondrate = builder.get_object::<gtk::ComboBoxText>("log_area").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) =>
config.bondrate = match cbx.get_active_text() {
Some(txt) => txt.to_string().parse::<i32>().unwrap_or(9600),
None => 9600
},
Err(_) => {
tmp_bar.push(1, "Failed to change bondrate!");
}
}
});
let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
let tmpconfig = Arc::clone(&config); let tmpconfig = Arc::clone(&config);
std::thread::spawn(move || { std::thread::spawn(move || {
match tmpconfig.status { match tmpconfig.lock().unwrap().status {
Status::JAGRIT => { Status::JAGRIT => {
sender.send(Message::UpdateLabel(String::from("jagrit"))).unwrap(); sender.send(Message::UpdateLabel(String::from("jagrit"))).unwrap();
}, Status::SAYAN => { }, Status::SAYAN => {
sender.send(Message::UpdateLabel(String::from("sayan"))).unwrap();
}, Status::AVRODTIH => { }, Status::AVRODTIH => {
sender.send(Message::UpdateLabel(String::from("avrodhit"))).unwrap(); sender.send(Message::UpdateLabel(String::from("avrodhit"))).unwrap();
} }
} }
}); });
let bar = builder.get_object::<gtk::Statusbar>("status_bar").expect("Resource file missing!");
let log_area = builder.get_object::<gtk::TextView>("log_area").expect("Resource file missing!");
receiver.attach(None, move |msg| { receiver.attach(None, move |msg| {
match msg { match msg {
Message::UpdateLabel(text) => { Message::UpdateLabel(text) => {
bar.push(1, &text); bar.push(1, &text);
let buf = log_area.get_buffer()
.expect("Couldn't get window");
buf.insert(&mut buf.get_end_iter(), &text);
}, },
} }
glib::Continue(true) glib::Continue(true)

View File

@ -1,12 +1,12 @@
use gio::prelude::*; use gio::prelude::*;
use std::env::args; use std::env::args;
use std::sync::Arc; use std::sync::{Arc, Mutex};
fn main() { fn main() {
let app = gtk::Application::new(Some("sng.trangm"), Default::default()) let app = gtk::Application::new(Some("sng.trangm"), Default::default())
.expect("Failed to initiate gtk"); .expect("Failed to initiate gtk");
let config = Arc::new(trangam::Config::new()); let config = Arc::new(Mutex::new(trangam::Config::new()));
app.connect_activate(move |app| { app.connect_activate(move |app| {
let config = Arc::clone(&config); let config = Arc::clone(&config);

View File

@ -1,13 +1,14 @@
use std::io::prelude::*; use std::io::prelude::*;
use std::io::BufReader; use std::io::BufReader;
use std::time::Duration;
#[test] #[test]
fn start() { fn start() {
let ports = serialport::available_ports(); let ports = serialport::available_ports();
println!("{:?}",ports); println!("{:?}",ports);
let p = serialport::open("/dev/ttyUSB0") let p = serialport::new("/dev/ttyUSB0", 9600).timeout(Duration::from_millis(10))
.unwrap(); .open().expect("Failed to open port");
let mut read = BufReader::new(p); let mut read = BufReader::new(p);
let mut buf = String::new(); let mut buf = String::new();
@ -15,7 +16,7 @@ fn start() {
match read.read_line(&mut buf) { match read.read_line(&mut buf) {
Ok(_) => Ok(_) =>
{ {
print!("{} seconds", buf.parse::<f64>().unwrap() / 60.0); print!("{}", buf);
buf.clear(); buf.clear();
}, },
Err(_) => {} Err(_) => {}

View File

@ -191,8 +191,7 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can-focus">False</property> <property name="can-focus">False</property>
<child> <child>
<object class="GtkComboBox"> <object class="GtkComboBoxText">
<property name="width-request">200</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can-focus">False</property> <property name="can-focus">False</property>
</object> </object>
@ -229,6 +228,25 @@
<property name="homogeneous">True</property> <property name="homogeneous">True</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkToolItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkComboBoxText">
<property name="visible">True</property>
<property name="can-focus">False</property>
<items>
<item id="&lt;Enter ID&gt;" translatable="yes">9600</item>
</items>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
@ -263,7 +281,7 @@
<property name="vexpand">True</property> <property name="vexpand">True</property>
<property name="shadow-type">in</property> <property name="shadow-type">in</property>
<child> <child>
<object class="GtkTextView"> <object class="GtkTextView" id="log_area">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can-focus">True</property> <property name="can-focus">True</property>
<property name="monospace">True</property> <property name="monospace">True</property>

View File

@ -191,8 +191,7 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can-focus">False</property> <property name="can-focus">False</property>
<child> <child>
<object class="GtkComboBox"> <object class="GtkComboBoxText">
<property name="width-request">200</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can-focus">False</property> <property name="can-focus">False</property>
</object> </object>
@ -229,6 +228,25 @@
<property name="homogeneous">True</property> <property name="homogeneous">True</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkToolItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkComboBoxText">
<property name="visible">True</property>
<property name="can-focus">False</property>
<items>
<item id="&lt;Enter ID&gt;" translatable="yes">9600</item>
</items>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
@ -263,7 +281,7 @@
<property name="vexpand">True</property> <property name="vexpand">True</property>
<property name="shadow-type">in</property> <property name="shadow-type">in</property>
<child> <child>
<object class="GtkTextView"> <object class="GtkTextView" id="log_area">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can-focus">True</property> <property name="can-focus">True</property>
<property name="monospace">True</property> <property name="monospace">True</property>