Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: Albytok on December 18, 2012, 04:30:46 pm

Title: Stuck in Key Loop
Post by: Albytok 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. ::)
Title: Re: Stuck in Key Loop
Post by: thepenguin77 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
Title: Re: Stuck in Key Loop
Post by: Albytok on December 22, 2012, 04:20:00 pm
I didn't even know about the key debouncing thing. Thanks!
Title: Re: Stuck in Key Loop
Post by: FloppusMaximus 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.