-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchilld.go
More file actions
72 lines (61 loc) · 1.63 KB
/
chilld.go
File metadata and controls
72 lines (61 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/tmsmr/chilld/fancurve"
"github.com/tmsmr/cm4iofan"
"github.com/tmsmr/pithermal"
)
const (
ticksPerSecond = 1
)
func main() {
debug := flag.Bool("debug", false, "sets log level to debug")
flag.Parse()
if !*debug {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC1123})
log.Info().Msgf("starting ChillD")
fanctl, err := cm4iofan.New()
if err != nil {
log.Fatal().Str("err", err.Error()).Msg("failed to initialize fan controller")
}
trap := make(chan os.Signal, 1)
signal.Notify(trap, syscall.SIGINT, syscall.SIGTERM)
ticks := time.NewTicker((1000 / ticksPerSecond) * time.Millisecond)
defer ticks.Stop()
currspeed := -1
go func() {
for range ticks.C {
speed := 100
temp, terr := pithermal.GetCpuTemp()
if terr != nil {
log.Error().Str("err", err.Error()).Msg("failed to read cpu temp, using maximum fan speed")
} else {
speed = fancurve.LinearFanSpeedFor(temp)
}
if speed != currspeed {
msg := "setting fan speed to"
if terr == nil {
log.Debug().Msgf("%s %d%% @ %.2f°C", msg, speed, temp)
} else {
log.Warn().Msgf("%s %d%% @ UNKNOWN°C", msg, speed)
}
if err := fanctl.SetDutyCycle(speed); err != nil {
log.Error().Str("err", err.Error()).Msg("failed to to set target speed in fan controller")
} else {
currspeed = speed
}
}
}
}()
<-trap
log.Info().Msg("ChillD terminating, trying to set maximum fan speed")
_ = fanctl.SetDutyCycle(100)
}