Page 1 of 1

Messages between tasks

Posted: 14 Jan 2011, 03:29
by thomdaley
Hi,
Is it possible to send messages between tasks in NXC? I know about the StopAllTasks routine but was wondering if it can be done.

Thanks

Re: Messages between tasks

Posted: 14 Jan 2011, 03:42
by timpattinson
If you mean using a mutex (mutually exclusive) variable to control using a shared resource(motors, display etc) then yes
NXC Guide wrote:In NXC the mutex type is a 32-bit value that is used to synchronize access to resources shared across multiple threads.

For this reason there is never a reason to declare a mutex variable inside a task or a function. It is designed for global variables that all tasks or functions can Acquire or Release in order to obtain exclusive access to a resource that other tasks or functions are also trying to use

Code: Select all

  mutex motorMutex;
  task t1()
  {
     while (true) {
       Acquire(motorMutex);
       // use the motor(s) protected by this mutex.
       Release(motorMutex);
       Wait(MS_500);
     }
  }
  task t2()
  {
     while (true) {
       Acquire(motorMutex);
       // use the motor(s) protected by this mutex.
       Release(motorMutex);
       Wait(MS_200);
     }
  }
  task main()
  {
    Precedes(t1, t2);
  }
-Tim

Re: Messages between tasks

Posted: 14 Jan 2011, 04:32
by mattallen37
You want to have a variable that can be accessed by all tasks? Just use global variables, not local.

Re: Messages between tasks

Posted: 14 Jan 2011, 05:18
by muntoo
mattallen37 wrote:You want to have a variable that can be accessed by all tasks? Just use global variables, not local.
Yes, as Matt said, you can do this:

Code: Select all

int g_iCharVal; // declare global variable (that will store the value of the current ASCII character position)


// Displays stuff!
task display()
{
    string cOut; // lol :) Technically, it should be szOut, but I like messing things up

    while(1)
    {
        // Convert g_iCharVal to string and store in cOut
        cOut = FlattenVar(g_iCharVal);

        // Display information on screen
        ClearScreen();
        NumOut(0, LCD_LINE1, g_iCharVal, 0);
        TextOut(0, LCD_LINE2, cOut, 0);
    }
}


// Changes the variable according to user input
task change()
{
    while(1)
    {
        if(ButtonPressed(BTNLEFT, 0))
            g_iCharVal--;

        if(ButtonPressed(BTNRIGHT, 0))
            g_iCharVal++;

        if(g_iCharVal < 0x20) // 0x20 == ' '
            g_iCharVal = 0x7F;

        if(g_iCharVal > 0x7F) // 0x7F == '!', IIRC
            g_iCharVal = 0x20;
    }
}


task main()
{
    g_iCharVal = 0x20;
    // start other 2 tasks
    Precedes(display, change);

//    doNotUse = 4; // ERROR!
}


int doNotUse = 3;
Make sure your global variables are at the top, though, if you want all your tasks to be able to access them.

Re: Messages between tasks

Posted: 14 Jan 2011, 13:18
by thomdaley
Thanks for the quick replies! I did know about mutex, I used it in a simple line following program.

Thanks again