Passing variable memory address to a method
robotics
Posts: 90
Hi,
I am having a bit of trouble understanding how to start a new cog to run a method while passing a variable's ("X") memory address to the method so that the changing value of the variable represented by the variable's memory address can be monitored by the calling method. A pretty much bare essentials example follows whereby the looped printing of the variable's address for some reason prints only one number "1408" over and over again where I was attempting to have it print a number incrementing by "1" for each pass through the loop. (I also don't understand where the repeating number "1408" came from! ) Any help is greatly appreciated. In advance, thank you.
Here's the code:
CON
_xinfreq = 5_000_000
_clkmode = xtal1 + pll16x
VAR
long SqStack[noparse][[/noparse]60]
OBJ
debug : "FullDuplexSerialPlus"
PUB Main | X
debug.Start(31, 30, 0, 57600)
waitcnt(clkfreq * 2 + cnt)
debug.tx(16) ' CLS - Clear Screen
X := 10
cognew( Increase(@X), @SqStack)
repeat
debug.dec(@X)
debug.str(string(13))
waitcnt(clkfreq * 2 + cnt)
PUB Increase(XAddr)
repeat
XAddr := XAddr + 1
waitcnt(clkfreq * 2 + cnt)
I am having a bit of trouble understanding how to start a new cog to run a method while passing a variable's ("X") memory address to the method so that the changing value of the variable represented by the variable's memory address can be monitored by the calling method. A pretty much bare essentials example follows whereby the looped printing of the variable's address for some reason prints only one number "1408" over and over again where I was attempting to have it print a number incrementing by "1" for each pass through the loop. (I also don't understand where the repeating number "1408" came from! ) Any help is greatly appreciated. In advance, thank you.
Here's the code:
CON
_xinfreq = 5_000_000
_clkmode = xtal1 + pll16x
VAR
long SqStack[noparse][[/noparse]60]
OBJ
debug : "FullDuplexSerialPlus"
PUB Main | X
debug.Start(31, 30, 0, 57600)
waitcnt(clkfreq * 2 + cnt)
debug.tx(16) ' CLS - Clear Screen
X := 10
cognew( Increase(@X), @SqStack)
repeat
debug.dec(@X)
debug.str(string(13))
waitcnt(clkfreq * 2 + cnt)
PUB Increase(XAddr)
repeat
XAddr := XAddr + 1
waitcnt(clkfreq * 2 + cnt)
Comments
2) XAddr is set to the address of X. To reference the variable itself, use LONG[noparse][[/noparse] XAddr ] as in LONG[noparse][[/noparse] XAddr ] := LONG[noparse][[/noparse] XAddr ] + 1. Look up the description of this in the Propeller Manual or the Prop Tool help files.
3) @X is the address of X. Your "debug.dec(@X)" is displaying the address of X. Use "debug.dec(X)".
· repeat
··· debug.dec(X)
··· debug.str(string(13))
··· waitcnt(clkfreq * 2 + cnt)
PUB Increase(XAddr)
repeat
· long[noparse][[/noparse]XAddr] := long[noparse][[/noparse]XAddr] + 1
· waitcnt(clkfreq * 2 + cnt)
Edit: Mike posted as I was editing my post.· We're basically suggesting the same thing.
Many thanks for your help, this was a very elusive problem for me!
http://forums.parallax.com/showthread.php?p=919156 will show step by step, how I solved this problem for me. I'm not using another method any more, because it is now "normalized", that is, I no longer think about how to pass a variable, I just change the number and the table and that's it. But like always: in the beginning, one might ask: why do it so complicated? The answer: because it is incredible simple!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
cmapspublic3.ihmc.us:80/servlet/SBReadResourceServlet?rid=1181572927203_421963583_5511&partName=htmltext
Hello Rest Of The World
Hello Debris
Install a propeller and blow them away