PDA

View Full Version : help clean up some code



longpole001
- 7th April 2014, 23:05
hi guys , this code is rubbish but it looks like it is what i need to do , looking for better ways of writing it

cheers

Sheldon


if counter= 1 then
if value0 <> newdata then
value1 = newdata
counter = counter +1
else
return
endif
endif

if counter = 2 then
if value0 <> newdata then
if Value1 <> newdata then
value2 = newdata
counter = counter +1
else
return
endif
endif
endif

if counter = 3 then
if value0 <> newdata then
if Value1 <> newdata then
if Value2 <> newdata then
value3 = newdata
counter = counter +1
else
return
endif
endif
endif
endif


if counter = 4 then
if value0 <> newdata then
if Value1 <> newdata then
if Value2 <> newdata then
if Value3 <> newdata then
value4 = newdata
counter = counter +1
else
return
endif
endif
endif
endif
endif

if counter = 5 then
if value0 <> newdata then
if Value1 <> newdata then
if Value2 <> newdata then
if Value3 <> newdata then
if Value4 <> newdata then
value5 = newdata
counter = counter +1
else
return
endif
endif
endif
endif
endif
endif
if counter = 6 then
if value0 <> newdata then
if Value1 <> newdata then
if Value2 <> newdata then
if Value3 <> newdata then
if Value4 <> newdata then
if Value5 <> newdata then
value6 = newdata
counter= counter +1
else
return
endif
endif
endif
endif
endif
endif

mpgmike
- 7th April 2014, 23:30
Here's what I'd do:
if counter = 1 and value0 <> newdata then
value1 = newdata
counter = counter +1
return
endif
if counter = 2 and Value0 <> newdata and Value1 <> newdata then
value2 = newdata
counter = counter + 1
return
endif" .......

There is probably a clean "select case" application here as well.

Mike

longpole001
- 8th April 2014, 00:22
yes i could do that but i think the actual structure could be improved , the "newdata" and Value0-6 are actully a 3 values tested to see if that table of arrays(49 arrays values) should add to the larger table of arrays with a total of 6 records

in essence it just test value if it new then add it to the table and inc the counter , if its not then dont add to the table

mpgmike
- 9th April 2014, 01:24
Main:
select case counter
case is = 0
if value0 <> newdata then
value1 = newdata
counter = counter +1
endif
case is = 1
if value0 <> newdata and value1 <> new data then
value2 = newdata
counter = counter + 1
endif.......
return

longpole001
- 9th April 2014, 06:19
thanks mike , both option work ok , as it turned out i decided to go with "and" option , it just really large array group and as such it has about 30 " and " statements per match group of the records it takes about 10k compile for that section , large arrays do take space , i need to write the sorting index routine for this large array in PBP next , not seen any examples where using large data arrays to sort as yet

cheers

Sheldon

HenrikOlsson
- 9th April 2014, 08:12
Hi,
Generally, using nested IF/THENs produces less code than using AND. For example

IF A = 1 THEN
IF B = 2 THEN
IF C = 3 THEN
IF D = 4 THEN
IF E = 5 THEN
HIGH PortB.0
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
Compiled for a 16F628 is 30 words and 76 bytes for 18F4520 (all variables are bytes) while using AND, like this

IF (A = 1) AND (B = 2) AND (C = 3) AND (D = 4) AND (E = 5) THEN
HIGH PortB.0
ENDIF
takes 79 words for the 16F628 and 162 bytes for the 18F4520.

/Henrik.

richard
- 9th April 2014, 10:24
how does this look ?

value var byte[6]
chk var byte

chknewval:
chk=0
while chk<6
if value[chk] = 0 then
value[chk]=newvalue
chk=6
else
if value[chk]=newvalue then ;we already have this value
chk=6
else
chk=chk+1
endif
endif
end while
return ?

mpgmike
- 9th April 2014, 16:30
I just learned something new about PBP! (Use of [brackets]). Thanks Richard!

Mike

longpole001
- 10th April 2014, 01:33
i always knew the if , then way produces less words but with 30 "and" compares per test , if then way look really bad and hard to follow, there seems to be no really short way to do large compares for arrays

HenrikOlsson
- 10th April 2014, 08:56
Hi,
I haven't looked into it in detail but perhaps you could use ARRAYREAD with the WAIT or WAITSTR modifer to do what you want?


Value VAR BYTE[6] ' Array of 6 bytes for the "new values"
DummyVAR 'Needed for ArrayWrite to do what we want - possibly

ARRAYREAD Value, 6, NoMatch, [WAIT("123456"), DummyVAR] ' Search the Value-array for 123456, jump to NoMatch if not found.
' Continue here if match is found
' ....
' ....

NoMatch:
' Continue here if no match was found
The above tries to match the array to a static string of 123456, if what you want to match is changing then use a second array for the "match condition" and use WAITSTR instead of WAIT. At least that's how I THINK it should work, I haven't actually tried it though.....

/Henrik.

longpole001
- 10th April 2014, 12:00
THANKS HENDRICK , i not looked at that command , will investigate further , i finding it very hard for large arrays and indexing with match , i am working now on arrays using 78 variables per index per group match which 5 variable in the array need to be matched before adding / removing / reindexing , and then needs tobe saved , i have a lot of time before displaying the results on GLCD about 1 sec , i am finding that pbp need to to have more inbuilt functions in this area . iam looking at writing these sort of fuctions in asm , but my asm really need to improve
space is now becoming an issue

i may pm if thats ok
cheers

sheldon