PDA

View Full Version : answer to: previous posted thread - fast case like...


dfitz
03-07-08, 09:06 AM
Hi,

This answer is for the thread I started just before which isn't up online yet....

I seem to have figured out my problem, and it's actually rather easy. But I think may be beneficial for some people doing any kind of serial decoding.

refer to the following code:

------------------------------------
SCRUB

\ commands
: CMD_1
CR ." test 1"
; EEWORD

: CMD_2
CR ." test 2"
; EEWORD

: CMD_3
CR ." test 3"
; EEWORD


CREATE SERIAL_CMD_TABLE
\ Lookup table for command functions
' CMD_1 CFA P,
' CMD_2 CFA P,
' CMD_3 CFA P,
EEWORD

: RUN_CMD
\ get address & execute
SERIAL_CMD_TABLE + P@
EXECUTE
; EEWORD

---------------------------------

so basically you define a number of functions that you want to run with a certain index (for example my example before about decoding a serial packet with a command byte). Then you can use this index to jump straight to the function you wish to execute WITHOUT have many MANY nested if statements.

This works a treat :)

For example, with the above code, to testyou would simply parse:
0 RUN_CMD
1 RUN_CMD
2 RUN_CMD

to give the result:
test 1
test 2
test 3

in my previous posted decoding code, it would be:
iReceiveCommand @ RUN_CMD

Thanks to the example in the post I quoted in my previous message. I was actually aboe to decipher it afterall :-p

cheers
Daniel