Skip to content

Commit

Permalink
Merge pull request #2 from markusressel/prevent_fan_stop
Browse files Browse the repository at this point in the history
Prevent fan stop
  • Loading branch information
markusressel authored Jan 26, 2021
2 parents 7be3dc3 + eefc57c commit f5705a6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
42 changes: 29 additions & 13 deletions internal/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
)

const (
MaxPwmValue = 255
MinPwmValue = 0
MaxPwmValue = 255
MinPwmValue = 0
InitialLastSetPwm = -10
)

var (
Expand Down Expand Up @@ -439,16 +440,6 @@ func calculateTargetSpeed(fan *Fan) int {

ratio := (float64(avgTemp) - float64(minTemp)) / (float64(maxTemp) - float64(minTemp))
return int(ratio * 255)

// Toggling between off and "full on" for testing
//pwm := GetPwm(fan)
//if pwm < 255 {
// return 255
//}
//
//return 1

//return rand.Intn(getMaxPwmValue(fan))
}

// Finds controllers and fans
Expand Down Expand Up @@ -513,6 +504,7 @@ func createFans(devicePath string) []*Fan {
StartPwm: MinPwmValue,
MaxPwm: MaxPwmValue,
FanCurveData: &map[int]*rolling.PointPolicy{},
LastSetPwm: InitialLastSetPwm,
})
}

Expand Down Expand Up @@ -627,11 +619,35 @@ func setPwm(fan *Fan, pwm int) (err error) {
target := minPwm + int((float64(pwm)/MaxPwmValue)*(float64(maxPwm)-float64(minPwm)))

current := GetPwm(fan)
if fan.LastSetPwm != InitialLastSetPwm && fan.LastSetPwm != current {
log.Printf("WARNING: PWM of %s was changed by third party! Last set PWM value was: %d but is now: %d",
fan.Config.Id, fan.LastSetPwm, current)
}

// make sure fans never stop by validating the current RPM
// and adjusting the target PWM value upwards if necessary
if fan.Config.NeverStop {
rpm := GetRpm(fan)
if rpm <= 0 && fan.LastSetPwm == target {
if target >= maxPwm {
log.Printf("CRITICAL: Fan RPM is %d, even at PWM value %d", rpm, target)
return nil
}
log.Printf("WARNING: Increasing startPWM of %s, which is supposed to never stop, but RPM is 0", fan.Config.Id)
fan.StartPwm++
target++
}
}

if target == current {
return nil
}
log.Printf("Setting %s (%s) to %d (mapped: %d) ...", fan.Config.Id, fan.Name, pwm, target)
return util.WriteIntToFile(target, fan.PwmOutput)
err = util.WriteIntToFile(target, fan.PwmOutput)
if err != nil {
fan.LastSetPwm = target
}
return err
}

// get the rpm value of a fan
Expand Down
1 change: 1 addition & 0 deletions internal/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Fan struct {
StartPwm int `json:"startpwm"` // lowest PWM value where the fans are still spinning
MaxPwm int `json:"maxpwm"` // highest PWM value that yields an RPM increase
FanCurveData *map[int]*rolling.PointPolicy `json:"fancurvedata"`
LastSetPwm int `json:"lastsetpwm"`
}

type Sensor struct {
Expand Down

0 comments on commit f5705a6

Please sign in to comment.