Reputation: 538
I have a class:
#ifndef _BUTTON_LISTENER_H_
#define _BUTTON_LISTENER_H_
#include <iostream>
#include <vector>
#include "mbed.h"
#include "Buttons/MenuButton.h"
#include "MenuNavigator.h"
class MenuNavigator;
class ButtonListener
{
public:
ButtonListener(MenuNavigator* navigator, unsigned int samplePeriod_us,
MenuButton* select, MenuButton* down,
MenuButton* up, MenuButton* cancel);
vector<MenuButton*> getButtons();
MenuNavigator* getNavigator();
protected:
void init();
void isr();
vector<MenuButton*> buttons;
MenuNavigator* navigator;
unsigned int samplePeriod_us;
Ticker ticker;
};
#endif
And its implementation:
#include "ButtonListener.h"
#include "Buttons/MenuButton.h"
ButtonListener::ButtonListener(MenuNavigator* navigator,
unsigned int samplePeriod, MenuButton* s, MenuButton* d,
MenuButton* u, MenuButton* c) :
navigator(navigator),
samplePeriod_us(samplePeriod_us)
{
buttons.push_back(s);
buttons.push_back(d);
buttons.push_back(u);
buttons.push_back(c);
init();
}
void ButtonListener::init()
{
ticker.attach_us(callback(this, &ButtonListener::isr), 500000);
}
void ButtonListener::isr()
{
printf("in isr\n");
}
I'm attaching isr()
method to create an interrupt so that it sends the text to the terminal window. But for some reason, it doesn't work.
If I put printf()
statement before or after the init()
method in the constructor, the text of printf()
gets printed, but the text in the isr()
doesn't.
Any help?
Upvotes: 0
Views: 471
Reputation: 538
Accidentally found the solution. I have MyClass
that instantiates ButtonListener
. In this class, I declared ButtonListener
as a pointer:
ButtonListener* blistener;
.
In the constructor of the MyClass
, I had ButtonListener buttonListener = new ButtonListener(args...)
. After I changed it to just buttonListener = new ButtonListener(args...)
things worked out.
Hope it's going to be helpful to someone else.
Upvotes: 1