Shop OBEX P1 Docs P2 Docs Learn Events
Variables on other cogs? — Parallax Forums

Variables on other cogs?

rapscaLLionrapscaLLion Posts: 75
edited 2007-04-23 05:42 in Propeller 1
Hi there,

Suppose I have "Main Program" for example, and it calls "Sub Program" as an object during starttup.
Sub Program immediately initialized it's method "Method" in a new cog. Is there any way from "Method"
running on the other cog to access read and write variables in the Main Programand vice versa?
I have tried using the "@" symbol in various ways, no dice.

If not, any good way to pass these values back and forth with ease?

Thanks!

Comments

  • MacGeek117MacGeek117 Posts: 747
    edited 2007-04-23 02:38
    Just use global variables like this:
    VAR
    byte MyGlobalVar

    All cogs can access global variables.
    RoboGeek

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "I reject your reality and subsitute my own!"

    Adam Savage, Mythbusters
    www.parallax.com
    www.goldmine-elec.com
    www.expresspcb.com
    www.startrek.com
    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-23 02:53
    Try not to confuse subroutines, methods, and objects with programs running on separate processors. They are different things. You can have all kinds of combinations of programs, sub-programs, method calls, local and global variables, all running in a single cog. You can have a hugely complex program that just uses one cog. On the other hand, you can have just one main object with one main method (subroutine) and one other subroutine and have 6 or 7 copies of that subroutine running simultaneously on 6 or 7 cogs with all the cogs interacting through various global variables. The concepts are different.

    As RoboGeek mentioned, all subroutines (methods) in an object have access to the other subroutines, DAT labels, and VAR variables (and CON constants) in the object. One object has access to the named constants and public methods in any subsidiary object defined in its OBJ section(s).
  • rapscaLLionrapscaLLion Posts: 75
    edited 2007-04-23 03:37
    Fine, but lets say I have the main program A, which is in one source file, and references object b in the OBJ section. OBJ b then starts it's main method on a new cog. I realize that the new cog has access to the globals, methods, subs, etc from object b, but how do I access (read/write) to globals in main program A from that object b subsidery cog? Am I being clear enough? Or is that too confusing?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-23 04:41
    It's a fine question, but it's independent of whether you're using an extra cog or not. The same issue would come up if you never used another cog, but some routine in object b needed to access data in the main program's global variables. Truthfully, having a cog still running in object b makes this more imperative, but doesn't change the basic issues.

    Basically, you pass the address of any shared globals in the main program to a method in object b. If these are part of the initial environment for the separate cog, these addresses can be passed as a parameter to the start routine for the cog which calls COGNEW with a call to some Spin method with some parameters, one or more of which are these addresses. This might look like:
    ' Object b
    ' Call from main program with: start(size,@buffer)
    ' Where there are declarations:
    ' CON size = 50
    ' VAR byte buffer[noparse][[/noparse]size]
    ' Note: This doesn't deal with the issue of sharing the buffer
    '       It assumes that there's some kind of arrangement for this.
    
    VAR long stack[noparse][[/noparse]30]
    
    PUB start( bufferSize, bufferAddr) : cog
       cog := COGNEW(firstMethod(bufferSize,bufferAddr),@stack) + 1
    
    PRI firstMethod(bufferSize, bufferAddr)
       bytefill(bufferAddr, 0, bufferSize)  ' Clear the buffer initially to zeroes
    
    
  • rapscaLLionrapscaLLion Posts: 75
    edited 2007-04-23 05:42
    ok thanks
Sign In or Register to comment.