mcp23017

package
v0.0.0-...-2e22e3b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

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

View Source
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)
)
View Source
const PinCount = 16

PinCount is the number of GPIO pins available on the chip.

Variables

View Source
var All = PinSlice{0xffff}

All is a convenience value that represents all pins high (or all mask bits one).

View Source
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

func NewI2C(bus drivers.I2C, address uint8) (*Device, error)

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

func (d *Device) GetModes(modes []PinMode) error

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) GetPins

func (d *Device) GetPins() (Pins, error)

GetPins reads all 16 pins from ports A and B.

func (*Device) Pin

func (d *Device) Pin(pin int) 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

func (d *Device) SetModes(modes []PinMode) error

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

func (d *Device) SetPins(pins, mask Pins) error

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

func (d *Device) TogglePins(mask Pins) error

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

func NewI2CDevices(bus drivers.I2C, addrs ...uint8) (Devices, error)

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

func (devs Devices) GetModes(modes []PinMode) error

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) GetPins

func (devs Devices) GetPins(pins PinSlice) error

GetPins returns pin values for all the pins.

func (Devices) Pin

func (devs Devices) Pin(pin int) Pin

Pin returns the pin for the given number.

func (Devices) SetModes

func (devs Devices) SetModes(modes []PinMode) error

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

func (devs Devices) SetPins(pins, mask PinSlice) error

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

func (devs Devices) TogglePins(mask PinSlice) error

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.

func (Pin) Get

func (p Pin) Get() (bool, error)

Get returns the current value of the given pin.

func (Pin) GetMode

func (p Pin) GetMode() (PinMode, error)

GetMode returns the mode of the pin.

func (Pin) High

func (p Pin) High() error

High is short for p.Set(true).

func (Pin) Low

func (p Pin) Low() error

High is short for p.Set(false).

func (Pin) Set

func (p Pin) Set(value bool) error

Set sets the pin to the given value.

func (Pin) SetMode

func (p Pin) SetMode(mode PinMode) error

SetMode configures the pin to the given mode.

func (Pin) Toggle

func (p Pin) Toggle() error

Toggle inverts the value output on the pin.

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

func (pins PinSlice) Ensure(length int) PinSlice

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

func (pins PinSlice) Get(i int) bool

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".

func (PinSlice) High

func (pins PinSlice) High(pin int)

High is short for p.Set(pin, true).

func (PinSlice) Low

func (pins PinSlice) Low(pin int)

High is short for p.Set(pin, false).

func (PinSlice) Set

func (pins PinSlice) Set(i int, value bool)

Set sets the value for the given pin.

func (PinSlice) Toggle

func (pins PinSlice) Toggle(pin int)

Toggle inverts the value of the given pin.

type Pins

type Pins uint16

Pins represents a bitmask of pin values. Port A values are in bits 0-8 (numbered from least significant bit) Port B values are in bits 9-15.

func (Pins) Get

func (p Pins) Get(pin int) bool

Get returns the value for the given pin.

func (*Pins) High

func (p *Pins) High(pin int)

High is short for p.Set(pin, true).

func (*Pins) Low

func (p *Pins) Low(pin int)

Low is short for p.Set(pin, false).

func (*Pins) Set

func (p *Pins) Set(pin int, value bool)

Set sets the value for the given pin.

func (*Pins) Toggle

func (p *Pins) Toggle(pin int)

Toggle inverts the value of the given pin.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL