1

I have a class:

#include "pico/stdlib.h"
#include "hardware/gpio.h"

class ButtonsController
{
public:
    ButtonsController(){};

    void UpdateButtons(bool first_enable, bool second_enable, std::string first_label, std::string second_label)
    {
        gpio_set_irq_enabled_with_callback(10, GPIO_IRQ_EDGE_FALL, false, ButtonCallback);
        gpio_set_irq_enabled_with_callback(11, GPIO_IRQ_EDGE_FALL, false, ButtonCallback);
        add_alarm_in_ms(500, EnableButtons, nullptr, false);
    }

    

private:

    void ButtonCallback(uint gpio, uint32_t events)
    {
        if(gpio == config::kButton_left_pin)
        {
            printf("ButtonCallback BTN 1 \n");
            btn1_pressed = true;
        }
        if(gpio == config::kButton_right_pin)
        {
            printf("ButtonCallback BTN 2 \n");
            btn2_pressed = true;
        }
    }
    
    int64_t EnableButtons(alarm_id_t id, void *irq_state)
    {
        gpio_set_irq_enabled_with_callback(10, GPIO_IRQ_EDGE_FALL, irq_btn_1_enabled, ButtonCallback);
        gpio_set_irq_enabled_with_callback(11, GPIO_IRQ_EDGE_FALL, irq_btn_2_enabled, ButtonCallback);
        
        return 0;
    }

    bool btn1_pressed{false};
    bool btn2_pressed{false};
    bool irq_btn_1_enabled;
    bool irq_btn_2_enabled;
};

The problem is that functions:

  • gpio_set_irq_enabled_with_callback(uint gpio, uint32_t events, bool enabled, void (*)(uint gpio, uint32_t event_mask))
  • add_alarm_in_ms(uint32_t ms, long long (*)(alarm_id_t id, void *user_data) callback, void *user_data, bool fire_if_past)

take pointer to a function as one of the parameters. In my case I want to pass pointer to a method of this class, i.e. ButtonCallbak() Is there a way that I could do this? Or should I completely change the concept? The idea that comes to my mind is to change those methods to static, but this would also require to change member variables to static... I'm not sure if this the way I should follow.

EDIT: I will have only one instance of ButtonsController.

4
  • Do you expect to have more then one ButtonsController instance? The static member method is only going to work if that's true. Commented Aug 26, 2024 at 13:53
  • I will have only one instance of ButtonsController. Why static member method would not work in such case? Commented Aug 26, 2024 at 14:01
  • Passing member function that accepts void * Commented Aug 26, 2024 at 14:07
  • @Dominik Because static members (variables + functions) will be global functions (they can't access the this pointer). So they cannot use any non-static member variables either. Commented Aug 26, 2024 at 15:18

2 Answers 2

1

Is there a way that I could do this?

No. A pointer to a free function and a pointer to a non-static class member function are fundamentally different. You cannot get the former from the latter on the fly.

Or should I completely change the concept?

If the library function expects a pointer to a free function you can only pass it a pointer to a free function. Lambdas can convert to function pointers, but only non-capturing ones, hence that's not going to help.

The idea that comes to my mind is to change those methods to static ...

Yes, that would work.

Sign up to request clarification or add additional context in comments.

Comments

0

The static function approach would work - with the limitations you mentioned - but you might find std::bind is preferable here, take a look at your favourite C++ reference site for details.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.