The quest for clean code...
Archiver
Posts: 46,084
The goal of this section is to read the highest pin that is high, and
report the corresponding number. Is there a
better/smaller/faster/cleaner way to do this? I do not like jumping to a
line just to set a variable.
Thanks
-paul
ReadFret:gosub ioset:
b0 = in15:if b0 then fret11
b0 = in14:if b0 then fret10
b0 = in13:if b0 then fret9
b0 = in12:if b0 then fret8
b0 = in11:if b0 then fret7
b0 = in10:if b0 then fret6
b0 = in9:if b0 then fret5
b0 = in8:if b0 then fret4
b0 = in7:if b0 then fret3
b0 = in6:if b0 then fret2
b0 = in5:if b0 then fret1
b0 = in4:if b0 then fret0
b1=1: return
fret0: b1=50:return
fret1: b1=2:return
fret2: b1=3:return
fret3: b1=4:return
fret4: b1=5:return
fret5: b1=6:return
fret6: b1=7:return
fret7: b1=8:return
fret8: b1=9:return
fret9: b1=10:return
fret10:b1=11:return
fret11:b1=12:return
[noparse][[/noparse]Non-text portions of this message have been removed]
report the corresponding number. Is there a
better/smaller/faster/cleaner way to do this? I do not like jumping to a
line just to set a variable.
Thanks
-paul
ReadFret:gosub ioset:
b0 = in15:if b0 then fret11
b0 = in14:if b0 then fret10
b0 = in13:if b0 then fret9
b0 = in12:if b0 then fret8
b0 = in11:if b0 then fret7
b0 = in10:if b0 then fret6
b0 = in9:if b0 then fret5
b0 = in8:if b0 then fret4
b0 = in7:if b0 then fret3
b0 = in6:if b0 then fret2
b0 = in5:if b0 then fret1
b0 = in4:if b0 then fret0
b1=1: return
fret0: b1=50:return
fret1: b1=2:return
fret2: b1=3:return
fret3: b1=4:return
fret4: b1=5:return
fret5: b1=6:return
fret6: b1=7:return
fret7: b1=8:return
fret8: b1=9:return
fret9: b1=10:return
fret10:b1=11:return
fret11:b1=12:return
[noparse][[/noparse]Non-text portions of this message have been removed]
Comments
b0=ncd ins
if b0=0 then no_high
b0=b0-5 ' gives 0-11
.
.
.
Al Williams
AWC
* Floating point A/D
http://www.al-williams.com/awce/pak9.htm
Original Message
From: Paul Jordan [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=ueOPlokSS0hL_yFPo4z8qz7LxfuVcgT62XupXiwGSy_Co2hnddm151lMO0834cgqPIBLPsPvBx3i1ym8HZjarj5zvwC-]automations@c...[/url
Sent: Tuesday, October 16, 2001 9:09 AM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] The quest for clean code...
The goal of this section is to read the highest pin that is high, and
report the corresponding number. Is there a
better/smaller/faster/cleaner way to do this? I do not like jumping to a
line just to set a variable.
Thanks
-paul
ReadFret:gosub ioset:
b0 = in15:if b0 then fret11
b0 = in14:if b0 then fret10
b0 = in13:if b0 then fret9
b0 = in12:if b0 then fret8
b0 = in11:if b0 then fret7
b0 = in10:if b0 then fret6
b0 = in9:if b0 then fret5
b0 = in8:if b0 then fret4
b0 = in7:if b0 then fret3
b0 = in6:if b0 then fret2
b0 = in5:if b0 then fret1
b0 = in4:if b0 then fret0
b1=1: return
fret0: b1=50:return
fret1: b1=2:return
fret2: b1=3:return
fret3: b1=4:return
fret4: b1=5:return
fret5: b1=6:return
fret6: b1=7:return
fret7: b1=8:return
fret8: b1=9:return
fret9: b1=10:return
fret10:b1=11:return
fret11:b1=12:return
[noparse][[/noparse]Non-text portions of this message have been removed]
To UNSUBSCRIBE, just send mail to:
basicstamps-unsubscribe@yahoogroups.com
from the same email address that you subscribed. Text in the Subject and
Body of the message will be ignored.
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
return a value (0 - 16) indicating the highest bit set in the variable used
with it. If zero, no bits were set. If 16, then Bit15 was set. LOOKUP
provides a neat way to enter a simple table (Note: the first element is
indexed at zero).
For your code, you could do this:
theBit VAR Byte
bitVal VAR Byte
theBit = NCD Ins
LOOKUP theBit,[noparse][[/noparse]1,1,1,1,1,50,2,3,4,5,6,7,8,9,10,11,12],bitVal
Notice how easy your code becomes when you take advantage of PBASIC's
functions. You could replace the LOOKUP line with multiple IF...THEN's, but
the code wouldn't be as clean or easy to modify.
STRONG SUGGESTION: DO NOT USE internal variable names like B0 and B1. Use
names that will give your code some meaning and use only the amount of space
you need. This will let the compiler organize the variable space and will
elimnate possible conflicts.
-- Jon Williams
-- Applications Engineer, Parallax
PS: Programming Note: You cannot RETURN from a GOTO which is implicit in
PBASIC's IF...THEN.
In a message dated 10/16/01 9:13:00 AM Central Daylight Time,
automations@c... writes:
> The goal of this section is to read the highest pin that is high, and
> report the corresponding number. Is there a
> better/smaller/faster/cleaner way to do this? I do not like jumping to a
> line just to set a variable.
> Thanks
> -paul
>
> ReadFret:gosub ioset:
> b0 = in15:if b0 then fret11
> b0 = in14:if b0 then fret10
> b0 = in13:if b0 then fret9
> b0 = in12:if b0 then fret8
> b0 = in11:if b0 then fret7
> b0 = in10:if b0 then fret6
> b0 = in9:if b0 then fret5
> b0 = in8:if b0 then fret4
> b0 = in7:if b0 then fret3
> b0 = in6:if b0 then fret2
> b0 = in5:if b0 then fret1
> b0 = in4:if b0 then fret0
> b1=1: return
>
> fret0: b1=50:return
> fret1: b1=2:return
> fret2: b1=3:return
> fret3: b1=4:return
> fret4: b1=5:return
> fret5: b1=6:return
> fret6: b1=7:return
> fret7: b1=8:return
> fret8: b1=9:return
> fret9: b1=10:return
> fret10:b1=11:return
>
[noparse][[/noparse]Non-text portions of this message have been removed]
based on an index.
Original Message
> The goal of this section is to read the highest pin that is high, and
> report the corresponding number. Is there a
> better/smaller/faster/cleaner way to do this? I do not like jumping to a
> line just to set a variable.
> Thanks
> -paul
>
> ReadFret:gosub ioset:
> b0 = in15:if b0 then fret11
> b0 = in14:if b0 then fret10
> b0 = in13:if b0 then fret9
> b0 = in12:if b0 then fret8
> b0 = in11:if b0 then fret7
> b0 = in10:if b0 then fret6
> b0 = in9:if b0 then fret5
> b0 = in8:if b0 then fret4
> b0 = in7:if b0 then fret3
> b0 = in6:if b0 then fret2
> b0 = in5:if b0 then fret1
> b0 = in4:if b0 then fret0
> b1=1: return
>
> fret0: b1=50:return
> fret1: b1=2:return
> fret2: b1=3:return
> fret3: b1=4:return
> fret4: b1=5:return
> fret5: b1=6:return
> fret6: b1=7:return
> fret7: b1=8:return
> fret8: b1=9:return
> fret9: b1=10:return
> fret10:b1=11:return
> fret11:b1=12:return
suggested, the LOOKUP command will get you the rest of the way.
Regards,
Al Williams
AWC
* Floating point math for the Stamp, PIC, SX, or any microcontroller
http://www.al-williams.com/awce/pak1.htm
Original Message
From: Al Williams [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=ylibVH38heIpSemZm-GHlNtM_KIgzNqSfrJgvyLNv8b0h3ysQaeQz1dsOgru59XGES3O8nm24726cloOIQ]alw@a...[/url
Sent: Tuesday, October 16, 2001 9:25 AM
To: basicstamps@yahoogroups.com
Subject: RE: [noparse][[/noparse]basicstamps] The quest for clean code...
How about:
b0=ncd ins
if b0=0 then no_high
b0=b0-5 ' gives 0-11
.
.
.
Al Williams
AWC
* Floating point A/D
http://www.al-williams.com/awce/pak9.htm
Original Message
From: Paul Jordan [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=NIBEv609xEuhxxJ3S8yrgFM-ftsxrUPumChizXCsS7h0tMTQvIOMqc3wNoX4XMvuCHc8i8Q_uUW1WEDilPdfovTvqeum]automations@c...[/url
Sent: Tuesday, October 16, 2001 9:09 AM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] The quest for clean code...
The goal of this section is to read the highest pin that is high, and
report the corresponding number. Is there a
better/smaller/faster/cleaner way to do this? I do not like jumping to a
line just to set a variable.
Thanks
-paul
ReadFret:gosub ioset:
b0 = in15:if b0 then fret11
b0 = in14:if b0 then fret10
b0 = in13:if b0 then fret9
b0 = in12:if b0 then fret8
b0 = in11:if b0 then fret7
b0 = in10:if b0 then fret6
b0 = in9:if b0 then fret5
b0 = in8:if b0 then fret4
b0 = in7:if b0 then fret3
b0 = in6:if b0 then fret2
b0 = in5:if b0 then fret1
b0 = in4:if b0 then fret0
b1=1: return
fret0: b1=50:return
fret1: b1=2:return
fret2: b1=3:return
fret3: b1=4:return
fret4: b1=5:return
fret5: b1=6:return
fret6: b1=7:return
fret7: b1=8:return
fret8: b1=9:return
fret9: b1=10:return
fret10:b1=11:return
fret11:b1=12:return
[noparse][[/noparse]Non-text portions of this message have been removed]
To UNSUBSCRIBE, just send mail to:
basicstamps-unsubscribe@yahoogroups.com
from the same email address that you subscribed. Text in the Subject and
Body of the message will be ignored.
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
To UNSUBSCRIBE, just send mail to:
basicstamps-unsubscribe@yahoogroups.com
from the same email address that you subscribed. Text in the Subject and
Body of the message will be ignored.
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
algorithm specific to the result. By clamping a
Minimum at 1, we can address the cases where any input
above in4 is high together with the cases where no
input between in4 and in15 is high. The special case
of in4 being the highest true input does not present
an elegant algebraic solution, so we skip around.
theBit VAR Byte
bitVal VAR Byte
theBit = NCD Ins
bitVal = 50 ' handle special case for in4
if theBit = 5 then skp ' retain value for special case
bitVal = (theBit - 4) Min 1 ' handle all other cases
skp:
Bob Pence
--- jonwms@a... wrote:
> Yes, there is an easier way: The NCD
> fuction,combined with LOOKUP. NCD will
> return a value (0 - 16) indicating the highest bit
> set in the variable used
> with it. If zero, no bits were set. If 16, then
> Bit15 was set. LOOKUP
> provides a neat way to enter a simple table (Note:
> the first element is
> indexed at zero).
>
> For your code, you could do this:
>
> theBit VAR Byte
> bitVal VAR Byte
>
> theBit = NCD Ins
> LOOKUP
>
theBit,[noparse][[/noparse]1,1,1,1,1,50,2,3,4,5,6,7,8,9,10,11,12],bitVal
>
> Notice how easy your code becomes when you take
> advantage of PBASIC's
> functions. You could replace the LOOKUP line with
> multiple IF...THEN's, but
> the code wouldn't be as clean or easy to modify.
>
> STRONG SUGGESTION: DO NOT USE internal variable
> names like B0 and B1. Use
> names that will give your code some meaning and use
> only the amount of space
> you need. This will let the compiler organize the
> variable space and will
> elimnate possible conflicts.
>
> -- Jon Williams
> -- Applications Engineer, Parallax
>
> PS: Programming Note: You cannot RETURN from a GOTO
> which is implicit in
> PBASIC's IF...THEN.
>
>
> In a message dated 10/16/01 9:13:00 AM Central
> Daylight Time,
> automations@c... writes:
>
>
> > The goal of this section is to read the highest
> pin that is high, and
> > report the corresponding number. Is there a
> > better/smaller/faster/cleaner way to do this? I do
> not like jumping to a
> > line just to set a variable.
> > Thanks
> > -paul
> >
> > ReadFret:gosub ioset:
> > b0 = in15:if b0 then fret11
> > b0 = in14:if b0 then fret10
> > b0 = in13:if b0 then fret9
> > b0 = in12:if b0 then fret8
> > b0 = in11:if b0 then fret7
> > b0 = in10:if b0 then fret6
> > b0 = in9:if b0 then fret5
> > b0 = in8:if b0 then fret4
> > b0 = in7:if b0 then fret3
> > b0 = in6:if b0 then fret2
> > b0 = in5:if b0 then fret1
> > b0 = in4:if b0 then fret0
> > b1=1: return
> >
> > fret0: b1=50:return
> > fret1: b1=2:return
> > fret2: b1=3:return
> > fret3: b1=4:return
> > fret4: b1=5:return
> > fret5: b1=6:return
> > fret6: b1=7:return
> > fret7: b1=8:return
> > fret8: b1=9:return
> > fret9: b1=10:return
> > fret10:b1=11:return
> >
>
>
>
>
> [noparse][[/noparse]Non-text portions of this message have been
> removed]
>
>
> To UNSUBSCRIBE, just send mail to:
> basicstamps-unsubscribe@yahoogroups.com
> from the same email address that you subscribed.
> Text in the Subject and Body of the message will be
> ignored.
>
>
> Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/
>
>
=====
Regards,
Bob Pence
NOTE: This mail is sent using my wireless phone. Calling me at 1 (610) 563-6198
is preferred to a reply. If replying to this email address, please do not quote
the original message.
__________________________________________________
Do You Yahoo!?
Make a great connection at Yahoo! Personals.
http://personals.yahoo.com
tendancy to err on the side of easy, verbose code -- but there are as many
ways to write a program as there are to skin a cat (no offence to feline
lovers).
-- Jon Williams
In a message dated 10/16/01 2:13:24 PM Central Daylight Time,
bobpence_2000@y... writes:
> We can save 8 more bytes in program memory by using an
> algorithm specific to the result. By clamping a
> Minimum at 1, we can address the cases where any input
> above in4 is high together with the cases where no
> input between in4 and in15 is high. The special case
> of in4 being the highest true input does not present
> an elegant algebraic solution, so we skip around.
>
> theBit VAR Byte
> bitVal VAR Byte
> theBit = NCD Ins
> bitVal = 50 ' handle special case for in4
> if theBit = 5 then skp ' retain value for special case
> bitVal = (theBit - 4) Min 1 ' handle all other cases
>
[noparse][[/noparse]Non-text portions of this message have been removed]