Skip to content

Commit

Permalink
better detect console output
Browse files Browse the repository at this point in the history
  • Loading branch information
markusressel committed Jan 18, 2021
1 parent 27c537a commit 840d8b4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 38 deletions.
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,41 @@ sudo cp ./fan2go /usr/bin/fan2go

### Configuration

To configure fan2go create a YAML configuration file in **one** of the following locations:
Use `fan2go detect` to print a list of all usable devices:

```shell
Detected Devices:
acpitz
temp1_input (1): 27800
temp2_input (2): 29800
nvme
temp1_input (1): 52850
temp2_input (2): 52850
temp3_input (3): 64850
coretemp
temp1_input (1): 59000
temp2_input (2): 57000
temp3_input (3): 52000
temp4_input (4): 56000
temp5_input (5): 50000
it8620
pwm1 (1): RPM: 0 PWM: 70 Auto: false
pwm2 (2): RPM: 0 PWM: 70 Auto: false
pwm3 (3): RPM: 709 PWM: 106 Auto: false
pwm4 (4): RPM: 627 PWM: 94 Auto: false
pwm5 (5): RPM: 684 PWM: 100 Auto: false
temp1_input (1): 29000
temp2_input (2): 32000
temp3_input (3): 49000
temp4_input (4): 29000
temp5_input (5): 46000
temp6_input (6): 46000
nouveau
pwm1 (1): RPM: 1560 PWM: 31 Auto: false
temp1_input (1): 33000
```

Then configure fan2go by creating a YAML configuration file in **one** of the following locations:

* `./fan2go.yaml`
* `/etc/fan2go/fan2go.yaml` (recommended)
Expand Down Expand Up @@ -49,7 +83,7 @@ sensors:
# If sensors displays a group with the title "nouveau-pci-0100",
# the platform would be "nouveau"
platform: coretemp
# The index of this sensor as displayed by `sensors`.
# The index of this sensor as displayed by `fan2go detect`.
index: 1
# The minimum target temp for this sensor.
# If the sensor falls below this value, a fan configured
Expand Down Expand Up @@ -80,7 +114,7 @@ fans:
# The platform of the controller which is
# connected to this fan (see sensor.platform above).
platform: it8620
# The index of this fan as displayed by `sensors`.
# The index of this fan as displayed by `fan2go detect`.
fan: 3 # HDD Cage (Front)
# Indicates whether this fan is allowed to fully stop.
neverStop: no
Expand Down
29 changes: 28 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"github.com/markusressel/fan2go/internal"
"github.com/markusressel/fan2go/internal/util"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -30,7 +31,33 @@ var detectCmd = &cobra.Command{
Short: "Detect devices",
Long: `Detects all fans and sensors and prints them as a list`,
Run: func(cmd *cobra.Command, args []string) {
internal.DetectDevices()

controllers, err := internal.FindControllers()
if err != nil {
log.Fatalf("Error detecting devices: %s", err.Error())
}

// === Print detected devices ===
fmt.Printf("Detected Devices:\n")

for _, controller := range controllers {
if len(controller.Name) <= 0 {
continue
}

fmt.Printf("%s\n", controller.Name)
for _, fan := range controller.Fans {
pwm := internal.GetPwm(fan)
rpm := internal.GetRpm(fan)
isAuto, _ := internal.IsPwmAuto(controller.Path)
fmt.Printf(" %s (%d): RPM: %d PWM: %d Auto: %v\n", fan.Name, fan.Index, rpm, pwm, isAuto)
}

for _, sensor := range controller.Sensors {
value, _ := util.ReadIntFromFile(sensor.Input)
fmt.Printf(" %s (%d): %d\n", sensor.Name, sensor.Index, value)
}
}
},
}

Expand Down
45 changes: 11 additions & 34 deletions internal/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Run() {

defer OpenPersistence().Close()

DetectDevices()
detectDevices()
mapConfigToControllers()

// === start sensor monitoring
Expand Down Expand Up @@ -69,17 +69,13 @@ func Run() {
select {}
}

func DetectDevices() {
func detectDevices() {
// === Detect devices ===
controllers, err := findControllers()
controllers, err := FindControllers()
if err != nil {
log.Fatalf("Error detecting devices: %s", err.Error())
}
Controllers = controllers

// === Print detected devices ===
log.Printf("Detected Devices:")
printDeviceStatus(Controllers)
}

func getProcessOwner() string {
Expand Down Expand Up @@ -209,8 +205,8 @@ func measureRpmSensors() {

// read the current value of a fan RPM sensor and append it to the moving window
func measureRpm(fan *Fan) {
pwm := getPwm(fan)
rpm := getRpm(fan)
pwm := GetPwm(fan)
rpm := GetRpm(fan)

pwmRpmMap := fan.FanCurveData
if pwmRpmMap == nil {
Expand Down Expand Up @@ -437,7 +433,7 @@ func calculateTargetSpeed(fan *Fan) int {
return int(ratio * 255)

// Toggling between off and "full on" for testing
//pwm := getPwm(fan)
//pwm := GetPwm(fan)
//if pwm < 255 {
// return 255
//}
Expand All @@ -448,7 +444,7 @@ func calculateTargetSpeed(fan *Fan) int {
}

// Finds controllers and fans
func findControllers() (controllers []*Controller, err error) {
func FindControllers() (controllers []*Controller, err error) {
hwmonDevices := util.FindHwmonDevicePaths()
i2cDevices := util.FindI2cDevicePaths()
allDevices := append(hwmonDevices, i2cDevices...)
Expand Down Expand Up @@ -540,7 +536,7 @@ func createSensors(devicePath string) []*Sensor {
}

// checks if the given output is in auto mode
func isPwmAuto(outputPath string) (bool, error) {
func IsPwmAuto(outputPath string) (bool, error) {
pwmEnabledFilePath := outputPath + "_enable"

if _, err := os.Stat(pwmEnabledFilePath); err != nil {
Expand Down Expand Up @@ -597,7 +593,7 @@ func getMinPwmValue(fan *Fan) (result int) {
}

// get the pwm speed of a fan (0..255)
func getPwm(fan *Fan) int {
func GetPwm(fan *Fan) int {
value, err := util.ReadIntFromFile(fan.PwmOutput)
if err != nil {
return MinPwmValue
Expand All @@ -621,7 +617,7 @@ func setPwm(fan *Fan, pwm int) (err error) {
// TODO: this assumes a linear curve, but it might be something else
target := minPwm + int((float64(pwm)/MaxPwmValue)*(float64(maxPwm)-float64(minPwm)))

current := getPwm(fan)
current := GetPwm(fan)
if target == current {
return nil
}
Expand All @@ -630,29 +626,10 @@ func setPwm(fan *Fan, pwm int) (err error) {
}

// get the rpm value of a fan
func getRpm(fan *Fan) int {
func GetRpm(fan *Fan) int {
value, err := util.ReadIntFromFile(fan.RpmInput)
if err != nil {
return 0
}
return value
}

// ===== Console Output =====

func printDeviceStatus(devices []*Controller) {
for _, device := range devices {
log.Printf("Controller: %s", device.Name)
for _, fan := range device.Fans {
pwm := getPwm(fan)
rpm := getRpm(fan)
isAuto, _ := isPwmAuto(device.Path)
log.Printf("Fan %d (%s): RPM: %d PWM: %d Auto: %v", fan.Index, fan.Name, rpm, pwm, isAuto)
}

for _, sensor := range device.Sensors {
value, _ := util.ReadIntFromFile(sensor.Input)
log.Printf("Sensor %d (%s): %d", sensor.Index, sensor.Name, value)
}
}
}

0 comments on commit 840d8b4

Please sign in to comment.