It works
This commit is contained in:
77
src/main.rs
77
src/main.rs
@@ -1,10 +1,10 @@
|
||||
use core::f32;
|
||||
use std::io::stdin;
|
||||
use std::{io::stdin, time::Duration};
|
||||
|
||||
use macroquad::prelude::*;
|
||||
|
||||
const THICKNESS: f32 = 2.0;
|
||||
const TAIL_LEN: usize = 250;
|
||||
const TAIL_LEN: usize = 500;
|
||||
|
||||
#[derive(Clone,Copy, PartialEq)]
|
||||
struct Point {
|
||||
@@ -61,10 +61,10 @@ struct Tail {
|
||||
px_per_s: f32,
|
||||
}
|
||||
impl Tail {
|
||||
fn new(axis: Axis) -> Self {
|
||||
fn new(axis: Axis, y_axis_offset: f32) -> Self {
|
||||
let (x_offset, y_offset) = match axis {
|
||||
Axis::One => (0., screen_height() /2.),
|
||||
Axis::Two => (screen_width() /2., screen_height() /2.),
|
||||
Axis::One => (0., y_axis_offset),
|
||||
Axis::Two => (screen_width() /2., y_axis_offset),
|
||||
};
|
||||
Self {
|
||||
// start all the dots off screen
|
||||
@@ -73,7 +73,7 @@ impl Tail {
|
||||
axis,
|
||||
x_offset,
|
||||
y_offset,
|
||||
px_per_s: 1000.,
|
||||
px_per_s: 750.,
|
||||
}
|
||||
}
|
||||
fn place_next(&mut self, delta_time: f32, x_displace: f32, y_displace: f32) {
|
||||
@@ -147,9 +147,10 @@ impl Tail {
|
||||
#[macroquad::main("Graph")]
|
||||
async fn main() {
|
||||
// Dot params
|
||||
let mut y_displacement = 0.;
|
||||
let mut x_displacement = 0.;
|
||||
let mut tail = Tail::new(Axis::One);
|
||||
let realtive_y_origin =screen_height()*0.75;
|
||||
// FIXME realtive_x_origin doesn't work yet
|
||||
let realtive_x_origin =0.;
|
||||
let mut tail = Tail::new(Axis::One, realtive_y_origin);
|
||||
|
||||
// Selection box
|
||||
let mut inital_x_pos = 0.;
|
||||
@@ -158,17 +159,60 @@ async fn main() {
|
||||
// Tooling
|
||||
let mut show_debug = true;
|
||||
|
||||
let stdin = stdin();
|
||||
let handle= stdin.lock();
|
||||
|
||||
// Serial
|
||||
let port = serialport::available_ports()
|
||||
.expect("No ports found!")
|
||||
.iter()
|
||||
.filter(|x| x.port_name.contains("ACM"))
|
||||
.fold(None, |_, x| {
|
||||
let x = serialport::new(x.port_name.clone(), 57600)
|
||||
.timeout(Duration::from_millis(10))
|
||||
.open().expect("Failed to open port");
|
||||
Some(x)
|
||||
});
|
||||
let mut port = port.expect("Failed to find valid serial port");
|
||||
let mut serial_buf: Vec<u8> = vec![0; 8];
|
||||
let mut plot_delta_time = 0.;
|
||||
loop {
|
||||
clear_background(BLACK);
|
||||
match tail.axis {
|
||||
Axis::One => {
|
||||
draw_line(realtive_x_origin,realtive_y_origin,screen_width(),realtive_y_origin,1.,DARKGRAY);
|
||||
},
|
||||
Axis::Two => {
|
||||
// horizontal (x)
|
||||
draw_line(0.,realtive_y_origin,screen_width(),realtive_y_origin,1.,DARKGRAY);
|
||||
// vertical (y)
|
||||
draw_line(realtive_x_origin,realtive_y_origin, realtive_x_origin,screen_height(),1.,DARKGRAY);
|
||||
},
|
||||
}
|
||||
let frame_delta_time = get_frame_time();
|
||||
let mut y_displacement = 0.;
|
||||
let mut x_displacement = 0.;
|
||||
// keep track of the delta time since last plot
|
||||
plot_delta_time += frame_delta_time;
|
||||
|
||||
let delta_time = get_frame_time();
|
||||
if let Ok(x) = port.read(serial_buf.as_mut_slice()) {
|
||||
let s: String = serial_buf.iter().take(x-1).map(|x| *x as char).collect();
|
||||
if let Ok(parse) = s.parse::<f32>() {
|
||||
y_displacement = parse;
|
||||
|
||||
// only draw when requied
|
||||
tail.place_next(plot_delta_time, x_displacement, -y_displacement);
|
||||
plot_delta_time = 0.;
|
||||
}
|
||||
}
|
||||
|
||||
tail.place_next(delta_time, x_displacement, y_displacement);
|
||||
tail.draw();
|
||||
|
||||
// Info about the cursor locaiton
|
||||
let (mouse_x, mouse_y) = mouse_position();
|
||||
|
||||
let size = measure_text(&format!("x{mouse_x}, y{}",-1.*(mouse_y-realtive_y_origin)), None, 30,1.);
|
||||
draw_rectangle(mouse_x, mouse_y, size.width, -(size.height+5.), WHITE);
|
||||
draw_text(&format!("x{}, y{}", mouse_x+realtive_x_origin,-1.*(mouse_y-realtive_y_origin)), mouse_x, mouse_y-5., 30., BLACK);
|
||||
|
||||
|
||||
if is_key_down(KeyCode::S) {
|
||||
if y_displacement < screen_height()/2. {
|
||||
y_displacement += 10.;
|
||||
@@ -207,9 +251,10 @@ async fn main() {
|
||||
// toggle debug box
|
||||
if show_debug {
|
||||
let mut debug = DebugWindow::new();
|
||||
debug.add_line(format!("FPS {}, Latency {:.2}ms", get_fps(), delta_time * 1000.0));
|
||||
debug.add_line(format!("Points {}", tail.points()));
|
||||
debug.add_line(format!("FPS {:03}, Latency {:.2}ms", get_fps(), frame_delta_time * 1000.0));
|
||||
debug.add_line(format!("Tail Length {TAIL_LEN}, px/s {:.2}", tail.px_per_s));
|
||||
debug.add_line(format!("Cursor Pos {:?}", mouse_position()));
|
||||
debug.add_line(format!("Serial Port {}, Baud Rate {}", port.name().unwrap_or("Unknown".to_string()), port.baud_rate().unwrap_or(0)));
|
||||
debug.draw();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user