|
Re: Buttons
This code at it's heart, just searches for the first True button, and tells you which one it found, if any.
The 12-possible buttons from the joystick come in as a cluster, so the code first converts that cluster into an array, so it can use one of the built-in array manipulation functions.
(Note that the Cypress I/O board inputs already come as an array of values.)
So we have an array of true/false values for each of the 12 possible buttons.
True if the button is being pushed, False if it is not being pushed.
Programming->Array has a simple function called Search 1D array that simply searches an input array for the value you specify. It returns the index into the array where it finds the first value that matches.
It returns a -1 if it cannot find that value at all.
The buttons in the array start with button 1 in the 0 array position.
So we get an index from -1 to 11 if we search the array for the first True value.
If we add 1 to that index we get out, our range is now 0-12.
0 now = no button pushed, and 1-12 is the button that is being pushed.
In this case we hand it the array of buttons and ask it to tell us the first one that is True.
If multiple buttons are being pushed, then it only recognizes the lowest number button and ignores any higher ones that might also be pushed at the same time.
Attach a case statement to the output index and you can make cases for each of the buttons you care about.
So, for instance, if you are using the trigger (button 1) elsewhere, your case statement can ignore it and only have cases for buttons 3-7. Default and no button would catch the others, or you could have cases for the extras that just do nothing with them.
You could even easily write your own code to search the button cluster for true values.
P.S.
The joystick example shows "Button 1" only so you can see what cluster the other code is manipulating. It isn't necessary or used.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
Last edited by Mark McLeod : 04-02-2011 at 13:37.
|