Shop OBEX P1 Docs P2 Docs Learn Events
Relationship between Cogs and Variables — Parallax Forums

Relationship between Cogs and Variables

Sal AmmoniacSal Ammoniac Posts: 213
edited 2010-01-24 06:22 in Propeller 1
I have an application I've written in Spin that reads temperature data from an ADC and outputs it to an LCD and to a serial port.

The initial cog starts the other objects in their own cogs, including the object that reads the ADC ("ADC") and the object that outputs data through the serial port ("SerialOut"). The initial cog then goes into a repeat loop continuously outputting the temperature data on an LCD. The ADC object loops reading the temperature and saving it into a variable. The serial output object loops writing the temperature data to the serial port once every 30 seconds.

The main loop calls ADC.GetTemp and outputs the temperature just fine on the LCD. The SerialOutput object also calls ADC.GetTemp, but it always returns 0.

The GetTemp method in the ADC object simply returns the value of a variable called Temperature, which is declared in a VAR block as a Long in ADC.

Why does the initial cog get the proper result when it calls ADC.GetTemp, but the code running in another cog does not?

The way I've assumed variables work in a case like this is as follows: The ADC object has a variable called Temperature, which, since it's declared in a VAR block resides in the 32K global RAM space. When the ADC object modifies its temperature variable, it modifies the value in global RAM. When code running in any other cog calls ADC.GetTemp, that code runs in the cog that called it, but it should have access to Temperature because that value is in global RAM. Is this reasoning faulty?

Comments

  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2010-01-24 02:02
    Sal Ammoniac -

    Can you post your code so we can take a look at it? The first thing I am wondering is how the code compiles if the ADC.GetTemp is in the SerialOut code but the SerialOut code didn't create it. Maybe you are trying to do something that is possible, but I didn't realize it is possible. Seeing your code will help me understand better.

    The way I would have probably approached it is as such:

    Main Cog - starts other cogs, declares place to store temp
    ADC cog - repeatedly gets temp and stuffs it in the temp variable location passed to it by main.
    SerialOut Cog - repeatedly checks the temp variable location passed to it by the main cog.
    LCD out Cog - repeatedly checks the temp variable location passed to it by the main cog.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" LCD Composite video display, eProto for SunSPOT
    www.tdswieter.com
  • StefanL38StefanL38 Posts: 2,292
    edited 2010-01-24 03:31
    I second what Timothy said. attach the COMPLETE code to a posting.
    Your reasoning is half right / half wrong.

    Variables declared in the VAR section reside in HUB-RAM. That's right.
    But variables declared in the VAR-section are accessible across cogs but NOT across objects.

    Imagine you have two ADCs that should work independent of each other. Each in his own cog. Would you like
    to write TWO object-files with ALL variables having a different name? No! The object is written ONCE and can be used
    1-8 times, but then the variables have to be object-exclusive.

    From your description it is not really clear to me how your code works so please attache your complete code by using the archive-function of the propellertool

    best regards

    Stefan
  • Sal AmmoniacSal Ammoniac Posts: 213
    edited 2010-01-24 05:58
    Now I see the problem: the main object and the SerialOut object have their own separate instantiations of the ADC object, so of course only the one that has its initialization method called actually works.

    Is there any way for the Spin code running on a cog to instantiate an object and then pass a reference to that object to it to another object running in another cog?
  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-24 06:22
    It is not possible for Spin to pass a reference to an object (except by cheating ... with knowledge of the internal format of the Spin bytecodes and internal tables used).

    An object can have a method that returns the address of its variables and/or addresses of specific variables. These can be used with BYTE / WORD / LONG when passed to other objects.
Sign In or Register to comment.