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

     1  package main
     2  
     3  import (
     4  	"machine"
     5  
     6  	"tinygo.org/x/drivers/mcp23017"
     7  )
     8  
     9  func main() {
    10  	err := machine.I2C0.Configure(machine.I2CConfig{
    11  		Frequency: machine.TWI_FREQ_400KHZ,
    12  	})
    13  	if err != nil {
    14  		panic(err)
    15  	}
    16  	dev, err := mcp23017.NewI2C(machine.I2C0, 0x20)
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  	// Configure pin 0 for input and all the others for output.
    21  	if err := dev.SetModes([]mcp23017.PinMode{
    22  		mcp23017.Input | mcp23017.Pullup,
    23  		mcp23017.Output,
    24  	}); err != nil {
    25  		panic(err)
    26  	}
    27  	input := dev.Pin(0)
    28  	outputMask := ^mcp23017.Pins(1 << 0) // All except pin 0
    29  	inputVal, err := input.Get()
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	println("input value: ", inputVal)
    34  	// Set the values of all the output pins.
    35  	err = dev.SetPins(0b1011011_01101110, outputMask)
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  }