-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
81 lines (70 loc) · 1.58 KB
/
controller.go
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
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
type Controller struct {
Config *Config
CURR_SPEED int
}
func NewController() *Controller {
c := Controller{Config: NewConfig()}
c.Config.Load()
return &c
}
func (c *Controller) SetSpeed(speed int) {
c.CURR_SPEED = speed
err := os.WriteFile("/sys/devices/pwm-fan/target_pwm", []byte(strconv.Itoa(speed)), 0644)
if err != nil {
panic(err)
}
}
func (c *Controller) GetTemp() (temp float32) {
file, err := os.ReadFile("/sys/devices/virtual/thermal/thermal_zone0/temp")
if err != nil {
panic(err)
}
tempStr := strings.Trim(string(file), "\n")
tempStr = strings.TrimSpace(tempStr)
tempStr = tempStr[:2] + "." + tempStr[2:]
f, _ := strconv.ParseFloat(tempStr, 32)
temp = float32(f)
return
}
func (c *Controller) SetClockSpeedMax() {
cmd := exec.Command("/usr/bin/jetson_clocks")
err := cmd.Run()
if err != nil {
panic(err)
}
}
func (c *Controller) SetClockSpeed() {
if c.Config.MAX_CLOCK_SPEED {
c.SetClockSpeedMax()
fmt.Println("Enabled Jetson performance mode.")
}
}
func (c *Controller) SetSpeedFromTemp() {
temp := c.GetTemp()
if temp < float32(c.Config.FAN_OFF_TEMP) {
c.SetSpeed(0)
return
}
curve := createCurve(float32(c.Config.FAN_OFF_TEMP), float32(c.Config.FAN_MAX_TEMP), 255.0)
speed := curve(temp)
c.SetSpeed(int(speed))
}
func square(x float32) (y float32) {
y = x * x
return
}
func createCurve(vertex float32, x2 float32, y2 float32) (curve func(x float32) (y float32)) {
curve = func(x float32) (y float32) {
y = y2 / square(x2-vertex) * square(x-vertex)
return
}
return
}