diff --git a/Cargo.lock b/Cargo.lock index e3599a9..4cd674c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -796,9 +796,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.3.9" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" +checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c" dependencies = [ "aho-corasick", "memchr", @@ -808,9 +808,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.18" +version = "0.6.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" +checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189" [[package]] name = "serde" @@ -934,6 +934,7 @@ dependencies = [ "libmath", "png", "rand 0.8.0", + "regex", "serialport", ] diff --git a/Cargo.toml b/Cargo.toml index a4d263e..1a883d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ glib = "0.10.3" png = "0.16.8" cairo-rs = { version = "0.9.1", features = ["png"] } rand = "0.8.0" +regex = "1.4.2" diff --git a/src/graph.rs b/src/graph.rs index f6c4c65..85359d8 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -2,14 +2,16 @@ use gtk::prelude::*; use gtk::DrawingArea; use std::rc::Rc; +use std::cell::RefCell; pub struct Graph { - area: DrawingArea, - scale_x_start: f64, - scale_x_end: f64, - scale_y_start: f64, - scale_y_end: f64, - lines: Vec + pub area: DrawingArea, + pub scale_x_start: f64, + pub scale_x_size: f64, + pub scale_y_start: f64, + pub scale_y_size: f64, + pub draw_patch: bool, + pub lines: Vec } pub struct Line { @@ -29,22 +31,24 @@ impl Line { impl Graph { pub fn new(area: DrawingArea, scale_x_start: f64, - scale_x_end: f64, + scale_x_size: f64, scale_y_start: f64, - scale_y_end: f64, - lines: Vec) -> Rc { + scale_y_size: f64, + draw_patch: bool, + lines: Vec) -> Rc> { - let graph = Rc::new(Graph { + let graph = Rc::new(RefCell::new(Graph { area, scale_x_start, - scale_x_end, + scale_x_size, scale_y_start, - scale_y_end, + scale_y_size, + draw_patch, lines - }); + })); let graph_tmp = Rc::clone(&graph); - graph.area.connect_draw(move |area,ctx| { + graph.borrow().area.connect_draw(move |area,ctx| { Graph::draw(area, ctx, &graph_tmp); Inhibit(false) }); @@ -54,52 +58,70 @@ impl Graph { fn draw(area: >k::DrawingArea, ctx: &cairo::Context, - graph: &Rc) { - + graph: &Rc>) { + ctx.set_source_rgb(0.1, 0.5, 0.5); ctx.paint(); let width = area.get_allocated_width() as f64; let height = area.get_allocated_height() as f64; + graph.borrow_mut().adjust_scale_automatic_y(); + graph.borrow_mut().adjust_scale_automatic_x(); + 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); - + let cell_size = 50.0; let v_bars = math::round::ceil(width/cell_size, 0) as i32; let h_bars= math::round::ceil(height/cell_size, 0) as i32; - let h_scale = (graph.scale_x_end - graph.scale_x_start)/(v_bars - 1) as f64; // ms + let h_scale = (graph.borrow().scale_x_size)/(v_bars - 1) as f64; // ms + let v_scale = (graph.borrow().scale_y_size)/(h_bars - 1) as f64; // ms + + 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 p in line.points.iter().enumerate() { + let xp = if p.0 < line.points.len() - 1 { + line.points[p.0 + 1] + } else { + line.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.borrow().scale_x_start))/h_scale + 40.0, height - ((cell_size as f64)*(xp.1 - graph.borrow().scale_y_start))/v_scale - 20.0); + ctx.line_to(((cell_size as f64)*(p.1.0 - graph.borrow().scale_x_start))/h_scale + 40.0, height - ((cell_size as f64)*(p.1.1 - graph.borrow().scale_y_start))/v_scale - 20.0); + ctx.stroke(); + + if draw_patch { + ctx.set_source_rgb(0.0, 0.0, 1.0); + ctx.arc(((cell_size as f64)*(p.1.0 - graph.borrow().scale_x_start))/h_scale + 40.0, height - ((cell_size as f64)*(p.1.1 - graph.borrow().scale_y_start))/v_scale - 20.0, 5.0, 0.0, std::f64::consts::PI * 2.0); + ctx.stroke(); + } + } + } + + ctx.set_source_rgb(0.1, 0.4, 0.4); + ctx.rectangle(0.0, 0.0, 40.0, height); + ctx.rectangle(0.0, height - 20.0, width, 20.0); + ctx.fill(); ctx.set_source_rgb(1.0, 1.0, 1.0); for x in 0..v_bars { - let text = math::round::floor(x as f64 * h_scale + graph.scale_x_start, 1).to_string(); + let text = math::round::floor(x as f64 * h_scale + graph.borrow().scale_x_start, 1).to_string(); let f = ctx.text_extents(&text); ctx.move_to(40.0 + x as f64 * cell_size - f.width, height - 10.0); ctx.show_text(&text); } - let v_scale = (graph.scale_y_end - graph.scale_y_start)/(h_bars - 1) as f64; // ms - for y in (0..h_bars).rev() { - let text = math::round::floor(y as f64 * v_scale + graph.scale_y_start, 1).to_string(); + let text = math::round::floor(y as f64 * v_scale + graph.borrow().scale_y_start, 1).to_string(); let f = ctx.text_extents(&text); ctx.move_to(40.0 - f.width, height - y as f64 * cell_size - f.height - 15.0); ctx.show_text(&text); } - - ctx.set_line_width(2.0); - ctx.set_line_cap(cairo::LineCap::Round); - 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(); - } } fn draw_boxes(ctx: &cairo::Context, area_width: f64, area_height: f64, src_x: f64, src_y: f64, cell_size: f64, color: f64) { @@ -117,4 +139,34 @@ impl Graph { } ctx.stroke(); } + + pub fn adjust_scale_automatic_y(&mut self) { + + let mut mx:Option = None; + let mut mi:Option = None; + + for line in self.lines.iter() { + if let None = mx { + mx = Some(line.points[0].0); + } + + if let None = mi { + mi = Some(line.points[0].0); + } + + for (_,y) in line.points.iter().skip(1) { + mx = Some(f64::max(mx.unwrap(), *y)); + mi = Some(f64::min(mi.unwrap(), *y)); + } + } + + let spread = (mx.unwrap() - mi.unwrap()).abs(); + + self.scale_y_start = mi.unwrap() - spread * 0.1; + self.scale_y_size = spread * 1.2; + } + + pub fn adjust_scale_automatic_x(&mut self) { + + } } diff --git a/src/lib.rs b/src/lib.rs index ad7b5ba..9ab1440 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ use gtk::prelude::*; use graph::{Graph, Line}; use std::sync::{Arc, Mutex}; +use std::rc::Rc; use std::io::prelude::*; use std::io::BufReader; @@ -41,19 +42,71 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { let bar = builder.get_object::("status_bar").expect("Resource file missing!"); let log_area = builder.get_object::("log_area").expect("Resource file missing!"); - let _ = Graph::new( + let graph = Graph::new( builder.get_object::("draw_area").expect("Resource file missing!"), 0.0, 100.0, 0.0, 100.0, + true, 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,0.0,0.0,vec![(50.0,10.0),(70.0,60.0)]) + 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)]) ] ); win.show_all(); + // pankti + let pankti = builder.get_object::("pankti").expect("Resource file missing!"); + + let tmp_graph = Rc::clone(&graph); + pankti.connect_value_changed(move |btn| { + tmp_graph.borrow_mut().scale_x_size = btn.get_value(); + tmp_graph.borrow().area.queue_draw(); + }); + + // stambh_1 + let stambh_1 = builder.get_object::("stambh_1").expect("Resource file missing!"); + + let tmp_bar = bar.clone(); + let tmp_graph = Rc::clone(&graph); + stambh_1.connect_changed(move |entry| { + let val = entry.get_text().parse::().unwrap_or(0.0); + let purana_y_start = tmp_graph.borrow().scale_y_start; + let y_size = tmp_graph.borrow().scale_y_size; + tmp_graph.borrow_mut().scale_y_start = val; + tmp_graph.borrow_mut().scale_y_size = y_size + (purana_y_start - val); + tmp_graph.borrow().area.queue_draw(); + }); + + // stambh_2 + let stambh_2 = builder.get_object::("stambh_2").expect("Resource file missing!"); + + let tmp_graph = Rc::clone(&graph); + stambh_2.connect_changed(move |entry| { + let val = entry.get_text().parse::().unwrap_or(0.0); + let y_start = tmp_graph.borrow().scale_y_start; + tmp_graph.borrow_mut().scale_y_size = (val - y_start).abs(); + tmp_graph.borrow().area.queue_draw(); + }); + + // nimna_stambh + let nimna_stambh = builder.get_object::("nimna_stambh").expect("Resource file missing!"); + + nimna_stambh.connect_clicked(move |btn| { + stambh_1.set_sensitive(btn.get_active()); + stambh_2.set_sensitive(btn.get_active()); + }); + + // draw_patches + let draw_patches = builder.get_object::("draw_patches").expect("Resource file missing!"); + + let tmp_graph = Rc::clone(&graph); + draw_patches.connect_clicked(move |btn| { + tmp_graph.borrow_mut().draw_patch = btn.get_active(); + tmp_graph.borrow().area.queue_draw(); + }); + // Bondrate let bondrate = builder.get_object::("bondrate").expect("Resource file missing!"); @@ -107,7 +160,16 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { } }); - //jagrit_btn + // clear_graph + let clear_graph = builder.get_object::("clear_graph").expect("Resource file missing!"); + + let tmp_graph = Rc::clone(&graph); + clear_graph.connect_clicked(move |_ | { + tmp_graph.borrow_mut().lines.clear(); + tmp_graph.borrow().area.queue_draw(); + }); + + // jagrit_btn let jagrit_btn = builder.get_object::("jagrit_btn").expect("Resource file missing!"); let tmp_bar = bar.clone(); @@ -161,7 +223,7 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { let tmp_bar = bar.clone(); let tmp_config = Arc::clone(&config); - send_btn.connect_activate(move |_| { + send_btn.connect_clicked(move |_| { send_text(&tmp_config, &send_entry, &tmp_bar); }); @@ -230,23 +292,39 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { } glib::Continue(true) }); + + + // let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); + // glib::timeout_add(100, move || { + // sender.send(()).unwrap(); + // glib::Continue(true) + // }); + + // let tmp_graph = Rc::clone(&graph); + // receiver.attach(None, move |_| { + // tmp_graph.borrow_mut().scale_x_start += 1.0; + // tmp_graph.borrow().area.queue_draw(); + // glib::Continue(true) + // }); } fn send_text(config: &Arc>, entry: >k::Entry, bar: >k::Statusbar) { match config.lock() { Ok(config) => { - let mut p = match serialport::new(&config.port, config.bondrate).open() { - Ok(p) => p, - Err(_) => { - bar.push(1, "Failed to change port!"); - return; + if let Status::JAGRIT = config.status { + let mut p = match serialport::new(&config.port, config.bondrate).open() { + Ok(p) => p, + Err(_) => { + bar.push(1, "Failed to change port!"); + return; + } + }; + + unsafe { + p.write_all(entry.get_text().to_string().as_bytes_mut()).unwrap(); } - }; - - unsafe { - p.write_all(entry.get_text().to_string().as_bytes_mut()).unwrap(); + entry.set_text(""); } - entry.set_text(""); }, Err(_) => { bar.push(1, "Failed to change port!"); } diff --git a/tests/plottest.rs b/tests/plottest.rs index 69f669d..c3b0b5f 100644 --- a/tests/plottest.rs +++ b/tests/plottest.rs @@ -1,5 +1,8 @@ #[test] fn start() { - + let a = [0,1,2,3,4,5,6,7,8,9]; + for x in a.iter().skip(1) { + println!("{}", x); + } } \ No newline at end of file diff --git a/ui/main_window.glade b/ui/main_window.glade index bf56256..bd6d6d0 100644 --- a/ui/main_window.glade +++ b/ui/main_window.glade @@ -2,11 +2,18 @@ - + True False document-save + + 5 + 500 + 100 + 0.5 + 10 + True False @@ -41,7 +48,7 @@ Save Log True False - image1 + document_save False @@ -185,13 +192,242 @@ True True - + 500 - 450 True False - True - True + vertical + + + True + False + + + True + False + Clear + True + + + False + True + + + + + True + False + + + Timeframe + True + True + False + True + + + + + False + True + + + + + True + False + + + Patches + True + True + False + True + True + + + + + False + True + + + + + True + False + + + False + True + + + + + True + False + + + True + False + 4 + Pankti + + + + + False + True + + + + + True + False + + + True + True + pankti_adjustment + 1 + True + True + + + + + False + True + + + + + False + True + 0 + + + + + 500 + 450 + True + False + True + True + + + False + True + 1 + + + + + True + False + + + True + False + + + nimna + True + True + False + True + + + + + False + True + + + + + True + False + + + True + False + 4 + S tambh 1 + + + + + False + True + + + + + True + False + True + + + True + False + True + 8 + 0 + + + + + False + True + + + + + True + False + + + True + False + 4 + Stambh 2 + + + + + False + True + + + + + True + False + True + + + True + False + True + 8 + 100 + + + + + False + True + + + + + False + True + 2 + + True @@ -305,7 +541,7 @@ - True + False True diff --git a/ui/main_window.glade~ b/ui/main_window.glade~ index bf56256..bd6d6d0 100644 --- a/ui/main_window.glade~ +++ b/ui/main_window.glade~ @@ -2,11 +2,18 @@ - + True False document-save + + 5 + 500 + 100 + 0.5 + 10 + True False @@ -41,7 +48,7 @@ Save Log True False - image1 + document_save False @@ -185,13 +192,242 @@ True True - + 500 - 450 True False - True - True + vertical + + + True + False + + + True + False + Clear + True + + + False + True + + + + + True + False + + + Timeframe + True + True + False + True + + + + + False + True + + + + + True + False + + + Patches + True + True + False + True + True + + + + + False + True + + + + + True + False + + + False + True + + + + + True + False + + + True + False + 4 + Pankti + + + + + False + True + + + + + True + False + + + True + True + pankti_adjustment + 1 + True + True + + + + + False + True + + + + + False + True + 0 + + + + + 500 + 450 + True + False + True + True + + + False + True + 1 + + + + + True + False + + + True + False + + + nimna + True + True + False + True + + + + + False + True + + + + + True + False + + + True + False + 4 + S tambh 1 + + + + + False + True + + + + + True + False + True + + + True + False + True + 8 + 0 + + + + + False + True + + + + + True + False + + + True + False + 4 + Stambh 2 + + + + + False + True + + + + + True + False + True + + + True + False + True + 8 + 100 + + + + + False + True + + + + + False + True + 2 + + True @@ -305,7 +541,7 @@ - True + False True