mcp23017: implement pin toggling

Also add an example for using multiple devices.
This commit is contained in:
Roger Peppe
2020-08-24 11:55:00 +01:00
committed by Ron Evans
parent edf9ba92be
commit ce5e443084
6 changed files with 146 additions and 0 deletions
+21
View File
@@ -117,6 +117,22 @@ func (devs Devices) SetPins(pins, mask PinSlice) error {
return nil
}
// TogglePins inverts the values on all pins for
// which mask is high.
func (devs Devices) TogglePins(mask PinSlice) error {
defaultMask := mask.extra()
for i, dev := range devs {
devMask := defaultMask
if i < len(mask) {
devMask = mask[i]
}
if err := dev.TogglePins(devMask); err != nil {
return err
}
}
return nil
}
// PinSlice represents an arbitrary nunber of pins, each element corresponding
// to the pins for one device. The value of the highest numbered pin in the
// slice is extended to all other pins beyond the end of the slice.
@@ -154,6 +170,11 @@ func (pins PinSlice) Low(pin int) {
pins[pin/PinCount].Low(pin % PinCount)
}
// Toggle inverts the value of the given pin.
func (pins PinSlice) Toggle(pin int) {
pins[pin/PinCount].Toggle(pin % PinCount)
}
// Ensure checks that pins has enough space to store
// at least length pins. If it does, it returns pins unchanged.
// Otherwise, it returns pins with elements appended as needed,