Skip to content

Commit

Permalink
WIP - Add some debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jackra1n committed May 15, 2024
1 parent c718928 commit 9250434
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/fan_controller.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use linux_embedded_hal::{I2cdev, i2cdev::core::I2CDevice};
use anyhow::{anyhow, Result};
use log::debug;

const SLAVE_ADDRESS: u16 = 0x20;
const FAN_ON_COMMAND: u8 = 0xFE;
Expand All @@ -14,6 +15,7 @@ pub struct FanController {

impl FanController {
pub fn new(temp_on: f32, temp_off: f32) -> Result<Self> {
debug!("Initializing FanController");
if temp_off <= 0.0 || temp_on <= 0.0 {
return Err(anyhow!("Temperatures must be greater than 0"));
}
Expand All @@ -22,6 +24,7 @@ impl FanController {
}

let i2c = I2cdev::new("/dev/i2c-1")?;
debug!("I2C initialized");

Ok(FanController {
i2c,
Expand All @@ -32,13 +35,15 @@ impl FanController {
}

pub fn fan_on(&mut self) -> Result<(), Box<dyn std::error::Error>> {
debug!("[FanController] Sending fan on command");
self.i2c.set_slave_address(SLAVE_ADDRESS)?;
self.i2c.smbus_write_byte(FAN_ON_COMMAND)?;
self.is_running = true;
Ok(())
}

pub fn fan_off(&mut self) -> Result<(), Box<dyn std::error::Error>> {
debug!("[FanController] Sending fan off command");
self.i2c.set_slave_address(SLAVE_ADDRESS)?;
self.i2c.smbus_write_byte(FAN_OFF_COMMAND)?;
self.is_running = false;
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs;
use std::thread;
use std::time::{Duration, Instant};
use sysinfo::{System, SystemExt, CpuExt, DiskExt};
use log::info;
use log::{info, debug};
use clap::Parser;
use env_logger::{Builder, Env};

Expand Down Expand Up @@ -49,11 +49,13 @@ fn main() -> Result<(), Box<dyn Error>> {

let ip_address = get_local_ip();
let temp = get_cpu_temperature();
debug!("CPU temperature: {}", temp);
let temp_str = format!("{:.1}", temp);
let cpu_usage = format!("{:.1}", get_cpu_usage(&sys));
let ram_usage = format!("{:.1}", get_ram_usage(&sys));


debug!("Checking fan controller. Fan running: {}", fan_controller.is_running);
debug!("Temp: {}, Temp-on: {}, Temp-off: {}", temp, fan_controller.temp_on, fan_controller.temp_off);
if fan_controller.is_running {
if temp <= fan_controller.temp_off {
fan_controller.fan_off()?;
Expand Down

0 comments on commit 9250434

Please sign in to comment.