tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/buzzer/buzzer.go (about)

     1  // Package buzzer provides a very simplistic driver for a connected buzzer or low-fidelity speaker.
     2  package buzzer // import "tinygo.org/x/drivers/buzzer"
     3  
     4  import (
     5  	"machine"
     6  
     7  	"time"
     8  )
     9  
    10  // Device wraps a GPIO connection to a buzzer.
    11  type Device struct {
    12  	pin  machine.Pin
    13  	High bool
    14  	BPM  float64
    15  }
    16  
    17  // New returns a new buzzer driver given which pin to use
    18  func New(pin machine.Pin) Device {
    19  	return Device{
    20  		pin:  pin,
    21  		High: false,
    22  		BPM:  96.0,
    23  	}
    24  }
    25  
    26  // On sets the buzzer to a high state.
    27  func (l *Device) On() (err error) {
    28  	l.pin.Set(true)
    29  	l.High = true
    30  	return
    31  }
    32  
    33  // Off sets the buzzer to a low state.
    34  func (l *Device) Off() (err error) {
    35  	l.pin.Set(false)
    36  	l.High = false
    37  	return
    38  }
    39  
    40  // Toggle sets the buzzer to the opposite of it's current state
    41  func (l *Device) Toggle() (err error) {
    42  	if l.High {
    43  		err = l.Off()
    44  	} else {
    45  		err = l.On()
    46  	}
    47  	return
    48  }
    49  
    50  // Tone plays a tone of the requested frequency and duration.
    51  func (l *Device) Tone(hz, duration float64) (err error) {
    52  	// calculation based off https://www.arduino.cc/en/Tutorial/Melody
    53  	tone := (1.0 / (2.0 * hz)) * 1000000.0
    54  
    55  	tempo := ((60 / l.BPM) * (duration * 1000))
    56  
    57  	// no tone during rest, just let the duration pass.
    58  	if hz == Rest {
    59  		time.Sleep(time.Duration(tempo) * time.Millisecond)
    60  		return
    61  	}
    62  
    63  	for i := 0.0; i < tempo*1000; i += tone * 2.0 {
    64  		if err = l.On(); err != nil {
    65  			return
    66  		}
    67  		time.Sleep(time.Duration(tone) * time.Microsecond)
    68  
    69  		if err = l.Off(); err != nil {
    70  			return
    71  		}
    72  		time.Sleep(time.Duration(tone) * time.Microsecond)
    73  	}
    74  
    75  	return
    76  }