Shop OBEX P1 Docs P2 Docs Learn Events
How to optimize this code? — Parallax Forums

How to optimize this code?

tcicatellitcicatelli Posts: 8
edited 2012-03-09 17:10 in BASIC Stamp
Hi-

I was trying to clean this code up with a for-next loop, but can't seem to reference the bits properly

xInputs         VAR Word                      
idx             VAR Nib

GetActiveButton:
IF (xinputs.BIT0  = 0) THEN btnActive = 1:  RETURN
IF (xinputs.BIT1  = 0) THEN btnActive = 2:  RETURN
IF (xinputs.BIT2  = 0) THEN btnActive = 3:  RETURN
IF (xinputs.BIT3  = 0) THEN btnActive = 4:  RETURN
IF (xinputs.BIT4  = 0) THEN btnActive = 5:  RETURN 
IF (xinputs.BIT5  = 0) THEN btnActive = 6:  RETURN 
IF (xinputs.BIT6  = 0) THEN btnActive = 7:  RETURN
IF (xinputs.BIT7  = 0) THEN btnActive = 8:  RETURN
IF (xinputs.BIT8  = 0) THEN btnActive = 9:  RETURN
IF (xinputs.BIT9  = 0) THEN btnActive = 10: RETURN
IF (xinputs.BIT10 = 0) THEN btnActive = 11: RETURN
IF (xinputs.BIT11 = 0) THEN btnActive = 12: RETURN
IF (xinputs.BIT12 = 0) THEN btnActive = 13: RETURN  
IF (xinputs.BIT13 = 0) THEN btnActive = 14: RETURN
IF (xinputs.BIT14 = 0) THEN btnActive = 15: RETURN  
IF (xinputs.BIT15 = 0) THEN btnActive = 16  RETURN

What I have is:
xInputs         VAR Word  
BTN1        VAR xInputs
BTN2        VAR BIT
BTN3        VAR BIT
BTN4        VAR BIT
BTN5        VAR BIT
BTN6        VAR BIT
BTN7        VAR BIT
BTN8        VAR BIT
BTN9        VAR BIT
BTN10       VAR BIT
BTN11       VAR BIT
BTN12       VAR BIT
BTN13       VAR BIT
BTN14       VAR BIT
BTN15       VAR BIT
BTN16       VAR BIT

idx             VAR Nib

GetActiveButton:
FOR idx = 0 TO 15
  IF xinputs(idx) = 0 THEN
    btnActive = idx + 1
    RETURN
  ENDIF
  NEXT
RETURN


xinputs usually equals 11111111111111 unless a button is pressed. Then one of the 1's gets flipped. The code keeps returning a 2? I'm not sure what I'm doing wrong. The first code works, but I'm trying to write tighter code.

Any help would be appreciated.

Thanks,
-Tom

Comments

  • davejamesdavejames Posts: 4,047
    edited 2012-03-09 11:50
    Hi Tom,

    Why BTN1 VAR xInputs?

    Shouldn't this be BTN1 VAR BIT?


    Curious in San Jose...
  • tcicatellitcicatelli Posts: 8
    edited 2012-03-09 12:59
    That coding style was in the end of the Stampworks manual under "power coding". I know it looks strange, but they recommend it.
  • davejamesdavejames Posts: 4,047
    edited 2012-03-09 14:43
    ...strange, yes. Guess I'll have to go re-read that section.

    As an excercise, could you walk me through what you believe the second example is suppposed to do? I'm seeing something I don't quite understand.

    Welcome to the Forum, BTW.
  • CatspawCatspaw Posts: 49
    edited 2012-03-09 14:50
    Receive the word, then shift the bits out using a >> or << (depending on whether you're right-handed or left-handed) in a loop.

    If you receive say a %0000000000001000 (button 3 using 0 thru 15) you'll loop 4 times before an AND test becomes true.

    o.k. I'm new to Pbasic...so I can't just write it out, but test the first bit of a word using [dot]BIT1 (look in Help of the editor under variables). False? then shift the word one time. Test again. Get a true, the loop index will tell you which button was pressed.

    You may or may not want to continue testing to determine whether more than one button was pressed.
  • tcicatellitcicatelli Posts: 8
    edited 2012-03-09 15:54
    Hi Guys-

    I figured it out.
    xInputs         VAR Word                      
    idx             VAR Nib
    
    FOR idx = 0 TO 15
      IF xinputs.BIT0(idx) = 0 THEN
            btnActive = idx + 1
            RETURN
      ENDIF
      NEXT
    RETURN
    

    Thanks
  • CatspawCatspaw Posts: 49
    edited 2012-03-09 17:10
    I missed the part about the array. Helpful thing to know. One of these days I'm actually going to read everything to see what I'm missing.
Sign In or Register to comment.