Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
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 AH::MCP23017< WireType >

Class for MCP23017 I²C I/O expanders.

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

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

Public Member Functions

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

Static Public Member Functions

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

Protected Attributes

Nodenext = nullptr
 
Nodeprevious = nullptr
 

Static Protected Attributes

static DoublyLinkedList< ExtendedIOElementupdatables
 

Private Member Functions

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

Private Attributes

WireTypewire
 
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()

template<class WireType >
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.

Member Function Documentation

◆ pinModeBuffered()

template<class WireType >
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.

◆ digitalWriteBuffered()

template<class WireType >
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.

◆ digitalReadBuffered()

template<class WireType >
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.

◆ analogReadBuffered()

template<class WireType >
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.

◆ analogWriteBuffered()

template<class WireType >
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.

◆ begin()

template<class WireType >
void begin ( )
overridevirtual

Initialize the extended IO element.

Implements ExtendedIOElement.

◆ updateBufferedOutputs()

template<class WireType >
void updateBufferedOutputs ( )
overridevirtual

Write the internal state to the physical outputs.

Implements ExtendedIOElement.

◆ updateBufferedInputs()

template<class WireType >
void updateBufferedInputs ( )
overridevirtual

Read the physical state into the input buffers.

Implements ExtendedIOElement.

◆ updateBufferedPinModes()

template<class WireType >
void updateBufferedPinModes ( )

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

◆ pinA()

template<class WireType >
pin_t pinA ( pin_t p)
inline

Get the identifier of the given pin in register A.

Parameters
pPin number in [0, 7]

Definition at line 57 of file MCP23017.hpp.

◆ pinB()

template<class WireType >
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()

template<class WireType >
bool hasInputs ( ) const
private

Check if any of the pins are configured as inputs.

◆ writeI2C() [1/2]

template<class WireType >
template<size_t N>
void writeI2C ( const uint8_t(&) values[N])
private

Write any data to the MCP23017.

◆ writeI2C() [2/2]

template<class WireType >
template<class... Args>
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.

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

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

template<class WireType >
constexpr uint8_t I2C_BASE_ADDRESS = 0x20
staticconstexprprivate

Definition at line 64 of file MCP23017.hpp.

◆ wire

template<class WireType >
WireType* wire
private

Definition at line 66 of file MCP23017.hpp.

◆ address

template<class WireType >
uint8_t address
private

Definition at line 67 of file MCP23017.hpp.

◆ interruptPin

template<class WireType >
pin_t interruptPin
private

Definition at line 68 of file MCP23017.hpp.

◆ pinModesDirty

template<class WireType >
bool pinModesDirty = true
private

Definition at line 71 of file MCP23017.hpp.

◆ bufferedPinModes

template<class WireType >
BitArray<16> bufferedPinModes
private

Definition at line 72 of file MCP23017.hpp.

◆ pullupsDirty

template<class WireType >
bool pullupsDirty = true
private

Definition at line 73 of file MCP23017.hpp.

◆ bufferedPullups

template<class WireType >
BitArray<16> bufferedPullups
private

Definition at line 74 of file MCP23017.hpp.

◆ outputsDirty

template<class WireType >
bool outputsDirty = true
private

Definition at line 75 of file MCP23017.hpp.

◆ bufferedOutputs

template<class WireType >
BitArray<16> bufferedOutputs
private

Definition at line 76 of file MCP23017.hpp.

◆ bufferedInputs

template<class WireType >
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

template<class Node >
Node* next = nullptr
protectedinherited

Definition at line 320 of file LinkedList.hpp.

◆ previous

template<class Node >
Node* previous = nullptr
protectedinherited

Definition at line 321 of file LinkedList.hpp.


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