Commit 01a8de59 authored by Simon Wörner's avatar Simon Wörner

added measure data vector

parent ae6ed20b
......@@ -7,6 +7,7 @@ authors = ["Simon Wörner <git@simon-woerner.de>"]
measure = []
[dependencies]
nix = "0.8.1"
regex = "^0.2"
separator = "^0.3.1"
time = "^0.1.36"
nix = "0.8.1"
\ No newline at end of file
extern crate kawaii;
use std::time::Duration;
use std::thread::sleep;
use kawaii::gpio::{SyncPort, AsyncPort, Direction, Edge, Value};
use kawaii::measure::Measure;
fn main() {
println!("Hello, world!");
#[cfg(feature = "measure")]
{
let mut measure = Measure::new(String::from("main({test: 1})"));
for _ in 0..5 {
measure.start();
sleep(Duration::new(0, 1_000_000));
measure.stop();
}
println!("{:?}", measure);
}
let mut trigger = SyncPort::new(27, Direction::Out).expect("Create Trigger GPIO failed");
let mut echo = AsyncPort::new(28, Edge::Both).expect("Create Echo GPIO failed");
......
extern crate time;
extern crate regex;
extern crate separator;
extern crate time;
use self::time::precise_time_ns;
use std;
use std::fs::File;
use std::io::prelude::*;
use std::io::{Error, ErrorKind};
use self::regex::Regex;
use self::separator::Separatable;
use self::time::precise_time_ns;
#[derive(Debug)]
pub struct Measure {
......@@ -10,6 +17,7 @@ pub struct Measure {
pub max: u64,
time: u64,
last: u64,
data: Vec<u64>,
pub name: String,
}
......@@ -20,6 +28,7 @@ impl Measure {
max: 0u64,
time: 0u64,
last: 0u64,
data: Vec::with_capacity(1_000_000),
name: name,
}
}
......@@ -31,7 +40,7 @@ impl Measure {
pub fn pause(&mut self) {
if self.last == 0 {
#[cfg(debug_assertions)]
println!("WARNING: Measure('{:?}') pause called without start!", self);
println!("WARNING: {:?} pause called without start!", self);
return;
}
......@@ -40,14 +49,15 @@ impl Measure {
}
pub fn stop(&mut self) {
if self.time == 0 {
if self.last == 0 {
#[cfg(debug_assertions)]
println!("WARNING: Measure('{:?}') stop called without start!", self);
println!("WARNING: {:?} stop called without start!", self);
return;
}
self.time += self.time_diff();
self.data.push(self.time);
if self.time < self.min {
self.min = self.time;
......@@ -68,6 +78,23 @@ impl Measure {
time_diff
}
fn write_data(&self) -> std::io::Result<()> {
let re = Regex::new(r"[^\w\-.]")
.or(Err(Error::new(ErrorKind::Other, "Create filename regex failed.")))?;
let file_name = format!("measure_{}.txt", self.name);
let file_name = re.replace_all(file_name.as_str(), "_").to_string();
println!("{}: Write data to {}", self.name, file_name);
let mut file = File::create(file_name)?;
for value in &self.data {
file.write_fmt(format_args!("{}\n", value))?;
}
Ok(())
}
}
impl Drop for Measure {
......@@ -75,6 +102,10 @@ impl Drop for Measure {
println!("{}:\n\tmin: {} ns\n\tmax: {} ns",
self.name,
self.min.separated_string(),
self.max.separated_string())
self.max.separated_string());
if let Err(e) = self.write_data() {
println!("{}: Write measure data failed: {}", self.name, e);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment