Started work on the UI application
This commit is contained in:
parent
e84e1a64b5
commit
7d5eb1887d
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "controller"
|
||||
name = "cheapsdo-control"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
|
@ -7,5 +7,6 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
cheapsdo-protocol = { path = "../protocol" }
|
||||
eframe = "0.24.1"
|
||||
postcard = {version = "1.0.8", features = ["use-std"]}
|
||||
serialport = "4.3.0"
|
||||
|
|
|
@ -1,17 +1,99 @@
|
|||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::io;
|
||||
use std::{io::Write, thread};
|
||||
use std::time::Duration;
|
||||
|
||||
use cheapsdo_protocol::*;
|
||||
use postcard::{to_stdvec_cobs, from_bytes_cobs};
|
||||
use eframe::egui;
|
||||
|
||||
fn main() {
|
||||
let mut port = serialport::new("/dev/ttyACM1", 115_200)
|
||||
use cheapsdo_protocol::*;
|
||||
use postcard::{from_bytes_cobs, to_stdvec_cobs};
|
||||
|
||||
fn main() -> Result<(), eframe::Error> {
|
||||
let options = eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default(),
|
||||
..Default::default()
|
||||
};
|
||||
eframe::run_native(
|
||||
"Cheapsdo Control",
|
||||
options,
|
||||
Box::new(|cc| Box::<CheapsdoControl>::default()),
|
||||
)
|
||||
}
|
||||
|
||||
struct CheapsdoControl {
|
||||
devicestate: Arc<Mutex<StatusMessage>>,
|
||||
serial_device: String,
|
||||
serial_connected: bool,
|
||||
}
|
||||
|
||||
impl Default for CheapsdoControl {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
devicestate: Arc::new(Mutex::new(StatusMessage::default())),
|
||||
serial_device: "".to_owned(),
|
||||
serial_connected: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for CheapsdoControl {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
let ports =
|
||||
serialport::available_ports().unwrap_or(Vec::<serialport::SerialPortInfo>::new());
|
||||
|
||||
|
||||
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
egui::ComboBox::from_label("Select Serialport")
|
||||
.selected_text(self.serial_device.to_owned())
|
||||
.show_ui(ui, |ui| {
|
||||
for port in ports {
|
||||
ui.selectable_value(
|
||||
&mut self.serial_device,
|
||||
port.port_name.to_owned(),
|
||||
port.port_name,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
if ui.add_enabled(!self.serial_connected, egui::Button::new("Open")).clicked() {
|
||||
self.serial_connected = true;
|
||||
|
||||
let serial_device = self.serial_device.clone();
|
||||
let devicestate = self.devicestate.clone();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
poll_device(serial_device, devicestate);
|
||||
});
|
||||
}
|
||||
|
||||
if ui.add_enabled(self.serial_connected, egui::Button::new("Close")).clicked() {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
{
|
||||
let mut state = self.devicestate.lock().unwrap();
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(format!("Measured: {}", state.measured_frequency));
|
||||
ui.label(format!("Average: {}", state.average_frequency));
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn poll_device(port: String, state: Arc<Mutex<StatusMessage>>) {
|
||||
let mut port = serialport::new(port, 115_200)
|
||||
.timeout(Duration::from_millis(10))
|
||||
.open().expect("Failed to open port");
|
||||
|
||||
|
||||
|
||||
loop {
|
||||
let host_msg = HostMessage::RequestStatus;
|
||||
let msg_bytes = to_stdvec_cobs(&host_msg).unwrap();
|
||||
|
@ -25,7 +107,14 @@ fn main() {
|
|||
println!("Data: {:?}", serial_buf);
|
||||
|
||||
let dev_msg = from_bytes_cobs::<DeviceMessage>(&mut serial_buf).unwrap();
|
||||
println!("Message: {:?}", dev_msg);
|
||||
|
||||
match dev_msg {
|
||||
DeviceMessage::Status(status_msg) => {
|
||||
let mut state = state.lock().unwrap();
|
||||
*state = status_msg;
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (),
|
||||
Err(e) => eprintln!("{:?}", e),
|
||||
|
@ -34,5 +123,4 @@ fn main() {
|
|||
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue