Shop OBEX P1 Docs P2 Docs Learn Events
cognew questions — Parallax Forums

cognew questions

Eric REric R Posts: 225
edited 2006-09-29 03:27 in Propeller 1
From the book:

{{Output.spin
Toggles two pins, simultaneously.}}
VAR
· long Stack[noparse][[/noparse]9]················ 'Stack space for new cog
Pub Main
· cognew(Toggle(16, 3_000_000, 10), @Stack)············· 'Toggle P16 ten times, 1/4 s each
· Toggle(17, 2_000_000, 20)···························· 'Toggle P17 twenty times, 1/6 s each
···
PUB Toggle(Pin, Delay, Count)
{{Toggle Pin, Count times with delay clock cycles in between.}}
· dira[noparse][[/noparse]Pin]~~·················· 'Set I/O pin to output direction
· repeat Count················· 'Repeat for Count iterations
··· !outa[noparse][[/noparse]Pin]················· '· Toggle I/O pin
··· waitcnt(Delay + cnt)······· '· Wait for delay cycles



As you can see, I am not far into the book but I have a question. Pub Main starts and fires a new cog then continues to run the code on cog 0. It makes sense that cognew starts cog one but in a larger code block, how will one know which cog is being started if you start multiple cogs. Can you specify a cog to start or do you just count cogs in your code? Does it really matter?
If you have 8 cogs running and the code ends on cog 3, can you start a new cog?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-29 02:44
    COGNEW is actually a function that returns the number of the cog started or -1 if no cogs were available. You can assign this value to a variable for use later with COGSTOP. It doesn't help to count cogs because you don't know which one was actually started. COGINIT allows you to specify a cog to use. It will stop whatever is running in that cog and start something new (a SPIN method call or an assembly routine).

    In assembly language, COGNEW and COGINIT are really the same instruction with an option bit specifying the difference in behavior.

    Post Edited (Mike Green) : 9/29/2006 2:47:53 AM GMT
  • Eric REric R Posts: 225
    edited 2006-09-29 03:04
    Thanks Mike.

    I guess I was reading more into it than needed as the next section talked about my question. I am sure that by the time I am finished with the book I will understand but for now I am entering code, testing but not understanding it all. Take this code for example:

    {{Output.spin}}


    VAR
    · long Stack[noparse][[/noparse]9]················ 'Stack space for new cog
    · byte Cog····················· 'Hold ID of cog in use if any
    ··
    PUB Start(Pin, Delay, Count): Success
    {{Start new toggling process in a new cog; Return true if sucessful}}


    · Stop
    · Success := (cog := cognew(Toggle(Pin, Delay, Count), @Stack) + 1)


    PUB Stop
    {{Stop toggling process, if any.}}


    · if Cog
    ··· cogstop(cog~ - 1)


    Pub Active: YesNo
    {{Return TRUE if process is active, FALSE otherwise.}}


    · YesNo := Cog > 0
    ···
    PUB Toggle(Pin, Delay, Count)
    {{Toggle Pin, Count times with delay clock cycles in between.
    · If count = 0, toggle Pin forever.}}


    · dira[noparse][[/noparse]Pin]~~·················· 'Set I/O pin to output direction
    · repeat······················· 'Repeat the following
    ··· !outa[noparse][[/noparse]Pin]················· '· Toggle I/O pin
    ··· waitcnt(Delay + cnt)······· '· Wait for delay cycles
    · while count := --Count #> -1· 'While not 0 (make min -1)
    · Cog~···
    ···


    Where do I find the cog ID from the byte or if the start was a success? Yes, I am getting tired and maybe re reading this section later will help. For now,·it looks like it was a success as the program is running. tongue.gif
  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-29 03:27
    The variable "cog" contains the cog ID which is either 0-7 or -1 (which is 255 in a byte value).
Sign In or Register to comment.