-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
112 lines (101 loc) · 3.38 KB
/
examples_test.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
package switchprocontroller_test
import (
"fmt"
"time"
"github.com/raspberrypi-go-drivers/switchprocontroller"
)
func Example_nonBlocking() {
controller := switchprocontroller.NewSwitchProController()
if err := controller.StartListener(0); err != nil {
fmt.Printf("impossible to start listener: %s", err.Error())
}
for {
// display button A state
aState, _ := controller.GetButtonState("a")
fmt.Printf("A:%d\n", aState)
// display left stick position
leftStick, _ := controller.GetStick("left")
fmt.Printf("x:%f - y:%f\n", leftStick.X, leftStick.Y)
time.Sleep(100 * time.Millisecond)
}
// Output:
// A:0
// x:0.000000 - y:0.000000
// [...]
}
func Example_blocking() {
controller := switchprocontroller.NewSwitchProController()
if err := controller.StartListener(0); err != nil {
fmt.Printf("impossible to start listener: %s", err.Error())
}
for {
select {
case event := <-controller.Events:
if event.Button != nil {
fmt.Printf("%s:%d\n", event.Button.Name, event.Button.State)
} else if event.Stick != nil {
fmt.Printf("%s - Y:%f X:%f\n", event.Stick.Name, event.Stick.Y, event.Stick.X)
}
case <-time.After(60 * time.Second):
fmt.Println("timeout")
return
}
}
}
func ExampleNewSwitchProController() {
controller := switchprocontroller.NewSwitchProController()
if err := controller.StartListener(0); err != nil {
fmt.Printf("impossible to start listener: %s", err.Error())
}
}
func ExampleSwitchProController_GetStick() {
controller := switchprocontroller.NewSwitchProController()
if err := controller.StartListener(0); err != nil {
fmt.Printf("impossible to start listener: %s", err.Error())
}
leftStick, _ := controller.GetStick("left")
fmt.Printf("x:%f - y:%f\n", leftStick.X, leftStick.Y)
// Output: x:0.000000 - y:0.000000
}
func ExampleSwitchProController_StartListener() {
controller := switchprocontroller.NewSwitchProController()
if err := controller.StartListener(0); err != nil {
fmt.Printf("impossible to start listener: %s", err.Error())
}
}
func ExampleSwitchProController_GetButton_released() {
controller := switchprocontroller.NewSwitchProController()
if err := controller.StartListener(0); err != nil {
fmt.Printf("impossible to start listener: %s", err.Error())
}
aButton, _ := controller.GetButton("a")
fmt.Printf("name:%s - state:%d\n", aButton.Name, aButton.State)
// Output: name:a - state:0
}
func ExampleSwitchProController_GetButton_pressed() {
controller := switchprocontroller.NewSwitchProController()
if err := controller.StartListener(0); err != nil {
fmt.Printf("impossible to start listener: %s", err.Error())
}
aButton, _ := controller.GetButton("a")
fmt.Printf("name:%s - state:%d\n", aButton.Name, aButton.State)
// Output: name:a - state:1
}
func ExampleSwitchProController_GetButtonState_released() {
controller := switchprocontroller.NewSwitchProController()
if err := controller.StartListener(0); err != nil {
fmt.Printf("impossible to start listener: %s", err.Error())
}
aState, _ := controller.GetButtonState("a")
fmt.Printf("state:%d\n", aState)
// Output: state:0
}
func ExampleSwitchProController_GetButtonState_pressed() {
controller := switchprocontroller.NewSwitchProController()
if err := controller.StartListener(0); err != nil {
fmt.Printf("impossible to start listener: %s", err.Error())
}
aState, _ := controller.GetButtonState("a")
fmt.Printf("state:%d\n", aState)
// Output: state:1
}