diff --git a/Cargo.lock b/Cargo.lock index 4cd674c..f7178be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -118,13 +118,19 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "crc32fast" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", ] [[package]] @@ -311,11 +317,11 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4" +checksum = "4060f4657be78b8e766215b02b18a2e862d83745545de804638e2b545e81aee6" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi", ] @@ -556,7 +562,7 @@ checksum = "dd0eaf8df8bab402257e0a5c17a254e4cc1f72a93588a1ddfb5d356c801aa7cb" dependencies = [ "bitflags", "cc", - "cfg-if", + "cfg-if 0.1.10", "libc", "void", ] @@ -732,13 +738,13 @@ dependencies = [ [[package]] name = "rand" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12" +checksum = "c24fcd450d3fa2b592732565aa4f17a27a61c65ece4726353e000939b0edee34" dependencies = [ "libc", "rand_chacha", - "rand_core 0.6.0", + "rand_core 0.6.1", "rand_hc", ] @@ -749,7 +755,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" dependencies = [ "ppv-lite86", - "rand_core 0.6.0", + "rand_core 0.6.1", ] [[package]] @@ -769,9 +775,9 @@ checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" [[package]] name = "rand_core" -version = "0.6.0" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8b34ba8cfb21243bd8df91854c830ff0d785fff2e82ebd4434c2644cb9ada18" +checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5" dependencies = [ "getrandom", ] @@ -782,7 +788,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" dependencies = [ - "rand_core 0.6.0", + "rand_core 0.6.1", ] [[package]] @@ -827,7 +833,7 @@ dependencies = [ "CoreFoundation-sys", "IOKit-sys", "bitflags", - "cfg-if", + "cfg-if 0.1.10", "libudev", "mach 0.2.3", "nix", @@ -933,8 +939,7 @@ dependencies = [ "gtk", "libmath", "png", - "rand 0.8.0", - "regex", + "rand 0.8.1", "serialport", ] @@ -970,9 +975,9 @@ checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" [[package]] name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" +version = "0.10.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +checksum = "93c6c3420963c5c64bca373b25e77acb562081b9bb4dd5bb864187742186cea9" [[package]] name = "winapi" diff --git a/Cargo.toml b/Cargo.toml index 1a883d1..ce60aa8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,4 @@ gio = "0.9.1" glib = "0.10.3" png = "0.16.8" cairo-rs = { version = "0.9.1", features = ["png"] } -rand = "0.8.0" -regex = "1.4.2" +rand = "0.8.1" diff --git a/src/graph.rs b/src/graph.rs index e55f303..0a32253 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -3,6 +3,7 @@ use gtk::DrawingArea; use std::rc::Rc; use std::cell::RefCell; +use std::collections::HashMap; pub struct Graph { pub area: DrawingArea, @@ -12,7 +13,8 @@ pub struct Graph { pub scale_y_size: f64, pub draw_patch: bool, pub auto_adjust_y: bool, - pub lines: Vec + pub lines: HashMap, + pub pankti_sankya: f64 } pub struct Line { @@ -37,7 +39,8 @@ impl Graph { scale_y_size: f64, draw_patch: bool, auto_adjust_y: bool, - lines: Vec) -> Rc> { + lines: HashMap, + pankti_sankya: f64) -> Rc> { let graph = Rc::new(RefCell::new(Graph { area, @@ -47,7 +50,8 @@ impl Graph { scale_y_size, draw_patch, auto_adjust_y, - lines + lines, + pankti_sankya })); let graph_tmp = Rc::clone(&graph); @@ -69,11 +73,11 @@ impl Graph { let width = area.get_allocated_width() as f64; let height = area.get_allocated_height() as f64; - graph.borrow_mut().trim_lines(); if graph.borrow().auto_adjust_y { graph.borrow_mut().adjust_scale_automatic_y(); } - // graph.borrow_mut().adjust_scale_automatic_x(); + graph.borrow_mut().adjust_scale_automatic_x(); + graph.borrow_mut().trim_lines(); Graph::draw_boxes(ctx, width, height, 40.0, 20.0, 5.0, 0.3); Graph::draw_boxes(ctx, width, height, 40.0, 20.0, 50.0, 0.1); @@ -89,7 +93,7 @@ impl Graph { ctx.set_line_width(2.0); ctx.set_line_cap(cairo::LineCap::Round); let draw_patch = graph.borrow().draw_patch; - for line in graph.borrow().lines.iter() { + for (_,line) in graph.borrow().lines.iter() { for p in line.points.iter().enumerate() { let xp = if p.0 < line.points.len() - 1 { line.points[p.0 + 1] @@ -147,11 +151,20 @@ impl Graph { } pub fn adjust_scale_automatic_y(&mut self) { - + if self.lines.len() == 0 { + return; + } + let mut mx:Option = None; let mut mi:Option = None; - for line in self.lines.iter() { + for (_, line) in self.lines.iter() { + if line.points.len() == 0 { + mx = Some(0.0); + mi = Some(0.0); + continue; + } + if let None = mx { mx = Some(line.points[0].1); } @@ -166,15 +179,8 @@ impl Graph { } } - let mx = match mx { - Some(val) => val, - None => 0.0 - }; - - let mi = match mi { - Some(val) => val, - None => 0.0 - }; + let mx = mx.unwrap(); + let mi = mi.unwrap(); let spread = (mx - mi).abs(); @@ -183,10 +189,20 @@ impl Graph { } pub fn adjust_scale_automatic_x(&mut self) { + if self.lines.len() == 0 { + return; + } + let mut mx:Option = None; let mut mi:Option = None; - for line in self.lines.iter() { + for (_, line) in self.lines.iter() { + if line.points.len() == 0 { + mx = Some(0.0); + mi = Some(0.0); + continue; + } + if let None = mx { mx = Some(line.points[0].0); } @@ -211,27 +227,24 @@ impl Graph { } pub fn trim_lines(&mut self) { - let mut j = 0; - while j < self.lines.len() { + for (_, line) in self.lines.iter_mut() { let mut i = 0; - while i < self.lines[j].points.len() { - println!("{:?}", self.lines[j].points); - match self.lines[j].points.get(i + 2) { + while i < line.points.len() { + match line.points.get(i + 2) { Some(_) => { - if self.lines[j].points[i+1].0 < self.scale_x_start { - self.lines[j].points.remove(i); + if line.points[i+1].0 < self.scale_x_start { + line.points.remove(i); } }, None => { - if self.lines[j].points[self.lines[j].points.len() - 1].0 < self.scale_x_start { - self.lines.remove(j); + if line.points[line.points.len() - 1].0 < self.scale_x_start { + line.points.clear(); break; } } } i += 1; } - j += 1; } } } diff --git a/src/lib.rs b/src/lib.rs index 2d1edc0..9eac61b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,16 @@ mod graph; use gtk::prelude::*; -use graph::{Graph, Line}; -use std::sync::{Arc, Mutex}; +use rand::Rng; + +use std::{collections::HashMap, sync::{Arc, Mutex}}; use std::rc::Rc; - use std::io::prelude::*; use std::io::BufReader; +use graph::Graph; + pub enum Status { JAGRIT, SAYAN, AVRODTIH, PARIVARTIT, NIKAS } @@ -48,15 +50,32 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { 0.0, 100.0, true, true, - vec![ - Line::new(1.0,1.0,0.0,vec![(10.0,10.0),(20.0,20.0),(30.0,25.0), (40.0, 50.0),(50.0,25.0)]), - // Line::new(1.0,0.0,0.0,vec![(50.0,10.0),(70.0,60.0)]), - // Line::new(0.0,1.0,0.0,vec![(50.0,50.0)]) - ] + HashMap::new(), + 0.0 ); win.show_all(); + // exit_menu + let exit_menu = builder.get_object::("exit_menu").expect("Resource file missing!"); + let tmp_win = win.clone(); + exit_menu.connect_activate(move |_|{ + unsafe { + tmp_win.destroy(); + } + }); + + // about_menu + let about_menu = builder.get_object::("about_menu").expect("Resource file missing!"); + let about = builder.get_object::("about").expect("Resource file missing!"); + + about_menu.connect_activate(move |_|{ + about.show_all(); + }); + + // save_log + + // pankti let pankti = builder.get_object::("pankti").expect("Resource file missing!"); @@ -167,6 +186,7 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { let tmp_graph = Rc::clone(&graph); clear_graph.connect_clicked(move |_ | { + tmp_graph.borrow_mut().pankti_sankya = 0.0; tmp_graph.borrow_mut().lines.clear(); tmp_graph.borrow().area.queue_draw(); }); @@ -176,9 +196,13 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { let tmp_bar = bar.clone(); let tmp_config = Arc::clone(&config); + let tmp_graph = Rc::clone(&graph); jagrit_btn.connect_clicked(move |_ | { match tmp_config.lock() { Ok(mut config) => { + tmp_graph.borrow_mut().pankti_sankya = 0.0; + tmp_graph.borrow_mut().lines.clear(); + tmp_graph.borrow().area.queue_draw(); tmp_bar.push(1, "Jagrit"); config.status = Status::PARIVARTIT; }, Err(_) => { @@ -187,7 +211,7 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { } }); - //jagrit_btn + //avrodith_btn let avrodith_btn = builder.get_object::("avrodith_btn").expect("Resource file missing!"); let tmp_bar = bar.clone(); @@ -276,11 +300,66 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { }); let full_log = builder.get_object::("full_log").expect("Resource file missing!"); + let tmp_graph = Rc::clone(&graph); receiver.attach(None, move |msg| { match msg { Message::Msg(text) => { - if !full_log.get_active() && text.starts_with("#") { + if text.starts_with("#") { + tmp_graph.borrow_mut().pankti_sankya += 1.0; + for (index, line) in text[1..].split(" ").enumerate() { + let part = line.split("="); + let part = part.into_iter().collect::>(); + if part.len() == 1 { + let num = match part[0].trim().parse::() { + Ok(val) => val, + Err(_) => { + continue; + } + }; + let mut gp = tmp_graph.borrow_mut(); + + let sankhya = gp.pankti_sankya; + match gp.lines.get_mut(&index.to_string()) { + Some(val) => { + val.points.push((sankhya, num)); + } None => { + let v = vec![(sankhya, num)]; + let mut rng = rand::thread_rng(); + gp.lines.insert(index.to_string(), graph::Line::new(rng.gen_range(0.0..1.0), 0.0, rng.gen_range(0.0..1.0), v)); + } + } + gp.area.queue_draw(); + } else if part.len() == 2 { + let num = match part[1].trim().parse::() { + Ok(val) => val, + Err(_) => { + continue; + } + }; + let mut gp = tmp_graph.borrow_mut(); + + let sankhya = gp.pankti_sankya; + match gp.lines.get_mut(part[0]) { + Some(val) => { + val.points.push((sankhya, num)); + } None => { + let v = vec![(sankhya, num)]; + + let mut rng = rand::thread_rng(); + gp.lines.insert(part[0].to_owned(), graph::Line::new(rng.gen_range(0.0..1.0), 0.0, rng.gen_range(0.0..1.0), v)); + } + } + gp.area.queue_draw(); + } + } + + if full_log.get_active(){ + 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); + } } else { let buf = log_area.get_buffer() .expect("Couldn't get log_area"); @@ -295,7 +374,7 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { glib::Continue(true) }); - + // Time ke hisab se pankti ko aage bhadhay // let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); // glib::timeout_add(300, move || { // sender.send(()).unwrap(); diff --git a/tests/plottest.rs b/tests/plottest.rs index b68374a..d1ecdad 100644 --- a/tests/plottest.rs +++ b/tests/plottest.rs @@ -1,34 +1,5 @@ #[test] fn start() { - let a = vec![0,1,2,3,4,5,6,7,8,9]; - let mut skiped = false; - let b: Vec = a.into_iter().filter(|x| { - if *x <= 4 { - return false; - } - - if !skiped { - skiped = true; - return false; - } - - return true; - }).collect(); - - println!("{:?}", b); - - // for (i, x) in a.iter().enumerate() { - // if *x <= 4 { - // continue; - // } - - // if !skiped { - // skiped = true; - // continue; - // } - - // a.remove(i); - // } } \ No newline at end of file diff --git a/ui/main_window.glade b/ui/main_window.glade index bd6d6d0..ff44941 100644 --- a/ui/main_window.glade +++ b/ui/main_window.glade @@ -2,6 +2,42 @@ + + False + dialog + Tarangam (तरंगम्) + A simple serial plotter. +एक सरल सीरीय्ल पलौटर। + Piyush Mishra + image-missing + + + False + vertical + 2 + + + False + end + + + + + + + + + False + False + 0 + + + + + + + + True False @@ -44,7 +80,7 @@ True False - + Save Log True False @@ -59,7 +95,7 @@ - + gtk-quit True False @@ -82,7 +118,7 @@ True False - + gtk-about True False @@ -213,25 +249,6 @@ True - - - True - False - - - Timeframe - True - True - False - True - - - - - False - True - - True diff --git a/ui/main_window.glade~ b/ui/main_window.glade~ index bd6d6d0..ea6ea31 100644 --- a/ui/main_window.glade~ +++ b/ui/main_window.glade~ @@ -2,6 +2,42 @@ + + False + dialog + Tarangam (तरंगम्) + A simple serial plotter. +एक सरल सीरीय्ल पलौटर। + Piyush Mishra + image-missing + + + False + vertical + 2 + + + False + end + + + + + + + + + False + False + 0 + + + + + + + + True False @@ -59,7 +95,7 @@ - + gtk-quit True False @@ -82,7 +118,7 @@ True False - + gtk-about True False @@ -213,25 +249,6 @@ True - - - True - False - - - Timeframe - True - True - False - True - - - - - False - True - - True