Shop OBEX P1 Docs P2 Docs Learn Events
Indirectly accessing spin OBJ — Parallax Forums

Indirectly accessing spin OBJ

fake_namefake_name Posts: 3
edited 2012-03-20 20:35 in Propeller 1
Is there any way to have an indirect reference to a spin OBJ?

Basically, I have a function that can talk to a number of different possible communication interfaces (as long as the interface presents a consistent API, anyways).

Is there any way I can pass the function a pointer to an arbitrary OBJ instance?

Right now, the only way I can think of to do this is with a great big IF THEN statement, which would have to be updated every time I add a new communication interface, and would have to be parsed every time I want to call the OBJ.

I'd much rather just pass the whole function a pointer to the OBJ, and let it just assume that the OBJ has a read() and write() function.

Thanks.

(Hmmm. Does this basically boil down to me wanting to do object-oriented programming in spin?)

Comments

  • Cluso99Cluso99 Posts: 18,069
    edited 2012-03-20 03:27
    Are you wanting to have multiple objects compiled into a binary and change them on the fly, or are you wanting to change the object at compile time?

    One of the ways is to use a predefined hub buffer location and pass the base location when the object is started. That way, the object knows where its read and write buffers are located.

    I guess we need a bit more info. There are probably many ways although some are not elegant.
  • kuronekokuroneko Posts: 3,623
    edited 2012-03-20 04:15
    Is it something like this what you're after?
  • JavalinJavalin Posts: 892
    edited 2012-03-20 04:17
    Yes - look at the fullduplexserial object or MikeG's web server code. There are a few examples around, but essentially a pre-defined section of hub ram is used to store parameters and the cog queries it and sets results for return as required.

    also look at : http://obex.parallax.com/objects/268/ for an simple example.

    James
  • fake_namefake_name Posts: 3
    edited 2012-03-20 19:18
    To Clarify:

    Say I have:
    OBJ
    
    
            SER         :     "FullDuplexSerial"  
            ETH          :    "EthernetInterface"
    

    Now, assume that both OBJs have tx() and rx() functions:

    I want to be able to do something like:
    PUB
        repeat
            communicateIF(SER)
            communicateIF(ETH)
    
            ~ extend for an arbitrary number of interfaces ~
    
    PRI communicateIF(objPointer) | temp1
        
        temp1 := objPointer.rx()
    
        ~ do stuff ~
        
        objPointer.tx(temp1)
    
        return
    
    
    

    Note that the tx() and rx() functions should be executed in the calling function's COG. Using another COG to liaise between two already running cogs seems a bit silly, particularly as I can guarantee there will not be any collisions (there is only one function caller COG).

    @kuroneko - That looks interesting, but it seems to be poking around in the raw spin bytecodes? I'm really not sure how it actually works.
    Is the spin bytecode documented anywhere? Preferably in small words that I can understand?
  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-20 19:49
    What kuroneko mentioned is something that could be used to implement what you want. You'd have to make some changes in the objects you're referencing ... it's not as simple as you would like, but it could be done. Essentially you have to "fool" the Spin interpreter into treating one object as if it were another. You will need to understand how the Spin interpreter handles objects and method addresses.
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-03-20 20:35
    A method pointer can certainly do the type of thing your suggesting. It can call a method in any object as long as they use the same calling parameters. As you said, the MethodPointer object has to do some bytecode manipulation to be able to do this because Spin does provide direct access to the base registers in the interpreter.
Sign In or Register to comment.