Tinyscreen buttons

Started by lambdacoder, April 26, 2015, 02:59:11 PM

Previous topic - Next topic

lambdacoder

*
Newbie
Posts: 2
Logged
Hi everyone,

the tiny screen library provides the method getButtons() which returns a uint8_t

is it possible to access to each button state individually ?
if yes how ?

thanks

lambdacoder

*
Newbie
Posts: 2
Logged
I have the answer :)

getButtons return a byte
the last 4 bites give the buttons state

bit 0 for left down
bit 1 for left up
bit 2 for right ip
bit 3 for right down

you get any combination when several buttons are pressed at the same time

enjoy !

zet23t

***
Full Member
Posts: 44
Logged
One nifty trick: Store the button state in a char value, use the upper 4 bits for the previous state and the lower 4 bits for the current state, e.g.


unsigned char screenButtonState = 0;
void updateScreenButtonState() {
  screenButtonState = screenButtonState << 4 | display.getButtons();
}


This way you can detect if a button was just recently pressed down ((screenButtonState & (1|16)) == 1), just released ((screenButtonState & (1|16)) == 16) or is now pressed for more than one frame ((screenButtonState & (1|16)) == 17). I found that useful more than once - besides, it's very cheap as it involves only a few opcodes and cycles to handle.

SMF spam blocked by CleanTalk