mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-19 06:03:57 +00:00
mcp23017: implement pin toggling
Also add an example for using multiple devices.
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user