Skip to main content
+ return types and definition of LcdMenu::flag.
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

In this particular instance, and since you are inheriting from a class of a known library, making your own class makes sense. You just have to be aware that, although you can in principle have multiple instances of a class, you can only have one single handler for a particular interrupt. Thus the handler cannot be a regular method of the class (a method that knows it has been called for this instance), but it can be a static method. Such a method is much like a regular function which uses (abuses?) the class as a sort of namespace.

I would try something along these lines:

class LcdMenu : public Adafruit_RGBLCDShield
{
public:
  LcdMenu();
  void begin([params]) {
    Adafruit_RGBLCDShield::begin([params]);
    attachInterrupt(digitalPinToInterrupt(Menu_PIN), handleInterrupt, FALLING);
  }
  void MenuFoo();
  void update() {
    if (flag) {
      MenuFoo();
      flag=false;
    }
  }
private:
  static volatile bool flag;
  static void handleInterrupt() { flag = true; }
}

volatile bool LcdMenu::flag;

Then you just have to call LcdM.update() in loop().

In this particular instance, and since you are inheriting from a class of a known library, making your own class makes sense. You just have to be aware that, although you can in principle have multiple instances of a class, you can only have one single handler for a particular interrupt. Thus the handler cannot be a regular method of the class (a method that knows it has been called for this instance), but it can be a static method. Such a method is much like a regular function which uses (abuses?) the class as a sort of namespace.

I would try something along these lines:

class LcdMenu : public Adafruit_RGBLCDShield
{
public:
  LcdMenu();
  begin([params]) {
    Adafruit_RGBLCDShield::begin([params]);
    attachInterrupt(digitalPinToInterrupt(Menu_PIN), handleInterrupt, FALLING);
  }
  MenuFoo();
  update() {
    if (flag) {
      MenuFoo();
      flag=false;
    }
  }
private:
  static volatile bool flag;
  static void handleInterrupt() { flag = true; }
}

Then you just have to call LcdM.update() in loop().

In this particular instance, and since you are inheriting from a class of a known library, making your own class makes sense. You just have to be aware that, although you can in principle have multiple instances of a class, you can only have one single handler for a particular interrupt. Thus the handler cannot be a regular method of the class (a method that knows it has been called for this instance), but it can be a static method. Such a method is much like a regular function which uses (abuses?) the class as a sort of namespace.

I would try something along these lines:

class LcdMenu : public Adafruit_RGBLCDShield
{
public:
  LcdMenu();
  void begin([params]) {
    Adafruit_RGBLCDShield::begin([params]);
    attachInterrupt(digitalPinToInterrupt(Menu_PIN), handleInterrupt, FALLING);
  }
  void MenuFoo();
  void update() {
    if (flag) {
      MenuFoo();
      flag=false;
    }
  }
private:
  static volatile bool flag;
  static void handleInterrupt() { flag = true; }
}

volatile bool LcdMenu::flag;

Then you just have to call LcdM.update() in loop().

Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

In this particular instance, and since you are inheriting from a class of a known library, making your own class makes sense. You just have to be aware that, although you can in principle have multiple instances of a class, you can only have one single handler for a particular interrupt. Thus the handler cannot be a regular method of the class (a method that knows it has been called for this instance), but it can be a static method. Such a method is much like a regular function which uses (abuses?) the class as a sort of namespace.

I would try something along these lines:

class LcdMenu : public Adafruit_RGBLCDShield
{
public:
  LcdMenu();
  begin([params]) {
    Adafruit_RGBLCDShield::begin([params]);
    attachInterrupt(digitalPinToInterrupt(Menu_PIN), handleInterrupt, FALLING);
  }
  MenuFoo();
  update() {
    if (flag) {
      MenuFoo();
      flag=false;
    }
  }
private:
  static volatile bool flag;
  static void handleInterrupt() { flag = true; }
}

Then you just have to call LcdM.update() in loop().