Access To Global Variables
3dogpottery
Posts: 78
I have a program running in a very fast SPIN2 loop that updates a global variable. Of course I can't create a method that would allow me to access this variable because this program is locked in a loop. Is there any other way to access this programs global variable?
Comments
You can call your access method from another cog. But what is this global variable for?
If you're running another Spin2 cog you can access that global variable. I do this all the time.
I created the global variable to see if I could access it from another cog. I think the only way to access another cogs global variables is through a method. However, the cog that is locked in a loop will not respond to an access method because it’s locked in a loop.
Thanks for your response Jon. I’ve tried to access the global variables of the cog that is locked in a loop, but to no avail. I’ve included the spin code that is “running in a loop and updating global variables” as a child object in another program. However, I am of the understanding that you can only access this child object’s public methods or its constants, not it’s global variables.
Incidentally, I have progressed up to your 6’th “Spin 2 for Beginner Series” video. My head is ready to explode.
One object can launch any number of cogs itself.
Thanks for your response evanh.
In my endeavor to learn how to program the P2, I created an endless repeat loop in Spin2 that updates a global variable. I named this piece of code "spinLoop.spin2". In another spin2 program, I created a child object called "childObject."spinLoop". So, "Why are you doing this?", you might ask. Well, I just wanted to see if there was a way to access a child objects global variable if this child object were locked in an endless loop. I think that it is not possible. I know you can access a child object's global variable through an accessor method, but not if the child object is locked in an endless loop.
It is most certainly possible. Can you post your code here?
What do you mean by "cog locked in a loop"? You may be calling a variable global when it's not. The attached program will show you that that two Spin cogs can access a global variable. The background cog runs a loop that updates counter every second. The main loop only displays the current value of counter. Note, though, that the main Spin cog and the background Spin cog are in the same file.
Now... if you're referring to a variable that is in the global space of a separate object file, then the you have a different problem. It's not hard to solve, though: when you start the child object you must expose the hub address (@) of the variable that you want another cog to monitor.
Tip: Post your code. One of the biggest problem for those who would help you is that we don't know that what you're asking includes all the details that would be exposed by sharing your code.
Here's an example that will make my last comments clear. It's a little timer/RTC/timecode object that has 1/100ths (cent) resolution. It is 100% Spin. Note that the object global timecode is updated by the background cog when running. But... this variable cannot be seen by the parent object -- unless we expose its hub address. That is provided by the object's start() method if there was a cog available to run the timer.
For clarity, in Spin there are no project globals -- only globals within an object. That said, if a variable lives in the hub and we know its address, we can access it from any cog.
The syntax used to access a variable by its hub address is:
...where type is byte, word, or long, and address is the hub address of the variable.
Update: Get corrected code from post #12
I really appreciate your help Jon. I will look at the examples you gave for sure! And yes, I shouldn’t have used the word “cog”.
Use the timecode demo that I've uploaded here -- I accidentally left a reference to jm_fullduplex_serial. spin2 (from my generic template) in the timecode object.
Updated 25 FEB 2024 : Added format selection to str() method.
Here's a version that uses PASM to update the timer. Since that happens in the cog, you have to send the hub address of the global to be updated to the cog.
Updated 25 FEB 2024 : Added format selection to str() method.
Thanks Jon for all the help. I looked at your example code and I see how it is possible to send the hub address of a global variable.
I wrote a simple program that illustrates the problem that I actually created. The way it's structured, the start method that is called gets stuck in a loop and never returns the hub address.
Yep. You're calling a loop that never ends which is causing the program to stick. I added text output to verify.
Here's your start() method:
This method never returns to the caller because the call to LoopRoutine() traps the program in a loop that never ends.
I showed you in my first example how to launch an infinite loop into its own Spin cog (second example shows how to do it with a PASM cog). That cog updates the object global variable. The start method of the child object returns the hub address of the object global variable to allow for direct access (which happens in my demo).
Other tips:
-- jm_serial.spin2 has long been retired -- please use jm_fullduplexserial.spin2 as its buffered and has output formatting
I've attached a template archive with updated objects.