Shop OBEX P1 Docs P2 Docs Learn Events
Spin Question: accessing objects from other objects — Parallax Forums

Spin Question: accessing objects from other objects

ypapelisypapelis Posts: 99
edited 2011-09-19 06:24 in Propeller 1
I am kind of disappointed to have to ask this question, as I am getting really comfortable with Spin and thought I could figure it out, but here it goes.

I have an object which uses its own cog (Spin + assembly) and need to call it from multiple other objects. Let's call that object, OBJ1. The interface uses the typical 'set a command and let assembly do its thing by waiting until the command location becomes zero again'.

Let's also call the cog 0 code OBJ0.

From OBJ0, I can start OBJ1 and call its methods as expected. However, I want OBJ0 to start another object, OBJ2, and now OBJ2 needs to internally call OBJ1. However, OBJ2 cannot call OBJ1.Start again, as there will be two copies of OBJ1 and they will step over each other, plus I do not want to utilize two cogs. What is the proper way to do this?

The only way I have come up with is to declare the internal variables needed for OBJ1 in OBJ0. So instead of the 'cmd' variable declared in OBJ1, it is declared in OBJ0 and its address passed to OBJ1. When OBJ0 starts OBJ2, it will pass OBJ1's cmd variable address to it so now OBJ2 can include OBJ1 but not call its 'Start' function, only call the spin level code by explicitly passing the address of the command variable. I think this can work, but it is just too convoluted. Is there another way?

As I am writing this, I also realize that this is the same problem I have run into with the debug terminal, I want all nested objects to be able to print messages, yet only the object that initialized the serial terminal can print.

Any guidance would be appreciated.

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2011-09-19 06:24
    Move any variables in OBJ1 from VAR to the DAT section. This will give you one instance of the variables, which will be used by any object that calls methods in OBJ1. This is the technique that is used to make an object like FullDuplexSerial usable from multiple objects.

    Note, this assumes that all objects calling OBJ1 are running in the same cog. If you call methods in OBJ1 from different cogs you would also need to use a lock so that only one cog at a time can use the command interface.
Sign In or Register to comment.