Shop OBEX P1 Docs P2 Docs Learn Events
I can't find an example of passing *two* parameters into cogstart method — Parallax Forums

I can't find an example of passing *two* parameters into cogstart method

John KauffmanJohn Kauffman Posts: 653
edited 2014-02-08 21:54 in Propeller 1
C language: I can't find an example of passing two parameters into cogstart method

I'm learning how to pass parameters into cog's function at cogstart.
Passing one arg works (below).
I can't find an example for passing two args.
Any advice? Thanks.
/**
Multicore Example Stripped to Most Basic -- With parameters version
Blink 2 LEDS at different Hz from different cogs
Arg1 passes which pin to use

remaining task:
??? how to pass 2nd arg (duration of blink) to method in cog?
*/

#include "simpletools.h"
// allocate shared memory.  Values are copied from examples
unsigned int stack1[(160 + (50 * 2)) / 4];
unsigned int stack2[(160 + (50 * 2)) / 4];
// forward declare methods 
void blink(void *pin);  // ??? second param ???

//----------------------------------------------
// main starts in cog 0; only starts cogs, no other tasks 
int main(){
// pin circuit test
  high(11);  high(12);  pause(500);
  low(11);   low(12);   pause(500);  

int  cog1 =  cogstart(blink,(void*) 11, stack1, sizeof(stack1));    // ??? second param ???
    pause(10);
int  cog2 =  cogstart(blink,(void*) 12, stack1, sizeof(stack2));
    pause(10);
} //main()  


//----------------------------------------------
// to do: pass in duration as second param so blink at different Hz
void blink(void *pin){     // ??? second param ???
  while(1){ //keep blinking
    high(pin); pause(1000);   //  // ??? use second param here instead of hard-code 1000
    low(pin); pause(1000);    
  } // while(1) to keep blinking
} // blink()

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-02-04 15:25
    Either declare a structure or use an array of a given type, then pass its address. Inside the cogstart'd function you extract the members of said structure/array.
  • SRLMSRLM Posts: 5,045
    edited 2014-02-04 15:45
    In this post I show how to use a C++ struct class to pass multiple parameters. You could do something similar: http://forums.parallax.com/showthread.php/153579-PropGCC-equivalent-of-someVar-long-someAddress?p=1239962#post1239962
  • jazzedjazzed Posts: 11,803
    edited 2014-02-04 16:47
    See update to your code here. Code has not been "compile tested."
    /**
    Multicore Example -- With parameters version
    Blink 2 LEDS at different Hz from different cogs
    Mailbox of type mbox_t passes which pins and delays to use.
    
    remaining task:
    ??? how to pass 2nd arg (duration of blink) to method in cog?
    See below
    */
    
    // define a structure and typedef to avoid extra typing
    // variables must be volatile
    // mbox_t becomes a type like int is a type
    //
    typedef struct mbox_struct {
      volatile int pin;
      volatile int delay;
    } mbox_t;
    
    #include "simpletools.h"
    // allocate shared memory.  Values are copied from examples
    unsigned int stack1[(160 + (50 * 2)) / 4];
    unsigned int stack2[(160 + (50 * 2)) / 4];
    
    // forward declare methods 
    void blink(void *par) 
    { 
      // cast par to mailbox type
      mbox_t *mb = (mbox_t*) par;
    
      // now use mailbox fields
      while(1) {
        high(mb->pin);
        pause(mb->delay);  
        low(mb->pin);
        pause(mb->delay);
      }
    }
    
    mbox_t mbox1;
    mbox_t mbox2;
     
    //----------------------------------------------
    // main starts in cog 0; only starts cogs, no other tasks 
    //
    int main()
    {
      int cog1, cog2;
    
      mbox1.pin = 11;
      mbox1.delay = 500;
    
      mbox2.pin = 12;
      mbox2.delay = 500;
    
      cog1 =  cogstart(blink,(void*) &mbox1, stack1, sizeof(stack1));    // ??? second param ... use mailbox mbox1
      pause(10);
      cog2 =  cogstart(blink,(void*) &mbox2, stack2, sizeof(stack2));
      pause(10);
    
      return 0; // main returns int to the operating system if any.
    } //main()  
    
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-02-07 06:58
    I think I got most of it.
    I'm going to hold off on that until I get better at C. Passing one pram will do for now.
    But will try this after other parts work
    Thanks
  • jazzedjazzed Posts: 11,803
    edited 2014-02-07 07:29
    I think I got most of it.
    I'm going to hold off on that until I get better at C. Passing one pram will do for now.
    But will try this after other parts work
    Thanks
    The alternative to using a struct is to use an array. It's a choice between good or bad C programming.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-02-07 08:44
    What kuroneko suggested is what I have seen most commonly. Instead of passing an argument, in place of that you pass the address to an array of arguments, that way you're not limited to just one or two, but as many as you need. I've done this with an array of values (variables) as well when the method uses several variables. So technically you're still passing one argument, but it is the pointer to much more information.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-02-08 09:37
    Thanks, Chris. I think that breaks it down into most succinct explanation. Now to try it in code...
    This is for my Basic PBASIC Tasks in C guide for people converting, so your time answering should be leveraged.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-02-08 10:10
    Keep in mind this has to do with the Propeller's architecture. When starting a new cog, we can pass exactly one parameter (in the par register). Most applications, Spin or C, pass the hub address of variables/data required by the cog.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-02-08 19:42
    Thanks, Jon. I think I see the picture now & logic behind it.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-02-08 21:54
    Jazzed: ran perfectly. Thanks. I'm now ready to write it up,
Sign In or Register to comment.