Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: Compynerd255 on March 15, 2011, 10:49:04 am

Title: Key Timers
Post by: Compynerd255 on March 15, 2011, 10:49:04 am
I started this topic not because I have a question, but I have something cool to share. By default, in Axe, you can either use GetCSC to read your keys or read the input directly. However, this means that the key either responds once per press or responds as long as it is held down. What if you would like the key to instead respond every few frames, like a variable key repeat function? The solution is a key timer, which I will demonstrate below:

1. Set aside an area of free RAM, one byte per key you plan to read (the example uses 4 keys, one for each directional arrow):
Code: [Select]
0→L1
Fill(L1, 2

2. Create a subroutine that updates the key timers for each key:
Code: [Select]
{L1}+1*getKey(1)→{L1}
{L1+1}+1*getKey(2)→{L1+1}
{L1+2}+1*getKey(3)→{L1+2}
{L1+3}+1*getKey(4)→{L1+3}
Alternatively, instead of +1, you can add a variable that is updated by an interrupt.

3. After calling that subroutine, check for each key like so and preform the action:
Code: [Select]
If {L1}+1<127
...action...
End
...check the other keys...

4. After doing all you want to with the key, insert the following between your action and the End:
Code: [Select]
{L1}-[key delay in frames]→{L1}

The total effect of this code means that a key will register the instant it is pressed, and will then repeat in the delay you set.
This code is useful because not only does it let you control your key repeats, but it also condenses all your direct key reads to one place, allowing you to easily change the keys.
Title: Re: Key Timers
Post by: jnesselr on March 16, 2011, 11:27:52 pm
That's actually pretty cool.  What happens if you do a delay for keys that already support multiple presses?
Title: Re: Key Timers
Post by: Juju on March 17, 2011, 12:09:03 am
Pretty nite tutorial, thanks :)
Title: Re: Key Timers
Post by: Compynerd255 on March 17, 2011, 10:28:02 am
What happens if you do a delay for keys that already support multiple presses?
It doesn't matter what key you use, since you're reading the key directly instead of using GetCSC.

If you want an example of this code in action, read the source for Eitrix.
Title: Re: Key Timers
Post by: ztrumpet on March 17, 2011, 09:14:21 pm
Hey, this looks pretty cool.  Great idea. ;D