Accessing an objects variables from another object?
How do I access an objects variables from another object without making a function to pass them?
Speed is critical but I would like to use objects for readability and ease of modification. I tried obj.var[1] but it expects a method not a variable.
Speed is critical but I would like to use objects for readability and ease of modification. I tried obj.var[1] but it expects a method not a variable.
Comments
Otherwise you could get a base memory address from the object the first time, and access the memory position from the main object.
I'm not sure it speeds up things...
Massimo
In the object I have a function that just returns the data based on the index number I pass to it.
Other ideas:
If you need a little bit more speed use a faster xtal (6 of 6.25 MHz work, just check decoupling capacitor discussions).
Otherwise check propbasic (it compiles to PASM), or Catalina (C).
Spinwise, if speed is so important check (with cnt) speed execution and experiment. For instance -variable is faster than variable:=-variable
You might find bottlenecks in other parts of your code....
Spin is much slower than pasm. If you need more speed use pasm, alone, or as an embedded code.
In this case there are at least 2 options. SpinLMM object, and Beau's embedded pasm object.
Massimo
I want to try this, how do I start a cog with asm code from another file. so say I have my main object to just start my cogs and have the global variables in just one file. So I have asm code for i2c and some math each in thier own file but have cognew (@i2c,@stack) and cognew (@math,@stack) ?
in each objectfile I added a getaddress method
then In my main object I have
I can get one or the other working like this but not both. Any suggestions?
the math writes to the array and the i2c reads from it. Can I not do it this way? I added a hart beat on pin 16 for math and pin 18 for i2c and they work independently but with them sharing the stack and a timer between each coginit only the last one starts.
You cannot share a timer between COGs. Or do you mean some variable that is used to count time?
With the stacks sorted out things should start to run. But...
How are these two methods using the shared data? If that data contains a bunch of values that must be treated as an "atomic" unit, i.e. all parts updated before the next process reads it, then you will need to be careful about how the two processes access shared the data and may end up needing locks to prevent data corruption.
Still, be careful how that shared data is accessed. by each COG.
then I will just add #8 to par to get the output[0] and so on.
But I have to ask: why are you using coginit instead of cognew? Such a practice is almost universally frowned upon, since it straitjackets your cog usage and may lead to conflicts down the road with other objects that commit the same faux pas.
-Phil
For starting a Spin method:
1) The COG id to be started.
2) The method name to be started in that COG
3) The address of some stack space to use.
For starting PASM:
1) The COG id to be started.
2) The address of a PASM routine to start
3) A value to pass as PAR.
Somehow the compiler works out how to load a Spin interpreter and run the Spin method in a COG or just load the PASM to a COG.
BUT what Rick_H has here is an undocumented use of COGINIT.
His calls just have any old number as the second parameter unrelated to any labels in the calling object. Seems just by chance that Spin does the right thing here.
Use of COGNEW is preferred.
Where do I have them reversed?
COGINIT (CogID, AsmAddress, Parameter )
asm1 and asm2 are the parameters and address is the entry to the asm code.
So the PAR obtained by the PASM contains the uninitialized value of asm1 and asm2. You should pass the address instead by using @:
na I caught that in my code on compile but didn't change it hear, sorry.
Heater, I would hope that the compiler is smart enough to default to an assembly cog if the first argument is not the name of a Spin routine. But I've been fooled before.
-Phil
So can I get an explanation why I can't use the same global var for 2 cogs started in the same object file. I was under the impression that par was just a pointer and nothing was done with it unless wrlong or something was used in asm. Or is it a placeholder that holds some data?
Who said you can't?
If you pass the same address (of some shared data) to two calls of COGINIT/NEW that start PASM codes then both of those COGs will get that same address in the PAR register when the start up. So they can both happily access the same data.
PAR is a register in the COG address space that holds a thirty two bit value passed to it from COGINIT/NEW.
Whether PAR contains anything useful or not is entirely up to you. Could be a value, could be bunch of flag bits or bit fields, could be HUB address of some data (a pointer). Could just be unused in which case using zero might be the neat thing to use in the COGINIT/NEW parameter.
-Phil