Shop OBEX P1 Docs P2 Docs Learn Events
Is there a way to pass a variable between cogs in code? — Parallax Forums

Is there a way to pass a variable between cogs in code?

SteelSteel Posts: 313
edited 2007-06-26 23:38 in Propeller 1
...or do you need to connect 2 pins together?

I am looking at having 1 cog communicate with a PC.· Another Cog outputting a value.· Whenever a value is recieved by Cog1, I would like it to update cog2.

As far as I can read, Cogs really work as independent processors...but there is a hub, so I was thinking that there would be memory in the hub that cogs could use to communicate with each other.

I am a bit confused.· I know that I could just jump 2 pins together, and have 1 cog talk to the other cog that way, but is there a way in code such as Cog1.Variable = Cog2.Variable?

please help.

Thanks.

Comments

  • BeanBean Posts: 8,129
    edited 2007-06-26 18:44
    Create a variable in spin, that variable will be in hub memory. Then pass the ADDRESS of that variable to each cog. Now each cog can read and write the variable in hub memory by using it's address.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    “The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • deSilvadeSilva Posts: 2,967
    edited 2007-06-26 18:49
    Everything is located in the HUB RAM in the beginning (i.e. after it has been loaded from the PC or EEPROM).
    So you can access all your SPIN variables straightforward without minding in which COG SPIN interpreters are running your program.


    Please do not mix up the instantiation of an object with the use of a COG!
  • RichardFRichardF Posts: 168
    edited 2007-06-26 20:29
    To further what was said, you don't have to pass addresses of variables. The variable contents written in hub ram (global) are available to all cogs and all methods. This isn't always a good thing because if a wrong value gets written to a global variable by one it may effect many parts of the program. To get around this always use local variables inside of a method if their values are not needed by other methods, ie minimize the use of global variables when possible.
  • SteelSteel Posts: 313
    edited 2007-06-26 22:25
    So as long as I declare it in the VAR Block in the top of the spin code, it will update it in the cogs?

    I just want to make sure...Im sorry.

    This is pseudo-code below without the 'locks'...but will this work? This seems much easier than I imagined when I got the chip...





    {Declare variable in spin}
    VAR
    ·· Word MyVar

    {Begin Program}
    PUB Run_Program
    ··· 'code - the below is just an example...
    ··· Cognew(SerialIN)
    ····Cognew(Data_Out)

    PUB Serial_IN
    ··· REPEAT
    ······ 'Code here to continously save data to 'MyVar'


    PUB Data_Out
    ····REPEAT
    ···· 'code here to continously output data from 'MyVar'
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-06-26 22:39
    Steel the only time you have to worry about "in cog" or "out of cog" is with assembly code. If you are dealing with spin code located in the same object the variable space is identical.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • SteelSteel Posts: 313
    edited 2007-06-26 22:46
    Thanks for all the help. I just got a sample running.

    Sorry for the waste of bandwidth
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-06-26 22:56
    No problem, thats what we're here for.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-06-26 23:38
    There's a way to pass info, albeit slowly, through the Ram in Hitachi's HD44780U (LCD-II). The storage looks to be about 40 bytes and is meant to be user designed characters. Data seems 8 bits wide. Once you set the address, opcodes let you read/write with autoincrement or autodecrement.

    Whether this is worth the trouble is another consideration. (I bought a pair of 40x2 char LCDs for $13 from Allelectronics, so I've been boning up on spec sheets...)
Sign In or Register to comment.