Shop OBEX P1 Docs P2 Docs Learn Events
Reference to an object — Parallax Forums

Reference to an object

RicjeRicje Posts: 1
edited 2009-06-27 21:32 in Propeller 1
Hi,

Comming from a vast C++ background, I'm pretty much certain that I'm not looking at this problem the right way.

I'm looking for a way to pass a reference to a SPIN object that I created in one object to another object that is running on the same COG. Basically, I'd like to have a global object that I can use from any object running in a given COG. I don't want to access it from different COG.

I've tried the obvious @MyObject to get the adress of my object and pass it to other object but the compiler does't let me use the adress operator on an object.

Surely there is a way to do what I'm trying to do, but how?

Thanks

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-27 15:11
    There's no way to do what you want in Spin without "cheating". There are internal tables in the Spin interpretive code which contain the addresses of the objects declared within an object and the addresses of the method code for the methods used in the object, but there's no notation to get at these tables. Once you get the address of one of these object tables, there's no way in Spin to use the addresses to do anything useful. You can define a "dummy" object and stuff its internal table with pointers from another internal table, but I wouldn't recommend that for anything but experimentation.

    One way to functionally get what you want is for your "global" object to have all its variables in a DAT section instead of a VAR section. This way the variables are shared by all instances of the object. The method code is already shared and is read-only. You can incorporate this object in any other objects in your program and there will be only one instance of its variables and code. There will be a separate internal table for each declared instance, but this table is small.

    Here's one example of this technique: obex.parallax.com/objects/189/
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2009-06-27 20:25
    Objects in spin are quite well insulated, except from the top down. There is no such a thing as callbacks. Although there are times when calls to an object are very nice, this restriction can be used to your advantage. I think it can create cleaner looking code.

    For example, the code I am working on right now in C is for the Pic24. It has two I2C ports on it, and to access them, I had to create two *.c objects, but all functions had to be named different between them. In spin it is as easy as creating two sources of the object, and calling the functions remains very similar.
  • heaterheater Posts: 3,370
    edited 2009-06-27 21:10
    Erik. Why create multiple copies of the I2C driver code? Why not have multiple copies of the data structures required for each driver and parameterize a single version of the code? As a bonus it saves on code space.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2009-06-27 21:15
    I am calling one set from an interrupt, and the other is not, and I they are somewhat different in the way they are called. Also there is enough setup that is different between the two modules that in the end the code size would probably be the same.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-06-27 21:20
    @erik:
    You are right when you say it creates cleaner looking code, but your example is more a lack of knowledge than a lack of C. If your functions are the same, but have to operate on different data/hardware-adresses you'd simply put all the differences into a structure. So, you'd have one structure for I2C port one and one structure for I2C port two. When calling the functions you simply pass the adress of the structure it should currently use.
    Using such an indirection is basically what all the object oriented languages (C++ / SPIN) do.
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2009-06-27 21:32
    I was a little afraid I'd get hammered there smile.gif

    The code would have to be set up quite a bit different than the excerpt shown below then. All the parameters would have to be passed as a pointer to the structure, and the function would have to do a bit of decoding each time, not?

    You are correct about my lack of knowledge of C, however C (and spin) is pretty powerful, even in the hands of dummies.

    char i2c_read(void)
        {
    
              int i = 0;
              char data = 0;
    
              //set I2C module to receive
              I2C1CONbits.RCEN = 1;
    
              //if no response, break
              while (!I2C1STATbits.RBF)
              {
              i ++;
              if (i > 2000) break;
              }
    
              //get data from I2CRCV register
              data = I2C1RCV;
    
              //return data
              return data;
    
        }
    
Sign In or Register to comment.