// Copyright (c) 2004 Rowley Associates Limited.
//
// This file may be distributed under the terms of the License Agreement
// provided with this software.
//
// THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

// Test byte queues

#include <ctl_api.h>

CTL_TASK_t main_task, new_task; // task blocks

CTL_BYTE_QUEUE_t bq;
#define QSIZE 10
unsigned char queue[QSIZE]; // the message queue characters

#define CALLSTACKSIZE 16 // this is only required for AVR builds
#define STACKSIZE 64          
unsigned new_task_stack[1+STACKSIZE+1];

void 
new_task_code(void *p)
{  
  unsigned char v=0;
  while (1)
    {                  
      ctl_byte_queue_post(&bq, v, 0, 0);
      v++;
    }  
}

void
ctl_handle_error(CTL_ERROR_CODE_t e)
{
  while (1);
}

int main(void)
{
 
  ctl_board_init();
  ctl_task_init(&main_task, 255, "main");
  ctl_start_timer(ctl_increment_tick_from_isr);
  ctl_byte_queue_init(&bq, queue, QSIZE); 
  memset(new_task, 0xcd, sizeof(new_task));
  new_task_stack[0]=new_task_stack[1+STACKSIZE]=0xfacefeed;
  ctl_task_run(&new_task, 1, new_task_code, 0, "new_task", STACKSIZE, new_task_stack, CALLSTACKSIZE);  
  ctl_task_set_priority(&main_task, 0);
  while (1)
    {         
      unsigned char v;
      if (!ctl_byte_queue_receive(&bq, &v, 0, 0))
        ctl_handle_error(CTL_UNSPECIFIED_ERROR);
     }
  return 0;
}
