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

     1  // to override the USB Manufacturer or Product names:
     2  //
     3  // tinygo flash -target circuitplay-express -ldflags="-X main.usbManufacturer='TinyGopher Labs' -X main.usbProduct='GopherKeyboard' -X main.usbSerial='XXXXX'" examples/hid-keyboard
     4  //
     5  // you can also override the VID/PID. however, only set this if you know what you are doing,
     6  // since changing it can make it difficult to reflash some devices.
     7  package main
     8  
     9  import (
    10  	"machine"
    11  	"machine/usb"
    12  	"machine/usb/hid/keyboard"
    13  	"strconv"
    14  	"time"
    15  )
    16  
    17  var usbVID, usbPID string
    18  var usbManufacturer, usbProduct, usbSerial string
    19  
    20  func main() {
    21  	button := machine.BUTTON
    22  	button.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
    23  
    24  	kb := keyboard.Port()
    25  
    26  	for {
    27  		if !button.Get() {
    28  			kb.Write([]byte("tinygo"))
    29  			time.Sleep(200 * time.Millisecond)
    30  		}
    31  	}
    32  }
    33  
    34  func init() {
    35  	if usbVID != "" {
    36  		vid, _ := strconv.ParseUint(usbVID, 0, 16)
    37  		usb.VendorID = uint16(vid)
    38  	}
    39  
    40  	if usbPID != "" {
    41  		pid, _ := strconv.ParseUint(usbPID, 0, 16)
    42  		usb.ProductID = uint16(pid)
    43  	}
    44  
    45  	if usbManufacturer != "" {
    46  		usb.Manufacturer = usbManufacturer
    47  	}
    48  
    49  	if usbProduct != "" {
    50  		usb.Product = usbProduct
    51  	}
    52  
    53  	if usbSerial != "" {
    54  		usb.Serial = usbSerial
    55  	}
    56  }