/
foosball.go
111 lines (97 loc) · 2.76 KB
/
foosball.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
package main
import (
"flag"
"fmt"
"github.com/samuelkadolph/go/phidgets"
"log"
"time"
)
const (
AttachmentTimeout = 2 * time.Second
FanTimeout = 10 * time.Minute
)
var (
host = flag.String("host", "", "Host for the server to listen on")
port = flag.Int("port", 5555, "Port for the server to listen on")
)
func check(err error, m string, a ...interface{}) {
if err != nil {
log.Fatalf(m+": %s", append(a, err)...)
}
}
func main() {
flag.Parse()
ifk, err := phidgets.NewInterfaceKit()
check(err, "Unable to create InterfaceKit")
ir1, err := phidgets.NewIR()
check(err, "Unable to create IR")
ir2, err := phidgets.NewIR()
check(err, "Unable to create IR")
check(ifk.Open(phidgets.Label{"foosifk"}), "Unable to open foosifk")
check(ifk.WaitForAttachment(AttachmentTimeout), "Unable to attach to foosifk")
defer ifk.Close()
log.Printf("Attached to foosifk")
// check(ir1.Open(phidgets.Label{"foosfan1"}), "Unable to open foosfan1")
// check(ir1.WaitForAttachment(AttachmentTimeout), "Unable to attach to foosfan1")
// defer ir1.Close()
// log.Printf("Attached to foosfan1")
check(ir2.Open(phidgets.Label{"foosfan2"}), "Unable to open foosfan2")
check(ir2.WaitForAttachment(AttachmentTimeout), "Unable to attach to foosfan2")
defer ir2.Close()
log.Printf("Attached to foosfan2")
horn := NewHorn()
blackFan := NewFan(ir1)
blackPhotogate := NewPhotogate(ifk, []int{0, 1, 2})
yellowFan := NewFan(ir2)
yellowPhotogate := NewPhotogate(ifk, []int{3, 4, 5})
check(blackPhotogate.Test(), "Black Photogate is bad")
log.Printf("Black Photogate is good")
check(yellowPhotogate.Test(), "Yellow Photogate is bad")
log.Printf("Yellow Photogate is good")
blackPhotogate.Activate()
defer blackPhotogate.Deactivate()
log.Printf("Black Photogate activated")
yellowPhotogate.Activate()
defer yellowPhotogate.Deactivate()
log.Printf("Yellow Photogate activated")
fans := map[string]*Fan{
"black": blackFan,
"yellow": yellowFan,
}
photogates := map[string]*Photogate{
"black": blackPhotogate,
"yellow": yellowPhotogate,
}
server := NewServer(horn, fans, photogates)
addr := fmt.Sprintf("%s:%d", *host, *port)
listener, err := server.Listen(addr)
check(err, "Unable to listen on %s", addr)
defer listener.Close()
log.Printf("Listening on %s", addr)
fanTimer := NewTimer(FanTimeout)
go func() {
for {
<-fanTimer.Done
log.Printf("Turning fans off")
blackFan.SetPower(FanPowerOff)
yellowFan.SetPower(FanPowerOff)
}
}()
go func() {
for _ = range blackPhotogate.Detected {
server.Scored <- "black"
log.Printf("Black Scored")
horn.Play()
fanTimer.Touch()
}
}()
go func() {
for _ = range yellowPhotogate.Detected {
server.Scored <- "yellow"
log.Printf("Yellow Scored")
horn.Play()
fanTimer.Touch()
}
}()
select {}
}