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

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"machine/usb/hid/joystick"
     6  	"math"
     7  	"time"
     8  )
     9  
    10  var js = joystick.Port()
    11  
    12  func main() {
    13  	log.SetFlags(log.Lmicroseconds)
    14  	ticker := time.NewTicker(10 * time.Millisecond)
    15  	cnt := 0
    16  	const f = 3.0
    17  	for range ticker.C {
    18  		t := float64(cnt) * 0.01
    19  		x := 32767 * math.Sin(2*math.Pi*f*t)
    20  		button := cnt%100 > 50
    21  		js.SetButton(2, button)
    22  		js.SetButton(3, !button)
    23  		js.SetAxis(0, int(x))
    24  		js.SendState()
    25  		cnt++
    26  	}
    27  }