Skip to content

Commit

Permalink
feat(gpio): Make GPIOIntr inherits from GPIOInput
Browse files Browse the repository at this point in the history
This will force the user to have a GPIO input setup when using the interrupt
functionalities.
  • Loading branch information
SoucheSouche committed Oct 3, 2022
1 parent 34a01c5 commit ac75cad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
16 changes: 2 additions & 14 deletions examples/gpio_intr_cxx/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,10 @@ using namespace std::placeholders;
class IntHdlr {
public:
IntHdlr(const GPIONum gpio_num, const GPIOPullMode mode, const GPIODriveStrength strength, const GPIOIntrType type):
gpio_input(gpio_num),
gpio_intr(gpio_num),
gpio_intr(gpio_num, type, mode, strength, "name", std::bind(&IntHdlr::callback, this, _1)),
counter(0),
missed_counter(0)
{
gpio_input.set_pull_mode(mode);
gpio_input.set_drive_strength(strength);

try {
gpio_intr.set_type(type);
gpio_intr.add_callback("some name", std::bind(&IntHdlr::callback, this, _1));
}
catch (const GPIOException& e) {
printf("[0x%x] occured: %s\n", e.error, e.what());
}
}

size_t get_counter() const
Expand All @@ -56,7 +45,7 @@ class IntHdlr {

void callback(GPIONum gpio_num)
{
if (gpio_input.get_gpio_num() == gpio_num)
if (gpio_intr.get_gpio_num() == gpio_num)
{
counter++;
}
Expand All @@ -65,7 +54,6 @@ class IntHdlr {
}
}
private:
GPIOInput gpio_input;
GPIOIntr gpio_intr;
size_t counter;
size_t missed_counter;
Expand Down
14 changes: 14 additions & 0 deletions gpio_intr_cxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ void stop_service(void)
gpio_uninstall_isr_service();
}

GPIOIntr::GPIOIntr(const GPIONum gpio_number,
const GPIOIntrType type,
const GPIOPullMode mode,
const GPIODriveStrength strength,
std::string cb_name,
interrupt_callback_t cb):
GPIOInput(gpio_number)
{
set_pull_mode(mode);
set_drive_strength(strength);
set_type(type);
add_callback(cb_name, cb);
}

void GPIOIntr::set_type(const GPIOIntrType type)
{
GPIO_CHECK_THROW(gpio_set_intr_type(gpio_num.get_value<gpio_num_t>(),
Expand Down
26 changes: 15 additions & 11 deletions include/gpio_intr_cxx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,26 @@ class GPIOIntrType : public StrongValueComparable<uint8_t> {
}
};

class GPIOIntr {
class GPIOIntr: public GPIOInput {
public:
GPIOIntr(const GPIONum gpio_number): gpio_num(gpio_number)
{
}

/**
* @brief Callback footprint declaration for the GPIO interrupt users.
*/
typedef std::function<void(GPIONum)> interrupt_callback_t;

/**
* @brief Constructor of GPIOIntr
*
* @param gpio_number
* @param type
* @param mode
* @param strength
* @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);

/**
* @brief Set the interrupt type on the GPIO input
Expand Down Expand Up @@ -262,12 +272,6 @@ class GPIOIntr {
*/
user_cb_table_t cb_table;

/**
* @brief Reference to the input GPIO on which the interrupt is
* defined.
*/
GPIONum gpio_num;

/**
* @brief Returns the iterator in cb_table where the callback with the given name
* was found, returns the end of the list if no callback was found
Expand Down

0 comments on commit ac75cad

Please sign in to comment.