Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added fan.MinPwm fan.StartPwm distinction #37

Merged
merged 1 commit into from
Oct 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
bin
fan2go.db
fan2go.db
test.db
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var curveCmd = &cobra.Command{
tab := table.Table{
Headers: []string{"", ""},
Rows: [][]string{
{"Start PWM", strconv.Itoa(fan.StartPwm)},
{"Start PWM", strconv.Itoa(fan.MinPwm)},
{"Max PWM", strconv.Itoa(fan.MaxPwm)},
},
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ require (
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
github.com/mitchellh/go-homedir v1.1.0
github.com/oklog/run v1.1.0
github.com/pterm/pterm v0.12.31 // indirect
github.com/pterm/pterm v0.12.31
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/tomlazar/table v0.1.0
go.etcd.io/bbolt v1.3.6
)
12 changes: 7 additions & 5 deletions internal/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func fanController(ctx context.Context, db *bolt.DB, fan *Fan, tick <-chan time.
return err
}

ui.Info("Start PWM of %s (%s, %s): %d", fan.Config.Id, fan.Label, fan.Name, fan.StartPwm)
ui.Info("Start PWM of %s (%s, %s): %d", fan.Config.Id, fan.Label, fan.Name, fan.MinPwm)
ui.Info("Max PWM of %s (%s, %s): %d", fan.Config.Id, fan.Label, fan.Name, fan.MaxPwm)

err = trySetManualPwm(fan)
Expand Down Expand Up @@ -427,6 +427,8 @@ func AttachFanCurveData(curveData *map[int][]float64, fan *Fan) (err error) {
}

fan.StartPwm, fan.MaxPwm = GetPwmBoundaries(fan)
// TODO: we don't have a way to determine this yet
fan.MinPwm = fan.StartPwm

return err
}
Expand Down Expand Up @@ -586,7 +588,7 @@ func createFans(devicePath string) (fans []*Fan) {
PwmOutput: output,
RpmInput: inputs[idx],
RpmMovingAvg: 0,
StartPwm: MinPwmValue,
MinPwm: MinPwmValue,
MaxPwm: MaxPwmValue,
FanCurveData: &map[int]*rolling.PointPolicy{},
LastSetPwm: InitialLastSetPwm,
Expand Down Expand Up @@ -680,7 +682,7 @@ func getMinPwmValue(fan *Fan) (result int) {
// if the fan is never supposed to stop,
// use the lowest pwm value where the fan is still spinning
if fan.Config.NeverStop {
return fan.StartPwm
return fan.MinPwm
}

return MinPwmValue
Expand Down Expand Up @@ -751,8 +753,8 @@ func calculateTargetPwm(fan *Fan, currentPwm int, pwm int) int {
ui.Error("CRITICAL: Fan avg. RPM is %f, even at PWM value %d", avgRpm, target)
return -1
}
ui.Warning("WARNING: Increasing startPWM of %s from %d to %d, which is supposed to never stop, but RPM is %f", fan.Config.Id, fan.StartPwm, fan.StartPwm+1, avgRpm)
fan.StartPwm++
ui.Warning("WARNING: Increasing startPWM of %s from %d to %d, which is supposed to never stop, but RPM is %f", fan.Config.Id, fan.MinPwm, fan.MinPwm+1, avgRpm)
fan.MinPwm++
target++

// set the moving avg to a value > 0 to prevent
Expand Down
24 changes: 13 additions & 11 deletions internal/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ var (
}
)

func createFan(neverStop bool, curveData map[int][]float64) *Fan {
func createFan(neverStop bool, curveData map[int][]float64) (fan *Fan, err error) {
CurrentConfig.RpmRollingWindowSize = 10

fan := Fan{
fan = &Fan{
Config: &FanConfig{
Id: "fan1",
Platform: "platform",
Expand All @@ -48,16 +48,18 @@ func createFan(neverStop bool, curveData map[int][]float64) *Fan {
Sensor: "sensor",
},
FanCurveData: &map[int]*rolling.PointPolicy{},
PwmOutput: "fan1_output",
RpmInput: "fan1_rpm",
}

AttachFanCurveData(&curveData, &fan)
err = AttachFanCurveData(&curveData, fan)

return &fan
return fan, err
}

func TestLinearFan(t *testing.T) {
// GIVEN
fan := createFan(false, linearFan)
fan, _ := createFan(false, linearFan)

// WHEN
startPwm, maxPwm := GetPwmBoundaries(fan)
Expand All @@ -69,7 +71,7 @@ func TestLinearFan(t *testing.T) {

func TestNeverStoppingFan(t *testing.T) {
// GIVEN
fan := createFan(false, neverStoppingFan)
fan, _ := createFan(false, neverStoppingFan)

// WHEN
startPwm, maxPwm := GetPwmBoundaries(fan)
Expand All @@ -81,7 +83,7 @@ func TestNeverStoppingFan(t *testing.T) {

func TestCappedFan(t *testing.T) {
// GIVEN
fan := createFan(false, cappedFan)
fan, _ := createFan(false, cappedFan)

// WHEN
startPwm, maxPwm := GetPwmBoundaries(fan)
Expand All @@ -93,7 +95,7 @@ func TestCappedFan(t *testing.T) {

func TestCappedNeverStoppingFan(t *testing.T) {
// GIVEN
fan := createFan(false, cappedNeverStoppingFan)
fan, _ := createFan(false, cappedNeverStoppingFan)

// WHEN
startPwm, maxPwm := GetPwmBoundaries(fan)
Expand All @@ -114,7 +116,7 @@ func TestCalculateTargetSpeedLinear(t *testing.T) {
MovingAvg: avgTmp,
}

fan := createFan(false, linearFan)
fan, _ := createFan(false, linearFan)

// WHEN
optimal := calculateOptimalPwm(fan)
Expand All @@ -134,13 +136,13 @@ func TestCalculateTargetSpeedNeverStop(t *testing.T) {
MovingAvg: avgTmp,
}

fan := createFan(true, cappedFan)
fan, _ := createFan(true, cappedFan)

// WHEN
optimal := calculateOptimalPwm(fan)
target := calculateTargetPwm(fan, 0, optimal)

// THEN
assert.Equal(t, 0, optimal)
assert.Equal(t, fan.StartPwm, target)
assert.Equal(t, fan.MinPwm, target)
}
3 changes: 2 additions & 1 deletion internal/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type Fan struct {
RpmMovingAvg float64 `json:"rpmmovingavg"`
PwmOutput string `json:"pwmoutput"`
Config *FanConfig `json:"config"`
StartPwm int `json:"startpwm"` // lowest PWM value where the fans are still spinning
StartPwm int `json:"startpwm"` // the min PWM at which the fan starts to rotate from a stand still
MinPwm int `json:"minpwm"` // lowest PWM value where the fans are still spinning, when spinning previously
MaxPwm int `json:"maxpwm"` // highest PWM value that yields an RPM increase
FanCurveData *map[int]*rolling.PointPolicy `json:"fancurvedata"`
OriginalPwmEnabled int `json:"originalpwmenabled"`
Expand Down
42 changes: 42 additions & 0 deletions internal/persistence_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package internal

import (
"github.com/stretchr/testify/assert"
"testing"
)

const (
dbTestingPath = "./test.db"
)

func TestWriteFan(t *testing.T) {
// GIVEN
db := OpenPersistence(dbTestingPath)
defer db.Close()

fan, _ := createFan(false, linearFan)

// WHEN
err := SaveFanPwmData(db, fan)

// THEN
assert.Nil(t, err)
}

func TestReadFan(t *testing.T) {
// GIVEN
db := OpenPersistence(dbTestingPath)
defer db.Close()

fan, _ := createFan(false, neverStoppingFan)
err := SaveFanPwmData(db, fan)

fan, _ = createFan(false, linearFan)

// WHEN
fanData, err := LoadFanPwmData(db, fan)

// THEN
assert.Nil(t, err)
assert.NotNil(t, fanData)
}