#include <stdio.h>
#include "rtkernel.h"

#define MAIN_PRIORITY 3
#define Slots 7		// number of data stored in mailbox Box

Mailbox Box;		// mailbox

void TaskA(void)
{
   int i;

   printf("<task A> is waiting at mailbox\n");
   RTKGet(Box, &i);
   printf("\n<task A> has received a %i \n", i);
   printf("<task A> - Finish -\n");
}

void TaskB(void)
{
   int i;

   printf("<task B> is waiting at mailbox\n");
   do {
     RTKGet(Box, &i);
     printf("\n<task B> has received %i [Exit=2]\n", i);
   } while(i!=2);
   printf("<task B> - Finish -\n");
}

void main(void)
{
   int i;

   clrscr();
   RTKernelInit(MAIN_PRIORITY);

   // create mailbox size = sizeof(int), number of data = Slots
   Box = RTKCreateMailbox(sizeof(int), Slots);
   RTKCreateTask(TaskA, MAIN_PRIORITY+1, 1024, "Task A");
   RTKCreateTask(TaskB, MAIN_PRIORITY+2, 1024, "Task B");
   do {
      printf("\n* main * Please enter a number [Exit=1]: ");
      scanf("%i", &i);
      printf("* main * PUT %d to mailbox.\n",i);
      RTKPut(Box, &i);		// send data of i into mailbox Box
      printf("* main * after PUT %d to mailbox.\n",i);
   } while ( i!= 1);
   printf("\n* main * - Finish -\n");
}

