Button management

Hi guys, I don’t know if anyone want’s this, but it’s what I have been using for managing my inputs, some people might prefer it…

// Button updates
#define HELD 0
#define NEW 1
#define RELEASE 2
byte CompletePad, ExPad, TempPad;
bool _A[3], _B[3], _Up[3], _Down[3], _Left[3], _Right[3];

void UPDATEPAD(int pad, int var){
  _B[pad] = (var & B_BUTTON) >> 2; // 4
  _A[pad] = (var & A_BUTTON) >> 3; // 8
  _Down[pad] = (var & DOWN_BUTTON) >> 4; // 16
  _Left[pad] = (var & LEFT_BUTTON) >> 5; // 32
  _Right[pad] = (var & RIGHT_BUTTON) >> 6; // 64
  _Up[pad] = (var & UP_BUTTON) >> 7; // 128
}
void UpdatePad(int joy_code) 
{
  ExPad = CompletePad;
  CompletePad = joy_code;
  UPDATEPAD(HELD, CompletePad); // held
  UPDATEPAD(RELEASE, (ExPad & (~CompletePad))); // released
  UPDATEPAD(NEW, (CompletePad & (~ExPad))); // newpress
}

void loop() {
  UpdatePad(arduboy.buttonsState()); // update every loop, only once

// then simple check for _A[HELD], _A[RELEASE], _A[NEW] etc.
if(_A[NEW]){
  arduboy.print("A Hit!");
}
  arduboy.display();
}

I don’t know if it’s any better or worse than other peoples solutions, but it works for me.

http://community.arduboy.com/t/best-practices-for-button-handling/2444/1

I’m kind of surprised there isn’t more use of the pin change interrupts. I suppose polling frequency is high enough, but with properly implemented pin change interrupts you’re getting the most real time response you can get from the device.

Unless you make the entire game interrupt event driven, polling the buttons for each frame is sufficient. Using interrupts to receive button changes instantly doesn’t give you any advantage if you’re only updating at a constant fixed frame rate.

Yeah I guess the super fast frame rate we have now makes it unnecessary. When I first started the project with software SPI graphics library it was totally possible to get multiple key presses in between frames. And then your game wouldn’t respond quite in the way you expected.