Spin Question Regarding the Same Obj in Multiple Modules
Martin_H
Posts: 4,051
I'm having a bit of a hard time wrapping my head around Spin's Obj program section. Is the reference in the Obj section like an C# using statement, or more like a static instance declaration within that module's scope?
My usage case is that I have a Spin project with two modules: "Inverse Kinematic Demo.spin" and "Arm.spin". Both modules include some of the same objects, such as: "F32.spin", and "FloatString.spin". I've found that unless I call F32's start method in both modules, I can't use floating point function calls.
The observed behavior implies to me that the Obj section is like a static instance declaration, but does that mean that each instance consumes a cog after the start call?
Is this expected or am I making a mistake some how?
If I am consuming multiple cogs is there a way to avoid it?
If this is expected, can I include the same object more than once in the Obj section and have unique state in each included instance?
My usage case is that I have a Spin project with two modules: "Inverse Kinematic Demo.spin" and "Arm.spin". Both modules include some of the same objects, such as: "F32.spin", and "FloatString.spin". I've found that unless I call F32's start method in both modules, I can't use floating point function calls.
The observed behavior implies to me that the Obj section is like a static instance declaration, but does that mean that each instance consumes a cog after the start call?
Is this expected or am I making a mistake some how?
If I am consuming multiple cogs is there a way to avoid it?
If this is expected, can I include the same object more than once in the Obj section and have unique state in each included instance?
Comments
The question of what cogs are used is dependent on the code written. The object mechanism has nothing to do with cog usage. By convention, if a new cog is needed to make the particular object work, that cog is started in a "start" method in the object.
There are a few objects in the Object Exchange that are written so they can be included multiple times in a program, usually in different sub-objects. One in particular is a serial I/O driver used for debugging purposes. Have a look at that for an example.
VAR
long f32_Cmd
byte cog
to this:
DAT
f32_Cmd long 0
cog long 0 'cog flag/id
and removed the second call to start and it worked. There might be other ramifications to making that change, but I will keep it for now and let the author know of this behavior.