Shop OBEX P1 Docs P2 Docs Learn Events
Converting spin method to object question — Parallax Forums

Converting spin method to object question

MinimumWageMinimumWage Posts: 72
edited 2008-07-31 15:56 in Propeller 1
Hello,

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

  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-30 23:02
    First of all, you can't use "pass.stuff(...)" as the 1st argument to COGNEW/COGINIT. It's not really documented in the Manual, but the method call provided as the 1st argument to COGNEW/COGINIT has to be a method in the same object as the COGNEW/COGINIT.

    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?
  • Cluso99Cluso99 Posts: 18,069
    edited 2008-07-30 23:05
    Not sure what you are trying to achieve, but this may help

    x := 7
    x: = callinc (x)

    PUB callinc (x) : y
    y := x + 1
  • MinimumWageMinimumWage Posts: 72
    edited 2008-07-30 23:54
    Mike,

    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
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-07-31 04:37
    hello 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
  • JamesxJamesx Posts: 132
    edited 2008-07-31 10:02
    Mike

    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
  • MinimumWageMinimumWage Posts: 72
    edited 2008-07-31 15:56
    Stefan - Right! Now I totally understand why objects have the start/stop methods. I was trying to keep my little test program simple and ended up working myself right into a corner.

    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.
Sign In or Register to comment.