Arduino Helpers master
Utility library for Arduino
Public Member Functions | Static Public Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
MCP23017< WireType > Class Template Reference

#include <AH/Hardware/ExtendedInputOutput/MCP23017.hpp>

Detailed Description

template<class WireType>
class MCP23017< WireType >

Class for MCP23017 I²C I/O expanders.

Template Parameters
WireTypeThe type of the I²C driver to use.
Examples
MCP23017.ino.

Definition at line 18 of file MCP23017.hpp.

+ Inheritance diagram for MCP23017< WireType >:
+ Collaboration diagram for MCP23017< WireType >:

Enabling and disabling updatables

void enable ()
 Enable this updatable: insert it into the linked list of instances, so it gets updated automatically. More...
 
void disable ()
 Disable this updatable: remove it from the linked list of instances, so it no longer gets updated automatically. More...
 
bool isEnabled () const
 Check if this updatable is enabled. More...
 
void moveDown ()
 Move down this element in the list. More...
 
static void enable (UpdatableCRTP *element)
 Enable this updatable: insert it into the linked list of instances, so it gets updated automatically. More...
 
static void enable (UpdatableCRTP &element)
 Enable this updatable: insert it into the linked list of instances, so it gets updated automatically. More...
 
static void enable (U(&array)[N])
 Enable this updatable: insert it into the linked list of instances, so it gets updated automatically. More...
 
static void disable (UpdatableCRTP *element)
 Disable this updatable: remove it from the linked list of instances, so it no longer gets updated automatically. More...
 
static void disable (UpdatableCRTP &element)
 Disable this updatable: remove it from the linked list of instances, so it no longer gets updated automatically. More...
 
static void disable (U(&array)[N])
 Disable this updatable: remove it from the linked list of instances, so it no longer gets updated automatically. More...
 

Public Member Functions

 MCP23017 (WireType &wire, uint8_t addressOffset=0, pin_t interruptPin=NO_PIN)
 Constructor. More...
 
void pinModeBuffered (pin_t pin, PinMode_t mode) override
 Set the mode of a given pin in the software buffer. More...
 
void digitalWriteBuffered (pin_t pin, PinStatus_t status) override
 Set the output of a given pin in the software buffer. More...
 
PinStatus_t digitalReadBuffered (pin_t pin) override
 Read the state of the given pin from the software buffer. More...
 
analog_t analogReadBuffered (pin_t pin) override
 Read the analog value of the given pin from the software buffer. More...
 
void analogWriteBuffered (pin_t, analog_t) override
 Write an analog (or PWM) value to the software buffer given pin. More...
 
void begin () override
 Initialize the extended IO element. More...
 
void updateBufferedOutputs () override
 Write the internal state to the physical outputs. More...
 
void updateBufferedInputs () override
 Read the physical state into the input buffers. More...
 
void updateBufferedPinModes ()
 Send the new pin modes to the chip after calling pinModeBuffered. More...
 
pin_t pinA (pin_t p)
 Get the identifier of the given pin in register A. More...
 
pin_t pinB (pin_t p)
 Get the identifier of the given pin in register B. More...
 
Array< pin_t, N > pins () const
 Get an array containing all pins of the element. More...
 
virtual void pinMode (pin_t pin, PinMode_t mode)
 Set the mode of a given pin. More...
 
virtual void digitalWrite (pin_t pin, PinStatus_t state)
 Set the output of the given pin to the given state. More...
 
virtual PinStatus_t digitalRead (pin_t pin)
 Read the state of the given pin. More...
 
virtual void analogWrite (pin_t pin, analog_t val)
 Write an analog (or PWM) value to the given pin. More...
 
virtual analog_t analogRead (pin_t pin)
 Read the analog value of the given pin. More...
 
pin_t pin (pin_t pin) const
 Get the extended IO pin number of a given physical pin of this extended IO element. More...
 
pin_t operator[] (pin_t pin) const
 Get the extended IO pin number of a given physical pin of this extended IO element. More...
 
pin_t getLength () const
 Get the number of pins this IO element has. More...
 
pin_t getEnd () const
 Get the largest global extended IO pin number that belongs to this extended IO element. More...
 
pin_t getStart () const
 Get the smallest global extended IO pin number that belongs to this extended IO element. More...
 

Static Public Member Functions

static constexpr uint16_t length ()
 
static void beginAll ()
 Initialize all extended IO elements. More...
 
static void updateAllBufferedOutputs ()
 Write the internal states to the physical outputs for all extended IO elements. More...
 
static void updateAllBufferedInputs ()
 Read the physical state into the input buffers for all extended IO elements. More...
 
static DoublyLinkedList< ExtendedIOElement > & getAll ()
 Get the list of all Extended IO elements. More...
 

Protected Attributes

ExtendedIOElementnext
 
ExtendedIOElementprevious
 

Static Protected Attributes

static DoublyLinkedList< ExtendedIOElementupdatables
 

Private Member Functions

bool hasInputs () const
 Check if any of the pins are configured as inputs. More...
 
template<size_t N>
void writeI2C (const uint8_t(&values)[N])
 Write any data to the MCP23017. More...
 
template<class... Args>
void writeI2C (uint8_t addr, Args... values)
 Write any data to the MCP23017. More...
 

Private Attributes

WireType * wire
 
uint8_t address
 
pin_t interruptPin
 
bool pinModesDirty = true
 
BitArray< 16 > bufferedPinModes
 
bool pullupsDirty = true
 
BitArray< 16 > bufferedPullups
 
bool outputsDirty = true
 
BitArray< 16 > bufferedOutputs
 
BitArray< 16 > bufferedInputs
 
const pin_t start
 
const pin_t end
 

Static Private Attributes

static constexpr uint8_t I2C_BASE_ADDRESS = 0x20
 
static pin_t offset = NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS
 

Constructor & Destructor Documentation

◆ MCP23017()

MCP23017 ( WireType &  wire,
uint8_t  addressOffset = 0,
pin_t  interruptPin = NO_PIN 
)

Constructor.

Parameters
wireThe Wire/I²C interface to use.
addressOffsetA number between 0 and 7 reflecting the state of the I2C address pins of the MCP23017.
interruptPinThe pin connected to the MCP23017 interrupt pin. This doesn't have to be an interrupt pin of the Arduino.
The interrupt pin of the MCP23017 is configured as open-drain active-low output, and the interrupt pins of GPIO banks A and B are mirrored/OR'ed together. This means that you only need to connect a single interrupt pin for each MCP23017, and you can connect the interrupt pins of multiple MCP23017s to a single Arduino pin.

Definition at line 30 of file MCP23017.ipp.

Member Function Documentation

◆ pinModeBuffered()

void pinModeBuffered ( pin_t  pin,
PinMode_t  mode 
)
overridevirtual

Set the mode of a given pin in the software buffer.

The buffer is written to the ExtIO device when updateBufferedOutputs is called.

Note
This function might not be implemented by all subclasses.
Some extended IO types, such as shift registers, can only be used as outputs.
On others, it might be implemented, but it could impact all pins of the IO element. For example, enabling the internal pull-up resistor on an analog multiplexer affects all pins of the mux.
Parameters
pinThe (zero-based) pin of this IO element.
modeThe mode to set the pin to (e.g. INPUT, OUTPUT or INPUT_PULLUP).

Implements ExtendedIOElement.

Definition at line 40 of file MCP23017.ipp.

◆ digitalWriteBuffered()

void digitalWriteBuffered ( pin_t  pin,
PinStatus_t  state 
)
overridevirtual

Set the output of a given pin in the software buffer.

The buffer is written to the ExtIO device when updateBufferedOutputs is called.

Parameters
pinThe (zero-based) pin of this IO element.
stateThe new state to set the pin to.

Implements ExtendedIOElement.

Definition at line 58 of file MCP23017.ipp.

◆ digitalReadBuffered()

PinStatus_t digitalReadBuffered ( pin_t  pin)
overridevirtual

Read the state of the given pin from the software buffer.

To update the buffer, you have to call updateBufferedInputs first.

Parameters
pinThe (zero-based) pin of this IO element.
Returns
The state of the given pin.

Implements ExtendedIOElement.

Definition at line 65 of file MCP23017.ipp.

◆ analogReadBuffered()

analog_t analogReadBuffered ( pin_t  pin)
overridevirtual

Read the analog value of the given pin from the software buffer.

To update the buffer, you have to call updateBufferedInputs first.

Parameters
pinThe (zero-based) pin of this IO element.
Returns
The new analog value of pin.

Implements ExtendedIOElement.

Definition at line 70 of file MCP23017.ipp.

◆ analogWriteBuffered()

void analogWriteBuffered ( pin_t  pin,
analog_t  val 
)
overridevirtual

Write an analog (or PWM) value to the software buffer given pin.

The buffer is written to the ExtIO device when updateBufferedOutputs is called.

Parameters
pinThe (zero-based) pin of this IO element.
valThe new analog value to set the pin to.

Implements ExtendedIOElement.

Definition at line 75 of file MCP23017.ipp.

◆ begin()

void begin ( )
overridevirtual

Initialize the extended IO element.

Implements ExtendedIOElement.

Definition at line 80 of file MCP23017.ipp.

◆ updateBufferedOutputs()

void updateBufferedOutputs ( )
overridevirtual

Write the internal state to the physical outputs.

Implements ExtendedIOElement.

Definition at line 97 of file MCP23017.ipp.

◆ updateBufferedInputs()

void updateBufferedInputs ( )
overridevirtual

Read the physical state into the input buffers.

Implements ExtendedIOElement.

Definition at line 108 of file MCP23017.ipp.

◆ updateBufferedPinModes()

void updateBufferedPinModes

Send the new pin modes to the chip after calling pinModeBuffered.

Definition at line 122 of file MCP23017.ipp.

◆ pinA()

pin_t pinA ( pin_t  p)
inline

Get the identifier of the given pin in register A.

Parameters
pPin number in [0, 7]
Examples
MCP23017.ino.

Definition at line 57 of file MCP23017.hpp.

◆ pinB()

pin_t pinB ( pin_t  p)
inline

Get the identifier of the given pin in register B.

Parameters
pPin number in [0, 7]

Definition at line 61 of file MCP23017.hpp.

◆ hasInputs()

bool hasInputs
private

Check if any of the pins are configured as inputs.

Definition at line 141 of file MCP23017.ipp.

◆ writeI2C() [1/2]

void writeI2C ( const uint8_t(&)  values[N])
private

Write any data to the MCP23017.

Definition at line 147 of file MCP23017.ipp.

◆ writeI2C() [2/2]

void writeI2C ( uint8_t  addr,
Args...  values 
)
private

Write any data to the MCP23017.

Parameters
addrThe address of the register to write to.
valuesThe values to write.

Definition at line 155 of file MCP23017.ipp.

◆ pins()

Array< pin_t, N > pins ( ) const
inlineinherited

Get an array containing all pins of the element.

Definition at line 24 of file StaticSizeExtendedIOElement.hpp.

◆ length()

static constexpr uint16_t length ( )
inlinestaticconstexprinherited

Definition at line 28 of file StaticSizeExtendedIOElement.hpp.

◆ pinMode()

virtual void pinMode ( pin_t  pin,
PinMode_t  mode 
)
inlinevirtualinherited

Set the mode of a given pin.

Note
This function might not be implemented by all subclasses.
Some extended IO types, such as shift registers, can only be used as outputs.
On others, it might be implemented, but it could impact all pins of the IO element. For example, enabling the internal pull-up resistor on an analog multiplexer affects all pins of the mux.
Parameters
pinThe (zero-based) pin of this IO element.
modeThe mode to set the pin to (e.g. INPUT, OUTPUT or INPUT_PULLUP).

Reimplemented in MAX7219< NumChips, SPIDriver >, and ShiftRegisterOutBase< N >.

Definition at line 97 of file ExtendedIOElement.hpp.

◆ digitalWrite()

virtual void digitalWrite ( pin_t  pin,
PinStatus_t  state 
)
inlinevirtualinherited

Set the output of the given pin to the given state.

Parameters
pinThe (zero-based) pin of this IO element.
stateThe new state to set the pin to.

Reimplemented in MAX7219< NumChips, SPIDriver >, and ShiftRegisterOutBase< N >.

Definition at line 118 of file ExtendedIOElement.hpp.

◆ digitalRead()

virtual PinStatus_t digitalRead ( pin_t  pin)
inlinevirtualinherited

Read the state of the given pin.

Parameters
pinThe (zero-based) pin of this IO element.
Returns
The state of the given pin.

Reimplemented in MAX7219< NumChips, SPIDriver >, and ShiftRegisterOutBase< N >.

Definition at line 138 of file ExtendedIOElement.hpp.

◆ analogWrite()

virtual void analogWrite ( pin_t  pin,
analog_t  val 
)
inlinevirtualinherited

Write an analog (or PWM) value to the given pin.

Parameters
pinThe (zero-based) pin of this IO element.
valThe new analog value to set the pin to.

Reimplemented in MAX7219< NumChips, SPIDriver >, and ShiftRegisterOutBase< N >.

Definition at line 158 of file ExtendedIOElement.hpp.

◆ analogRead()

virtual analog_t analogRead ( pin_t  pin)
inlinevirtualinherited

Read the analog value of the given pin.

Parameters
pinThe (zero-based) pin of this IO element.
Returns
The new analog value of pin.

Reimplemented in MAX7219< NumChips, SPIDriver >, and ShiftRegisterOutBase< N >.

Definition at line 178 of file ExtendedIOElement.hpp.

◆ beginAll()

void beginAll ( )
staticinherited

Initialize all extended IO elements.

Definition at line 17 of file ExtendedIOElement.cpp.

◆ updateAllBufferedOutputs()

void updateAllBufferedOutputs ( )
staticinherited

Write the internal states to the physical outputs for all extended IO elements.

Definition at line 21 of file ExtendedIOElement.cpp.

◆ updateAllBufferedInputs()

void updateAllBufferedInputs ( )
staticinherited

Read the physical state into the input buffers for all extended IO elements.

Definition at line 25 of file ExtendedIOElement.cpp.

◆ pin()

pin_t pin ( pin_t  pin) const
inherited

Get the extended IO pin number of a given physical pin of this extended IO element.

Parameters
pinThe zero-based physical pin number of this IO element.
Returns
The global, unique extended IO pin number for the given pin.
Examples
1.MAX7219-Blink.ino, 1.SPI-Blink.ino, and 2.BitBang-Blink.ino.

Definition at line 29 of file ExtendedIOElement.cpp.

◆ operator[]()

pin_t operator[] ( pin_t  pin) const
inherited

Get the extended IO pin number of a given physical pin of this extended IO element.


It is alias for ExtendedIOElement::pin.

Parameters
pinThe zero-based physical pin number of this IO element.
Returns
The global, unique extended IO pin number for the given pin.

Definition at line 44 of file ExtendedIOElement.cpp.

◆ getLength()

pin_t getLength ( ) const
inherited

Get the number of pins this IO element has.

Returns
The number of pins this IO element has.

Definition at line 46 of file ExtendedIOElement.cpp.

◆ getEnd()

pin_t getEnd ( ) const
inherited

Get the largest global extended IO pin number that belongs to this extended IO element.

Definition at line 48 of file ExtendedIOElement.cpp.

◆ getStart()

pin_t getStart ( ) const
inherited

Get the smallest global extended IO pin number that belongs to this extended IO element.

Definition at line 50 of file ExtendedIOElement.cpp.

◆ getAll()

DoublyLinkedList< ExtendedIOElement > & getAll ( )
staticinherited

Get the list of all Extended IO elements.

Definition at line 52 of file ExtendedIOElement.cpp.

◆ enable() [1/4]

void enable ( )
inlineinherited

Enable this updatable: insert it into the linked list of instances, so it gets updated automatically.

Definition at line 96 of file Updatable.hpp.

◆ enable() [2/4]

static void enable ( UpdatableCRTP< ExtendedIOElement > *  element)
inlinestaticinherited

Enable this updatable: insert it into the linked list of instances, so it gets updated automatically.

Definition at line 125 of file Updatable.hpp.

◆ enable() [3/4]

static void enable ( UpdatableCRTP< ExtendedIOElement > &  element)
inlinestaticinherited

Enable this updatable: insert it into the linked list of instances, so it gets updated automatically.

Definition at line 127 of file Updatable.hpp.

◆ enable() [4/4]

static void enable ( U(&)  array[N])
inlinestaticinherited

Enable this updatable: insert it into the linked list of instances, so it gets updated automatically.

Definition at line 130 of file Updatable.hpp.

◆ disable() [1/4]

void disable ( )
inlineinherited

Disable this updatable: remove it from the linked list of instances, so it no longer gets updated automatically.

Definition at line 106 of file Updatable.hpp.

◆ disable() [2/4]

static void disable ( UpdatableCRTP< ExtendedIOElement > *  element)
inlinestaticinherited

Disable this updatable: remove it from the linked list of instances, so it no longer gets updated automatically.

Definition at line 136 of file Updatable.hpp.

◆ disable() [3/4]

static void disable ( UpdatableCRTP< ExtendedIOElement > &  element)
inlinestaticinherited

Disable this updatable: remove it from the linked list of instances, so it no longer gets updated automatically.

Definition at line 138 of file Updatable.hpp.

◆ disable() [4/4]

static void disable ( U(&)  array[N])
inlinestaticinherited

Disable this updatable: remove it from the linked list of instances, so it no longer gets updated automatically.

Definition at line 141 of file Updatable.hpp.

◆ isEnabled()

bool isEnabled ( ) const
inlineinherited

Check if this updatable is enabled.

Note
Assumes that the updatable is not added to a different linked list by the user.

Definition at line 120 of file Updatable.hpp.

◆ moveDown()

void moveDown ( )
inlineinherited

Move down this element in the list.

Definition at line 147 of file Updatable.hpp.

Member Data Documentation

◆ I2C_BASE_ADDRESS

constexpr uint8_t I2C_BASE_ADDRESS = 0x20
staticconstexprprivate

Definition at line 64 of file MCP23017.hpp.

◆ wire

WireType* wire
private

Definition at line 66 of file MCP23017.hpp.

◆ address

uint8_t address
private

Definition at line 67 of file MCP23017.hpp.

◆ interruptPin

pin_t interruptPin
private

Definition at line 68 of file MCP23017.hpp.

◆ pinModesDirty

bool pinModesDirty = true
private

Definition at line 71 of file MCP23017.hpp.

◆ bufferedPinModes

BitArray<16> bufferedPinModes
private

Definition at line 72 of file MCP23017.hpp.

◆ pullupsDirty

bool pullupsDirty = true
private

Definition at line 73 of file MCP23017.hpp.

◆ bufferedPullups

BitArray<16> bufferedPullups
private

Definition at line 74 of file MCP23017.hpp.

◆ outputsDirty

bool outputsDirty = true
private

Definition at line 75 of file MCP23017.hpp.

◆ bufferedOutputs

BitArray<16> bufferedOutputs
private

Definition at line 76 of file MCP23017.hpp.

◆ bufferedInputs

BitArray<16> bufferedInputs
private

Definition at line 77 of file MCP23017.hpp.

◆ start

const pin_t start
privateinherited

Definition at line 267 of file ExtendedIOElement.hpp.

◆ end

const pin_t end
privateinherited

Definition at line 268 of file ExtendedIOElement.hpp.

◆ offset

pin_t offset = NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS
staticprivateinherited

Definition at line 269 of file ExtendedIOElement.hpp.

◆ updatables

DoublyLinkedList< ExtendedIOElement > updatables
staticprotectedinherited

Definition at line 152 of file Updatable.hpp.

◆ next

ExtendedIOElement * next
protectedinherited

Definition at line 320 of file LinkedList.hpp.

◆ previous

ExtendedIOElement * previous
protectedinherited

Definition at line 321 of file LinkedList.hpp.


The documentation for this class was generated from the following files: