Tinyscreen buttons

lambdacoder

  • Newbie
  • *
    • Posts: 2
    • View Profile
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
    • View Profile
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
    • View Profile
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.

Code: [Select]
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