diff --git a/Cargo.lock b/Cargo.lock index a424553..e3599a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -309,6 +309,17 @@ dependencies = [ "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]] name = "gio" version = "0.9.1" @@ -345,9 +356,9 @@ dependencies = [ [[package]] name = "glib" -version = "0.10.2" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f11a45bde4cf9567653a14ff96cdbb01e6d2a89651746dbc118319dfdac433e" +checksum = "0c685013b7515e668f1b57a165b009d4d28cb139a8a989bbd699c10dad29d0c5" dependencies = [ "bitflags", "futures-channel", @@ -539,9 +550,9 @@ dependencies = [ [[package]] name = "nix" -version = "0.14.1" +version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c722bee1037d430d0f8e687bbdbf222f27cc6e4e68d5caf630857bb2b6dbdce" +checksum = "dd0eaf8df8bab402257e0a5c17a254e4cc1f72a93588a1ddfb5d356c801aa7cb" dependencies = [ "bitflags", "cc", @@ -617,9 +628,9 @@ checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" [[package]] name = "png" -version = "0.16.7" +version = "0.16.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfe7f9f1c730833200b134370e1d5098964231af8450bce9b78ee3ab5278b970" +checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6" dependencies = [ "bitflags", "crc32fast", @@ -627,6 +638,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + [[package]] name = "proc-macro-crate" version = "0.1.5" @@ -713,6 +730,28 @@ dependencies = [ "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]] name = "rand_core" version = "0.3.1" @@ -728,6 +767,24 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "rdrand" version = "0.4.0" @@ -763,9 +820,9 @@ checksum = "96fe57af81d28386a513cbc6858332abc6117cfdb5999647c6444b8f43a370a5" [[package]] name = "serialport" -version = "3.3.0" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8d3ecaf58010bedccae17be55d4ed6f2ecde5646fc48ce8c66ea2d35a1419c" +checksum = "22f37409d980045734250d679750bdf11bd875fec5bb5417dd21bb75d04d31a1" dependencies = [ "CoreFoundation-sys", "IOKit-sys", @@ -876,6 +933,7 @@ dependencies = [ "gtk", "libmath", "png", + "rand 0.8.0", "serialport", ] @@ -909,6 +967,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 7f7395d..a4d263e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,9 +8,10 @@ edition = "2018" [dependencies] libmath = "0.2.1" -serialport = "3.3.0" +serialport = "4.0.0" gtk = "0.9.2" gio = "0.9.1" -glib = "0.10.2" -png = "0.16.7" +glib = "0.10.3" +png = "0.16.8" cairo-rs = { version = "0.9.1", features = ["png"] } +rand = "0.8.0" diff --git a/src/graph.rs b/src/graph.rs index 95e278f..468dc45 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -1,5 +1,6 @@ use gtk::prelude::*; use gtk::DrawingArea; +use rand::prelude::*; use std::rc::Rc; pub struct Graph { @@ -8,7 +9,21 @@ pub struct Graph { scale_x_end: f64, scale_y_start: f64, scale_y_end: f64, - points: Vec<(f64,f64)> + lines: Vec +} + +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 { @@ -17,7 +32,7 @@ impl Graph { scale_x_end: f64, scale_y_start: f64, scale_y_end: f64, - points: Vec<(f64,f64)>) -> Rc { + lines: Vec) -> Rc { let graph = Rc::new(Graph { area, @@ -25,7 +40,7 @@ impl Graph { scale_x_end, scale_y_start, scale_y_end, - points + lines }); let graph_tmp = Rc::clone(&graph); @@ -76,12 +91,15 @@ impl Graph { ctx.set_line_width(2.0); ctx.set_line_cap(cairo::LineCap::Round); - for p in graph.points.iter().skip(1).enumerate() { - let xp = graph.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); + for line in graph.lines.iter() { + ctx.set_source_rgb(line.color.0, line.color.1, line.color.2); + for p in line.points.iter().skip(1).enumerate() { + 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) { diff --git a/src/lib.rs b/src/lib.rs index 9cc01c0..ef43a34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,22 +1,24 @@ mod graph; use gtk::prelude::*; -use graph::Graph; +use graph::{Graph, Line}; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; pub enum Status { JAGRIT, SAYAN, AVRODTIH } pub struct Config { - status: Status + status: Status, + bondrate: i32 } impl Config { pub fn new() -> Config { Config { - status: Status::AVRODTIH + status: Status::AVRODTIH, + bondrate: 9600 } } } @@ -25,41 +27,66 @@ enum Message { UpdateLabel(String), } -pub fn build_ui(app: >k::Application, config: Arc::) { +pub fn build_ui(app: >k::Application, config: Arc::>) { let builder = gtk::Builder::from_file("ui/main_window.glade"); 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 graph = Graph::new( + let _ = Graph::new( builder.get_object::("draw_area").expect("Resource file missing!"), - 0.0, 100.0, - 0.0, 100.0, - vec![(10.0,10.0),(20.0,20.0)] + 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)]) + ] ); win.show_all(); + let bondrate = builder.get_object::("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::().unwrap_or(9600), + 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.status { + match tmpconfig.lock().unwrap().status { Status::JAGRIT => { sender.send(Message::UpdateLabel(String::from("jagrit"))).unwrap(); }, Status::SAYAN => { - sender.send(Message::UpdateLabel(String::from("sayan"))).unwrap(); + }, Status::AVRODTIH => { sender.send(Message::UpdateLabel(String::from("avrodhit"))).unwrap(); } } }); - let bar = builder.get_object::("status_bar").expect("Resource file missing!"); + + 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); + let buf = log_area.get_buffer() + .expect("Couldn't get window"); + buf.insert(&mut buf.get_end_iter(), &text); }, } glib::Continue(true) diff --git a/src/main.rs b/src/main.rs index d7ce8f8..7476050 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,12 @@ use gio::prelude::*; use std::env::args; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; fn main() { let app = gtk::Application::new(Some("sng.trangm"), Default::default()) .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| { let config = Arc::clone(&config); diff --git a/tests/serial_port_test.rs b/tests/serial_port_test.rs index 61f6926..0ac4c33 100644 --- a/tests/serial_port_test.rs +++ b/tests/serial_port_test.rs @@ -1,13 +1,14 @@ use std::io::prelude::*; use std::io::BufReader; +use std::time::Duration; #[test] fn start() { let ports = serialport::available_ports(); println!("{:?}",ports); - let p = serialport::open("/dev/ttyUSB0") - .unwrap(); + let p = serialport::new("/dev/ttyUSB0", 9600).timeout(Duration::from_millis(10)) + .open().expect("Failed to open port"); let mut read = BufReader::new(p); let mut buf = String::new(); @@ -15,7 +16,7 @@ fn start() { match read.read_line(&mut buf) { Ok(_) => { - print!("{} seconds", buf.parse::().unwrap() / 60.0); + print!("{}", buf); buf.clear(); }, Err(_) => {} diff --git a/ui/main_window.glade b/ui/main_window.glade index 123d8bf..4a294d2 100644 --- a/ui/main_window.glade +++ b/ui/main_window.glade @@ -191,8 +191,7 @@ True False - - 200 + True False @@ -229,6 +228,25 @@ True + + + True + False + + + True + False + + 9600 + + + + + + False + True + + False @@ -263,7 +281,7 @@ True in - + True True True diff --git a/ui/main_window.glade~ b/ui/main_window.glade~ index 123d8bf..4a294d2 100644 --- a/ui/main_window.glade~ +++ b/ui/main_window.glade~ @@ -191,8 +191,7 @@ True False - - 200 + True False @@ -229,6 +228,25 @@ True + + + True + False + + + True + False + + 9600 + + + + + + False + True + + False @@ -263,7 +281,7 @@ True in - + True True True