Shop OBEX P1 Docs P2 Docs Learn Events
Variable — Parallax Forums

Variable

PrincPrinc Posts: 12
edited 2013-10-02 11:41 in Learn with BlocklyProp
Hi
i would like to ask if somebody know how to get a variable from cogstart function i tried it like that:

int b = cogstart(&function1, 2, stack, sizeof(stack));

// i would like to have a return from function1 but i don´t know how to do
// i know that i can make a global variable but i would like to give the information back to cog 0 right after cog 1 finish

int function1(int x){
....
....
return y;
}

Thanks a lot and thanks for advice
(Sorry for my english i´m not from UK)

Comments

  • jazzedjazzed Posts: 11,803
    edited 2013-10-01 14:57
    The return from cogstart is the COG ID.

    Look at this link for cogstart usage:
    https://sites.google.com/site/propellergcc/documentation/libraries/propeller-h-library#TOC-cogstart

    The "pin" variable in the example is a variable that is shared with the COG. The do_toggle(void* arg) passes a pointer to the second parameter in the cogstart call. The do_toggle example shows how to set up the pin variable (it is a little strange, but that's the generic way to do it in C). You can set the value of pin from do_toggle if you like.

    There is another way to share variables by using the gnu assembler for advanced users.
  • edited 2013-10-02 11:41
    There are also examples here of how to share variables between cogs:

    http://learn.parallax.com/propeller-c-functions/multicore-example

    In this scheme, the other cog is sharing variables with the cog that launched it. Note that the shared variables are declared volatile.

    You might have other functions that look at those volatile variables, and return their values (or copies of their values). There is an example in Documents\SimpleIDE\Learn\Simple Libraries\Utility\libmstimer. Open libmstimer.side, then click the bottom-left Show Project Manager button (if it isn't already open). Then, click mstimer.h and mstimer.c to see those files.

    Inside mstimer.c, the mstime_get function returns a copy of one shared variable, and mstime_reset changes a shared variable's value to zero. The only function in this library running in another cog is ms_timer, and it is updating the value of a variable named t. The rest of the functions are called by an application, in the main file for example. But since t is static volatile, all the functions in the file can see it.
Sign In or Register to comment.