Passing Variables in and out of new cog
steprog
Posts: 227
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
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
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..
Let's say your object that contains PUB GetMyVar is named MySecondObject
in your first *.SPIN-file you define
best regards
Stefan
Greg
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
Greg
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.
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)?
Thanks,
Greg