Shop OBEX P1 Docs P2 Docs Learn Events
Passing Variables in and out of new cog — Parallax Forums

Passing Variables in and out of new cog

steprogsteprog Posts: 227
edited 2010-11-29 20:24 in Propeller 1
Hi Guys,
Simple question, but I can't seem to find a simple answer. If you call a new cog to run a method and it generates new data that the first cog needs how do you pass it back to that cog. I know how to do it when the new cog runs an assembly routine, but not when it is a simple spin program. This should be real simple, but for some reason I'm not getting it.
Help

Thanks,
Greg

Comments

  • jbalatjbalat Posts: 96
    edited 2010-11-29 15:18
    I would just declare it as a global variable that way you dont have to pass it...

    You dont want to get into a situation where you are trying to write to it with 2 cogs. If so you would need to use the lock commands..
  • StefanL38StefanL38 Posts: 2,292
    edited 2010-11-29 15:48
    easiest way in SPIN is to define PUB methods GetVar
    VAR
      long MyVar
    
    PUB GetMyVar 
      result := MyVar
    


    Let's say your object that contains PUB GetMyVar is named MySecondObject


    in your first *.SPIN-file you define
    OBJ 
      MyObj2 : "MySecondObject"
    
    VAR
      long MyVarINSpin1
    
    PUB YourCodeInSPIN1
      '...
    
      MyVarINSpin1 := MyObj2.GetMyVar
    

    best regards

    Stefan
  • steprogsteprog Posts: 227
    edited 2010-11-29 16:18
    Thanks guys, I knew this had to be simple.
    Greg
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-11-29 17:31
    steprog,

    Just to summarize, so you understand the difference between the two answers you got: jbalat's answer is correct when both Spin cogs are part of the same object. If they're not, then StefanL38's "accessor function" method is the one to use.

    Cogs and objects are different entities altogether, and it's important to grok the difference between them when programming.

    -Phil
  • steprogsteprog Posts: 227
    edited 2010-11-29 19:50
    I'm still trying to get my head around Stefan's explanation. I called parallax and they claim that if it is declared globally then I can use it just like any other global variable. I am calling another method in a new cog however. But a global variable is a global variable so I thought.
    Greg
  • kuronekokuroneko Posts: 3,623
    edited 2010-11-29 20:03
    steprog wrote: »
    I called parallax and they claim that if it is declared globally then I can use it just like any other global variable. I am calling another method in a new cog however. But a global variable is a global variable so I thought.

    Running a method in a new cog means the method is located in the current SPIN file (doesn't work any other way). So let the method access the global variable.
    VAR
      long  a, b, stack[16]
      
    PUB null
    
      cognew(secondary, @stack[0])
      repeat
        a := b
    
    PRI secondary
    
      repeat
        b++
    
    DAT
    
    The only way this doesn't work is when the method is hidden in another object. Then it simply can't see the global variable defined in it's parent file but you could still pass an address reference which would allow the method to access it (e.g. long[address] := value).

    Maybe you could tell us where your 2nd method is located (same or different object)?
  • steprogsteprog Posts: 227
    edited 2010-11-29 20:24
    Right now I'm having another problem with simply getting a cog to start. It might be related, so let me hold off until I work that out.
    Thanks,
    Greg
Sign In or Register to comment.