Accessing var in another cog?
Stephen
Posts: 53
I've tried searching for this and I just can't find anything... Can code in one cog read a global variable defined in another object/cog? Sort of like defining an extern?
If not how can I define and write to a variable that is global across all cogs? Can this only be done via object methods?
If not how can I define and write to a variable that is global across all cogs? Can this only be done via object methods?
Comments
1) Accessing variables from two or more cogs
2) Accessing variables in another object
These are two completely different issues.
You can have several cogs that are executing the same or different methods in a single object program (without any references to other objects). The cogs simply use the variables, but have to be careful that two or more cogs are not changing the variables at the same time. If there's a complex interaction involving several variables, you may have to use LOCKxxx calls to prevent two cogs from accessing the same resources at the same time. See the manual regarding LOCKNEW, LOCKSET, LOCKCLR, and LOCKRET.
When you have multiple objects in a program, none of the objects can directly reference variables in the other objects regardless of how many cogs are used to run the parts of the objects. You can have a complex program with many objects that all use only a single cog to run the whole thing. You can communicate between objects in two ways:
1) Use Get and Set methods in the sub-objects to reference and change certain variables in the sub-object from the next higher object
2) Use Address methods to return the address of one or more variables (possibly as the address of the start of a block of variables), then use BYTE, WORD, and LONG to reference or change the variables given their address.
There's no mention of the GET method in the prop manual... Where can I find info on it?
If your code is all in one object than something like this will work.
This should work fine but if the tempCode is in another object than you will need to change this slightly so that you give tempCode a pointer to temperature like this
You would also have to use the '@' operator to get the address of the temperature variable.
bdickens...
I've provided my code here if you want to look at it. It's working pretty good now, using 4 cogs:
- main
- ezLCD-002 display
- DS1620 temp sensor
- Servo