Shop OBEX P1 Docs P2 Docs Learn Events
Problem accessing variables via address between objects — Parallax Forums

Problem accessing variables via address between objects

scurrierscurrier Posts: 32
edited 2010-11-11 20:08 in Propeller 1
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!

Comments

  • scurrierscurrier Posts: 32
    edited 2010-11-11 19:52
    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).
  • kuronekokuroneko Posts: 3,623
    edited 2010-11-11 20:04
    scurrier wrote: »
    ''Array.spin
    {{Returns the element at position (index1, index2) ...
    }}
    PUB wb ( index1 , index2 , num_index2 , address , value )
      byte[[COLOR="Red"]@[/COLOR]address][index2*num_index2 + index1] := value
    
    a.wb ( i , i2 , 4 , [COLOR="Red"]@testlong[/COLOR] , 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 @).
  • scurrierscurrier Posts: 32
    edited 2010-11-11 20:05
    AHHHHH I should have caught that! Thank you, I'll test now!
  • scurrierscurrier Posts: 32
    edited 2010-11-11 20:08
    Tested my object a few different ways, it definitely works now! I believe this also explains the other problem I was having earlier! THANKS!
Sign In or Register to comment.