Shop OBEX P1 Docs P2 Docs Learn Events
more than 15 parameters to pass to a cog? — Parallax Forums

more than 15 parameters to pass to a cog?

Zap-oZap-o Posts: 452
edited 2009-08-26 01:50 in Propeller 1
I need to pass more than 15 parameters to a cog. Can this be done?

Cog.start (_ff, _Tf, _X0, _X1, _Y0, _Y1, _Vr2, _aR1, _aR2, _Pb1, _Pb2, _Pb3, _Ab1, _Ab2, _Ab3,_kb1, _kb2, _kb3, _max, _min, _all) 

Comments

  • jazzedjazzed Posts: 11,803
    edited 2009-08-25 18:57
    Don't know if there's a constraint on th number of parameters, but you could try this.

    pub start(_ff, _Tf, _X0, _X1, _Y0, _Y1, _Vr2, _aR1, _aR2, _Pb1, _Pb2, _Pb3, _Ab1, _Ab2, _Ab3,_kb1, _kb2, _kb3, _max, _min, _all)
      cognew(@pasm, @_ff)
    
    dat
    pasm org 0
    ' ... 
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • Agent420Agent420 Posts: 439
    edited 2009-08-25 18:59
    I'm no Propeller wizard, but the first thing that comes to mind is to look at how the video examples pass their parameters.· They store the data in the DAT section and pass the address and count.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Mike GreenMike Green Posts: 23,101
    edited 2009-08-25 19:18
    Usually how this is done is that you have a series of long variables declared in order, then pass the address of the first variable like this:
    VAR
       long _ff, _Tf, _X0, _X1, _Y0, _Y1, _Vr2, _aR1, _aR2, _Pb1, _Pb2, _Pb3, _Ab1, _Ab2, _Ab3, _kb1
       long _kb2, _kb3, _max, _min, _all
    
    PUB mainMethod
       cog.start(@_ff)
    


    PUB start(address)
       cognew(@pasm,address)
    
    DAT
                 org     0
    pasm:    mov   temp,PAR          ' Hub address of parameters
                 movd move,#table      ' Cog address of copy of parameters
                 mov   count,#21         ' Number of parameters
    move:    rdlong 0-0,temp          ' Read one parameter
                 add    move,destIncr   ' Increment destination address
                 add    temp,#4           ' Increment source address
                 djnz   count,#move     ' Repeat for all parameters
    ' Here we continue with other code
    ...
    destIncr long   $200                 ' Add one to destination field
    count     res     1
    temp      res    1
    table      res    21
    


    You can also copy the parameters in the Spin start method like:
    PUB start(address)
       longmove(@table,address,21)
       cognew(@pasm,0)
    
    DAT
                 org    0
    ' Here we continue with other code
    ...
    table      long   0[noparse][[/noparse]21]                ' Allocate space for a copy of all the parameters
    ' You can't use RES here because the table has to occupy hub space (so it can be copied)
    

    Post Edited (Mike Green) : 8/25/2009 7:24:42 PM GMT
  • jazzedjazzed Posts: 11,803
    edited 2009-08-25 19:25
    Agent420 offers good advice.
    Either way you go, it will be painful though ... unless your interface parameters lose weight [noparse]:)[/noparse]
    You could use a generic buffer instead and provide methods to do get/set on the parameters.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • Zap-oZap-o Posts: 452
    edited 2009-08-25 19:29
    Thanks I got it working.
  • BradCBradC Posts: 2,601
    edited 2009-08-26 01:23
    jazzed said...
    Don't know if there's a constraint on th number of parameters, but you could try this.

    pub start(_ff, _Tf, _X0, _X1, _Y0, _Y1, _Vr2, _aR1, _aR2, _Pb1, _Pb2, _Pb3, _Ab1, _Ab2, _Ab3,_kb1, _kb2, _kb3, _max, _min, _all)
      cognew(@pasm, @_ff)
    
    
    


    Bad, bad, bad, no, no, no!
    You are passing the long address of a set of parameters on the stack. By the time the cog has loaded the spin routine will have returned and possibly damaged the stack contents. Don't do this.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lt's not particularly silly, is it?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-08-26 01:35
    Good catch, Brad! To eliminate this problem, one could either program a delay before returning or wait for the newly-started cog to change one of the parameters to indicate, "I got it!" In any event, the new cog needs to copy these values to its own cog memory posthaste, since they are transient.

    But I'd be much more inclined just to do a longmove from @_ff to a parallel area in the DAT section before doing the cognew.

    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 8/26/2009 1:40:15 AM GMT
  • jazzedjazzed Posts: 11,803
    edited 2009-08-26 01:50
    Oops you're right of course blush.gif

    I usually add a delay after cognew or poll for the cog to finish in my code to make sure the cog loads before i try to use it. For example, even doing the RightThing(tm) as in FullDuplexSerial and not waiting for it to load before calling fd.str(string("Hello")) pretty much guarantees output failure. Shrug shoulders [noparse]:)[/noparse]

    So, Is Python really named after Monty Python?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
Sign In or Register to comment.