From f8e4e34a30169a76b4066c2b8752cc257335d701 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Sat, 28 May 2022 12:26:29 +0200 Subject: [PATCH 1/3] Add begin() method usual for Arduino driver libs and the supporting ctor. --- src/MCP23017.cpp | 11 +++++++++++ src/MCP23017.h | 12 +++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/MCP23017.cpp b/src/MCP23017.cpp index b36cb8f..631186f 100644 --- a/src/MCP23017.cpp +++ b/src/MCP23017.cpp @@ -5,6 +5,11 @@ MCP23017::MCP23017(uint8_t address, TwoWire& bus) { _bus = &bus; } +MCP23017::MCP23017(TwoWire& bus) { + _deviceAddr = MCP23017_I2C_ADDRESS; + _bus = &bus; +} + MCP23017::~MCP23017() {} void MCP23017::init() @@ -25,6 +30,12 @@ void MCP23017::init() writeRegister(MCP23017Register::GPPU_A, 0xFF, 0xFF); } +void MCP23017::begin(uint8_t address) +{ + _deviceAddr = address; + init(); +} + void MCP23017::portMode(MCP23017Port port, uint8_t directions, uint8_t pullups, uint8_t inverted) { writeRegister(MCP23017Register::IODIR_A + port, directions); diff --git a/src/MCP23017.h b/src/MCP23017.h index e665108..7ecc372 100644 --- a/src/MCP23017.h +++ b/src/MCP23017.h @@ -3,6 +3,7 @@ #include #include +#define MCP23017_I2C_ADDRESS 0x20 ///< The default I2C address of MCP23017. #define _MCP23017_INTERRUPT_SUPPORT_ ///< Enables support for MCP23017 interrupts. enum class MCP23017Port : uint8_t @@ -65,10 +66,19 @@ class MCP23017 * Instantiates a new instance to interact with a MCP23017 at the specified address. */ MCP23017(uint8_t address, TwoWire& bus = Wire); + /** + * Instantiates a new instance to interact with a MCP23017 at the + * MCP23017_I2C_ADDRESS default. + */ + MCP23017(TwoWire& bus = Wire); ~MCP23017(); #ifdef _DEBUG void debug(); #endif + /** + * May override the I2C address set by the constructor. Implicitly calls init(). + */ + void begin(uint8_t address = MCP23017_I2C_ADDRESS); /** * Initializes the chip with the default configuration. * Enables Byte mode (IOCON.BANK = 0 and IOCON.SEQOP = 1). @@ -221,4 +231,4 @@ class MCP23017 void clearInterrupts(uint8_t& portA, uint8_t& portB); #endif -}; \ No newline at end of file +}; From 6e15eedb183161165ac791ff4b87817bfe173073 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Tue, 31 May 2022 21:47:23 +0200 Subject: [PATCH 2/3] Begin without address argument shall use the ctor provided address, not reset it. --- src/MCP23017.cpp | 5 +++++ src/MCP23017.h | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/MCP23017.cpp b/src/MCP23017.cpp index 631186f..8530e05 100644 --- a/src/MCP23017.cpp +++ b/src/MCP23017.cpp @@ -30,6 +30,11 @@ void MCP23017::init() writeRegister(MCP23017Register::GPPU_A, 0xFF, 0xFF); } +void MCP23017::begin() +{ + init(); +} + void MCP23017::begin(uint8_t address) { _deviceAddr = address; diff --git a/src/MCP23017.h b/src/MCP23017.h index 7ecc372..a4a9067 100644 --- a/src/MCP23017.h +++ b/src/MCP23017.h @@ -76,9 +76,13 @@ class MCP23017 void debug(); #endif /** - * May override the I2C address set by the constructor. Implicitly calls init(). + * Uses the I2C address set during construction. Implicitly calls init(). */ - void begin(uint8_t address = MCP23017_I2C_ADDRESS); + void begin(); + /** + * Overrides the I2C address set by the constructor. Implicitly calls init(). + */ + void begin(uint8_t address); /** * Initializes the chip with the default configuration. * Enables Byte mode (IOCON.BANK = 0 and IOCON.SEQOP = 1). From d5de9ded15c7c470570c23bacb9ac86a360b5d90 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" <19971886+dok-net@users.noreply.github.com> Date: Thu, 2 Jun 2022 22:47:58 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Cascade from `begin` with I2C address argument to `begin` w/o argument, it's clearer than calling `init()` from both places. Co-authored-by: Bertrand Lemasle --- src/MCP23017.cpp | 2 +- src/MCP23017.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/MCP23017.cpp b/src/MCP23017.cpp index 8530e05..edd69cd 100644 --- a/src/MCP23017.cpp +++ b/src/MCP23017.cpp @@ -38,7 +38,7 @@ void MCP23017::begin() void MCP23017::begin(uint8_t address) { _deviceAddr = address; - init(); + begin(); } void MCP23017::portMode(MCP23017Port port, uint8_t directions, uint8_t pullups, uint8_t inverted) diff --git a/src/MCP23017.h b/src/MCP23017.h index a4a9067..3427261 100644 --- a/src/MCP23017.h +++ b/src/MCP23017.h @@ -80,7 +80,8 @@ class MCP23017 */ void begin(); /** - * Overrides the I2C address set by the constructor. Implicitly calls init(). + * Overrides the I2C address set by the constructor. Implicitly calls begin(). + */ void begin(uint8_t address); /**