-
Notifications
You must be signed in to change notification settings - Fork 2
/
sensor.go
56 lines (49 loc) · 1.17 KB
/
sensor.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strconv"
)
//todo: read moar sensors (preferably 2 left / 2 right)
func GetAverageTemp() float64 {
temp1 := readSensor(g_cpu_die_sensor)
avg := temp1 / 1000.0
return avg
}
func GetFanSpeed() float64 {
speed := readSensor(g_fan_sensor)
return speed
}
func SetFanSpeed(new_speed float64) {
if new_speed > g_max_fan_speed {
new_speed = g_max_fan_speed
}
if new_speed < g_min_fan_speed {
new_speed = g_min_fan_speed
}
s := fmt.Sprintf("%d", int32(new_speed))
b := []byte(s)
err := ioutil.WriteFile(g_sensors_base_dir+g_fan_out, b, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't set fan speed: %s\n", err)
return
}
verbOutp("Setting Fan Speed to:", int32(new_speed))
verbOutp("")
}
func readSensor(sensorname string) float64 {
s, err := ioutil.ReadFile(g_sensors_base_dir + sensorname)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't open sensor: %s\n", g_sensors_base_dir+sensorname)
return 0.0
}
s = bytes.Trim(s, "\r\n")
f, err := strconv.ParseFloat(string(s), 64)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't convert value to float64: ", string(s), s)
return 0.0
}
return f
}