Author Topic: Stuck in Key Loop  (Read 3313 times)

0 Members and 1 Guest are viewing this topic.

Offline Albytok

  • LV0 Newcomer (Next: 5)
  • Posts: 2
  • Rating: +1/-1
    • View Profile
Stuck in Key Loop
« on: December 18, 2012, 04:30:46 pm »
So, what this routine is supposed to do is pause the program until the user hits the DEL key. For some reason though, whenever I use this routine, the calculator doesn't respond to the DEL key. In fact, it won't respond to any key, and just keeps looping forever.

Here's the code:
Code: [Select]
    ld c, 1

WaitKey:
    ld a, $FF
    out (c), a
    ld a, $FD
    out (c), a
    nop
    nop
   
    in a, (c)
       
    bit 7, a
    jp nz, WaitKey
   
    ld a, $FF
    out (c), a
    ret

What am I doing wrong?  :-\

EDIT: Wait, nevermind. I was just reading from the wrong key group. ::)
« Last Edit: December 19, 2012, 01:04:35 am by Albytok »
Sig.

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Stuck in Key Loop
« Reply #1 on: December 19, 2012, 11:57:15 pm »
Well, we can't just let a 0 reply post sit here, so here is my better version of your routine.

Code: [Select]
                                ;you don't need to do the $FF thing
        ld      a, $FD
        out     (01), a         ;once this is set, you don't have to reset it unless you change it
        ei                      ;this is for the halts
waitKey:
        halt                    ;this will lower battery usage (lol) and is roughly a 1/100 sec delay
        in      a, (01)
        add     a, a            ;moves bit 7 into carry position
        jr      c, waitKey
        halt                    ;this is known as debouncing, when keys are pressed, they often barely
        halt                    ; connect, disconnect, and then solidly reconnect
        halt                    ; without debouncing, you might accidentally press the button twice
        in      a, (01)         ; this is only needed in menus (and even then, it's optional)
        add     a, a            ; the .03 sec delay was rather arbitrary
        jr      c, waitKey

        ret

Not only does mine do key debouncing, but it ended up smaller ;D
« Last Edit: December 20, 2012, 12:02:56 am by thepenguin77 »
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline Albytok

  • LV0 Newcomer (Next: 5)
  • Posts: 2
  • Rating: +1/-1
    • View Profile
Re: Stuck in Key Loop
« Reply #2 on: December 22, 2012, 04:20:00 pm »
I didn't even know about the key debouncing thing. Thanks!
Sig.

Offline FloppusMaximus

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 290
  • Rating: +57/-5
    • View Profile
Re: Stuck in Key Loop
« Reply #3 on: December 22, 2012, 04:47:48 pm »
thepenguin77: that won't work unless you disable the OS's key scanning (either by setting indicOnly, or handling interrupts yourself with an IM 2 handler.)

Also, if you're doing something (like text input or a menu) that requires debouncing, you should probably be using GetCSC anyway.  The OS's key scanning routine is well-designed, and does a good job of handling all the corner cases that most people never think about.  Use GetCSC unless you have a good reason not to (e.g. a game that needs to handle multiple keys at once, or that uses the keys as "analog" inputs) - in my experience, such cases also tend to be the cases where debouncing is not useful.