Documentation ¶
Overview ¶
Package mcp23017 implements a driver for the MCP23017 I2C port expander chip. See https://www.microchip.com/wwwproducts/en/MCP23017 for details of the interface.
It also provides a way of joining several such devices into one logical device (see the Devices type).
Index ¶
Constants ¶
const ( // Input configures a pin as an input. Input = PinMode(0) // Output configures a pin as an output. Output = PinMode(1) // Direction is the bit mask of the pin mode representing // the I/O direction. Direction = PinMode(1) // Pullup can be bitwise-or'd with Input // to cause the pull-up resistor on the pin to // be enabled. Pullup = PinMode(2) // Invert can be bitwise-or'd with Input to // cause the pin value to reflect the inverted // value on the pin. Invert = PinMode(4) )
const PinCount = 16
PinCount is the number of GPIO pins available on the chip.
Variables ¶
var All = PinSlice{0xffff}
All is a convenience value that represents all pins high (or all mask bits one).
var ErrInvalidHWAddress = errors.New("invalid hardware address")
ErrInvalidHWAddress is returned when the hardware address of the device is not valid (only some bits can be set by the address pins).
Functions ¶
This section is empty.
Types ¶
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device represents an MCP23017 device.
func NewI2C ¶
New returns a new MCP23017 device at the given I2C address on the given bus. It returns ErrInvalidHWAddress if the address isn't possible for the device.
By default all pins are configured as inputs.
func (*Device) GetModes ¶
GetModes reads the modes of all the pins into modes. It's OK if len(modes) is not PinCount - excess entries will be left unset.
func (*Device) Pin ¶
Pin returns a Pin representing the given pin number (from 0 to 15). Pin numbers from 0 to 7 represent port A pins 0 to 7. Pin numbers from 8 to 15 represent port B pins 0 to 7.
func (*Device) SetModes ¶
SetAllModes sets the mode of all the pins in a single operation. If len(modes) is less than PinCount, all remaining pins will be set fo modes[len(modes)-1], or PinMode(0) if modes is empty.
If len(modes) is greater than PinCount, the excess entries will be ignored.
func (*Device) SetPins ¶
SetPins sets all the pins for which mask is high to their respective values in pins.
That is, it does the equivalent of:
for i := 0; i < PinCount; i++ { if mask.Get(i) { d.Pin(i).Set(pins.Get(i)) } }
func (*Device) TogglePins ¶
TogglePins inverts the values on all pins for which mask is high.
type Devices ¶
type Devices []*Device
Devices holds a slice of devices that can be treated as one contiguous set of devices. Earlier entries in the slice have lower-numbered pins, so index 0 holds pins 0-7, index 1 holds pins 8-15, etc.
func NewI2CDevices ¶
NewI2CDevices returns a Devices slice holding the Device values for all the given addresses on the given bus. When more than one bus is in use, create the slice yourself.
func (Devices) GetModes ¶
GetModes gets the pin modes from the devices. It's OK if modes isn't the same length as all the pins: extra entries will be left unchanged.
func (Devices) SetModes ¶
SetModes sets the pin modes of all the pins on all the devices in devs. If there are less entries in modes than there are pins, the last entry is replicated to all of them (or PinMode(0) if modes is empty).
func (Devices) SetPins ¶
SetPins sets all the pins for which mask is high to their respective values in pins.
That is, it does the equivalent of:
for i := 0; i < PinCount*len(devs); i++ { if mask.Get(i) { d.Pin(i).Set(pins.Get(i)) } }
func (Devices) TogglePins ¶
TogglePins inverts the values on all pins for which mask is high.
type Pin ¶
type Pin struct {
// contains filtered or unexported fields
}
Pin represents a single GPIO pin on the device.
type PinMode ¶
type PinMode uint8
PinMode represents a possible I/O mode for a pin. The zero value represents the default value after the chip is reset (input).
type PinSlice ¶
type PinSlice []Pins
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.
func (PinSlice) Ensure ¶
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, populating additonal elements by replicating the highest pin (mirroring the behavior of PinSlice.Get).
func (PinSlice) Get ¶
Get returns the value for the given pin. If the length of pins is too short for the pin number, the value of the highest available pin is returned. That is, the highest numbered pin in the last element of pins is effectively replicated to all other elements.
This means that PinSlice{} means "all pins high" and PinSlice{0xffff} means "all pins low".