forked from tinygo-org/gobadge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sounds.go
89 lines (80 loc) · 1.76 KB
/
sounds.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
package main
import (
"time"
"tinygo.org/x/drivers/shifter"
"tinygo.org/x/tinyfont"
"tinygo.org/x/tinyfont/freesans"
)
type SoundsUI struct{}
func (u SoundsUI) Start() {
display.FillScreen(colors[WHITE])
tinyfont.WriteLine(&display, &freesans.Bold24pt7b, 0, 50, "MUSIC", colors[GOPHERBLUE])
tinyfont.WriteLine(&display, &freesans.Bold9pt7b, 20, 100, "Press any key", colors[FUCHSIA])
for {
buttons.ReadInput()
if buttons.Pins[shifter.BUTTON_START].Get() {
break
}
if buttons.Pins[shifter.BUTTON_SELECT].Get() {
tone(5274)
}
if buttons.Pins[shifter.BUTTON_A].Get() {
tone(1046)
}
if buttons.Pins[shifter.BUTTON_B].Get() {
tone(1975)
}
if buttons.Pins[shifter.BUTTON_LEFT].Get() {
tone(329)
}
if buttons.Pins[shifter.BUTTON_RIGHT].Get() {
tone(739)
}
if buttons.Pins[shifter.BUTTON_UP].Get() {
tone(369)
}
if buttons.Pins[shifter.BUTTON_DOWN].Get() {
tone(523)
}
}
}
// NOTE: this is only for gouroutine usage, do not use
func Sounds() {
for {
time.Sleep(3 * time.Millisecond)
if !settings.SoundsEnabled {
continue
}
buttons.ReadInput()
if buttons.Pins[shifter.BUTTON_SELECT].Get() {
tone(5274)
}
if buttons.Pins[shifter.BUTTON_A].Get() {
tone(1046)
}
if buttons.Pins[shifter.BUTTON_B].Get() {
tone(1975)
}
if buttons.Pins[shifter.BUTTON_LEFT].Get() {
tone(329)
}
if buttons.Pins[shifter.BUTTON_UP].Get() {
tone(369)
}
if buttons.Pins[shifter.BUTTON_DOWN].Get() {
tone(523)
}
if buttons.Pins[shifter.BUTTON_RIGHT].Get() {
tone(739)
}
time.Sleep(3 * time.Millisecond)
}
}
func tone(tone int) {
for i := 0; i < 10; i++ {
buzzer.High()
time.Sleep(time.Duration(tone) * time.Microsecond)
buzzer.Low()
time.Sleep(time.Duration(tone) * time.Microsecond)
}
}