Skip to content

Commit

Permalink
update gui, move some data and functions around
Browse files Browse the repository at this point in the history
  • Loading branch information
NotQuiteApex committed Jan 27, 2024
1 parent 5a2fa92 commit e317e5e
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 103 deletions.
127 changes: 80 additions & 47 deletions software/src/gui.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,7 +19,27 @@ pub fn basic_gui() {
..Default::default()
};

let (tx1, rx1) = channel::<G2SCommands>();
let (tx2, rx2) = channel::<SystemReport>();

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(
Expand All @@ -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");
}
41 changes: 2 additions & 39 deletions software/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
);
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 => {
Expand Down
40 changes: 40 additions & 0 deletions software/src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,46 @@ fn transmit_tasks_loop(f: &mut Box<dyn SerialPort>, pcs: &PCSystem) -> Result<bo
Ok(false)
}

pub fn serial_get_device() -> Result<Box<dyn SerialPort>, 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::<Vec<_>>()
);
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<dyn SerialPort>) -> Result<(), ExitMsg> {
let mut pcs = PCSystem::new()?;

Expand Down
68 changes: 51 additions & 17 deletions software/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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!!!");
Expand Down

0 comments on commit e317e5e

Please sign in to comment.