Propeller Manual

Table Of Contents
2: Spin Language Reference – COGNEW
If Method is determined to be code that is truly important to run in another cog, rather than
write code like the example above, SomeObject should instead be rewritten similar to the
example below.
VAR
long StackSpace[8] 'Stack space for new cog
byte CogID 'Stores the ID of new cog
PUB Start
Stop 'Prevent multiple starts
CogID := cognew(Method, @StackSpace) 'Launch method in another cog
PUB Stop
if CogID > -1
cogstop(CogID) 'Stop previously launched cog
PRI Method
<some code here>
The sample above includes two public interface methods, Start and Stop, which an outside
object can use to properly launch the object’s code into another cog. The important principle
is that the object itself is providing this capability, and in doing so, is managing the stack
memory required for proper operation. Also note that
Method was changed to a private (PRI)
method to discourage direct calling from the outside.
Propeller Assembly Code (Syntax 2) )
To run Propeller Assembly code in another cog, the
COGNEW command needs the address of
the assembly routine and a value that can optionally be used by the assembly routine. For
example:
PUB Main
cognew(@Toggle, 0) 'Launch Toggle code
DAT
org 0 'Reset assembly pointer
Toggle rdlong Delay, #0 'Get clock frequency
shr Delay, #2 'Divide by 4
mov Time, cnt 'Get current time
add Time, Delay 'Adjust by 1/4 second
mov dira, #1 'set pin 0 to output
Propeller Manual v1.1 · Page 81