Shop OBEX P1 Docs P2 Docs Learn Events
Spin Question Regarding the Same Obj in Multiple Modules — Parallax Forums

Spin Question Regarding the Same Obj in Multiple Modules

Martin_HMartin_H Posts: 4,051
edited 2011-03-20 16:42 in Propeller 1
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?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-20 12:29
    It depends on how the objects are written. Any variables declared in the VAR sections of the object are instance variables. Each declaration of that object, whether it's in the same or different objects produces a separate instance and a different copy of the instance variables. The DAT sections are global and only one instance of any variables (or assembly code) is created no matter how many times the object is declared. Effectively, you have a static instance declaration with the code and DAT sections considered read-only and only one instance of these are created no matter how they're actually used.

    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.
  • Martin_HMartin_H Posts: 4,051
    edited 2011-03-20 12:41
    Thanks Mike. Based on that and reading the F32.spin code I think it consumes a cog per instance created. That would also explain my issues using the serial terminal in more than one module at a time. I'll read through that SerialMirror to see how it works next.
  • Martin_HMartin_H Posts: 4,051
    edited 2011-03-20 13:04
    After reading SerialMirror it uses a dat instead of a var to track the cog. So in F32.spin I changed this:

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-20 13:37
    Do be careful about using SerialMirror or the modified F32 from more than one cog. If you do, you'll probably need to use locks.
  • Martin_HMartin_H Posts: 4,051
    edited 2011-03-20 16:42
    Mike, understood, I've made the object no longer thread safe by using the equivalent of a C++/C# static variable. I'm only invoking it from one cog and only to add radian values together.
Sign In or Register to comment.