template<class WireType, class EncoderPositionType = int32_t, bool InterruptSafe = false>
class AH::MCP23017Encoders< WireType, EncoderPositionType, InterruptSafe >
Class for reading 8 rotary encoders using a MCP23017 I²C port expander.
Encoders are indexed by a number from 0 to 7. Encoder #0 is connected to pins GPA0 and GPA1, Encoder #1 is connected to pins GPA2 and GPA3, ..., Encoder #7 is connected to pins GPB6 and GPB7.
- Template Parameters
-
| WireType | The type of the Wire I²C driver to use. |
| EncoderPositionType | The type used for saving the encoder positions. int32_t is the default because this matches the Encoder library. You can use small unsigned types such as uint8_t or uint16_t if you're just interrested in the deltas. |
| InterruptSafe | Make the update method safe to use inside of an interrupt. It makes the necessary variables volatile and disables interrupts while reading the positions from the main program. |
Definition at line 33 of file MCP23017Encoders.hpp.
|
| | MCP23017Encoders (WireType &wire, uint8_t addr_offset=0, pin_t interrupt_pin=NO_PIN) |
| | Constructor.
|
| void | begin () |
| | Initialize the MCP23017.
|
| void | update () |
| | If the state of the MCP23017's GPIO changed, read the new state and update the encoder positions.
|
| EncoderPositionType | read (uint8_t idx) const |
| | Read the position of the given encoder.
|
| EncoderPositionType | readAndReset (uint8_t idx) |
| | Read the position of the given encoder and reset it to zero.
|
| void | write (uint8_t idx, EncoderPositionType pos) |
| | Set the position of the given encoder.
|
| MCP23017Encoder | operator[] (uint8_t index) |
| | Get a proxy to one of the encoders managed by this MCP23017.
|
template<class WireType, class EncoderPositionType = int32_t, bool InterruptSafe = false>
Initialize the MCP23017.
The configuration register is set to the right config, all GPIO pins are set to input mode, the pin change interrupts are enabled, and the internal pull-up resistors are enabled as well.
The interrupt pin on the Arduino is configured as input with the internal pull-up resistor enabled, because the MCP23017's interrupt output is configured in open-drain mode.
This function does not initialize the I²C interface. You have to call Wire.begin() before calling this function.
Initialize the state
- Examples
- MCP23017-Encoders-No-Interrupts.ino.
Definition at line 125 of file MCP23017Encoders.hpp.
template<class WireType, class EncoderPositionType = int32_t, bool InterruptSafe = false>
If the state of the MCP23017's GPIO changed, read the new state and update the encoder positions.
Can be called from within an ISR on boards that support I²C inside of ISRs, on the condition that InterruptSafe is set to true.
Don't call this function both from the ISR and from your main program, only call it from one of the two.
- Examples
- MCP23017-Encoders-Interrupts.ino.
Definition at line 179 of file MCP23017Encoders.hpp.
template<class WireType, class EncoderPositionType = int32_t, bool InterruptSafe = false>
| EncoderPositionType read |
( |
uint8_t | idx | ) |
const |
|
inline |
Read the position of the given encoder.
Does not update the state, just reads it from the buffered position.
Don't call this function from within an ISR.
- Parameters
-
| idx | The index of the encoder to read [0, 7]. |
Definition at line 199 of file MCP23017Encoders.hpp.
template<class WireType, class EncoderPositionType = int32_t, bool InterruptSafe = false>
| EncoderPositionType readAndReset |
( |
uint8_t | idx | ) |
|
|
inline |
Read the position of the given encoder and reset it to zero.
Does not update the state, just reads it from the buffered position.
Don't call this function from within an ISR.
- Parameters
-
| idx | The index of the encoder to read [0, 7]. |
Definition at line 211 of file MCP23017Encoders.hpp.
template<class WireType, class EncoderPositionType = int32_t, bool InterruptSafe = false>
| void write |
( |
uint8_t | idx, |
|
|
EncoderPositionType | pos ) |
|
inline |
Set the position of the given encoder.
Don't call this function from within an ISR.
- Parameters
-
| idx | The index of the encoder to write [0, 7]. |
| pos | The position value to write. |
Definition at line 225 of file MCP23017Encoders.hpp.