6 years, 5 months ago.

Classes with interrupts

Hello everyone!

I'm studying mbed for STM32 and I'm very new in this.

I have a question about using classes in C++ and initializing interrupts in it.

Task I'm trying to solve: To create class, that takes 2 arguments:

  1. PinNumber, where the LED is attached (through the resistor to the ground);
  2. PinNumber, where the control button is attached (after debouncing circuit; button is pressed if pin input is low)

In class initializing following needs to be done:

  1. create InterruptIn on specified pin
  2. create the DigitalOut on specified pin
  3. make the LED turned off by default
  4. attach the function (as class member) to interrupt that changes the LED state

Here are links, I'm using info from: https://www.geeksforgeeks.org/constructors-c/ https://os.mbed.com/docs/latest/reference/interruptin.html

Here is my code:

Using interrupt for LED class

#include <mbed.h>

class LED_toggleable {
        
        public:
            void on (void){
                _control = 1;
                };
            
            void off(void){
                _contol = 0;
                };
            
            void flip(void){
                _contol = !_control;
                };
            
            LED_toggleable(PinName button_pin, PinName output_pin){
                _control(output_pin);
                _button(button_pin);
                _button.fall(callback(this, &LED_toggleable::flip));
                off();
                };

        private:
            DigitalOut _control;
            InterruptIn _button;
    };

int main() {
    LED_toggleable(PC_14, PC_14);

    while(1) {
    }
};

But compilation results in errors:

Quote:

Error: Identifier "_contol" is undefined in "main.cpp", Line: 11, Col: 18 Error: Identifier "_contol" is undefined in "main.cpp", Line: 15, Col: 18 Error: No default constructor exists for class "mbed::DigitalOut" in "main.cpp", Line: 18, Col: 68 Error: No default constructor exists for class "mbed::InterruptIn" in "main.cpp", Line: 18, Col: 68 Error: Call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type in "main.cpp", Line: 19, Col: 18 Error: Call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type in "main.cpp", Line: 20, Col: 18

Please, help me to solve the problem.

1 Answer

6 years, 5 months ago.

Hi there,

Please give this a try:

#include "mbed.h"
// The Mbed OS library is always included with #include "mbed.h"
 
class LED_toggleable {
        
    public:
        void on(void){
            _control = 1;
        };
        
        void off(void){
            _control = 0;
        };
        
        void flip(void){
            _control = !_control;
        };
        
        // Because _control and _button are private, you need to initialize them with an initializer list
        LED_toggleable(PinName button_pin, PinName output_pin) : _control(output_pin), _button(button_pin) {
            _button.fall(callback(this, &LED_toggleable::flip));
            off();
        };

    private:
        DigitalOut _control;
        InterruptIn _button;
};
 
int main() {
    LED_toggleable(PC_14, PC_14);
 
    while(1) {
    }
}

Please let me know if you have any questions!

- Jenny, team Mbed

If this solved your question, please make sure to click the "Thanks" link below!

Accepted Answer

Thank you. Now it works!

posted by Yehor Pererva 20 Jun 2018