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

     1  package main
     2  
     3  import (
     4  	"encoding/hex"
     5  	"time"
     6  	"tinygo.org/x/drivers/onewire"
     7  )
     8  
     9  func main() {
    10  
    11  	pin := machine.D2
    12  
    13  	ow := onewire.New(pin)
    14  
    15  	for {
    16  		time.Sleep(3 * time.Second)
    17  
    18  		println()
    19  		println("Device:", machine.Device)
    20  
    21  		romIDs, err := ow.Search(onewire.SEARCH)
    22  		if err != nil {
    23  			println(err)
    24  		}
    25  		for _, romid := range romIDs {
    26  			println(hex.EncodeToString(romid))
    27  		}
    28  
    29  		if len(romIDs) == 1 {
    30  			// only 1 device on bus
    31  			r, err := ow.ReadAddress()
    32  			if err != nil {
    33  				println(err)
    34  			}
    35  			println(hex.EncodeToString(r))
    36  
    37  		}
    38  
    39  	}
    40  }