tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/mcp23017-multiple/main.go (about) 1 // This example demonstrates putting several mcp23017 devices together into 2 // a single virtual I/O array. 3 package main 4 5 import ( 6 "machine" 7 8 "tinygo.org/x/drivers/mcp23017" 9 ) 10 11 func main() { 12 err := machine.I2C0.Configure(machine.I2CConfig{ 13 Frequency: machine.TWI_FREQ_400KHZ, 14 }) 15 if err != nil { 16 panic(err) 17 } 18 // Assume the devices are at addresses 0x20, 0x21 19 dev, err := mcp23017.NewI2CDevices(machine.I2C0, 0x20, 0x21) 20 if err != nil { 21 panic(err) 22 } 23 // Configure pin 0 for input and all the others for output. 24 if err := dev.SetModes([]mcp23017.PinMode{ 25 mcp23017.Input | mcp23017.Pullup, 26 mcp23017.Output, 27 }); err != nil { 28 panic(err) 29 } 30 input := dev.Pin(0) 31 // Make a mask that represents all the output pins. 32 // Note that this leverages the driver behaviour which replicates the highest bit in 33 // the last slice element (1 in this case) to all other pins 34 outputMask := mcp23017.PinSlice{^mcp23017.Pins(1 << 0)} // All except pin 0 35 inputVal, err := input.Get() 36 if err != nil { 37 panic(err) 38 } 39 println("input value: ", inputVal) 40 // Set the values of all the output pins. 41 err = dev.SetPins(mcp23017.PinSlice{0b1011011_01101110, 0b11111101_11100110}, outputMask) 42 if err != nil { 43 panic(err) 44 } 45 }