Initiating new cogs - Newbie question!
robotics
Posts: 90
Hi,
I understand that the cognew and coginit commands many be used to initiate code to run in new cogs as long as stack space is also specified in the CON block.
However, I have seen examples such as in the Propeller Tool Help->Propeller Help-->Spin Programming Tutorial-->Spin Exercise 7-1:Blinker2.spin example (below) that shows cogs being initiated without using the cognew or coginit commands and without the use of specifying stack space for each cog. Please explain. In advance, thank you!
{{ Blinker2.spin }}
CON
MAXLEDS = 6 'Number of LED objects to use
OBJ
LED[noparse][[/noparse]6] : "Output"
PUB Main
{Toggle pins at different rates, simultaneously}
dira[noparse][[/noparse]16..23]~~ 'Set pins to outputs
LED[noparse][[/noparse]NextObject].Start(16, 3_000_000, 0) 'Blink LEDs
LED[noparse][[/noparse]NextObject].Start(17, 2_000_000, 0)
LED[noparse][[/noparse]NextObject].Start(18, 600_000, 300)
LED[noparse][[/noparse]NextObject].Start(19, 6_000_000, 40)
LED[noparse][[/noparse]NextObject].Start(20, 350_000, 300)
LED[noparse][[/noparse]NextObject].Start(21, 1_250_000, 250)
LED[noparse][[/noparse]NextObject].Start(22, 750_000, 200) '<-Postponed
LED[noparse][[/noparse]NextObject].Start(23, 400_000, 160) '<-Postponed
LED[noparse][[/noparse]0].Start(20, 12_000_000, 0) 'Restart object 0
repeat 'Loop endlessly
PUB NextObject : Index
{Scan LED objects and return index of next available LED object.
Scanning continues until one is available.}
repeat
repeat Index from 0 to MAXLEDS-1
if not LED[noparse][[/noparse]Index].Active
quit
while Index == MAXLEDS
I understand that the cognew and coginit commands many be used to initiate code to run in new cogs as long as stack space is also specified in the CON block.
However, I have seen examples such as in the Propeller Tool Help->Propeller Help-->Spin Programming Tutorial-->Spin Exercise 7-1:Blinker2.spin example (below) that shows cogs being initiated without using the cognew or coginit commands and without the use of specifying stack space for each cog. Please explain. In advance, thank you!
{{ Blinker2.spin }}
CON
MAXLEDS = 6 'Number of LED objects to use
OBJ
LED[noparse][[/noparse]6] : "Output"
PUB Main
{Toggle pins at different rates, simultaneously}
dira[noparse][[/noparse]16..23]~~ 'Set pins to outputs
LED[noparse][[/noparse]NextObject].Start(16, 3_000_000, 0) 'Blink LEDs
LED[noparse][[/noparse]NextObject].Start(17, 2_000_000, 0)
LED[noparse][[/noparse]NextObject].Start(18, 600_000, 300)
LED[noparse][[/noparse]NextObject].Start(19, 6_000_000, 40)
LED[noparse][[/noparse]NextObject].Start(20, 350_000, 300)
LED[noparse][[/noparse]NextObject].Start(21, 1_250_000, 250)
LED[noparse][[/noparse]NextObject].Start(22, 750_000, 200) '<-Postponed
LED[noparse][[/noparse]NextObject].Start(23, 400_000, 160) '<-Postponed
LED[noparse][[/noparse]0].Start(20, 12_000_000, 0) 'Restart object 0
repeat 'Loop endlessly
PUB NextObject : Index
{Scan LED objects and return index of next available LED object.
Scanning continues until one is available.}
repeat
repeat Index from 0 to MAXLEDS-1
if not LED[noparse][[/noparse]Index].Active
quit
while Index == MAXLEDS
Comments
{{ 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 blinking process in new cog; return TRUE if successful}}
· 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…
· 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~································ 'Clear Cog ID variable
C:\Program Files\Parallax Inc\Propeller Tool v1.2.7 (R2)\Examples\Help\Spin Tutorial\Exercise 07-1
...are two files...
Blinker2.spin
Output.spin