Can't use attachInterrupt in a library

前端 未结 4 941
眼角桃花
眼角桃花 2021-01-13 11:58

I\'m writing a simple library for an ultrasonic distance sensor and thought i\'d try using interrupts.

However i can\'t set my functions in the attachCallback<

4条回答
  •  难免孤独
    2021-01-13 12:19

    As I stumbled upon this question and it hasn't had an accepted answer, I write what I found, which worked for me:

    The interrupt has to be called by a global wrapper. This wrapper needs to call a handleInterupt function of the class. Therefore it has to know the class. This can be done by storing it in a global variable. If multiple instances of the class should be used, multiple such global variables have to be used. But as the interrupt pins are just a few you can write a global variable and function for every pin:

    MyClass theInstance_pin3 = NULL;
    MyClass theInstance_pin7 = NULL;
    
    // Somewhere, fill in an initialized copy of MyClass,
    // and set theInstance_pin3 or theInstance_pin7 to it
    
    void ISR_3()
    {
       if (theInstance_pin3)
           theInstance_pin3->handleInterrupt();
    }
    void ISR_7()
    {
       if (theInstance_pin7)
           theInstance_pin7->handleInterrupt();
    }
    

    as a reference see: http://forum.arduino.cc/index.php?topic=41713.0 or http://forum.arduino.cc/index.php?topic=160101.0

提交回复
热议问题