Shop OBEX P1 Docs P2 Docs Learn Events
A few questions about SPIN functions — Parallax Forums

A few questions about SPIN functions

DizzyDizzy Posts: 9
edited 2008-04-24 20:37 in Propeller 1
Hello,

I have a project where I need a few cogs to be polling something independantly. My question centers around the fullduplexserial object and a lot of the other objects included in general. I thought originally that an object that starts a new cog would have all that methods from that object loaded into that cog. It seems more like when you declare that you're using an object in the main program it loads the functions into the memory of the main cog. The reason this troubles me is because I need the serial cog to be constantly polling the serial lines for input but when I put an endless loop in one of the functions of the body for the serial object it locks up the main program. I noticed within the object that when you call fullduplexserial.start() it actually starts a cog pointing to an area of assembly that looks as though it runs its own endless loop. My question is can I get that assembly that starts in a cog to call a function in the object body? How can I make the cog that runs for the serial poll using the function I write as well as go about its usual business?

Also:

I read "global" variables everywhere. Is there anyway for me to declare a variable in an Object and make it truly global so that any other cog or object can access it without passing it?

thanks,

Diz

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-30 23:21
    1) Global variables are global within the object only. They have no existence outside of the object. The only true globals are constants and methods (procedures / functions).

    2) An object and a cog are two separate things. You can have objects that all run in a single cog and you can have a single object that forks off several processors to do the same or different things. Most typically, an object will start a cog to handle the part of its function that runs in parallel with the "main" processor and the object supplies a set of interface methods that run on the processor of the caller and communicate with the processor (cog) that's running in parallel. As you noticed, the most common way this is done is with an assembly program that runs in another cog and sits there waiting to be told what to do, then does it, and waits again. FullDuplexSerial works that way as do most I/O drivers.

    If you need a serial receiver that does something more complex than say FullDuplexSerial, like looks for a particular serial input pattern, you can either modify FullDuplexSerial to do it or take the Simple_Serial driver and modify it to run independently in its own cog and do the pattern match as part of its functioning.

    You can't call a Spin routine from assembly language, partly because the whole cog memory is normally occupied by the Spin interpreter and that would replace the assembly program that "calls" it so no return is possible.
  • RaymanRayman Posts: 14,162
    edited 2008-03-30 23:29
    I read "global" variables everywhere. Is there anyway for me to declare a variable in an Object and make it truly global so that any other cog or object can access it without passing it?
    I think all of HUB RAM is essentially global to all cogs, whether running SPIN or assembly...

    So, all you have to do is pass the address of the variable in question to the objects.· Or, you could use a predetermined value.· I believe some programs use the last few bytes of RAM for this...
  • DizzyDizzy Posts: 9
    edited 2008-04-24 15:09
    Thanks for the replies. I ended up buckling down and just passing the variables through function calls.
  • SteelSteel Posts: 313
    edited 2008-04-24 19:49
    Dizzy-
    ·· I made an

    Instead of passing the variables through the function calls, you can pass a pointer to the variable location(@X)...So you can have the function continually looping and checking the address of the variable and updating if it changes.

    I believe the following would work for you:

    Object 1 (Start):
    **************************************

    OBJ
    · XY : "XY_OUTPUT"
    VAR
    ···Long X_VALUE

    PUB MAIN
    ·· XY.XY_OUTPUT(@X_VALUE)·· '''''NOTICE THE "@"?
    ·· REPEAT
    ***************************************
    Object 2 (XY):

    PUB XY_OUTPUT(POINTER)
    ···REPEAT
    ······ 'Code to output·what is located at·pointer...or the address of the variable


    ·
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-04-24 20:37
    Data encapsulation is one aspect of SPIN that allows objects to be as reusable as they are. If truly global variables were allowed then you could get potential conflicts between objects written by different programmers that happened to use the same global variable name for different purposes. It's best to use functions to pass values or pointers where data sharing is explicitly needed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sign In or Register to comment.