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.
thispointer). So they cannot use any non-static member variables either.