PDA

View Full Version : Problem accessing variables via address between objects



scurrier
11-12-2010, 02:50 AM
Hello,
I wrote a simple object for making an array 2D. I can't seem to get it to work. I was also having trouble accessing variables by address earlier today in a separate project. Can I get some help?

Here is the object for doing 2d arrays:

''Array.spin
{{Returns the element at position (index1, index2) in an array that has num_index2 elements along the index2 direction
}}
PUB b ( index1 , index2 , num_index2 , address )
result := byte[@address][index2*num_index2 + index1]

PUB wb ( index1 , index2 , num_index2 , address , value )
byte[@address][index2*num_index2 + index1] := value

PUB l ( index1 , index2 , num_index2 , address )
result := long[@address][index2*num_index2 + index1]

PUB wl ( index1 , index2 , num_index2 , address , value)
long[@address][index2*num_index2 + index1] := value

Here is the object I am using to test the 2d array object:

''test.spin
CON
_xinfreq = 5000000
_clkmode = xtal1 + pll16x
VAR
byte UseDebug
byte testlong[16]
OBJ
Debug: "FullDuplexSerialPlus"
a: "Array"
PUB Main | i, i2
UseDebug := 1
if UseDebug
Debug.Start(31,30,0,57600)
waitcnt(clkfreq*1+cnt)
Debug.Tx(Debug#CLS)
waitcnt(clkfreq*1+cnt)
i:= 0
i2 := 0
a.wb ( i , i2 , 4 , @testlong , i+i2)
Debug.Dec(i+i2)
Debug.Tx(13)
Debug.Dec(a.b(i,i2,4,@testlong))
Debug.Tx(13)

And here is the output I get from the serial terminal:

0
109

Thank you!

scurrier
11-12-2010, 02:52 AM
One other symptom I forgot to mention. If i change code above the debug output statements, the second number output from the debug changes (the output from the array reading object).

kuroneko
11-12-2010, 03:04 AM
''Array.spin
{{Returns the element at position (index1, index2) ...
}}
PUB wb ( index1 , index2 , num_index2 , address , value )
byte[@address][index2*num_index2 + index1] := value



a.wb ( i , i2 , 4 , @testlong , i+i2)
Debug.Dec(i+i2)
Debug.Tx(13)
Debug.Dec(a.b(i,i2,4,@testlong))
Debug.Tx(13)

You pass an address to a.wb already so inside that function there is no need to dereference it again. Just use byte[address][...] (without @).

scurrier
11-12-2010, 03:05 AM
AHHHHH I should have caught that! Thank you, I'll test now!

scurrier
11-12-2010, 03:08 AM
Tested my object a few different ways, it definitely works now! I believe this also explains the other problem I was having earlier! THANKS!