Skip to content

Commit

Permalink
Fixed detection of CPU sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nortank12 committed Jun 30, 2024
1 parent 2dcd511 commit 4775e2c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "deepcool-digital-linux"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

[dependencies]
Expand Down
33 changes: 29 additions & 4 deletions src/device.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
use std::{
fs::File,
io::{BufRead, BufReader},
process::exit,
thread::sleep,
time::Duration
};
use cpu_monitor::CpuInstant;

/// Looks for the appropriate CPU temperature sensor datastream in the hwmon folder.
pub fn find_cpu_sensor() -> String {
let mut i = 0;
loop {
let mut line = String::new();

match File::open(format!("/sys/class/hwmon/hwmon{i}/name")) {
Ok(file) => {
let mut reader = BufReader::new(file);
reader.read_line(&mut line).unwrap();
let hwname = line.trim_end();
if hwname == "k10temp" || hwname == "coretemp" {
return format!("/sys/class/hwmon/hwmon{i}/temp1_input");
}
},
Err(_) => {
println!("CPU temperature sensor not found!");
exit(1);
},
}
i += 1;
}
}

/// Reads the value of the `k10temp` sensor and returns it as a rounded integer.
fn get_temp(fahrenheit: bool) -> u8 {
fn get_temp(cpu_sensor: &str, fahrenheit: bool) -> u8 {
// Read sensor data
let mut line = String::new();
let file = File::open("/sys/class/hwmon/hwmon2/temp1_input")
let file = File::open(cpu_sensor)
.expect("Sensor data not found!");
let mut reader = BufReader::new(file);
reader.read_line(&mut line).unwrap();
Expand Down Expand Up @@ -39,12 +64,12 @@ fn get_usage(duration_ms: u64) -> u8 {
}

/// Reads CPU information and converts the data into a digestible format for the device.
pub fn status_message(mode: &str, fahrenheit: bool, alarm: bool) -> [u8; 64] {
pub fn status_message(cpu_sensor: &str, mode: &str, fahrenheit: bool, alarm: bool) -> [u8; 64] {
let mut data: [u8; 64] = [0; 64];
data[0] = 16;

let usage = get_usage(750);
let temp = get_temp(fahrenheit);
let temp = get_temp(cpu_sensor, fahrenheit);
data[2] = (usage as f32 / 10 as f32).round() as u8;

match mode {
Expand Down
14 changes: 10 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod device;
use device::{startup_message, status_message};
use device::{find_cpu_sensor, startup_message, status_message};

use std::process::exit;
use libc::geteuid;
Expand Down Expand Up @@ -60,6 +60,9 @@ fn main() {
// Connect
let device = api.open(VENDOR, product_id).expect("Failed to open HID device");

// Find CPU temp. sensor
let cpu_hwmon_path = find_cpu_sensor();

// Start
device.write(&startup_message()).expect("Failed to write data");

Expand All @@ -78,15 +81,18 @@ fn main() {
if args.mode == "auto" {
loop {
for _ in 0..8 {
device.write(&status_message("temp", args.fahrenheit, args.alarm)).expect("Failed to write data");
device.write(&status_message(&cpu_hwmon_path, "temp", args.fahrenheit, args.alarm))
.expect("Failed to write data");
}
for _ in 0..8 {
device.write(&status_message("usage", args.fahrenheit, args.alarm)).expect("Failed to write data");
device.write(&status_message(&cpu_hwmon_path, "usage", args.fahrenheit, args.alarm))
.expect("Failed to write data");
}
}
} else {
loop {
device.write(&status_message(&args.mode, args.fahrenheit, args.alarm)).expect("Failed to write data");
device.write(&status_message(&cpu_hwmon_path, &args.mode, args.fahrenheit, args.alarm))
.expect("Failed to write data");
}
}
}

0 comments on commit 4775e2c

Please sign in to comment.