简体   繁体   中英

MBED Ticker doesn't attach interrupt

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM