This commit is contained in:
2024-10-02 02:26:53 -06:00
parent 6e02c6b46e
commit 33291108bd
3 changed files with 186 additions and 18 deletions

126
Cargo.lock generated
View File

@@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "adler2"
@@ -32,6 +32,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "bytemuck"
version = "1.18.0"
@@ -56,6 +62,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "crc32fast"
version = "1.4.2"
@@ -122,12 +134,51 @@ dependencies = [
"png",
]
[[package]]
name = "io-kit-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "617ee6cf8e3f66f3b4ea67a4058564628cde41901316e19f559e14c7c72c5e7b"
dependencies = [
"core-foundation-sys",
"mach2",
]
[[package]]
name = "libc"
version = "0.2.159"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
[[package]]
name = "libudev"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78b324152da65df7bb95acfcaab55e3097ceaab02fb19b228a9eb74d55f135e0"
dependencies = [
"libc",
"libudev-sys",
]
[[package]]
name = "libudev-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324"
dependencies = [
"libc",
"pkg-config",
]
[[package]]
name = "mach2"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b955cdeb2a02b9117f121ce63aa52d08ade45de53e48fe6a38b39c10f6f709"
dependencies = [
"libc",
]
[[package]]
name = "macroquad"
version = "0.4.13"
@@ -186,6 +237,17 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1bcdd74c20ad5d95aacd60ef9ba40fdf77f767051040541df557b7a9b2a2121"
[[package]]
name = "nix"
version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b"
dependencies = [
"bitflags 1.3.2",
"cfg-if",
"libc",
]
[[package]]
name = "num-traits"
version = "0.2.19"
@@ -213,13 +275,19 @@ dependencies = [
"portable-atomic",
]
[[package]]
name = "pkg-config"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
[[package]]
name = "png"
version = "0.17.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52f9d46a34a05a6a57566bc2bfae066ef07585a6e3fa30fbbdff5936380623f0"
dependencies = [
"bitflags",
"bitflags 1.3.2",
"crc32fast",
"fdeflate",
"flate2",
@@ -246,6 +314,7 @@ name = "quad"
version = "0.1.0"
dependencies = [
"macroquad",
"serialport",
]
[[package]]
@@ -263,6 +332,30 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serialport"
version = "4.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ba776acc8c373b9175829206229366273225436845c04f9c20aab8099960e2e"
dependencies = [
"bitflags 2.6.0",
"cfg-if",
"core-foundation-sys",
"io-kit-sys",
"libudev",
"mach2",
"nix",
"scopeguard",
"unescaper",
"winapi",
]
[[package]]
name = "simd-adler32"
version = "0.3.7"
@@ -289,12 +382,41 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "ttf-parser"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b3e06c9b9d80ed6b745c7159c40b311ad2916abb34a49e9be2653b90db0d8dd"
[[package]]
name = "unescaper"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c878a167baa8afd137494101a688ef8c67125089ff2249284bd2b5f9bfedb815"
dependencies = [
"thiserror",
]
[[package]]
name = "unicode-ident"
version = "1.0.13"

View File

@@ -5,3 +5,4 @@ edition = "2021"
[dependencies]
macroquad = "0.4.13"
serialport = "4.5.1"

View File

@@ -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();
}