Skip to content

Commit

Permalink
Fixed Missing Graph Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
PiyushXCoder committed Feb 26, 2021
1 parent 32136bb commit 4c14c62
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 12 deletions.
75 changes: 74 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tarangam"
version = "0.1.1"
version = "0.1.2"
authors = ["PiyushXCoder <piyush.raj.kit@gmail.com>"]
license = "GPL 3.0"
edition = "2018"
Expand All @@ -12,10 +12,12 @@ keywords = ["plotter","serial","serialplotter","arduino","gtk"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
gtk = "0.9.2"
gdk = "0.13.2"
gio = "0.9.1"
glib = "0.10.3"
png = "0.16.8"
cairo-rs = { version = "0.9.1", features = ["png"] }
rand = "0.8.1"
libmath = "0.2.1"
serialport = "4.0.0"
serialport = "4.0.0"
clap = "2.33.3"
36 changes: 36 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
This file is part of Tarangam.
Tarangam is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Tarangam is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Tarangam. If not, see <https://www.gnu.org/licenses/>
*/

//! Feel free to see through codes. Application is not written to be used as a library for other app. :)

pub struct Config {
pub ui_file: String
}

impl Config {
pub fn generate() -> Self {
let ui_file = std::env::var("TARANGAM_UI_FILE");

Config {
ui_file: match ui_file {
Ok(val) => val,
Err(_) => std::env::current_exe().unwrap().parent().unwrap()
.join("ui.glade").to_str().unwrap().to_owned()
}
}
}
}
1 change: 1 addition & 0 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::rc::Rc;
use std::cell::RefCell;
use std::collections::HashMap;
/// A single line
#[derive(Debug)]
pub struct Line {
pub points: Vec<(f64,f64)>,
pub color: (f64, f64, f64)
Expand Down
32 changes: 27 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,23 @@ impl Config {
}

/// For communication between mpsc of graph and serial port
#[derive(Debug)]
enum MessageSerialThread {
Msg(String, MessageSerialThreadMsgType),
Points(Vec<(String, f64)>),
Status(String)
}

#[derive(Debug)]
enum MessageSerialThreadMsgType {
Point,
Log
}

// Building and configuring GUI
pub fn build_ui(app: &gtk::Application) {
pub fn build_ui(app: &gtk::Application, ui_file: &str) {
let config = Arc::new(Mutex::new(Config::new()));
let builder = gtk::Builder::from_file(std::env::current_exe().unwrap().parent().unwrap().join("ui.glade"));
let builder = gtk::Builder::from_file(ui_file);

let win = builder.get_object::<gtk::ApplicationWindow>("win").expect("Resource file missing!");
win.set_application(Some(app));
Expand Down Expand Up @@ -107,7 +109,7 @@ pub fn build_ui(app: &gtk::Application) {
let about_menu = builder.get_object::<gtk::MenuItem>("about_menu").expect("Resource file missing!");
let about_window = builder.get_object::<gtk::AboutDialog>("about_window").expect("Resource file missing!");
about_window.set_transient_for(Some(&win));

about_window.set_version(Some(env!("CARGO_PKG_VERSION")));
about_window.connect_delete_event(|win,_| {
win.hide();
Inhibit(true)
Expand Down Expand Up @@ -389,14 +391,15 @@ pub fn build_ui(app: &gtk::Application) {

// Reciver for MessageSerialThread from the "Thread to manage Serial Port" and works accordingly
let full_log = builder.get_object::<gtk::CheckButton>("full_log").expect("Resource file missing!");
let graph_data = builder.get_object::<gtk::TextView>("graph_data").expect("Resource file missing!");
let tmp_graph = Rc::clone(&graph);
receiver.attach(None, move |msg| {
match msg {
MessageSerialThread::Msg(text, msg_type) => {
receiver_for_msg(text, &msg_type, &full_log, &log_area);
},
MessageSerialThread::Points(points) => {
receiver_for_points(points, &tmp_graph);
receiver_for_points(points, &tmp_graph, &graph_data);
}
MessageSerialThread::Status(text) => {
bar.push(1, &text);
Expand Down Expand Up @@ -499,7 +502,7 @@ fn receiver_for_msg(text: String, msg_type: &MessageSerialThreadMsgType, full_lo
}

// Receives MessageSerialThread from Serial Port managing thread and add points to draw on graph
fn receiver_for_points(points: Vec<(String, f64)>, graph: &Rc<RefCell<Graph>>) {
fn receiver_for_points(points: Vec<(String, f64)>, graph: &Rc<RefCell<Graph>>, graph_data: &gtk::TextView) {
for (line, point) in points {
let mut gp = graph.borrow_mut();

Expand All @@ -511,6 +514,25 @@ fn receiver_for_points(points: Vec<(String, f64)>, graph: &Rc<RefCell<Graph>>) {
let v = vec![(sankhya, point)];
let mut rng = rand::thread_rng();
gp.lines.insert(line, graph::Line::new(rng.gen_range(0.0..1.0), 0.0, rng.gen_range(0.0..1.0), v));
let buf = graph_data.get_buffer().expect("Couldn't get graph_data");
buf.set_text("");
gp.lines.iter().for_each(|(key, line)| {
buf.insert(&mut buf.get_end_iter(), "##");

let tag = gtk::TextTag::new(None);
let rgba = gdk::RGBA {
red: line.color.0,
green: line.color.1,
blue: line.color.2,
alpha: 1.0
};
tag.set_property_background_rgba(Some(&rgba));
tag.set_property_foreground_rgba(Some(&rgba));
buf.get_tag_table().unwrap().add(&tag);
buf.apply_tag(&tag, &buf.get_iter_at_offset(buf.get_end_iter().get_offset() - 2), &buf.get_end_iter());
buf.insert(&mut buf.get_end_iter(), &format!(" {}, ", key));
});
graph_data.queue_draw();
}
}
gp.redraw();
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

mod config;

use gio::prelude::*;
use std::env::args;

fn main() {
let conf = config::Config::generate();

let app = gtk::Application::new(Some("sng.tarangm"), Default::default())
.expect("Failed to initiate gtk");

app.connect_activate(move |app| {
tarangam::build_ui(app);
tarangam::build_ui(app, &conf.ui_file);
});

app.run(&args().collect::<Vec<_>>());
Expand Down
29 changes: 26 additions & 3 deletions ui.glade
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<property name="icon">chitra.svg</property>
<property name="type-hint">dialog</property>
<property name="program-name">Tarangam (तरंगम्)</property>
<property name="version">0.1.1</property>
<property name="comments" translatable="yes">A simple serial plotter.
एक सरल सीरीय्ल पलौटर।</property>
<property name="website">https://github.com/PiyushXCoder/Tarangam</property>
Expand Down Expand Up @@ -422,6 +421,30 @@
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="height-request">35</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="vscrollbar-policy">never</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkTextView" id="graph_data">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="editable">False</property>
<property name="left-margin">5</property>
<property name="top-margin">7</property>
<property name="cursor-visible">False</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkDrawingArea" id="draw_area">
<property name="width-request">500</property>
Expand All @@ -434,7 +457,7 @@
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
<property name="position">2</property>
</packing>
</child>
<child>
Expand Down Expand Up @@ -540,7 +563,7 @@
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
<property name="position">3</property>
</packing>
</child>
</object>
Expand Down

0 comments on commit 4c14c62

Please sign in to comment.