From e317e5ef31f9958254c294f6615dd7b896ff9a38 Mon Sep 17 00:00:00 2001 From: Logan Hickok-Dickson Date: Fri, 26 Jan 2024 21:32:16 -0500 Subject: [PATCH] update gui, move some data and functions around --- software/src/gui.rs | 127 ++++++++++++++++++++++++++--------------- software/src/main.rs | 41 +------------ software/src/serial.rs | 40 +++++++++++++ software/src/system.rs | 68 ++++++++++++++++------ 4 files changed, 173 insertions(+), 103 deletions(-) diff --git a/software/src/gui.rs b/software/src/gui.rs index 9ca1955..bfe1abc 100644 --- a/software/src/gui.rs +++ b/software/src/gui.rs @@ -1,5 +1,14 @@ +use std::{sync::mpsc::channel, time::Duration}; +use std::thread::{self, sleep}; + use eframe::egui; -use egui::{Color32, RichText, Separator}; +use egui::{Align, Color32, Rect, RichText}; + +use crate::system::{PCSystem, SystemReport}; + +pub enum G2SCommands { + UpdateDevice, +} pub fn basic_gui() { let options = eframe::NativeOptions { @@ -10,7 +19,27 @@ pub fn basic_gui() { ..Default::default() }; + let (tx1, rx1) = channel::(); + let (tx2, rx2) = channel::(); + + thread::spawn(move || { + let mut pcs = PCSystem::new().expect("COULD NOT MAKE PC REPORTER"); + + loop { + sleep(Duration::from_secs(1)); + pcs.update(); + tx2.send(pcs.get_report()).expect("COULD NOT SEND PC REPORT"); + } + }); + + let mut sr = SystemReport::default(); + eframe::run_simple_native("JukeBox Desktop", options, move |ctx, _frame| { + let nsr = rx2.recv_timeout(Duration::from_secs(0)); + if let Ok(snsr) = nsr { + sr = snsr; + } + egui::CentralPanel::default().show(ctx, |ui| { ui.horizontal(|ui| { ui.label( @@ -24,57 +53,61 @@ pub fn basic_gui() { ui.separator(); - ui.horizontal_top(|ui| { - ui.columns(2, |c| { - c[0].columns(2, |c| { - c[0].separator(); - c[0].label("CPU: "); - c[0].label("CPU Freq: "); - c[0].label("CPU Load: "); - c[0].label("CPU Temp: "); - c[0].separator(); - c[0].label("GPU: "); - c[0].label("GPU Core Freq: "); - c[0].label("GPU Core Load: "); - c[0].label("GPU VRAM Freq: "); - c[0].label("GPU VRAM Load: "); - c[0].label("GPU Temp: "); - c[0].separator(); - c[0].label("Memory Used: "); - c[0].label("Memory Total: "); - c[0].separator(); - - c[1].separator(); - c[1].label("(N/A)"); - c[1].label("(N/A)"); - c[1].label("(N/A)"); - c[1].label("(N/A)"); - c[1].separator(); - c[1].label("(N/A)"); - c[1].label("(N/A)"); - c[1].label("(N/A)"); - c[1].label("(N/A)"); - c[1].label("(N/A)"); - c[1].label("(N/A)"); - c[1].separator(); - c[1].label("(N/A)"); - c[1].label("(N/A)"); - c[1].separator(); - }); - - c[1].vertical_centered(|ui| { - if ui.button("Set RGB to red").clicked() { - println!("you shouldnt have done that"); - } - if ui.button("Update JukeBox").clicked() { - println!("Updating JukeBox..."); - } - }); + ui.horizontal(|ui| { + ui.vertical(|ui| { + // ui.separator(); + ui.label("CPU: "); + ui.label("CPU Freq: "); + ui.label("CPU Load: "); + ui.label("CPU Temp: "); + // ui.separator(); + ui.label("GPU: "); + ui.label("GPU Core Freq: "); + ui.label("GPU Core Load: "); + ui.label("GPU VRAM Freq: "); + ui.label("GPU VRAM Load: "); + ui.label("GPU Temp: "); + // ui.separator(); + ui.label("Memory Used: "); + ui.label("Memory Total: "); + // ui.separator(); + }); + ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| { + // ui.separator(); + ui.label(format!("{}", sr.cpu_name)); + ui.label(format!("{}", sr.cpu_freq)); + ui.label(format!("{}", sr.cpu_load)); + ui.label(format!("{}", sr.cpu_temp)); + // ui.separator(); + ui.label(format!("{}", sr.gpu_name)); + ui.label(format!("{}", sr.gpu_core_clock)); + ui.label(format!("{}", sr.gpu_core_load)); + ui.label(format!("{}", sr.gpu_memory_clock)); + ui.label(format!("{}", sr.gpu_memory_load)); + ui.label(format!("{}", sr.gpu_temp)); + // ui.separator(); + ui.label(format!("{}", sr.memory_used)); + ui.label(format!("{}", sr.memory_total)); + // ui.separator(); }); }); ui.separator(); + + if ui.button("Set RGB to red").clicked() { + println!("you shouldnt have done that"); + } + if ui.button("Update JukeBox").clicked() { + println!("Updating JukeBox..."); + } + + ui.separator(); + }); + + // Call a new frame every frame, bypassing the limited updates. + // NOTE: This is a bad idea, we should probably change this later. + ctx.request_repaint(); }) .expect("eframe error"); } diff --git a/software/src/main.rs b/software/src/main.rs index 10fa0ab..ad861a0 100644 --- a/software/src/main.rs +++ b/software/src/main.rs @@ -55,48 +55,11 @@ fn deffered_main() -> Result<(), ExitMsg> { match cli.command { Commands::Probe => { - PCSystem::new()?.probe_report(); + PCSystem::new()?.get_report().log_report(); Ok(()) } Commands::Commune => { - let ports = serialport::available_ports().map_err(|why| { - ExitMsg::new( - ExitCode::GenericError, - format!("Failed to enumerate serial ports, reason: \"{}\".", why), - ) - })?; - let ports: Vec<_> = ports - .iter() - .filter(|p| match &p.port_type { - SerialPortType::UsbPort(p) => p.pid == 0xF20A && p.vid == 0x1209, - _ => false, - }) - .collect(); - log::info!( - "Found ports: {:?}", - ports - .iter() - .map(|f| f.port_name.clone()) - .collect::>() - ); - if ports.len() == 0 { - return Err(ExitMsg::new( - ExitCode::GenericError, - format!("Failed to find JukeBox serial port."), - )); - } - let port = ports.get(0).unwrap(); // TODO: provide an argument to choose from this vector - - let mut f = serialport::new(port.port_name.clone(), 115200) - .timeout(std::time::Duration::from_millis(10)) - .open() - .map_err(|why| { - ExitMsg::new( - ExitCode::GenericError, - format!("Failed to open serial port, reason: \"{}\".", why), - ) - })?; - + let mut f = serial::serial_get_device()?; serial::serial_task(&mut f) } Commands::Gui => { diff --git a/software/src/serial.rs b/software/src/serial.rs index 4c95614..09e5c77 100644 --- a/software/src/serial.rs +++ b/software/src/serial.rs @@ -236,6 +236,46 @@ fn transmit_tasks_loop(f: &mut Box, pcs: &PCSystem) -> Result Result, ExitMsg> { + let ports = serialport::available_ports().map_err(|why| { + ExitMsg::new( + ExitCode::GenericError, + format!("Failed to enumerate serial ports, reason: \"{}\".", why), + ) + })?; + let ports: Vec<_> = ports + .iter() + .filter(|p| match &p.port_type { + serialport::SerialPortType::UsbPort(p) => p.pid == 0xF20A && p.vid == 0x1209, + _ => false, + }) + .collect(); + log::info!( + "Found ports: {:?}", + ports + .iter() + .map(|f| f.port_name.clone()) + .collect::>() + ); + if ports.len() == 0 { + return Err(ExitMsg::new( + ExitCode::GenericError, + format!("Failed to find JukeBox serial port."), + )); + } + let port = ports.get(0).unwrap(); // TODO: provide an argument to choose from this vector + + Ok(serialport::new(port.port_name.clone(), 115200) + .timeout(std::time::Duration::from_millis(10)) + .open() + .map_err(|why| { + ExitMsg::new( + ExitCode::GenericError, + format!("Failed to open serial port, reason: \"{}\".", why), + ) + })?) +} + pub fn serial_task(f: &mut Box) -> Result<(), ExitMsg> { let mut pcs = PCSystem::new()?; diff --git a/software/src/system.rs b/software/src/system.rs index 4326688..91639e5 100644 --- a/software/src/system.rs +++ b/software/src/system.rs @@ -151,6 +151,23 @@ impl PCSystem { // self.sys.refresh_components(); } + pub fn get_report(&self) -> SystemReport { + SystemReport { + cpu_name: self.cpu_name(), + gpu_name: self.gpu_name(), + memory_total: self.memory_total(), + cpu_freq: self.cpu_freq(), + cpu_temp: self.cpu_temp(), + cpu_load: self.cpu_load(), + memory_used: self.memory_used(), + gpu_temp: self.gpu_temp(), + gpu_core_clock: self.gpu_core_clock(), + gpu_core_load: self.gpu_core_load(), + gpu_memory_clock: self.gpu_memory_clock(), + gpu_memory_load: self.gpu_memory_load(), + } + } + pub fn cpu_name(&self) -> String { self.sys.cpus().get(0).unwrap().brand().trim().to_owned() } @@ -216,31 +233,48 @@ impl PCSystem { pub fn sensors(&self) -> Components { Components::new_with_refreshed_list() } +} - pub fn probe_report(&self) { +#[derive(Default)] +pub struct SystemReport { + pub cpu_name: String, + pub gpu_name: String, + pub memory_total: String, + pub cpu_freq: String, + pub cpu_temp: String, + pub cpu_load: String, + pub memory_used: String, + pub gpu_temp: String, + pub gpu_core_clock: String, + pub gpu_core_load: String, + pub gpu_memory_clock: String, + pub gpu_memory_load: String, +} +impl SystemReport { + pub fn log_report(&self) { log::info!("PROBE START..."); // log::info!(""); - log::info!("CPU Name: ---- '{}'", self.cpu_name()); - log::info!("GPU Name: ---- '{}'", self.gpu_name()); - log::info!("Total Memory: - {} GiB", self.memory_total()); + log::info!("CPU Name: ---- '{}'", self.cpu_name); + log::info!("GPU Name: ---- '{}'", self.gpu_name); + log::info!("Total Memory: - {} GiB", self.memory_total); // log::info!("CPU Vendor: -- '{}'", self.sys.global_cpu_info().vendor_id()); // log::info!(""); - log::info!("CPU Freq: ------- {} GHz", self.cpu_freq()); - log::info!("CPU Temp: ------- {} * C", self.cpu_temp()); - log::info!("CPU Load: ------- {} %", self.cpu_load()); - log::info!("Memory Used: ---- {} GiB", self.memory_used()); - log::info!("GPU Temp: ------- {} * C", self.gpu_temp()); - log::info!("GPU Core Clock: - {} MHz", self.gpu_core_clock()); - log::info!("GPU Core Load: -- {} %", self.gpu_core_load()); - log::info!("GPU VRAM Clock: - {} MHz", self.gpu_memory_clock()); - log::info!("GPU VRAM Load: -- {} %", self.gpu_memory_load()); + log::info!("CPU Freq: ------- {} GHz", self.cpu_freq); + log::info!("CPU Temp: ------- {} * C", self.cpu_temp); + log::info!("CPU Load: ------- {} %", self.cpu_load); + log::info!("Memory Used: ---- {} GiB", self.memory_used); + log::info!("GPU Temp: ------- {} * C", self.gpu_temp); + log::info!("GPU Core Clock: - {} MHz", self.gpu_core_clock); + log::info!("GPU Core Load: -- {} %", self.gpu_core_load); + log::info!("GPU VRAM Clock: - {} MHz", self.gpu_memory_clock); + log::info!("GPU VRAM Load: -- {} %", self.gpu_memory_load); // log::info!(""); - log::info!("Sensors:"); - for (i, c) in self.sensors().iter().enumerate() { - log::info!("\t{}. {:?}", i + 1, c) - } + // log::info!("Sensors:"); + // for (i, c) in self.sensors().iter().enumerate() { + // log::info!("\t{}. {:?}", i + 1, c) + // } // log::info!(""); log::info!("PROBE ENDED!!!");