Skip to content

Commit ea5602f

Browse files
committed
speaker: add implementation of speaker using Buzzer.
Signed-off-by: Ron Evans <[email protected]>
1 parent 2be48d2 commit ea5602f

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

examples/speaker/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"time"
5+
6+
"github.com/hybridgroup/gopherbot"
7+
)
8+
9+
func main() {
10+
speaker := gopherbot.Speaker()
11+
12+
left := gopherbot.LeftButton()
13+
right := gopherbot.RightButton()
14+
15+
for {
16+
if right.Pushed() {
17+
speaker.Bleep()
18+
}
19+
20+
if left.Pushed() {
21+
speaker.Bloop()
22+
}
23+
24+
time.Sleep(200 * time.Millisecond)
25+
}
26+
}

speaker.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package gopherbot
2+
3+
import (
4+
"machine"
5+
6+
"tinygo.org/x/drivers/buzzer"
7+
)
8+
9+
// SpeakerDevice is the Gopherbot speaker.
10+
type SpeakerDevice struct {
11+
buzzer.Device
12+
}
13+
14+
// Speaker returns the SpeakerDevice.
15+
func Speaker() *SpeakerDevice {
16+
enable := machine.PA30
17+
enable.Configure(machine.PinConfig{Mode: machine.PinOutput})
18+
enable.Set(true)
19+
20+
speaker := machine.A0
21+
speaker.Configure(machine.PinConfig{Mode: machine.PinOutput})
22+
23+
bzr := buzzer.New(speaker)
24+
return &SpeakerDevice{bzr}
25+
}
26+
27+
// Bleep makes a bleep sound using the speaker.
28+
func (s *SpeakerDevice) Bleep() {
29+
// do bleep
30+
s.Tone(buzzer.C3, buzzer.Eighth)
31+
}
32+
33+
// Bloop makes a bloop sound using the speaker.
34+
func (s *SpeakerDevice) Bloop() {
35+
// do bloop
36+
s.Tone(buzzer.C5, buzzer.Quarter)
37+
}

0 commit comments

Comments
 (0)