-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (117 loc) · 3.39 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"errors"
"log"
"os/exec"
"slices"
"strconv"
"strings"
)
func doCommand(command []string) string {
out, err := exec.Command("bash", "-c", strings.Join(command, " ")).Output()
if err != nil {
log.Fatal("Command run error: " + err.Error())
}
return strings.Replace(string(out), "\n", "", -1)
}
func lookup(command string, srcList []string, src string) string {
if !slices.Contains(srcList, src) {
log.Fatal("Invalid argument error: " + src + " not in " + strings.Join(srcList, ", "))
}
var cmd []string
cmd = append(cmd, "vcgencmd", command, src)
output := doCommand(cmd)
return output
}
func MeasureTemp() (float64, error) {
output := lookup("measure_temp", []string{""}, "")
temp := strings.Split(output, "=")[1]
temp = strings.Replace(temp, "'C", "", -1)
floatTemp, err := strconv.ParseFloat(temp, 64)
if err != nil {
return 0, errors.New("temp convert problem")
}
return floatTemp, nil
}
var kMemorySources = []string{"arm", "gpu"}
func MemorySources() []string {
return kMemorySources
}
func GetMemory(src string) (int, error) {
output := lookup("get_mem", kMemorySources, src)
mem := strings.Split(output, "=")[1]
num, _ := strconv.Atoi(mem[:len(mem)-1])
lastChar := mem[len(mem)-1:]
if lastChar == "M" {
return num * 1024 * 1024, nil
} else if lastChar == "G" {
return num * 1024 * 1024, nil
}
return 0, errors.New("memory problem")
}
var kCodecSources = []string{"h264", "mpg2", "wvc1", "mpg4", "mjpg", "wmv9", "hevc"}
func CodecSources() []string {
return kCodecSources
}
func CodecEnabled(src string) bool {
output := lookup("codec_enabled", kCodecSources, src)
status := strings.Split(output, "=")[1]
if status == "enabled" {
return true
} else {
return false
}
}
var kVoltageSources = []string{"core", "sdram_c", "sdram_i", "sdram_p"}
func GetVoltageSources() []string {
return kVoltageSources
}
func measureVolts(src string) (float64, error) {
output := lookup("measure_volts", kVoltageSources, src)
volt := strings.Split(output, "=")[1]
volt = strings.Replace(volt, "V", "", -1)
float, err := strconv.ParseFloat(volt, 64)
if err != nil {
return 0, errors.New("measure voltage problem")
}
return float, nil
}
var kFreqSources = []string{"arm", "core", "h264", "isp", "v3d", "uart", "pwm", "emmc",
"pixel", "vec", "hdmi", "dpi"}
func GetFrequencySources() []string {
return kFreqSources
}
func MeasureClock(src string) (int, error) {
output := lookup("measure_clock", kFreqSources, src)
value := strings.Split(output, "=")[1]
freq, err := strconv.Atoi(value)
if err != nil {
return 0, errors.New("freq convert error")
}
return freq, nil
}
func main() {
/*
func main() {
if temp, err := MeasureTemp(); err == nil {
log.Println("Temperature: " + strconv.FormatFloat(temp, 'g', -1, 64))
}
if memory, err := GetMemory("gpu"); err == nil {
log.Println("GPU Memory: " + strconv.Itoa(memory))
}
if memory, err := GetMemory("arm"); err == nil {
log.Println("ARM Memory: " + strconv.Itoa(memory))
}
isSupportsHevc := CodecEnabled("hevc")
log.Println("hevc: ", isSupportsHevc)
isSupportsH264 := CodecEnabled("h264")
log.Println("h264: ", isSupportsH264)
if volts, err := measureVolts("core"); err == nil {
log.Println("Core Volts: " + strconv.FormatFloat(volts, 'g', -1, 64))
}
if clock, err := MeasureClock("core"); err == nil {
log.Println("Core freq: " + strconv.Itoa(clock))
}
}
*/
}