global variable, multiple cogs
mikea
Posts: 283
If a parent object calls a secondary object that contains global variables that is intended for both objects to share. Do I have to specifically address them in the parent object? Also the secondary object will be running in a separate cog. I've read or kit book and manual but I'm not clear how to do this.
Comments
As far as different cogs is concerned, you just need to worry about one cog changing a variable when it's not expected. Because of the round robin nature of the hub, only one cog can access a variable at a time.
Remember, the Spin code stays in the hub. It's the Spin interpreter that's launched in a newcog when instructed. All the Spin variables stay in the hub so all the cogs can access them.
The piezo program I posted earlier today returns the address of the note array in the child object. The parent object doesn't use this information but I wanted the parent to have it in case I wanted the parent to directly access the array.
A lot of the TV object pass the address of a bunch of parameters from the parent to the child.
It can be a pain to deal with variable by address since one has to write "long[addressOfVariable]" whenever one whats to access the variable. I frequently just make one big program if there's a lot of variables to be shared.
It is quite possible for your program to use sub objects that never start another COG. They can just provide methods for you to call. Everything runs in the same COG.
Conversely it is quite possible that a single object can start many COGs each running some Spin method or PASM code.
You can see on the bottom line the location of "note" is returned.
The parent looks like this.
To assign the variable c1 the value of the 13th element (#12 since we start counting with zero) of the note array, the parent object would use:
More correctly since the outputs are or'ed together that means that any cog can set the output high but another cog won't be able to take it low. So if all cogs made a pin an output and only pulsed it high when it needed to while holding it low the rest of the time then it will still work. If the output was an active high alarm for instance then any cog could just set it but there's not too many instances where we could take advantage of the or'ing.
If debugging is the art of removing bugs then programming must be the art of......
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
I'll try to find (or write) a simple example.
Cogs have got nothing to do with whether or not a variable gets shared or not. It's objects you need to worry about.
The only time a separate cog won't have access to a variable is if it's running PASM code. If it's running PASM, all reads (and writes) to the hub require a PASM command to access a variable location. You should need to worry about how PASM behaves for now.
I just remembered a program which passes an address. It's in the same piezo music program. I'll post the example in a minute.
Since "stackAddress" contains the address of the array "stack" the address symbol shouldn't be used since I'm not interested in the address of "stackAddress" but the value of "stackAddress". This is a very important concept and you want to make sure you understand the difference between the contents of a variable and the location of a variable.
In this example "stackAddress" contains the address of "stack" in the parent object.
Here's the parent:
and the child:
I've attached the achieve with these two files to this post.
Edit: I had an incorrect comment added. Comment deleted.
Correct. It's just different objects that don't share global variables. Multiple cogs can share variables just fine.
An exception to different objects not sharing variables is when you're using multiple instances of the same object. To save space, only one copy of the object's code is loaded into the Propeller. The Propeller can use this same code multiple times without the multiple instances clashing. To keep global variables from clashing, each instance has its own "VAR" section in RAM. However, variables stored in the "DAT" section are shared among the multiple instances. This is one time when variables are shared among multiple objects. This has limited application since the objects sharing the variables have the exact some code. There are occasions when these shared "DAT" variables can be very handy.
Its how many of my spin magic projects were accomplished with ease.
The DAT section is a very powerful memory/variable space if you use it right. But it can get you into big trouble if you use it wrong.