Can't use attachInterrupt in a library

前端 未结 4 956
眼角桃花
眼角桃花 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:29

    I got around this by making a singleton base class which represents the hardware as a whole (which kinda makes sense in this situation anyway).

    Any function pointers can then be passed to the sub-component class, and handled by the singleton, whose member variables and methods are all static.

    Example headers (untested):

    // Sub-component
    class LampButton {
    public:
        LampButton(int pin, void(*pushHandler)());
    }
    
    // Sub-component
    class LampLed {
    public:
        LampLed(int pin);
        void toggle();
    }
    
    // Singleton represents the hardware in it's entirety
    class Lamp {
    public:
        // Call this instead of a constructor
        static void initialize(int buttonPin, int ledPin);
    
        // Function implemented inline for clarity - don't do this
        static void handleButtonPush() {
            led.toggle();
        }
    
    private:
        static LampButton button;
        static LampLed led;
    }
    

提交回复
热议问题