///////////////////////////////////////////////////////////////////////////////////////////////////
//
//                 Program to Demonstrate Multiple Tasks on The NXT
//
////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma platform(NXT) // This progra monly runs on the NXT

const int kMinDelayTime =  300;
const int kDelayTime    = 1000;

const bool kPlaySounds = false;



task task1()
{
  int nCount = 0;

  while(true)
  {
    wait1Msec(kMinDelayTime + random(kDelayTime));
    ++nCount;
    nxtDisplayTextLine(1,  "%s. Cycle:%4d", "task1", nCount);
    if (kPlaySounds)
      PlaySound(soundDownwardTones);
  }
  return;
}


task task2()
{
  int nCount = 0;

  while (true)
  {
    wait1Msec(kMinDelayTime + random(kDelayTime));
    ++nCount;
    nxtDisplayTextLine(2,  "%s. Cycle:%4d", "task2", nCount);
    if (kPlaySounds)
      PlaySound(soundUpwardTones);
  }
  return;
}


task task3()
{
  int nCount = 0;

  while (true)
  {
    wait1Msec(kMinDelayTime + random(kDelayTime));
    ++nCount;
    nxtDisplayTextLine(3,  "%s. Cycle:%4d", "task3", nCount);
    if (kPlaySounds)
      PlaySound(soundFastUpwardTones);
  }
  return;
}


task task4()
{
  int nCount = 0;

  while (true)
  {
    wait1Msec(kMinDelayTime + random(kDelayTime));
    ++nCount;
    nxtDisplayTextLine(4,  "%s. Cycle:%4d", "task4", nCount);
    if (kPlaySounds)
      PlaySound(soundBeepBeep);
  }
  return;
}

task task5()
{
  int nCount = 0;

  while (true)
  {
    wait1Msec(kMinDelayTime + random(kDelayTime));
    ++nCount;
    nxtDisplayTextLine(5,  "%s. Cycle:%4d", "task5", nCount);
    if (kPlaySounds)
      PlaySound(soundBlip);
  }
  return;
}


{
  int nCount = 0;

  nVolume = 2;

  //
  // Boost priority so that all tasks start immediately
  //
  nSchedulePriority = kHighPriority;

  StartTask(task1);
  StartTask(task2);
  StartTask(task3);
  StartTask(task4);
  StartTask(task5);

  //
  // Restore to normal priority
  //
  nSchedulePriority = kDefaultTaskPriority;

  while (true)
  {
    wait1Msec(kMinDelayTime + random(kDelayTime));
    ++nCount;
    nxtDisplayTextLine(0,  "%s.  Cycle:%4d", "main", nCount);
  }
  return;
}