Shop OBEX P1 Docs P2 Docs Learn Events
Cogs and Variables Question — Parallax Forums

Cogs and Variables Question

xanaduxanadu Posts: 3,347
edited 2012-01-23 16:06 in Propeller 1
I'm working on the PE kit and have a question about how to 'pass' a variable to another cog. In my program I'm using an SPST switch pulled low to pin 5. The switch assigns a number to a variable 'var1'. Then another cog is launched referencing the variable, the new cog outputs the 'var1' value to the pst.

My question is, since this doesn't work is it because I'm starting the new cog before the variable is assigned? If that is the case, how would I start the cog properly without it repeating with the switch sensor code?

Or, am I supposed to be reading the 'var1' from memory inside the second cog?

As it stands, the main program is outputting the 'var1' however the cog I start is outputting "0" over pst. Here is my code:
CON
   
  _clkmode = xtal1 + pll16x                  
  _xinfreq = 5_000_000

VAR

 long stack[30]

OBJ
   
  pst : "Parallax Serial Terminal"           
                                             
   
PUB Main | var1

dira[5] := 0                                       'Pin 5 input

 pst.Start(115_200)                               
   
   cognew(output(var1), @stack[0])                 'Start new cog with 'var1' variable
   
   repeat                                          'Assigning the variable with toggle switch on pin 5 pulled low
     
     if ina[5] == 1
      var1 := 10
      
     if ina[5] == 0
      var1 := 20
      
    pst.Dec(var1)                                  'Debug var1 on this cog
    waitcnt(clkfreq*1 + cnt)                       
      
      
                   
PUB output(var1)                                   'Output var1 via pst

 repeat
   pst.Dec(var1)
   waitcnt(clkfreq + cnt)


Thanks :)

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-01-23 15:59
    There are a couple ways to do what you want:

    1. If you define var1 in the VAR section, both cogs will "know" about it, so you don't have to do anything special.

    2. Instead of starting the output cog with var1, start it with @var1, which is the address of var1. Then you can output its value with pst.dec(long[var1_addr]).

    The reason that this is necessary is that parameters in Spin are passed by value, not by reference. The "@" operator creates the necessary reference through which you can access the current value, and not just the value that output was started with.

    -Phil
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-23 16:00
    The easy way around this is to make "var1" a global variable (in the VAR section). As your code is not you have two local variables with the same name. This problem doesn't have anything to do with cogs it's just a mater or local vs global variables.

    Edit: Phil beat me again.
  • xanaduxanadu Posts: 3,347
    edited 2012-01-23 16:06
    Thank you! Problem Solved :) That answers more questions I had too. Back to the books, thanks again.
Sign In or Register to comment.