-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
155 lines (119 loc) · 3.84 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/platforms/raspi"
)
const (
MODE_DEBUG = "debug"
MODE_PI = "pi"
)
var Mode = MODE_PI
var GameEvents chan string
func main() {
// initialize our base configuration for the system
initConfiguration()
// initialize a new gobot
gbot := gobot.NewGobot()
// initialize a raspberry pi adaptor
raspberry := raspi.NewRaspiAdaptor("raspi")
// start/stop button for a woman
buttonWoman := gpio.NewButtonDriver(raspberry, "buttonWoman", "11") // GPIO #17 (Low)
ledWoman := gpio.NewLedDriver(raspberry, "ledWoman", "36") // GPIO #16 (Low)
// start/stop buttom for a man
buttonMan := gpio.NewButtonDriver(raspberry, "buttonMan", "12") // GPIO #18 (Low)
ledMan := gpio.NewLedDriver(raspberry, "ledMan", "40") // GPIO #21 (Low)
// contact with the wire (start- and finish-area)
contactStart := gpio.NewButtonDriver(raspberry, "contactStart", "13") // GPIO #27 (Low)
contactFinish := gpio.NewButtonDriver(raspberry, "contactFinish", "22") // GPIO #25 (Low)
// user made contact with the wire (use buzzer to indicate audible)
contactWire := gpio.NewButtonDriver(raspberry, "contactWire", "16") // GPIO #23 (Low)
buzzer := gpio.NewLedDriver(raspberry, "buzzer", "32") // GPIO #12 (Low)
// create a channel for game events
GameEvents = make(chan string)
// simulate events with keyboard interaction
go simulate(GameEvents)
// define the work to be done by the robot (i.e. react to events)
work := func() {
// user pushed the start/stop button
gobot.On(buttonWoman.Event("push"), func(data interface{}) {
handleButtonPress(FEMALE)
})
gobot.On(buttonMan.Event("push"), func(data interface{}) {
handleButtonPress(MALE)
})
// user made contact with wire
gobot.On(contactWire.Event("push"), handleWireContact)
// user is starting the game (must touch the starting area)
gobot.On(contactStart.Event("push"), handleStartContact)
// user finished the game (touched finish area)
gobot.On(contactFinish.Event("push"), handleFinishContact)
go func() {
for event := range GameEvents {
switch event {
// sound the buzzer
case "soundBuzzer":
go func() {
buzzer.On()
<-time.After(300 * time.Millisecond)
buzzer.Off()
}()
// enable/disable the led for the woman button
case "enableLedWoman":
ledWoman.On()
case "disableLedWoman":
ledWoman.Off()
// enable/disable the lef for the man button
case "enableLedMan":
ledMan.On()
case "disableLedMan":
ledMan.Off()
// disable all leds
case "ledOff":
ledWoman.Off()
ledMan.Off()
// simulated events
case "button":
handleButtonPress(FEMALE)
case "contact":
handleWireContact(nil)
case "start":
handleStartContact(nil)
case "finish":
handleFinishContact(nil)
case "off":
ledMan.Off()
ledWoman.Off()
buzzer.Off()
}
}
}()
}
// we need to define a robot to be used with gobot
var robot *gobot.Robot
// switch cases depending on the mode
if Mode == MODE_DEBUG {
// debug mode is run on the mac without physical connections
fmt.Println("RUNNING IN DEBUG-MODE")
robot = gobot.NewRobot("buzzwire",
[]gobot.Connection{},
[]gobot.Device{},
work)
} else {
// all other modes are run on the pi with physical connections
robot = gobot.NewRobot("buzzwire",
[]gobot.Connection{raspberry},
[]gobot.Device{buttonWoman, ledWoman, buttonMan, ledMan, contactStart, contactWire, buzzer, contactFinish},
work)
}
// add the robot to the fleet
gbot.AddRobot(robot)
// start the webserver in a separate go routine
go startServer("localhost:8484")
// open the default webseite
go openInDefaultBrowser("localhost:8484")
// start the robot (blocking)
gbot.Start()
}