From e71b50cdc621588936b2bbd0d64c52d1831dd0d5 Mon Sep 17 00:00:00 2001 From: Guillaume Souchere Date: Fri, 16 Sep 2022 10:49:31 +0200 Subject: [PATCH] feat(gpio): add a constructor to GPIOInput that fully configures a GPIO The new constructor takes gpio number, pull mode and driver strength as parameters. This constructor by used in GPIOIntr constructor. --- examples/gpio_intr_cxx/main/main.cpp | 2 +- gpio_cxx.cpp | 7 +++++++ gpio_intr_cxx.cpp | 6 ++---- include/gpio_cxx.hpp | 9 +++++++++ include/gpio_intr_cxx.hpp | 5 ++--- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/examples/gpio_intr_cxx/main/main.cpp b/examples/gpio_intr_cxx/main/main.cpp index 612ac9b..fbe490f 100644 --- a/examples/gpio_intr_cxx/main/main.cpp +++ b/examples/gpio_intr_cxx/main/main.cpp @@ -27,7 +27,7 @@ using namespace std::placeholders; class IntHdlr { public: IntHdlr(const GPIONum gpio_num, const GPIOPullMode mode, const GPIODriveStrength strength, const GPIOIntrType type): - gpio_intr(gpio_num, type, mode, strength, "name", std::bind(&IntHdlr::callback, this, _1)), + gpio_intr(gpio_num, mode, strength, type, "name", std::bind(&IntHdlr::callback, this, _1)), counter(0), missed_counter(0) { diff --git a/gpio_cxx.cpp b/gpio_cxx.cpp index 3a28e6f..c741def 100644 --- a/gpio_cxx.cpp +++ b/gpio_cxx.cpp @@ -156,6 +156,13 @@ GPIOInput::GPIOInput(GPIONum num) : GPIOBase(num) GPIO_CHECK_THROW(gpio_set_direction(gpio_num.get_value(), GPIO_MODE_INPUT)); } +GPIOInput::GPIOInput(const GPIONum num, const GPIOPullMode mode, const GPIODriveStrength strength) + : GPIOInput(num) +{ + set_pull_mode(mode); + set_drive_strength(strength); +} + GPIOLevel GPIOInput::get_level() const noexcept { int level = gpio_get_level(gpio_num.get_value()); diff --git a/gpio_intr_cxx.cpp b/gpio_intr_cxx.cpp index 75a201d..38a678b 100644 --- a/gpio_intr_cxx.cpp +++ b/gpio_intr_cxx.cpp @@ -21,15 +21,13 @@ void stop_service(void) } GPIOIntr::GPIOIntr(const GPIONum gpio_number, - const GPIOIntrType type, const GPIOPullMode mode, const GPIODriveStrength strength, + const GPIOIntrType type, std::string cb_name, interrupt_callback_t cb): -GPIOInput(gpio_number) +GPIOInput(gpio_number, mode, strength) { - set_pull_mode(mode); - set_drive_strength(strength); set_type(type); add_callback(cb_name, cb); } diff --git a/include/gpio_cxx.hpp b/include/gpio_cxx.hpp index ddd22c8..3b035e5 100644 --- a/include/gpio_cxx.hpp +++ b/include/gpio_cxx.hpp @@ -320,6 +320,15 @@ class GPIOInput : public GPIOBase { */ GPIOInput(GPIONum num); + /** + * @brief Constructs and fully configure a GPIO as input + * + * @param num GPIO pin number of the GPIO to be configured. + * @param mode The pull mode to configure on the GPIO + * @param strength The internal pull resistor strength to setup on the GPIO + */ + GPIOInput(const GPIONum num, const GPIOPullMode mode, const GPIODriveStrength strength); + /** * @brief Read the current level of the GPIO. * diff --git a/include/gpio_intr_cxx.hpp b/include/gpio_intr_cxx.hpp index 6c99df0..82571b2 100644 --- a/include/gpio_intr_cxx.hpp +++ b/include/gpio_intr_cxx.hpp @@ -199,9 +199,8 @@ class GPIOIntr: public GPIOInput { * @param cb_name * @param cb */ - GPIOIntr(const GPIONum gpio_number, const GPIOIntrType type, - const GPIOPullMode mode, const GPIODriveStrength strength, - std::string cb_name, interrupt_callback_t cb); + GPIOIntr(const GPIONum gpio_number, const GPIOPullMode mode, const GPIODriveStrength strength, + const GPIOIntrType type, std::string cb_name, interrupt_callback_t cb); /** * @brief Set the interrupt type on the GPIO input