forked from tinygo-org/drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buzzer.go
76 lines (63 loc) · 1.51 KB
/
buzzer.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
// Package buzzer provides a very simplistic driver for a connected buzzer or low-fidelity speaker.
package buzzer // import "tinygo.org/x/drivers/buzzer"
import (
"machine"
"time"
)
// Device wraps a GPIO connection to a buzzer.
type Device struct {
pin machine.Pin
High bool
BPM float64
}
// New returns a new buzzer driver given which pin to use
func New(pin machine.Pin) Device {
return Device{
pin: pin,
High: false,
BPM: 96.0,
}
}
// On sets the buzzer to a high state.
func (l *Device) On() (err error) {
l.pin.Set(true)
l.High = true
return
}
// Off sets the buzzer to a low state.
func (l *Device) Off() (err error) {
l.pin.Set(false)
l.High = false
return
}
// Toggle sets the buzzer to the opposite of it's current state
func (l *Device) Toggle() (err error) {
if l.High {
err = l.Off()
} else {
err = l.On()
}
return
}
// Tone plays a tone of the requested frequency and duration.
func (l *Device) Tone(hz, duration float64) (err error) {
// calculation based off https://www.arduino.cc/en/Tutorial/Melody
tone := (1.0 / (2.0 * hz)) * 1000000.0
tempo := ((60 / l.BPM) * (duration * 1000))
// no tone during rest, just let the duration pass.
if hz == Rest {
time.Sleep(time.Duration(tempo) * time.Millisecond)
return
}
for i := 0.0; i < tempo*1000; i += tone * 2.0 {
if err = l.On(); err != nil {
return
}
time.Sleep(time.Duration(tone) * time.Microsecond)
if err = l.Off(); err != nil {
return
}
time.Sleep(time.Duration(tone) * time.Microsecond)
}
return
}