Shop OBEX P1 Docs P2 Docs Learn Events
Accessing var in another cog? — Parallax Forums

Accessing var in another cog?

StephenStephen Posts: 53
edited 2008-02-24 16:40 in Propeller 1
I've tried searching for this and I just can't find anything... Can code in one cog read a global variable defined in another object/cog? Sort of like defining an extern?

If not how can I define and write to a variable that is global across all cogs? Can this only be done via object methods?

Comments

  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-24 06:07
    Yes, you can just use the variables that have been declared in the VAR blocks for this. Note that if you have multiple copies of an object you get a copy of the VAR for each object while there is only one DAT section shared by all objects of that type.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-02-24 06:09
    Yes. Code running in one cog can read a global variable defined in another object, but not directly. There are two separate issues here:
    1) Accessing variables from two or more cogs
    2) Accessing variables in another object

    These are two completely different issues.

    You can have several cogs that are executing the same or different methods in a single object program (without any references to other objects). The cogs simply use the variables, but have to be careful that two or more cogs are not changing the variables at the same time. If there's a complex interaction involving several variables, you may have to use LOCKxxx calls to prevent two cogs from accessing the same resources at the same time. See the manual regarding LOCKNEW, LOCKSET, LOCKCLR, and LOCKRET.

    When you have multiple objects in a program, none of the objects can directly reference variables in the other objects regardless of how many cogs are used to run the parts of the objects. You can have a complex program with many objects that all use only a single cog to run the whole thing. You can communicate between objects in two ways:

    1) Use Get and Set methods in the sub-objects to reference and change certain variables in the sub-object from the next higher object

    2) Use Address methods to return the address of one or more variables (possibly as the address of the start of a block of variables), then use BYTE, WORD, and LONG to reference or change the variables given their address.
  • StephenStephen Posts: 53
    edited 2008-02-24 06:36
    I just want a cog running a temp sensor to continuously update a var with the latest result, say every second. Then another cog can read that variable and act on it, display it, etc. It's not critical info, and it's not being written by any other cog other than the host object, so there shouldn't be ant read-modify-write issue.

    There's no mention of the GET method in the prop manual... Where can I find info on it?
  • StephenStephen Posts: 53
    edited 2008-02-24 06:36
    BTW, thanks for the quick responses! This is like the ultimate tech support!
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-24 07:25
    Stephen said...
    I just want a cog running a temp sensor to continuously update a var with the latest result, say every second. Then another cog can read that variable and act on it, display it, etc. It's not critical info, and it's not being written by any other cog other than the host object, so there shouldn't be ant read-modify-write issue.

    There's no mention of the GET method in the prop manual... Where can I find info on it?
    There isn't one. You have to write it yourself smile.gif

    If your code is all in one object than something like this will work.
    VAR
      long temperature 'this could be a word or byte depending on how large your variable is
      long stack[noparse][[/noparse]20] 'this is stack space for the temp sensor cog
    PUB main
      'start the temp sensor code running
      cognew(tempCode,@stack)
      
      'loop forever
      repeat
        'read the temperature that has been set by another cog
        doSomethingWithTemp(temperature)
    
    PUB tempCode|tempJustRead
      'get your data
      '....
      'save the temperature
      temperature:=tempJustRead
    
    PUB doSomethingWithTemp(temp)
      'do something here
    


    This should work fine but if the tempCode is in another object than you will need to change this slightly so that you give tempCode a pointer to temperature like this
    PUB tempCode(tempPtr)|tempJustRead
      'get your data
      '....
      long[noparse][[/noparse]tempPtr]:=tempJustRead
    



    You would also have to use the '@' operator to get the address of the temperature variable.
  • bdickensbdickens Posts: 110
    edited 2008-02-24 16:11
    It would be really nice for someone who knows what they are doing (which immediately exludes me) to put together a lab on this topic. I had the same question, but figured I would wait until I was better with SPIN before I tackled deeper understanding. My current project has exactly the same issue stephens does. One more addon the education labs would seem to be a big hit. Thanks
  • StephenStephen Posts: 53
    edited 2008-02-24 16:40
    Thanks Steven for the description. I am already doing that myself. I just thought maybe there was some other method that was provided in the spin language that I should be using.

    bdickens...

    I've provided my code here if you want to look at it. It's working pretty good now, using 4 cogs:
    - main
    - ezLCD-002 display
    - DS1620 temp sensor
    - Servo
Sign In or Register to comment.