This repository has been archived by the owner on Jun 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
sample.go
217 lines (174 loc) · 4.25 KB
/
sample.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"fmt"
"github.com/mattrajca/GoEV3/Button"
"github.com/mattrajca/GoEV3/LED"
"github.com/mattrajca/GoEV3/Motor"
"github.com/mattrajca/GoEV3/Sensors"
"github.com/mattrajca/GoEV3/Sound"
"github.com/mattrajca/GoEV3/TTS"
"time"
)
func playTune() {
Sound.SetVolume(50)
Sound.PlayToneAndRest(262, 180, 20)
Sound.PlayToneAndRest(262, 180, 20)
Sound.PlayToneAndRest(392, 180, 20)
Sound.PlayToneAndRest(392, 180, 20)
Sound.PlayToneAndRest(440, 180, 20)
Sound.PlayToneAndRest(440, 180, 20)
Sound.PlayToneAndRest(392, 380, 20)
Sound.SetVolume(100)
Sound.PlayToneAndRest(349, 180, 20)
Sound.PlayToneAndRest(349, 180, 20)
Sound.PlayToneAndRest(330, 180, 20)
Sound.PlayToneAndRest(330, 180, 20)
Sound.PlayToneAndRest(294, 180, 20)
Sound.PlayToneAndRest(294, 180, 20)
Sound.PlayToneAndRest(262, 400, 200)
}
func testTouchAndSound() {
fmt.Println("Press the touch sensor (on port 2) to play a tune")
sensor := Sensors.FindTouchSensor(Sensors.InPort2)
sensor.Wait()
playTune()
}
func testUltrasonic() {
fmt.Println("Reading ultrasonic sensor (on port 2)")
sensor := Sensors.FindUltrasonicSensor(Sensors.InPort2)
value := sensor.ReadDistance()
fmt.Printf("Ultrasonic distance reading: %d\n", value)
}
func testInfrared() {
fmt.Println("Reading infrared sensor (on port 2)")
sensor := Sensors.FindInfraredSensor(Sensors.InPort2)
for n := 0; n < 10; n++ {
value := sensor.ReadProximity()
fmt.Printf("Infrared proximity reading: %d\n", value)
time.Sleep(time.Second / 2)
}
}
func testColor() {
fmt.Println("Reading color values")
sensor := Sensors.FindColorSensor(Sensors.InPort2)
for n := 0; n < 10; n++ {
color := sensor.ReadColor()
rl := sensor.ReadReflectedLightIntensity()
al := sensor.ReadAmbientLightIntensity()
fmt.Printf("Color: %v\n", color)
fmt.Printf("Reflected light: %v\n", rl)
fmt.Printf("Ambient light: %v\n", al)
time.Sleep(time.Second / 2)
}
}
func testTTSAndButtons() {
TTS.Speak("Hello! Press the following sequence of buttons:")
fmt.Println("Press the following sequence of buttons:")
fmt.Println("left, right, up, down, enter")
Button.Wait(Button.Left)
Button.Wait(Button.Right)
Button.Wait(Button.Up)
Button.Wait(Button.Down)
Button.Wait(Button.Enter)
}
func testMotors() {
fmt.Println("Make sure there is a motor plugged in to port A and press the center button to continue.")
Button.Wait(Button.Enter)
fmt.Println("Running motor A")
Motor.Run(Motor.OutPortA, 40)
for n := 0; n < 20; n++ {
time.Sleep(time.Second / 2)
fmt.Printf("Running motor A with speed %d and power %d\n", Motor.CurrentSpeed(Motor.OutPortA), Motor.CurrentPower(Motor.OutPortA))
}
Motor.Stop(Motor.OutPortA)
}
func testLEDs() {
for n := 0; n < 12; n++ {
if n%3 == 0 {
LED.TurnOff(LED.Red, LED.Left)
LED.TurnOff(LED.Red, LED.Right)
LED.TurnOn(LED.Amber, LED.Left)
LED.TurnOn(LED.Amber, LED.Right)
} else if n%3 == 1 {
LED.TurnOff(LED.Amber, LED.Left)
LED.TurnOff(LED.Amber, LED.Right)
LED.TurnOn(LED.Green, LED.Left)
LED.TurnOn(LED.Green, LED.Right)
} else {
LED.TurnOff(LED.Green, LED.Left)
LED.TurnOff(LED.Green, LED.Right)
LED.TurnOn(LED.Red, LED.Left)
LED.TurnOn(LED.Red, LED.Right)
}
time.Sleep(time.Second / 2)
}
}
func dispatchCommand(selection int) {
switch selection {
case 0:
{
testTouchAndSound()
}
case 1:
{
testUltrasonic()
}
case 2:
{
testInfrared()
}
case 3:
{
testColor()
}
case 4:
{
testTTSAndButtons()
}
case 5:
{
testMotors()
}
case 6:
{
testLEDs()
}
}
}
func main() {
selection := int(0)
for {
fmt.Println("\nWhat would you like to test? Press Escape to exit.")
var entries = [7]string{
"Touch sensor and sound",
"Ultrasonic sensor",
"Infrared sensor",
"Color sensor",
"Speech and brick buttons",
"Motors",
"LEDs",
}
for i, f := range entries {
checked := " "
if i == selection {
checked = "X"
}
fmt.Printf("[%s] %s\n", checked, f)
}
fmt.Print("\n")
button := Button.WaitAny()
if button == Button.Down {
if selection < len(entries)-1 {
selection++
}
} else if button == Button.Up {
if selection > 0 {
selection--
}
} else if button == Button.Enter {
dispatchCommand(selection)
} else if button == Button.Escape {
return
}
}
}