How to optimize this code?
tcicatelli
Posts: 8
Hi-
I was trying to clean this code up with a for-next loop, but can't seem to reference the bits properly
What I have is:
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
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
Why BTN1 VAR xInputs?
Shouldn't this be BTN1 VAR BIT?
Curious in San Jose...
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.
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.
I figured it out.
Thanks