View Full Version : Looking for guidance, examples, etc on how to launch a process in another cog

Don M
11-09-2011, 10:37 PM
I have a main loop running a menu and I want to start other routines from the menu and keep them going but still need to have control of the main menu loop and get some data from the routine running external of the main loop. I am guessing I need to run these other routines in another cog?

If anyone can suggest some good examples or reading material on this type of thing it would be greatly appreciated.



11-09-2011, 11:06 PM
The cognew method will be your friend. You can launch a Spin method -- usually constructed as an infinite loop -- into another cog and then share data and methods from your top object with that "background" cog. It takes a little getting used to but once you grasp the concept it can be very powerful.

Don M
11-09-2011, 11:49 PM
Thanks Jon. If there is a tutorial on doing this it would be great to read and learn from.

I have attached a visual of what I'm trying to understand.


Don M
11-09-2011, 11:59 PM
OK. I used cognew and my routine is running ok in its own cog while I am still able to run the menu system in the main routine. Now I need to learn how to pass data and variables back and forth.

What do I need to learn to do this? Any references?


Don M
11-10-2011, 12:16 AM
I'm reading chapter 5 "Methods & Cogs Lab" in the Propeller Education manual... this may have what I need to learn. Comments welcome from the forum.

11-10-2011, 12:32 AM
just make sure you declare about 100 longs for your new cog.
Forgetting to do this is a common mistake....

Don M
11-10-2011, 02:51 AM
Ok I'm stuck here- I have a method running in its own cog that I launched with cognew. I want to inquire from my main method a variable (value) from the cognew launched method. I haven't found information yet for the procedure for doing so. Can someone show me a framework of how this type of thing works?


11-10-2011, 03:22 AM
If both main method and helper method reside in the same SPIN file then sharing a global VAR is easiest:

long shared
long stack[32]

PUB main

cognew(helper, @stack{0})

dira[16] := 1
outa[16] := shared // 100 > 50

PRI helper

waitcnt(clkfreq/50 + cnt)


If the helper method lives in a different object (different SPIN file) then you have to communicate the address of said variable. So either the main program tells the child object what the address is (e.g. child.setup(@shared)) in which case the child object accesses shared through long[address]++ or it asks the child object what the address is (e.g. address := child.getAddr) and then uses the returned address like value := long[address].

Don M
11-10-2011, 10:57 AM
Kuroneko thanks that helps I think I understand now. The main and helper are both in same spin file. I was making it harder as if they were in seperate spin files. That will be a lesson for another day.

Edit: Got it to work! Thanks. :)