PDA

View Full Version : Indirectly accessing spin OBJ



fake_name
03-20-2012, 10:11 AM
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?)

Cluso99
03-20-2012, 10:27 AM
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.

kuroneko
03-20-2012, 11:15 AM
Is it something like this (http://obex.parallax.com/objects/697) what you're after?

Javalin
03-20-2012, 11:17 AM
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_name
03-21-2012, 02:18 AM
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 (http://forums.parallax.com/member.php?52678-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 Green
03-21-2012, 02:49 AM
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 Hein
03-21-2012, 03:35 AM
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.