I can't find an example of passing *two* parameters into cogstart method
John Kauffman
Posts: 653
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.
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
/** 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()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
This is for my Basic PBASIC Tasks in C guide for people converting, so your time answering should be leveraged.