-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathedgetpu.go
121 lines (94 loc) · 2.24 KB
/
edgetpu.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
)
type EdgeTPUDevice struct {
name string
path string
}
type EdgeTPUFinder interface {
FindDevices() []EdgeTPUDevice
}
type ApexClassFinder struct {}
func (a ApexClassFinder) FindDevices() []EdgeTPUDevice {
devices := make([]EdgeTPUDevice, 0)
// Attempt to lookup Apex devices by device class
files, err := filepath.Glob(sysfsRoot + "/class/apex/apex_*")
if err != nil {
return devices
}
for _, v := range files {
device := EdgeTPUDevice{
path: v,
name: filepath.Base(v),
}
devices = append(devices, device)
}
return devices
}
type UsbDeviceFinder struct {}
func readSysfsFile(path string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", fmt.Errorf("failed to read device attribute %s: %v", filepath.Base(path), err)
}
attrVal := strings.TrimSpace(string(data))
return attrVal, nil
}
func (u UsbDeviceFinder) FindDevices() []EdgeTPUDevice {
devices := make([]EdgeTPUDevice, 0)
files, err := filepath.Glob(sysfsRoot + "/bus/usb/devices/*/idVendor")
if err != nil {
return devices
}
for _, v := range files {
vid, err := readSysfsFile(v)
if err != nil {
continue
}
// Match vendor ID
if vid != "1a6e" {
continue
}
pid, err := readSysfsFile(filepath.Dir(v) + "/idProduct")
if err != nil {
continue
}
// Match product ID
if pid != "089a" {
continue
}
path := filepath.Dir(v)
device := EdgeTPUDevice{
path: path,
name: filepath.Base(path),
}
devices = append(devices, device)
}
return devices
}
func FindEdgeTPUDevices() []EdgeTPUDevice {
var finder EdgeTPUFinder
// Search for the class devices first, as this will already include all of the system devices
if stat, err := os.Stat(sysfsRoot + "/class/apex"); err == nil && stat.IsDir() {
finder = ApexClassFinder{}
} else {
// Fall back on manual bus enumeration via sysfs for USB device discovery
finder = UsbDeviceFinder{}
}
return finder.FindDevices()
}
func (d EdgeTPUDevice) Temperature() float64 {
data, err := ioutil.ReadFile(d.path + "/temp")
if err != nil {
return 0.0
}
tempStr := strings.TrimSpace(string(data))
temp, _ := strconv.ParseFloat(tempStr, 64)
return temp / 1000
}