github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/examples/dac/dac.go (about)

     1  // Simplistic example using the DAC on the Circuit Playground Express.
     2  //
     3  // To actually use the DAC for producing complex waveforms or samples requires a DMA
     4  // timer-based playback mechanism which is beyond the scope of this example.
     5  package main
     6  
     7  import (
     8  	"machine"
     9  	"time"
    10  )
    11  
    12  func main() {
    13  	speaker := machine.A0
    14  	speaker.Configure(machine.PinConfig{Mode: machine.PinOutput})
    15  
    16  	machine.DAC0.Configure(machine.DACConfig{})
    17  
    18  	data := []uint16{0xFFFF, 0x8000, 0x4000, 0x2000, 0x1000, 0x0000}
    19  
    20  	for {
    21  		for _, val := range data {
    22  			play(val)
    23  			time.Sleep(500 * time.Millisecond)
    24  		}
    25  	}
    26  }
    27  
    28  func play(val uint16) {
    29  	for i := 0; i < 100; i++ {
    30  		machine.DAC0.Set(val)
    31  		time.Sleep(2 * time.Millisecond)
    32  
    33  		machine.DAC0.Set(0)
    34  		time.Sleep(2 * time.Millisecond)
    35  	}
    36  }