forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphero_conways.go
125 lines (99 loc) · 1.95 KB
/
sphero_conways.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
package main
import (
"fmt"
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/sphero"
)
type conway struct {
alive bool
age int
contacts int
cell *sphero.SpheroDriver
}
func main() {
gbot := gobot.NewGobot()
spheros := []string{
"/dev/rfcomm0",
"/dev/rfcomm1",
"/dev/rfcomm2",
}
for _, port := range spheros {
spheroAdaptor := sphero.NewSpheroAdaptor("Sphero", port)
cell := sphero.NewSpheroDriver(spheroAdaptor, "Sphero"+port)
work := func() {
conway := new(conway)
conway.cell = cell
conway.birth()
gobot.On(cell.Event("collision"), func(data interface{}) {
conway.contact()
})
gobot.Every(3*time.Second, func() {
if conway.alive == true {
conway.movement()
}
})
gobot.Every(10*time.Second, func() {
if conway.alive == true {
conway.birthday()
}
})
}
robot := gobot.NewRobot("conway",
[]gobot.Connection{spheroAdaptor},
[]gobot.Device{cell},
work,
)
gbot.AddRobot(robot)
}
gbot.Start()
}
func (c *conway) resetContacts() {
c.contacts = 0
}
func (c *conway) contact() {
c.contacts++
}
func (c *conway) rebirth() {
fmt.Println("Welcome back", c.cell.Name, "!")
c.life()
}
func (c *conway) birth() {
c.resetContacts()
c.age = 0
c.life()
c.movement()
}
func (c *conway) life() {
c.cell.SetRGB(0, 255, 0)
c.alive = true
}
func (c *conway) death() {
fmt.Println(c.cell.Name, "died :(")
c.alive = false
c.cell.SetRGB(255, 0, 0)
c.cell.Stop()
}
func (c *conway) enoughContacts() bool {
if c.contacts >= 2 && c.contacts < 7 {
return true
}
return false
}
func (c *conway) birthday() {
c.age++
fmt.Println("Happy birthday", c.cell.Name, "you are", c.age, "and had", c.contacts, "contacts.")
if c.enoughContacts() == true {
if c.alive == false {
c.rebirth()
}
} else {
c.death()
}
c.resetContacts()
}
func (c *conway) movement() {
if c.alive == true {
c.cell.Roll(100, uint16(gobot.Rand(360)))
}
}