-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-usb.go
68 lines (62 loc) · 1.35 KB
/
test-usb.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
package pcbatest
import (
"fmt"
"os/exec"
"strings"
"time"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/gpio/gpioreg"
)
const (
USBPowerPin = "GPIO22"
)
func TestUSB(waitTime int, t *Tests) {
setUSBPower(false)
time.Sleep(time.Second * 2)
usbBusCount() // Sometimes the first count will still have the device that just turned off
initialUSBCount, err := usbBusCount()
if err != nil {
t.addFail(err.Error())
return
}
setUSBPower(true)
time.Sleep(time.Duration(waitTime) * time.Second)
secondUSBCount, err := usbBusCount()
if err != nil {
t.addFail(err.Error())
return
}
if secondUSBCount == initialUSBCount+1 {
t.addPass("USB power test passed")
} else {
t.addFail("USB power test failed")
}
}
func usbBusCount() (int, error) {
out, err := exec.Command("lsusb").Output()
if err != nil {
return 0, err
}
outStr := string(out)
lines := strings.Split(outStr, "\n")
busCount := 0
for _, line := range lines {
if strings.HasPrefix(line, "Bus") {
busCount++
}
}
return busCount, nil
}
func setUSBPower(on bool) error {
pin := gpioreg.ByName(USBPowerPin)
if on {
if err := pin.Out(gpio.High); err != nil {
return fmt.Errorf("failed to set USB power pin high: %v", err)
}
} else {
if err := pin.Out(gpio.Low); err != nil {
return fmt.Errorf("failed to set USB power pin low: %v", err)
}
}
return nil
}