TinyCircuits Forum

TinyCircuits Products => TinyDuino Processors & TinyShields => Topic started by: lambdacoder on April 26, 2015, 02:59:11 PM

Title: Tinyscreen buttons
Post by: lambdacoder on April 26, 2015, 02:59:11 PM
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
Title: Re: Tinyscreen buttons
Post by: lambdacoder on April 26, 2015, 03:37:25 PM
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 !
Title: Re: Tinyscreen buttons
Post by: zet23t on April 27, 2015, 04:23:53 AM
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.