Converting spin method to object question
MinimumWage
Posts: 72
Hello,
I'm playing around with the following test code:
As it stands the code works like I expect. The cog launches with the pass_func method and you can watch the variable increment in the terminal window. But what I really want to do is create a separate "passvar"·object that is equivalent to the local pass_func method. (i.e. it would be a separate Spin file with a method functionally·just like pass_func.)
I think·I understand that because of the scoping I have to explicitly return a result from the object and·set X to that,·but in order to return a value I have to exit the function, like·below. I would call this by changing the cognew statement to something like cognew(pass.stuff(@X), @stack):
I've been playing around with repeats, nested methods, etc.,·to try and get something that works like the repeating loop I made with the first set of code, but I'm not having any luck.·Is there·a way to keep passing an incrementing·result back short of calling the object over and over?·I'd appreciate any advice or explanation from the·group!
Mike
I'm playing around with the following test code:
CON _clkmode = xtal1 + pll16x 'set for PROTOBOARD!! _xinfreq = 5_000_000 TermRX = 31 'Prop plug comm pins for debugging TermTX = 30 OBJ PCTerm : "PC_Interface" 'pass : "passvar" VAR long stack[noparse][[/noparse]20] PUB Main | X PCTerm.start(TermRX, TermTX) PCTerm.str(string("Starting")) X := 2 cognew(pass_func(@X), @stack) repeat PCTerm.dec(X) waitcnt(clkfreq + cnt) PUB pass_func (x) repeat long[noparse][[/noparse]x] := long[noparse][[/noparse]x]+1 waitcnt(clkfreq + cnt)
As it stands the code works like I expect. The cog launches with the pass_func method and you can watch the variable increment in the terminal window. But what I really want to do is create a separate "passvar"·object that is equivalent to the local pass_func method. (i.e. it would be a separate Spin file with a method functionally·just like pass_func.)
I think·I understand that because of the scoping I have to explicitly return a result from the object and·set X to that,·but in order to return a value I have to exit the function, like·below. I would call this by changing the cognew statement to something like cognew(pass.stuff(@X), @stack):
PUB stuff (x) long[noparse][[/noparse]x] := long[noparse][[/noparse]x]+1 result := long[noparse][[/noparse]x]
I've been playing around with repeats, nested methods, etc.,·to try and get something that works like the repeating loop I made with the first set of code, but I'm not having any luck.·Is there·a way to keep passing an incrementing·result back short of calling the object over and over?·I'd appreciate any advice or explanation from the·group!
Mike
Comments
I'm not sure what you're really trying to accomplish. Could you give a more detailed explanation with some examples other than what you've shown so far?
x := 7
x: = callinc (x)
PUB callinc (x) : y
y := x + 1
Thanks, I didn't know about that restriction on COGNEW/COGINIT. That's probably why nothing was working for me in this case.
The code was just something I was playing around with. Ultimately the intention was to be able to poll sensor input from an object in another cog. I was trying to simulate that with the incrementing counter just to play around with the syntax when I got confused. Sorry to bother everyone!
Mike
If you take a look at other objects almost all of them have a method "start" and "stop".
and the cognew /cogstop is done INSIDE this method. And this construction is recomended in the manual to code in this
way to keep the start of a new cog standardized.
Inside your spinfile "passvar" you just write a method "start" that does the COGNEW and a method "GetSensorValue" and call this method passvar.GetSensorValue that's all
best regards
Stefan
Your goal looks similar to one I was stuck on. In my case, I was working on passing data from one data-collection cog back to the main routine. At the core, it was an issue of indirect addressing.
Take a look at http://forums.parallax.com/forums/default.aspx?f=25&m=275474&g=275488#m275488
Jim
Jim - Thanks for the link, that discussion was really helpful for me to read through. I got most of my addressing working by following examples in the manual but it's a lot clearer to me now why I had to use the long[noparse][[/noparse]x] syntax.
Thanks again to everyone for the help and advice.