Cogs and Variables Question
xanadu
Posts: 3,347
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:
Thanks
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
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
Edit: Phil beat me again.