Skip to content

Commit

Permalink
Serial port chooser on app start (#7)
Browse files Browse the repository at this point in the history
Probably not worth keeping, just playing around with
  • Loading branch information
DusterTheFirst authored Apr 26, 2024
1 parent d93561d commit 7e352ee
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 81 deletions.
171 changes: 109 additions & 62 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ path-guid = "DBF0F44A-EAF8-4A21-81E3-32F51BFCA43D"
upgrade-guid = "BB0323A3-DA8D-48B8-A258-CEF7E19980CC"

[profile.release]
codegen-units = 1
debug = 1
lto = "fat"
overflow-checks = true

# The profile that 'cargo dist' will build with
[profile.dist]
inherits = "release"
lto = "thin"
codegen-units = 1
inherits = "release"
lto = "fat"

[dependencies]
argh = "0.1.7"
color-eyre = "0.6.1"
colorous = "1.0.6"
eframe = "0.27.2"
egui-phosphor = "0.5.0"
egui_extras = "0.27.2"
egui_plot = "0.27.2"
git-version = "0.3.5"
Expand All @@ -43,7 +42,7 @@ ringbuffer = "0.15.0"
serialport = "4.1.0"
string-interner = "0.15.0"
tracing = "0.1.34"
tracing-subscriber = "0.3.11"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

[workspace]
members = ["crates/metric", "crates/serial", "crates/serial-agent"]
Expand All @@ -54,7 +53,7 @@ edition = "2021"
publish = false
repository = "https://github.com/aeroteameindhoven/kestrel"
rust-version = "1.77.2"
version = "0.1.0"
version = "0.1.1"

# Config for 'cargo dist'
[workspace.metadata.dist]
Expand Down
1 change: 1 addition & 0 deletions crates/metric/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub enum ManyValues {
F64(Box<[f64]>),
}

#[derive(Debug)]
pub enum MetricValueError {
BadLength { expected: usize, got: usize },
}
Expand Down
1 change: 1 addition & 0 deletions crates/serial/src/detacher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use tracing::{error, warn};

use super::SerialWorkerCommand;

// TODO: move this into the app
pub(super) fn main(command_tx: Sender<SerialWorkerCommand>) {
let listener = TcpListener::bind("127.0.0.1:6969").expect("failed to bind tcp listener");

Expand Down
1 change: 1 addition & 0 deletions crates/serial/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl From<io::Error> for TransportError {
}
}

#[derive(Debug)]
pub enum PacketReadError {
PoorLayout { section: usize, packet: Box<[u8]> },
BadPacketLength { expected: Option<usize>, got: usize },
Expand Down
9 changes: 6 additions & 3 deletions crates/serial/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,16 @@ impl SerialWorker {
expected: None,
got: packet.len(),
})?;
let packet_length = u16::from_le_bytes(packet_length) as usize - size_of::<u16>();
let packet_length =
(u16::from_le_bytes(packet_length) as usize).saturating_sub(size_of::<u16>());

if packet_length != packet.len() {
return Err(PacketReadError::BadPacketLength {
dbg!(buffer);

return dbg!(Err(PacketReadError::BadPacketLength {
expected: Some(packet_length),
got: packet.len(),
});
}));
}

packet
Expand Down
76 changes: 68 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::collections::{BTreeMap, BTreeSet};

use app::Application;
use argh::FromArgs;
use eframe::NativeOptions;
use ringbuffer::AllocRingBuffer;
use eframe::{egui::CentralPanel, NativeOptions};
use kestrel_metric::timestamp::Timestamp;
use kestrel_serial::SerialWorkerController;
use ringbuffer::AllocRingBuffer;
use tracing::info;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::EnvFilter;

use crate::version::GIT_VERSION;

Expand All @@ -34,7 +34,7 @@ struct Args {
fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
tracing_subscriber::fmt()
.with_max_level(LevelFilter::TRACE)
.with_env_filter(EnvFilter::from_default_env())
.compact()
.with_ansi(cfg!(debug_assertions))
.init();
Expand All @@ -43,9 +43,17 @@ fn main() -> color_eyre::Result<()> {

let args: Args = argh::from_env();

let serial_ports = || {
serialport::available_ports().map(|ports| {
ports
.into_iter()
.filter(|port| port.port_type != serialport::SerialPortType::Unknown)
})
};

if args.list {
// TODO:
dbg!(serialport::available_ports()?);
dbg!(serial_ports()?);

return Ok(());
}
Expand All @@ -54,13 +62,64 @@ fn main() -> color_eyre::Result<()> {
let port = if let Some(port) = args.port {
port
} else {
serialport::available_ports()?
.first()
serial_ports()?
.next()
.expect("no serial port available")
.port_name
.clone()
};

// let serial_ports = serial_ports()?.collect::<Vec<_>>();

// let mut fonts = eframe::egui::FontDefinitions::default();
// egui_phosphor::add_to_fonts(&mut fonts, egui_phosphor::Variant::Regular);

// eframe::run_simple_native(
// env!("CARGO_PKG_NAME"),
// Default::default(),
// move |ctx, frame| {
// ctx.set_fonts(fonts.clone()); // FIXME: this should be in setup

// CentralPanel::default().show(ctx, |ui| {
// for port in &serial_ports {
// ui.horizontal(|ui| {
// ui.label(&port.port_name);

// match &port.port_type {
// serialport::SerialPortType::UsbPort(info) => {
// ui.label(egui_phosphor::regular::USB);

// if let Some(product) = &info.product {
// ui.label(product);
// }

// if let Some(manufacture) = &info.manufacturer {
// ui.label(manufacture);
// }

// if let Some(serial_number) = &info.serial_number {
// ui.label(format!("({serial_number})"));
// }

// ui.label(format!("[{}:{}]", info.vid, info.pid));
// }
// serialport::SerialPortType::PciPort => {
// ui.label(egui_phosphor::regular::CPU);
// }
// serialport::SerialPortType::BluetoothPort => {
// ui.label(egui_phosphor::regular::BLUETOOTH);
// }
// serialport::SerialPortType::Unknown => {
// ui.label("?");
// }
// }
// });
// }
// });
// },
// )
// .unwrap();

eframe::run_native(
env!("CARGO_PKG_NAME"),
NativeOptions {
Expand Down Expand Up @@ -92,7 +151,8 @@ fn main() -> color_eyre::Result<()> {
),
})
}),
).unwrap(); // FIXME: not Send or Sync :/ color eyre does no like it
)
.unwrap(); // FIXME: not Send or Sync :/ color eyre does no like it

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/visualization/focused_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn focused_metrics_plot<'ui, 'iter>(
.include_y(0.0)
.include_y(1.0)
.x_axis_formatter(|grid_mark, chars, _range| {
assert!(chars >= 8, "Need to implement shrinkage");
// FIXME: assert!(chars >= 8, "Need to implement shrinkage");

x_value_formatter(grid_mark.value)
})
Expand Down

0 comments on commit 7e352ee

Please sign in to comment.