Shop OBEX P1 Docs P2 Docs Learn Events
Trying to Access a global variable from with in a object. — Parallax Forums

Trying to Access a global variable from with in a object.

Coder96Coder96 Posts: 42
edited 2010-10-26 08:09 in Propeller 1
I'm trying to access a global variable from with in a object.

When all the code is in one file. It works as expected.
CON
    _clkmode      = xtal1 + pll16x                       
    _xinfreq      = 5_000_000

    SerCon_Tx = 8
    SerCon_Rx = 9
VAR
    long Stack[14]
    long GlobalVar1
OBJ
    SerCon  : "FullDuplexSerialPlus"

PUB init

  cognew(OtherCog, @Stack)

  dira[16] := 0
  
  repeat
    if(ina[16] <> 0)
      GlobalVar1 := 1
    else
      GlobalVar1 := 0    
  
PUB OtherCog

  ' Serial Console port 
  SerCon.start(SerCon_Tx, SerCon_Rx, 0, 57600)
  SerCon.str(string(" ",SerCon#CR,SerCon#LF))
  SerCon.str(string(" Serial Console started.",SerCon#CR,SerCon#LF))
  
  repeat
    SerCon.dec(GlobalVar1)    
    SerCon.str(string(" ",SerCon#CR,SerCon#LF))
    waitcnt(100*(clkfreq/100) + cnt)

This outputs what I expect. I have 0's when the button is not pressed, and 1's when it is.

The problem comes into play when I put the OtherCog in a separate object/file.

File:test_global1.spin
CON 
    _clkmode      = xtal1 + pll16x                       
    _xinfreq      = 5_000_000
VAR
    long GlobalVar1
OBJ
   G2  : "test_global2"
 
PUB init

   G2.init

  dira[16] := 0
  
  repeat
    if(ina[16] <> 0)
      GlobalVar1 := 1
    else
     GlobalVar1 := 0

File:test_global2.spin
CON
    SerCon_Tx = 8
    SerCon_Rx = 9
VAR
    long Stack[14]
    long GlobalVar1 'Will not compile without this.
OBJ
    SerCon  : "FullDuplexSerialPlus"
 
PUB init

  cognew(OtherCog, @Stack)

PUB OtherCog

  ' Serial Console port 
  SerCon.start(SerCon_Tx, SerCon_Rx, 0, 57600)
  SerCon.str(string(" ",SerCon#CR,SerCon#LF))
  SerCon.str(string(" Serial Console started.",SerCon#CR,SerCon#LF))
  
  repeat
    SerCon.dec(GlobalVar1)    
    SerCon.str(string(" ",SerCon#CR,SerCon#LF))
    waitcnt(100*(clkfreq/100) + cnt

The value is 15 when run.

I'm new to the propeller and micro controllers in general.

what am I missing?

Comments

  • T ChapT Chap Posts: 4,223
    edited 2010-10-25 20:50
    When you start the new cog, pass it the address of the variable you want to access in the othercog as a parameter using the @

    cognew(OtherCog(@GlobalVar1), @Stack)
    PUB othercog(TheVarToAccess)
           long[TheVarToAccess]  :=  ???
    
    
    



    Then, in the othercog, access the var with long[TheVarToAccess]
  • Mike GreenMike Green Posts: 23,101
    edited 2010-10-25 20:51
    You're not missing anything. You can't directly access a variable in one object from another object. That's a deliberate feature of packaging things into objects. The GlobalVar1 in test_global1.spin is completely different from the GlobalVar1 in test_global2.spin.

    Typically this is handled by passing the address of a variable in the outer object (test_global1.spin) as a parameter to the initialization routine in the inner object (test_global2.spin) and saving the address of this variable in a variable in the inner object like:
    VAR long GlobalVarX
    OBJ inner : "test_global2"
    PUB main
       inner.start(@GlobalVarX)
    
    VAR long saveAddr
    PUB start(addr)
       saveAddr := addr
    PUB doSomething
       long[saveAddr] := 5
    
  • Coder96Coder96 Posts: 42
    edited 2010-10-26 07:10
    Thanks for the replies.

    With the amount of variables I'm working with I'll probably just move it back into one object.
  • T ChapT Chap Posts: 4,223
    edited 2010-10-26 08:09
    Coder96 wrote: »
    Thanks for the replies.

    With the amount of variables I'm working with I'll probably just move it back into one object.

    If you are suggesting that you would have to add a lot of parameters, that is not the case. You only need to point to the first variable in the line. Then access the following longs by +4. If using words or bytes, adjust the math accordingly.
    VAR  long1,long2,long3 'do not change this line or it will affect the var access in the cog
    
    cognew(OtherCog(@long1), @Stack)
    
    PUB othercog(TheVarToAccess)   |  x, y , z
           x := long[TheVarToAccess]  
           y := long[TheVarToAccess+4]  
           z := long[TheVarToAccess+8]
    
Sign In or Register to comment.