Skip to content

Commit

Permalink
added "fan mode" command to get/set the pwm mode of a specific fan
Browse files Browse the repository at this point in the history
  • Loading branch information
markusressel committed Apr 11, 2024
1 parent c765e77 commit 890f2aa
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@ devices that you have specified within your config.
> fan2go fan --id cpu rpm
546
> fan2go fan --id cpu mode
No control, 100% all the time (0)
> fan2go fan --id cpu mode auto
Automatic control by integrated hardware (2)
```

### Sensors
Expand Down
71 changes: 71 additions & 0 deletions cmd/fan/mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package fan

import (
"fmt"
"github.com/markusressel/fan2go/internal/fans"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"strconv"
"strings"
)

var modeCmd = &cobra.Command{
Use: "mode",
Short: "Get/Set the current pwm mode setting of a fan",
Long: ``,
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
pterm.DisableOutput()

fan, err := getFan(fanId)
if err != nil {
return err
}

if len(args) > 0 {
firstArg := args[0]
argAsInt, err := strconv.Atoi(firstArg)
var pwmEnabled fans.ControlMode
if err != nil {
switch strings.ToLower(firstArg) {
case "auto":
pwmEnabled = fans.ControlModeAutomatic
case "pwm":
pwmEnabled = fans.ControlModePWM
case "disabled":
pwmEnabled = fans.ControlModeDisabled
default:
return fmt.Errorf("unknown mode: %s, must be a integer in (1..3) or one of: 'auto', 'pwm', 'disabled'", firstArg)
}
} else {
pwmEnabled = fans.ControlMode(argAsInt)
}
err = fan.SetPwmEnabled(pwmEnabled)
if err != nil {
return err
}
}

pwmEnabled, err := fan.GetPwmEnabled()
if err != nil {
return err
}

switch fans.ControlMode(pwmEnabled) {
case fans.ControlModeDisabled:
fmt.Printf("No control, 100%% all the time (%d)", pwmEnabled)
case fans.ControlModePWM:
fmt.Printf("Manual PWM control, gives fan2go control (%d)", pwmEnabled)
case fans.ControlModeAutomatic:
fmt.Printf("Automatic control by integrated hardware (%d)", pwmEnabled)
default:
fmt.Printf("Unknown (%d)", pwmEnabled)
}

return err
},
}

func init() {
Command.AddCommand(modeCmd)
}

0 comments on commit 890f2aa

Please sign in to comment.