Shop OBEX P1 Docs P2 Docs Learn Events
Question about objects and sharing — Parallax Forums

Question about objects and sharing

JasonDorieJasonDorie Posts: 1,930
edited 2008-10-08 06:56 in Propeller 1
I have a bunch of code that I'm trying to keep clean, by using small objects, and a few of them need to use the Float32 object.· That object starts a cog running the PASM portion.

My question is: can multiple objects share a single 'instance' of the Float32 object?· Is there a way to 'pass it around' in the code, like a C++ class?

I have something like this:

-·ControlLogic (uses Float32)
·· |
·· +--PID X· (also uses Float32)
·· |
·· +--PID Y· (also uses Float32)


Since the ControlLogic object is calling the functions in the 2 PID objects, I won't have concurrency issues.· Can I use the same instance of the Float32 object?

According to the manual:
"Symbols defined in an object are global to that object, but are not available outside that object."· It doesn't say anything about passing the objects themselves around.

Anyone done this, or is this unsupported?· I have a few ideas on how to accomplish this by hacking a new start function for Float32, but I was wondering if there was a more 'correct', Spin-friendly way.

Thanks,
Jason
·

Comments

  • Erik FriesenErik Friesen Posts: 1,071
    edited 2008-10-07 11:31
    There is no sharing in Spin. Be advised however that adding a float32 object to many separate objects is doable. The compiler only uses one copy of an object so no code space is wasted.
  • JasonDorieJasonDorie Posts: 1,930
    edited 2008-10-07 18:28
    I did know that the object code is only created once, but the Float32 object starts a cog internally to run the PASM computation engine, and the cog index, command, and return values are stored in VARs which are per-object, meaning that each one HAS to start a cog to function properly. (assuming I've got that right - correct me if not) I'd rather not use 3 cogs for a single 'thread' of computation.

    I'll try altering the code to 'extract' out the ASM engine and see if I can get the three Float32 objects to use a single cog. It would be useful if the SPIN compiler would allow 'extern' on objects, or something along those lines so that existing objects could be more easily shared like this. I would guess this to be more of a syntax limitation than anything else. So far this is about the only thing about the Prop/Spin stuff that bugs me. That and Ctrl + Up/Down changing the font size instead of scrolling. [noparse]:)[/noparse]

    Thanks
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-10-07 20:29
    Hello Jason,

    to me it's not clear how your code is organized

    do you mean there is the main-spin-file
    and in the main-spinfile there are defined
    - an object controll-logic
    - an object PID X
    - an object PID Y

    and each of this three objects has codelines

    OBJ
      Float : "Float32"
    
    



    is this correct ?

    If you change the definitions of the variable

    VAR
    
      long  cog
      long  command, cmdReturn
      
    
    



    to DAT-RAM-space you can use them from different objects
    this technique is show in the serialmirror object from the obex
    obex.parallax.com/objects/189/

    best regards

    Stefan
  • KeezinatorKeezinator Posts: 21
    edited 2008-10-07 21:09
    I think what you are looking for is done in the 'DynamicMath' library in the OBEX.
  • JasonDorieJasonDorie Posts: 1,930
    edited 2008-10-08 05:37
    So the DAT section BEFORE the PUB/PRI routines is comparable to 'static' globals in C/C++, whereas the DAT section following PASM code is local to a given cog? Do I have that right?

    Stefan, to answer your question a little more specifically it's like this:

    MainControlLogic
    OBJ:
    F : Float32
    P : PIDRoutine

    PIDRoutine
    OBJ:
    F : Float32

    So I have a Float32 object in use by 'MainControlLogic', and one in each of the two 'PIDRoutine' objects, but 'MainControlLogic' calls the functions in 'PIDRoutine', so it's unnecessary to have 3 cogs running the Float32 processor, as one would be just as efficient in this case. For me at the moment it's ok because I have enough cogs around that it's not a big problem. On the other hand, I keep having to move where I define 'TV_Text' for debugging, because I can only have one instance of it on the designated pins, but I want debug prints in many places in my code. It would be nice to share that object too.

    If the DAT section of SPIN code works the way I now think it does, this should be reasonably easy to set up by just creating a different 'Float32' object that puts the VARs in a DAT section instead, and then don't call Float32.start for the 2nd and 3rd instances of it. Does that sound like I've got it right?

    Thanks guys,
    Jason
  • grasshoppergrasshopper Posts: 438
    edited 2008-10-08 05:55
    A bit off the path here yet still pertaining to the title. Can you send an array to another object.
    For example I want to send a global varable array x[noparse][[/noparse] 25 ] to an object for further analysis from my main object. Can this be done?
  • mparkmpark Posts: 1,305
    edited 2008-10-08 06:42
    JasonDorie said...
    So the DAT section BEFORE the PUB/PRI routines is comparable to 'static' globals in C/C++, whereas the DAT section following PASM code is local to a given cog? Do I have that right?

    Well, there's only one DAT section in a given object, even if it's split up in the source file. It can contain "static globals" and PASM code. When you start up a cog, you load it with 496 longs from the DAT section. Typically that's PASM code followed by some data (and then whatever follows it in hub memory). So as you say, whatever follows the PASM code becomes local to a given cog, in the sense that the cog works on its copy of that data. But in another sense, whatever follows the PASM code is still in hub memory, outside the cog. Hope that makes sense.

    I can't speak to your proposed modification to float32.
  • mparkmpark Posts: 1,305
    edited 2008-10-08 06:56
    grasshopper said...
    A bit off the path here yet still pertaining to the title. Can you send an array to another object.
    For example I want to send a global varable array x[noparse][[/noparse] 25 ] to an object for further analysis from my main object. Can this be done?

    You can't send the array directly, but you can send the address of the array. Use the @ operator to compute the address, then in your analysis method, use the "long[noparse]/noparse[noparse]/noparse" syntax to access your array (or "word[noparse]/noparse[noparse]/noparse" or "byte[noparse]/noparse[noparse]/noparse"). Here's an example:

    var
      long array[noparse][[/noparse]10]
      
    pub main | i
      text.start(24)              ' start tv_text
      repeat i from 0 to 9        ' initialize array
        array[noparse][[/noparse] i ] := i
      text.dec( ComputeSum( @array, 4 ) ) ' sending the address of array to ComputeSum
      text.out( 13 ) ' CR
    
    ' This could be in another object
    pub ComputeSum( pLongs, n ) ' sums the n longs pointed to by pLongs
      repeat while --n => 0
        result += long[noparse][[/noparse]pLongs][noparse][[/noparse]n]
    
    
Sign In or Register to comment.