m5stack / M5Stack

M5Stack Arduino Library
MIT License
1.19k stars 425 forks source link

M5.BtnA.wasReleased() never true if M5.update() is intermediately called again #290

Open klues opened 2 years ago

klues commented 2 years ago

I've the following scenario:

long lastCheck = 0;

void loop() {
   M5.update();
   if (millis() - lastCheck > 200) {
      lastCheck = millis();
      if (M5.BtnA.wasReleased()) {
         //do something - never reached!
      }
      // check other inputs via I2C
   }
}

So I'm checking for various inputs, the M5 buttons and other ones via I2C. I limit the rate of checking to 200ms. However in this code M5.BtnA.wasReleased() is never 1 or true, it seems like M5.update() internally resets the state, which shouldn't be the case IMO.

I'm doing this workaround, which works, but is not so clean:

long lastCheck = 0;
int btnAPressed = 0;

void loop() {
   M5.update();
   btnAPressed = btnAPressed || M5.BtnA.wasReleased();
   if (millis() - lastCheck > 200) {
      lastCheck = millis();
      if (btnAPressed) {
         //do something - works!
      }
      // check other inputs via I2C
      btnAPressed = 0;
   }
}