Shop OBEX P1 Docs P2 Docs Learn Events
Looking for guidance, examples, etc on how to launch a process in another cog — Parallax Forums

Looking for guidance, examples, etc on how to launch a process in another cog

Don MDon M Posts: 1,653
edited 2011-11-10 03:57 in Propeller 1
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.

Thanks

Don

Comments

  • JonnyMacJonnyMac Posts: 9,198
    edited 2011-11-09 16:06
    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 MDon M Posts: 1,653
    edited 2011-11-09 16:49
    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.

    Routine.png
    601 x 532 - 32K
  • Don MDon M Posts: 1,653
    edited 2011-11-09 16:59
    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?

    Thanks.
  • Don MDon M Posts: 1,653
    edited 2011-11-09 17:16
    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.
  • RaymanRayman Posts: 14,854
    edited 2011-11-09 17:32
    just make sure you declare about 100 longs for your new cog.
    Forgetting to do this is a common mistake....
  • Don MDon M Posts: 1,653
    edited 2011-11-09 19:51
    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?

    Thanks.
  • kuronekokuroneko Posts: 3,623
    edited 2011-11-09 20:22
    If both main method and helper method reside in the same SPIN file then sharing a global VAR is easiest:
    VAR
      long  [COLOR="#FFA500"]shared[/COLOR]
      long  stack[32]
    
    PUB main
    
      cognew(helper, @stack{0})
    
      dira[16] := 1
      repeat
        outa[16] := [COLOR="#FFA500"]shared[/COLOR] // 100 > 50
    
    PRI helper
    
      repeat
        waitcnt(clkfreq/50 + cnt)
        [COLOR="#FFA500"]shared[/COLOR]++
    
    DAT
    

    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 MDon M Posts: 1,653
    edited 2011-11-10 03:57
    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. :)
Sign In or Register to comment.